summaryrefslogtreewikicommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorTyler Murphy <tylermurphy534@gmail.com>2022-07-27 21:15:48 -0400
committerTyler Murphy <tylermurphy534@gmail.com>2022-07-27 21:15:48 -0400
commitaa159314100c701c4a76f01c7ae8f57e86fbcab2 (patch)
tree5144b3e45839353f5afc198aadfe4156aa8ebbdd /src/main/java
parentMerge branch 'master' of https://github.com/tylermurphy534/KenshinsHideAndSee... (diff)
downloadkenshinshideandseek-aa159314100c701c4a76f01c7ae8f57e86fbcab2.tar.gz
kenshinshideandseek-aa159314100c701c4a76f01c7ae8f57e86fbcab2.tar.bz2
kenshinshideandseek-aa159314100c701c4a76f01c7ae8f57e86fbcab2.zip
block snapping
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/game/listener/DisguiseHandler.java42
1 files changed, 37 insertions, 5 deletions
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DisguiseHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DisguiseHandler.java
index 9bc6504..e3eb341 100644
--- a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DisguiseHandler.java
+++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DisguiseHandler.java
@@ -15,27 +15,59 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import java.lang.reflect.InvocationTargetException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
public class DisguiseHandler implements Listener {
private static final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
+ private final Map<UUID,Location> locations = new HashMap<>();
+ private final Map<UUID,Long> times = new HashMap<>();
+
@EventHandler(priority = EventPriority.HIGHEST)
public void onMove(PlayerMoveEvent event) {
+ checkStandingStill(event.getPlayer());
FallingBlock block = Main.getInstance().getDisguiser().getBlock(event.getPlayer());
if(block == null) return;
+ UUID uuid = event.getPlayer().getUniqueId();
+ boolean finalFixLocation = times.containsKey(uuid) && new Date().getTime()-times.get(uuid) > 1000;
Bukkit.getOnlinePlayers().forEach(player -> {
- teleportEntity(player, block, event.getPlayer().getLocation());
+ teleportEntity(player, block, event.getPlayer().getLocation(), finalFixLocation);
});
}
- private void teleportEntity(Player player, FallingBlock block, Location location) {
+ private void checkStandingStill(Player player){
+ UUID uuid = player.getUniqueId();
+ Location lastLoc = locations.get(uuid);
+ Location currentLoc = player.getLocation();
+ if(lastLoc == null) lastLoc = currentLoc;
+ double distance = lastLoc.distance(currentLoc);
+ if(distance < .05){
+ if(!times.containsKey(uuid))
+ times.put(uuid, new Date().getTime());
+ } else {
+ times.remove(uuid);
+ }
+ locations.put(uuid, currentLoc);
+ }
+
+ private void teleportEntity(Player player, FallingBlock block, Location location, boolean fixLocation) {
PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.ENTITY_TELEPORT);
packet.getModifier().writeDefaults();
packet.getIntegers().write(0, block.getEntityId());
- packet.getDoubles().write(0, location.getX());
- packet.getDoubles().write(1, location.getY());
- packet.getDoubles().write(2, location.getZ());
+ if(fixLocation){
+ packet.getDoubles().write(0, Math.round(location.getX()+.5)-.5);
+ packet.getDoubles().write(1, (double)Math.round(location.getY()));
+ packet.getDoubles().write(2, Math.round(location.getZ()+.5)-.5);
+ } else {
+ packet.getDoubles().write(0, location.getX());
+ packet.getDoubles().write(1, location.getY());
+ packet.getDoubles().write(2, location.getZ());
+ }
+
try {
protocolManager.sendServerPacket(player, packet);
} catch (InvocationTargetException e) {