From 055abc11bd0818c80d4c7be36f35cabd7becbebd Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Fri, 29 Jul 2022 21:49:43 -0400 Subject: [PATCH] better hitboxes for blockhunt --- .../net/tylermurphy/hideAndSeek/Main.java | 1 + .../tylermurphy/hideAndSeek/game/Board.java | 12 ++ .../hideAndSeek/game/Disguiser.java | 15 +-- .../game/listener/DisguiseHandler.java | 32 ++--- .../game/listener/MovementHandler.java | 1 + .../hideAndSeek/game/util/Disguise.java | 115 ++++++++++++------ 6 files changed, 109 insertions(+), 67 deletions(-) diff --git a/src/main/java/net/tylermurphy/hideAndSeek/Main.java b/src/main/java/net/tylermurphy/hideAndSeek/Main.java index 9a589ae..c4666df 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/Main.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/Main.java @@ -96,6 +96,7 @@ public class Main extends JavaPlugin implements Listener { Bukkit.getServer().getMessenger().unregisterOutgoingPluginChannel(this); board.cleanup(); + disguiser.cleanUp(); } private void onTick() { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java b/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java index 050faa8..cf91c23 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java @@ -103,6 +103,9 @@ public class Board { } public void addHider(Player player) { + if(!Main.getInstance().supports(9)){ + player.spigot().setCollidesWithEntities(false); + } Hider.add(player.getUniqueId().toString()); Seeker.remove(player.getUniqueId().toString()); Spectator.remove(player.getUniqueId().toString()); @@ -110,6 +113,9 @@ public class Board { } public void addSeeker(Player player) { + if(!Main.getInstance().supports(9)){ + player.spigot().setCollidesWithEntities(false); + } Hider.remove(player.getUniqueId().toString()); Seeker.add(player.getUniqueId().toString()); Spectator.remove(player.getUniqueId().toString()); @@ -117,6 +123,9 @@ public class Board { } public void addSpectator(Player player) { + if(!Main.getInstance().supports(9)){ + player.spigot().setCollidesWithEntities(false); + } Hider.remove(player.getUniqueId().toString()); Seeker.remove(player.getUniqueId().toString()); Spectator.add(player.getUniqueId().toString()); @@ -124,6 +133,9 @@ public class Board { } public void remove(Player player) { + if(!Main.getInstance().supports(9)){ + player.spigot().setCollidesWithEntities(true); + } Hider.remove(player.getUniqueId().toString()); Seeker.remove(player.getUniqueId().toString()); Spectator.remove(player.getUniqueId().toString()); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/Disguiser.java b/src/main/java/net/tylermurphy/hideAndSeek/game/Disguiser.java index a8ac441..a14827b 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/Disguiser.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/Disguiser.java @@ -1,16 +1,12 @@ package net.tylermurphy.hideAndSeek.game; import net.tylermurphy.hideAndSeek.game.util.Disguise; -import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; -import org.bukkit.util.BlockVector; import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Map; -import java.util.Optional; -import java.util.Vector; public class Disguiser { @@ -32,11 +28,8 @@ public class Disguiser { } @Nullable - public Disguise getByBlockLocation(BlockVector loc){ - return disguises.values().stream().filter(disguise -> { - if(disguise.getSolidLocation() == null) return false; - return disguise.getSolidLocation().toVector().toBlockVector() == loc; - }).findFirst().orElse(null); + public Disguise getByHitBoxID(int ID){ + return disguises.values().stream().filter(disguise -> disguise.getHitBoxID() == ID).findFirst().orElse(null); } public void check(){ @@ -66,4 +59,8 @@ public class Disguiser { disguises.remove(player); } + public void cleanUp() { + disguises.values().forEach(Disguise::remove); + } + } 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 af58f91..587fab9 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DisguiseHandler.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DisguiseHandler.java @@ -65,34 +65,20 @@ public class DisguiseHandler implements Listener { // } private PacketAdapter createProtocol(){ - return new PacketAdapter(Main.getInstance(), USE_ITEM, USE_ENTITY) { + return new PacketAdapter(Main.getInstance(), USE_ENTITY) { @Override public void onPacketReceiving(PacketEvent event){ PacketContainer packet = event.getPacket(); Player player = event.getPlayer(); // if(!Main.getInstance().getBoard().isSeeker(player)) return; - if(packet.getType() == USE_ITEM) { - System.out.print("\nUse Item: "); - BlockPosition data; - try { data = packet.getBlockPositionModifier().read(0); } - catch (Exception e) { return; } - System.out.print(data + " "); - BlockVector loc = new BlockVector(data.getX(), data.getY(), data.getZ()); - System.out.print(loc + " "); - Disguise disguise = Main.getInstance().getDisguiser().getByBlockLocation(loc); - System.out.print("FOUND"); - handleAttack(disguise, player); - } else if(packet.getType() == USE_ENTITY) { - System.out.print("\nUse Entity: "); - int id = packet.getIntegers().read(0); - System.out.print(id + " "); - Disguise disguise = Main.getInstance().getDisguiser().getByEntityID(id); - System.out.print("FOUND"); - handleAttack(disguise, player); - } + int id = packet.getIntegers().read(0); + Disguise disguise = Main.getInstance().getDisguiser().getByEntityID(id); + if(disguise == null) disguise = Main.getInstance().getDisguiser().getByHitBoxID(id); + if(disguise == null) return; + event.setCancelled(true); + handleAttack(disguise, player); } - }; } @@ -100,6 +86,8 @@ public class DisguiseHandler implements Listener { private void handleAttack(Disguise disguise, Player seeker){ + if(disguise.getPlayer() == seeker) return; + double amount; if(Main.getInstance().supports(9)) { amount = seeker.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE).getValue(); @@ -107,7 +95,6 @@ public class DisguiseHandler implements Listener { amount = getItemDamageValue(seeker.getItemInHand(), disguise.getPlayer(), seeker); } - if(disguise == null) return; disguise.setSolidify(false); if(debounce.contains(disguise.getPlayer())) return; debounce.add(disguise.getPlayer()); @@ -197,4 +184,5 @@ public class DisguiseHandler implements Listener { return (int) Math.round(Math.max(damageValue, 0.0)); } + } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java index 7a7e8ae..d38c512 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java @@ -22,6 +22,7 @@ public class MovementHandler implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void onMove(PlayerMoveEvent event) { + if (event.getTo() == null || event.getTo().getWorld() == null) return; checkJumping(event); checkBounds(event); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/util/Disguise.java b/src/main/java/net/tylermurphy/hideAndSeek/game/util/Disguise.java index 691037b..330aefe 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/util/Disguise.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/util/Disguise.java @@ -10,11 +10,11 @@ import net.tylermurphy.hideAndSeek.Main; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.entity.FallingBlock; -import org.bukkit.entity.Player; +import org.bukkit.entity.*; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import org.jetbrains.annotations.Nullable; +import org.bukkit.scoreboard.Scoreboard; +import org.bukkit.scoreboard.Team; import java.lang.reflect.InvocationTargetException; @@ -24,62 +24,94 @@ public class Disguise { final Player hider; final Material material; - FallingBlock entity; + FallingBlock block; + Horse hitBox; Location solidLocation; boolean solid, solidify; + static Team hidden; + + static { + if(Main.getInstance().supports(9)) { + Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard(); + hidden = board.getTeam("KenshinHideAndSeek_CollisionGroup"); + if (hidden == null) { + hidden = board.registerNewTeam("KenshinHideAndSeek_CollisionGroup"); + } + hidden.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER); + hidden.setCanSeeFriendlyInvisibles(false); + } + } public Disguise(Player player, Material material){ this.hider = player; this.material = material; this.solid = false; - respawnEntity(); + respawnFallingBlock(); player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000, 0,false, false)); + if(Main.getInstance().supports(9)) { + hidden.addEntry(player.getName()); + } else { + hider.spigot().setCollidesWithEntities(false); + } } public void remove(){ - if(entity != null) - entity.remove(); + if(block != null) + block.remove(); + if(hitBox != null){ + if(Main.getInstance().supports(9)) + hidden.removeEntry(hitBox.getUniqueId().toString()); + hitBox.remove(); + } if(solid) sendBlockUpdate(Material.AIR); hider.removePotionEffect(PotionEffectType.INVISIBILITY); - } - - @Nullable - public Location getSolidLocation() { - return solidLocation; + if(Main.getInstance().supports(9)) { + hidden.removeEntry(hider.getName()); + } else { + hider.spigot().setCollidesWithEntities(true); + } } public int getEntityID() { - if(entity == null) return -1; - return entity.getEntityId(); + if(block == null) return -1; + return block.getEntityId(); + } + + public int getHitBoxID() { + if(hitBox == null) return -1; + return hitBox.getEntityId(); } public Player getPlayer() { return hider; } - public boolean isSolid(){ - return solid; - } - public void update(){ - if(entity == null || entity.isDead()){ - if(entity != null) entity.remove(); - respawnEntity(); + if(block == null || block.isDead()){ + if(block != null) block.remove(); + respawnFallingBlock(); } if(solidify){ - if(!solid) + if(!solid) { + solid = true; solidLocation = hider.getLocation().getBlock().getLocation(); - solid = true; + respawnHotbox(); + teleportEntity(hitBox, false); + } sendBlockUpdate(material); } else if(solid){ solid = false; + if(Main.getInstance().supports(9)) + hidden.removeEntry(hitBox.getUniqueId().toString()); + hitBox.remove(); + hitBox = null; sendBlockUpdate(Material.AIR); } - sendToggleFallingBlock(!solid); - sendFallingBlockUpdate(); + toggleEntityVisibility(block, !solid); + teleportEntity(block, solid); } public void setSolidify(boolean value){ @@ -99,16 +131,12 @@ public class Disguise { }); } - private void sendFallingBlockUpdate() { - if(entity == null || entity.isDead()){ - if(entity != null) entity.remove(); - respawnEntity(); - } + private void teleportEntity(Entity entity, boolean center) { final PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.ENTITY_TELEPORT); Location location = hider.getLocation(); packet.getModifier().writeDefaults(); packet.getIntegers().write(0, entity.getEntityId()); - if(solid){ + if(center){ 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); @@ -124,7 +152,8 @@ public class Disguise { }); } - private void sendToggleFallingBlock(boolean show){ + private void toggleEntityVisibility(Entity entity, boolean show){ + if(entity == null) return; Bukkit.getOnlinePlayers().forEach(receiver -> { if(receiver == hider) return; if(show) @@ -134,10 +163,24 @@ public class Disguise { }); } - private void respawnEntity(){ - entity = hider.getLocation().getWorld().spawnFallingBlock(hider.getLocation(), material, (byte)0); - entity.setGravity(false); - entity.setDropItem(false); + private void respawnFallingBlock(){ + block = hider.getLocation().getWorld().spawnFallingBlock(hider.getLocation(), material, (byte)0); + block.setGravity(false); + block.setDropItem(false); + block.setInvulnerable(true); + } + + private void respawnHotbox(){ + hitBox = (Horse) hider.getLocation().getWorld().spawnEntity(hider.getLocation().add(0, 1000, 0), EntityType.HORSE); + hitBox.setAI(false); + hitBox.setGravity(false); + hitBox.setInvulnerable(true); + hitBox.setCanPickupItems(false); + hitBox.setCollidable(false); + hitBox.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000, 0,false, false)); + if(Main.getInstance().supports(9)){ + hidden.addEntry(hitBox.getUniqueId().toString()); + } } } \ No newline at end of file