From 8015f5428de81f010fa73ba5bfe18afe6726054c Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 19 Sep 2015 08:36:23 +1200 Subject: [PATCH] Flags stuff --- .../core/packethandler/PacketHandler.java | 15 +- .../game/games/common/CaptureTheFlag.java | 154 +++++++++++++++++- .../game/games/common/ctf_data/Flag.java | 4 +- 3 files changed, 165 insertions(+), 8 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/packethandler/PacketHandler.java b/Plugins/Mineplex.Core/src/mineplex/core/packethandler/PacketHandler.java index 056b75877..f01efc2da 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/packethandler/PacketHandler.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/packethandler/PacketHandler.java @@ -4,7 +4,10 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashSet; +import net.minecraft.server.v1_7_R4.Block; +import net.minecraft.server.v1_7_R4.BlockSandStone; import net.minecraft.server.v1_7_R4.EnumProtocol; +import net.minecraft.server.v1_7_R4.Item; import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer; import org.bukkit.entity.Player; @@ -22,6 +25,7 @@ public class PacketHandler extends MiniPlugin { private NautHashMap _playerVerifierMap = new NautHashMap(); private HashSet _packetHandlers = new HashSet(); + public static Item BANNER = new Item().c("banner"); public PacketHandler(JavaPlugin plugin) { @@ -47,15 +51,16 @@ public class PacketHandler extends MiniPlugin EnumProtocol.PLAY.a().put(2, PacketPlayUseEntity.class); EnumProtocol.PLAY.a().put(PacketPlayUseEntity.class, 2); - - Method method = ProtocolInjector.class.getDeclaredMethod("addPacket", EnumProtocol.class,boolean.class, int.class, Class.class); + + Method method = ProtocolInjector.class.getDeclaredMethod("addPacket", EnumProtocol.class, boolean.class, int.class, + Class.class); method.setAccessible(true); method.invoke(null, EnumProtocol.PLAY, true, 67, PacketPlayOutCamera.class); method.invoke(null, EnumProtocol.PLAY, true, 68, PacketPlayOutWorldBorder.class); - - // EnumProtocol.PLAY.b().put(68, PacketPlayOutWorldBorder.class); - // EnumProtocol.PLAY.b().put(PacketPlayOutWorldBorder.class, 68); + + Item.REGISTRY.a(425, "banner", BANNER); + Block.REGISTRY.a(425, "banner", new BlockSandStone()); } catch (Exception ex) { diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/CaptureTheFlag.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/CaptureTheFlag.java index 62e3f8d08..46bb8459c 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/CaptureTheFlag.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/CaptureTheFlag.java @@ -1,5 +1,6 @@ package nautilus.game.arcade.game.games.common; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -18,6 +19,9 @@ import mineplex.core.common.util.UtilTime; import mineplex.core.elo.EloPlayer; import mineplex.core.elo.EloTeam; import mineplex.core.elo.GameResult; +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 mineplex.minecraft.game.core.combat.CombatComponent; @@ -35,10 +39,15 @@ import nautilus.game.arcade.game.games.champions.ChampionsCTF; import nautilus.game.arcade.game.games.common.ctf_data.Flag; import nautilus.game.arcade.game.games.common.dominate_data.PlayerData; import nautilus.game.arcade.kit.Kit; +import net.minecraft.server.v1_7_R4.NBTTagCompound; +import net.minecraft.server.v1_7_R4.PacketPlayOutEntityEquipment; +import net.minecraft.server.v1_7_R4.PacketPlayOutSetSlot; +import net.minecraft.server.v1_7_R4.PacketPlayOutWindowItems; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -75,6 +84,8 @@ public class CaptureTheFlag extends TeamGame private ConcurrentHashMap> _hotbars = new ConcurrentHashMap>(); private ConcurrentHashMap _helmets = new ConcurrentHashMap(); + private IPacketHandler _flagItemListener; + private Field _itemField; public CaptureTheFlag(ArcadeManager manager, GameType type, Kit[] kits) { @@ -95,8 +106,149 @@ public class CaptureTheFlag extends TeamGame this.WorldTimeSet = 2000; this.DeathSpectateSecs = 10; + + try + { + _itemField = PacketPlayOutSetSlot.class.getDeclaredField("c"); + _itemField.setAccessible(true); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + + _flagItemListener = new IPacketHandler() + { + @Override + public void handle(PacketInfo packetInfo) + { + if (packetInfo.getPacket() instanceof PacketPlayOutEntityEquipment) + { + PacketPlayOutEntityEquipment eqiup = (PacketPlayOutEntityEquipment) packetInfo + .getPacket(); + + ItemStack item = CraftItemStack.asBukkitCopy(eqiup.c); + + if (item != null && item.getType() != Material.AIR) + { + for (Flag flag : _flags) + { + if (flag.getRepresentation().isSimilar(item)) + { + eqiup.c = getFlag(flag.getTeam().GetColorData()); + break; + } + } + } + } + else if (packetInfo.getPacket() instanceof PacketPlayOutWindowItems + || packetInfo.getPacket() instanceof PacketPlayOutSetSlot) + { + Player player = packetInfo.getPlayer(); + + if (_hotbars.containsKey(player)) + { + ItemStack flagMaterial = null; + net.minecraft.server.v1_7_R4.ItemStack flagItem = null; + + for (Flag flag : _flags) + { + if (flag.getCarrier() == player) + { + flagMaterial = flag.getRepresentation(); + flagItem = getFlag(flag.getTeam() + .GetColorData()); + break; + } + } + + if (flagItem == null) + { + return; + } + + if (packetInfo.getPacket() instanceof PacketPlayOutWindowItems) + { + PacketPlayOutWindowItems packet = (PacketPlayOutWindowItems) packetInfo + .getPacket(); + + for (int i = 0; i < packet.b.length; i++) + { + ItemStack item = CraftItemStack + .asBukkitCopy(packet.b[i]); + + if (item != null + && item.isSimilar(flagMaterial)) + { + packet.b[i] = flagItem; + } + } + + } + else + { + PacketPlayOutSetSlot packet = (PacketPlayOutSetSlot) packetInfo + .getPacket(); + + ItemStack item = null; + + try + { + item = CraftItemStack + .asBukkitCopy((net.minecraft.server.v1_7_R4.ItemStack) _itemField + .get(packet)); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + + if (item != null && item.isSimilar(flagMaterial)) + { + packetInfo.setCancelled(true); + + packet = new PacketPlayOutSetSlot(packet.a, + packet.b, flagItem); + + packetInfo.getVerifier().bypassProcess(packet); + } + } + } + } + } + }; + } + + private net.minecraft.server.v1_7_R4.ItemStack getFlag(byte b) + { + net.minecraft.server.v1_7_R4.ItemStack item = new net.minecraft.server.v1_7_R4.ItemStack( + PacketHandler.BANNER, 1, 32767); + NBTTagCompound nbtBase = new NBTTagCompound(); + + NBTTagCompound nbt = new NBTTagCompound(); + + nbt.setInt("Base", b); + + nbtBase.set("BlockEntityTag", nbt); + item.setTag(nbtBase); + + return item; } + @EventHandler + public void handleFlagItem(GameStateChangeEvent event) + { + if (event.GetState() == GameState.Live) + { + getArcadeManager().getPacketHandler().addPacketHandler( + _flagItemListener); + } + else if (event.GetState() == GameState.Dead) + { + getArcadeManager().getPacketHandler().removePacketHandler( + _flagItemListener); + } + } @Override public void ParseData() { @@ -569,7 +721,7 @@ public class CaptureTheFlag extends TeamGame public void DropFlag(PlayerDropItemEvent event) { for (Flag flag : _flags) - if (flag.getRepresentation() == event.getItemDrop().getItemStack().getType()) + if (flag.getRepresentation().getType() == event.getItemDrop().getItemStack().getType()) if (flag.getCarrier() != null) if (flag.getCarrier().getName().equals(event.getPlayer().getName())) { diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/ctf_data/Flag.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/ctf_data/Flag.java index 9666ad9ee..1564998b8 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/ctf_data/Flag.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/ctf_data/Flag.java @@ -91,9 +91,9 @@ public class Flag return _flagBlocks[0].getLocation(); } - public Material getRepresentation() + public ItemStack getRepresentation() { - return _representation.getType(); + return _representation; } private void AnnounceCapture(Player cap)