From 3e8a53562eb15f93986ab3f639913444c953f52a Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Thu, 26 Nov 2015 15:49:28 +1300 Subject: [PATCH] Add new hologram interaction thingy so holograms can catch interactions that they would normally block --- .../src/mineplex/core/hologram/Hologram.java | 67 ++++--- .../core/hologram/HologramInteraction.java | 9 + .../core/hologram/HologramManager.java | 176 +++++++++++------- .../core/treasure/TreasureLocation.java | 17 +- .../Mineplex.Hub/src/mineplex/hub/Hub.java | 2 +- .../src/nautilus/game/arcade/Arcade.java | 2 +- 6 files changed, 172 insertions(+), 101 deletions(-) create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/hologram/HologramInteraction.java diff --git a/Plugins/Mineplex.Core/src/mineplex/core/hologram/Hologram.java b/Plugins/Mineplex.Core/src/mineplex/core/hologram/Hologram.java index 6be18e222..e772427cc 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/hologram/Hologram.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/hologram/Hologram.java @@ -2,11 +2,8 @@ package mineplex.core.hologram; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; -import java.util.Map.Entry; - import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -34,7 +31,7 @@ public class Hologram /** * 1.7 packets uses both EntityIDs while 1.8 uses only the first. */ - private ArrayList> _entityIds = new ArrayList>(); + private ArrayList _entityIds = new ArrayList(); private Entity _followEntity; private HologramManager _hologramManager; private String[] _hologramText = new String[0]; @@ -53,6 +50,7 @@ public class Hologram private int _viewDistance = 70; protected Vector relativeToEntity; private boolean _hideBoundingBox; + private HologramInteraction _interaction; public Hologram(HologramManager hologramManager, Location location, String... text) { @@ -61,6 +59,18 @@ public class Hologram setText(text); } + public Hologram setInteraction(HologramInteraction interact) + { + _interaction = interact; + + return this; + } + + public HologramInteraction getInteraction() + { + return _interaction; + } + /** * Adds the player to the Hologram to be effected by Whitelist or Blacklist */ @@ -151,6 +161,7 @@ public class Hologram nearbyPlayers.add(player); } } + return nearbyPlayers; } @@ -229,9 +240,7 @@ public class Hologram for (int i = 0; i < _entityIds.size(); i++) { - Entry entry = _entityIds.get(i); - - entityIds1_8[i] = entry.getKey(); + entityIds1_8[i] = _entityIds.get(i); } _destroy1_8 = new PacketPlayOutEntityDestroy(entityIds1_8); @@ -247,7 +256,7 @@ public class Hologram for (int i = _entityIds.size(); i < _hologramText.length; i++) { - _entityIds.add(new HashMap.SimpleEntry(UtilEnt.getNewEntityId(), UtilEnt.getNewEntityId())); + _entityIds.add(UtilEnt.getNewEntityId()); } } else @@ -261,9 +270,7 @@ public class Hologram } for (int textRow = 0; textRow < _hologramText.length; textRow++) { - Entry entityIds = this._entityIds.get(textRow); - - Packet[] packets1_8 = makeSpawnPackets1_8(textRow, entityIds.getKey(), _hologramText[textRow]); + Packet[] packets1_8 = makeSpawnPackets1_8(textRow, _entityIds.get(textRow), _hologramText[textRow]); for (int i = 0; i < packets1_8.length; i++) { @@ -326,8 +333,7 @@ public class Hologram public Hologram setFollowEntity(Entity entityToFollow) { _followEntity = entityToFollow; - relativeToEntity = entityToFollow == null ? null : this._location.clone().subtract(entityToFollow.getLocation()) - .toVector(); + relativeToEntity = entityToFollow == null ? null : _location.clone().subtract(entityToFollow.getLocation()).toVector(); return this; } @@ -340,7 +346,7 @@ public class Hologram */ public Hologram setHologramTarget(HologramTarget newTarget) { - this._target = newTarget; + _target = newTarget; return this; } @@ -405,11 +411,11 @@ public class Hologram if (x >= -128 && x <= 127 && y >= -128 && y <= 127 && z >= -128 && z <= 127) { _lastMovement.subtract(new Vector(x / 32D, y / 32D, z / 32D)); - for (Entry entityId : this._entityIds) + for (Integer entityId : _entityIds) { PacketPlayOutEntity.PacketPlayOutRelEntityMove relMove = new PacketPlayOutEntity.PacketPlayOutRelEntityMove(); - relMove.a = entityId.getKey(); + relMove.a = entityId; relMove.b = (byte) x; relMove.c = (byte) y; relMove.d = (byte) z; @@ -425,13 +431,12 @@ public class Hologram _lastMovement = new Vector(newLocation.getX() - (x / 32D), 0, newLocation.getZ() - (z / 32D)); - for (Entry entityId : this._entityIds) + for (Integer entityId : _entityIds) { PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(); - teleportPacket.a = entityId.getKey(); + teleportPacket.a = entityId; teleportPacket.b = x; - teleportPacket.c = (int) Math - .floor((oldLocation.getY() + (-2.1) + ((double) i * 0.285)) * 32); + teleportPacket.c = (int) Math.floor((oldLocation.getY() + (-2.1) + ((double) i * 0.285)) * 32); teleportPacket.d = z; packets1_8[i] = teleportPacket; @@ -458,6 +463,11 @@ public class Hologram return this; } + public boolean isEntityId(int entityId) + { + return _entityIds.contains(entityId); + } + /** * Set the hologram text */ @@ -491,28 +501,28 @@ public class Hologram { // Add entity id and send spawn packets // You add a entity id because the new hologram needs - Entry entry = new HashMap.SimpleEntry(UtilEnt.getNewEntityId(), UtilEnt.getNewEntityId()); - _entityIds.add(entry); + int entityId = UtilEnt.getNewEntityId(); + _entityIds.add(entityId); - packets1_8.addAll(Arrays.asList(makeSpawnPackets1_8(i, entry.getKey(), newText[i]))); + packets1_8.addAll(Arrays.asList(makeSpawnPackets1_8(i, entityId, newText[i]))); } // If less lines than previously else if (i >= newText.length) { // Remove entity id and send destroy packets - Entry entry = _entityIds.remove(newText.length); + Integer entityId = _entityIds.remove(newText.length); destroy1_8 = Arrays.copyOf(destroy1_8, destroy1_8.length + 1); - destroy1_8[destroy1_8.length - 1] = entry.getKey(); + destroy1_8[destroy1_8.length - 1] = entityId; } else if (!newText[i].equals(_hologramText[i])) { // Send update metadata packets - Entry entry = _entityIds.get(i); + Integer entityId = _entityIds.get(i); PacketPlayOutEntityMetadata metadata1_8 = new PacketPlayOutEntityMetadata(); - metadata1_8.a = entry.getKey(); + metadata1_8.a = entityId; DataWatcher watcher1_8 = new DataWatcher(null); @@ -552,7 +562,7 @@ public class Hologram */ public Hologram setViewDistance(int newDistance) { - this._viewDistance = newDistance; + _viewDistance = newDistance; return setLocation(getLocation()); } @@ -593,6 +603,7 @@ public class Hologram _playersTracking.clear(); _lastMovement = null; } + return this; } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/hologram/HologramInteraction.java b/Plugins/Mineplex.Core/src/mineplex/core/hologram/HologramInteraction.java new file mode 100644 index 000000000..b6bc659e3 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/hologram/HologramInteraction.java @@ -0,0 +1,9 @@ +package mineplex.core.hologram; + +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; + +public interface HologramInteraction +{ + public void onClick(Player player, ClickType clickType); +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/hologram/HologramManager.java b/Plugins/Mineplex.Core/src/mineplex/core/hologram/HologramManager.java index 8be93fc9a..df8a9352b 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/hologram/HologramManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/hologram/HologramManager.java @@ -5,90 +5,106 @@ import java.util.Iterator; import java.util.List; import mineplex.core.common.util.UtilPlayer; +import mineplex.core.packethandler.IPacketHandler; +import mineplex.core.packethandler.PacketHandler; +import mineplex.core.packethandler.PacketInfo; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; -import net.minecraft.server.v1_8_R3.Packet; +import net.minecraft.server.v1_8_R3.PacketPlayInUseEntity; +import net.minecraft.server.v1_8_R3.PacketPlayInUseEntity.EnumEntityUseAction; import org.bukkit.Bukkit; import org.bukkit.World; -import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.inventory.ClickType; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.util.Vector; -public class HologramManager implements Listener +public class HologramManager implements Listener, IPacketHandler { - private ArrayList _activeHolograms = new ArrayList(); + private ArrayList _activeHolograms = new ArrayList(); - public HologramManager(JavaPlugin arcadeManager) - { - Bukkit.getPluginManager().registerEvents(this, arcadeManager); - } + public HologramManager(JavaPlugin arcadeManager, PacketHandler packetHandler) + { + Bukkit.getPluginManager().registerEvents(this, arcadeManager); + packetHandler.addPacketHandler(this, true, PacketPlayInUseEntity.class); + } - void addHologram(Hologram hologram) - { - _activeHolograms.add(hologram); - } + void addHologram(Hologram hologram) + { + _activeHolograms.add(hologram); + } - void removeHologram(Hologram hologram) - { - _activeHolograms.remove(hologram); - } + void removeHologram(Hologram hologram) + { + _activeHolograms.remove(hologram); + } - @EventHandler(priority = EventPriority.LOWEST) - public void onTick(UpdateEvent event) - { - if (event.getType() != UpdateType.TICK || _activeHolograms.isEmpty()) - return; - List worlds = Bukkit.getWorlds(); - Iterator itel = _activeHolograms.iterator(); - while (itel.hasNext()) - { - Hologram hologram = itel.next(); - if (!worlds.contains(hologram.getLocation().getWorld())) - { - itel.remove(); - hologram.stop(); - } - else - { - if (hologram.getEntityFollowing() != null) - { - Entity following = hologram.getEntityFollowing(); - if (hologram.isRemoveOnEntityDeath() && !following.isValid()) - { - itel.remove(); - hologram.stop(); - continue; - } - if (!hologram.relativeToEntity.equals(following.getLocation().subtract(hologram.getLocation()).toVector())) - { - // And we do this so in the rare offchance it changes by a decimal. It doesn't start turning wonky. - Vector vec = hologram.relativeToEntity.clone(); - hologram.setLocation(following.getLocation().add(hologram.relativeToEntity)); - hologram.relativeToEntity = vec; - continue; // No need to do the rest of the code as setLocation does it. - } - } - ArrayList canSee = hologram.getNearbyPlayers(); - Iterator itel2 = hologram.getPlayersTracking().iterator(); - while (itel2.hasNext()) - { - Player player = itel2.next(); - if (!canSee.contains(player)) - { - itel2.remove(); - if (player.getWorld() == hologram.getLocation().getWorld()) - { - UtilPlayer.sendPacket(player, hologram.getDestroyPacket()); - } - } - } - for (Player player : canSee) + @EventHandler(priority = EventPriority.LOWEST) + public void onTick(UpdateEvent event) + { + if (event.getType() != UpdateType.TICK || _activeHolograms.isEmpty()) + return; + + List worlds = Bukkit.getWorlds(); + + Iterator itel = _activeHolograms.iterator(); + + while (itel.hasNext()) + { + Hologram hologram = itel.next(); + + if (!worlds.contains(hologram.getLocation().getWorld())) + { + itel.remove(); + hologram.stop(); + } + else + { + if (hologram.getEntityFollowing() != null) + { + Entity following = hologram.getEntityFollowing(); + + if (hologram.isRemoveOnEntityDeath() && !following.isValid()) + { + itel.remove(); + hologram.stop(); + continue; + } + if (!hologram.relativeToEntity.equals(following.getLocation().subtract(hologram.getLocation()).toVector())) + { + // And we do this so in the rare offchance it changes by a decimal. It doesn't start turning wonky. + Vector vec = hologram.relativeToEntity.clone(); + hologram.setLocation(following.getLocation().add(hologram.relativeToEntity)); + hologram.relativeToEntity = vec; + + continue; // No need to do the rest of the code as setLocation does it. + } + } + + ArrayList canSee = hologram.getNearbyPlayers(); + + Iterator itel2 = hologram.getPlayersTracking().iterator(); + + while (itel2.hasNext()) + { + Player player = itel2.next(); + + if (!canSee.contains(player)) + { + itel2.remove(); + if (player.getWorld() == hologram.getLocation().getWorld()) + { + UtilPlayer.sendPacket(player, hologram.getDestroyPacket()); + } + } + } + + for (Player player : canSee) { if (!hologram.getPlayersTracking().contains(player)) { @@ -96,8 +112,28 @@ public class HologramManager implements Listener UtilPlayer.sendPacket(player, hologram.getSpawnPackets()); } - } - } - } - } + } + } + } + } + + @Override + public void handle(PacketInfo packetInfo) + { + PacketPlayInUseEntity packetPlayIn = (PacketPlayInUseEntity) packetInfo.getPacket(); + + for (Hologram hologram : _activeHolograms) + { + if (!hologram.isEntityId(packetPlayIn.a)) + continue; + + if (hologram.getInteraction() != null) + { + hologram.getInteraction().onClick(packetInfo.getPlayer(), + packetPlayIn.action == EnumEntityUseAction.ATTACK ? ClickType.LEFT : ClickType.RIGHT); + } + + break; + } + } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/treasure/TreasureLocation.java b/Plugins/Mineplex.Core/src/mineplex/core/treasure/TreasureLocation.java index c5f591843..43447a030 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/treasure/TreasureLocation.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/treasure/TreasureLocation.java @@ -12,6 +12,7 @@ import mineplex.core.common.util.UtilTextMiddle; import mineplex.core.donation.DonationManager; import mineplex.core.gadget.event.GadgetBlockEvent; import mineplex.core.hologram.Hologram; +import mineplex.core.hologram.HologramInteraction; import mineplex.core.hologram.HologramManager; import mineplex.core.inventory.InventoryManager; import mineplex.core.reward.Reward; @@ -32,6 +33,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; +import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerMoveEvent; @@ -63,7 +65,20 @@ public class TreasureLocation implements Listener _hologramManager = hologramManager; _statusManager = statusManager; _currentTreasure = null; - _hologram = new Hologram(_hologramManager, chestBlock.getLocation().add(0.5, 2.5, 0.5), C.cGreen + C.Bold + "Open Treasure"); + _hologram = new Hologram(_hologramManager, chestBlock.getLocation().add(0.5, 2.5, 0.5), C.cGreen + C.Bold + + "Open Treasure"); + _hologram.setInteraction(new HologramInteraction() + { + + @Override + public void onClick(Player player, ClickType clickType) + { + if (clickType == ClickType.LEFT) + return; + + openShop(player); + } + }); setHoloChestVisible(true); _shop = new TreasureShop(treasureManager, _inventoryManager, clientManager, donationManager, this); } diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/Hub.java b/Plugins/Mineplex.Hub/src/mineplex/hub/Hub.java index 73a06ad64..9a0d8f1dc 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/Hub.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/Hub.java @@ -133,7 +133,7 @@ public class Hub extends JavaPlugin implements IRelation SkillConditionManager conditionManager = new SkillConditionManager(this); PersonalServerManager personalServerManager = new PersonalServerManager(this, clientManager); - HubManager hubManager = new HubManager(this, blockRestore, clientManager, donationManager, inventoryManager, conditionManager, disguiseManager, new TaskManager(this, clientManager, webServerAddress), portal, partyManager, preferenceManager, petManager, pollManager, statsManager, achievementManager, new HologramManager(this), npcManager, personalServerManager, packetHandler, punish, serverStatusManager, giveawayManager); + HubManager hubManager = new HubManager(this, blockRestore, clientManager, donationManager, inventoryManager, conditionManager, disguiseManager, new TaskManager(this, clientManager, webServerAddress), portal, partyManager, preferenceManager, petManager, pollManager, statsManager, achievementManager, new HologramManager(this, packetHandler), npcManager, personalServerManager, packetHandler, punish, serverStatusManager, giveawayManager); QueueManager queueManager = new QueueManager(this, clientManager, donationManager, new EloManager(this, clientManager), partyManager); diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/Arcade.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/Arcade.java index 28aa540dc..fb19678e3 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/Arcade.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/Arcade.java @@ -136,7 +136,7 @@ public class Arcade extends JavaPlugin BlockRestore blockRestore = new BlockRestore(this); ProjectileManager projectileManager = new ProjectileManager(this); - HologramManager hologramManager = new HologramManager(this); + HologramManager hologramManager = new HologramManager(this, packetHandler); //Inventory InventoryManager inventoryManager = new InventoryManager(this, _clientManager);