package mineplex.game.clans.items; import java.util.HashMap; import java.util.List; import java.util.Map; import mineplex.core.MiniPlugin; import mineplex.core.account.CoreClientManager; import mineplex.core.common.util.UtilServer; import mineplex.core.packethandler.IPacketHandler; import mineplex.core.packethandler.PacketHandler; import mineplex.core.packethandler.PacketInfo; import mineplex.core.portal.TransferHandler; import mineplex.core.portal.Commands.SendCommand; import mineplex.core.portal.Commands.ServerCommand; import mineplex.game.clans.items.attributes.ItemAttribute; import mineplex.game.clans.items.attributes.weapon.FlamingAttribute; import mineplex.game.clans.items.attributes.weapon.FrostedAttribute; import mineplex.game.clans.items.attributes.weapon.SharpAttribute; import mineplex.game.clans.items.commands.GearCommand; import mineplex.game.clans.items.generation.Weight; import mineplex.game.clans.items.generation.WeightSet; import mineplex.game.clans.items.legendaries.AlligatorsTooth; import mineplex.game.clans.items.legendaries.WindBlade; import mineplex.game.clans.items.smelting.SmeltingListener; import mineplex.serverdata.Region; import mineplex.serverdata.Utility; import mineplex.serverdata.commands.ServerCommandManager; import mineplex.serverdata.commands.TransferCommand; import mineplex.serverdata.serialization.RuntimeTypeAdapterFactory; import mineplex.serverdata.servers.ServerManager; import net.minecraft.server.v1_7_R4.Packet; import net.minecraft.server.v1_7_R4.PacketPlayOutSetSlot; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.java.JavaPlugin; import com.google.gson.Gson; import com.google.gson.GsonBuilder; /** * Manages creation and retrieval of associated {@link PlayerGear}s with online players, as well * as offering methods for parsing and handling {@link CustomItem}s. * @author MrTwiggy * */ public class GearManager extends MiniPlugin implements IPacketHandler { private static final String ITEM_SERIALIZATION_TAG = "-JSON-"; private static GearManager _instance; // Singleton instance private Map playerGears; // Mapping of player names (key) to cached gear set (value). private WeightSet _attributeWeights; // Weightings for randomly selecting number of attributes (1, 2, 3) private WeightSet _itemWeights; // Weightings for randomly selecting item type (legendary/rare) private WeightSet _gearWeights; // Weightings for randomly selecting gear type (armour/weapon) private static Gson _gson; public GearManager(JavaPlugin plugin, PacketHandler packetHandler) { super("CustomGear", plugin); _instance = this; playerGears = new HashMap(); // TODO: Introduce configurable non-hardcoded values for generation weights? _attributeWeights = new WeightSet(new Weight(3, 3), new Weight(20, 2), new Weight(77, 1)); _itemWeights = new WeightSet(new Weight(90, true), new Weight(10, false)); _itemWeights = new WeightSet(new Weight(50, true), new Weight(50, false)); System.out.println("-Testting-testing"); System.out.println(Utility.currentTimeSeconds()); // Register listeners UtilServer.getServer().getPluginManager().registerEvents(new ItemListener(), getPlugin()); UtilServer.getServer().getPluginManager().registerEvents(new SmeltingListener(), getPlugin()); // adding all different container classes with their flag RuntimeTypeAdapterFactory typeFactory = RuntimeTypeAdapterFactory .of(ItemAttribute.class, "AttributeType") .registerSubtype(SharpAttribute.class) .registerSubtype(FrostedAttribute.class) .registerSubtype(FlamingAttribute.class); // TODO: Register all item attributes automatically RuntimeTypeAdapterFactory customItemType = RuntimeTypeAdapterFactory .of(CustomItem.class, "ItemType") .registerSubtype(AlligatorsTooth.class) .registerSubtype(WindBlade.class); // TODO: Register all legendary weapons automatically GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapterFactory(typeFactory); builder.registerTypeAdapterFactory(customItemType); _gson = builder.create(); packetHandler.addPacketHandler(this); } @Override public void addCommands() { addCommand(new GearCommand(this)); } /** * @param player - the player whose {@link PlayerGear} set is to be fetched. * @return the cached or newly instantiated {@link PlayerGear} associated with {@code player}. */ public PlayerGear getPlayerGear(Player player) { String playerName = player.getName(); if (!playerGears.containsKey(playerName)) { PlayerGear gear = new PlayerGear(playerName); playerGears.put(playerName, gear); } return playerGears.get(playerName); } public CustomItem generateItem() { int attributeCount = _attributeWeights.generateRandom(); boolean isLegendary = _itemWeights.generateRandom(); boolean isArmour = _gearWeights.generateRandom(); // TODO: Generate custom item randomly using generated values above return null; } public static CustomItem parseItem(ItemStack item) { String serialization = getItemSerialization(item); if (serialization != null) { CustomItem customItem = deserialize(serialization); return customItem; } return null; // No serialization found in item's lore, not a custom item! } public static boolean isCustomItem(ItemStack item) { return getItemSerialization(item) != null; } public static String getItemSerialization(CustomItem customItem) { String tempSeri = serialize(customItem); String serialization = ITEM_SERIALIZATION_TAG; for (int i = 0; i < tempSeri.length(); i++) { if (i % 40 == 39) { serialization += "\n"; } serialization += tempSeri.charAt(i); } return serialization; } private static String getItemSerialization(ItemStack item) { if (item == null || item.getItemMeta() == null || item.getItemMeta().getLore() == null) return null; ItemMeta meta = item.getItemMeta(); /*for (String lore : meta.getLore()) { if (lore.startsWith(ITEM_SERIALIZATION_TAG)) // Found serialization lore-line { int tagLength = ITEM_SERIALIZATION_TAG.length(); String serialization = lore.substring(tagLength); return serialization; } }*/ // TODO: Implement packet intercepting to hide json encoded lore List lore = meta.getLore(); boolean serialized = false; String serialization = ""; for (int i = 0; i < lore.size(); i++) { String line = lore.get(i); if (line.startsWith(ITEM_SERIALIZATION_TAG)) { serialized = true; serialization += line.substring(ITEM_SERIALIZATION_TAG.length()); } else if (serialized) { serialization += line; } } if (serialized) { return serialization; } return null; // Unable to find any serialized lore lines, hence not a CustomItem. } public static String serialize(CustomItem customItem) { return _gson.toJson(customItem); } public static CustomItem deserialize(String serialization) { System.out.println("Serialization: " + serialization); return _gson.fromJson(serialization, CustomItem.class); } /** * @return singleton instance of {@link GearManager}. */ public static GearManager getInstane() { return _instance; } @Override public void handle(PacketInfo packetInfo) { Packet packet = packetInfo.getPacket(); if (packet instanceof PacketPlayOutSetSlot) { System.out.println("Item slot packet!"); PacketPlayOutSetSlot slotPacket = (PacketPlayOutSetSlot) packet; } } }