diff options
Diffstat (limited to 'src/main/java/net/tylermurphy/hideAndSeek/game/util/Disguise.java')
-rw-r--r-- | src/main/java/net/tylermurphy/hideAndSeek/game/util/Disguise.java | 113 |
1 files changed, 78 insertions, 35 deletions
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); + if(Main.getInstance().supports(9)) { + hidden.removeEntry(hider.getName()); + } else { + hider.spigot().setCollidesWithEntities(true); + } } - @Nullable - public Location getSolidLocation() { - return solidLocation; + public int getEntityID() { + if(block == null) return -1; + return block.getEntityId(); } - public int getEntityID() { - if(entity == null) return -1; - return entity.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 |