2015-05-05 21:33:42 +02:00
|
|
|
package mineplex.game.clans.items;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2015-05-25 20:22:06 +02:00
|
|
|
import java.util.List;
|
2015-05-05 21:33:42 +02:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import mineplex.core.MiniPlugin;
|
|
|
|
import mineplex.core.account.CoreClientManager;
|
2015-05-25 20:22:06 +02:00
|
|
|
import mineplex.core.common.util.UtilServer;
|
|
|
|
import mineplex.core.packethandler.IPacketHandler;
|
|
|
|
import mineplex.core.packethandler.PacketHandler;
|
|
|
|
import mineplex.core.packethandler.PacketInfo;
|
2015-05-05 21:33:42 +02:00
|
|
|
import mineplex.core.portal.TransferHandler;
|
|
|
|
import mineplex.core.portal.Commands.SendCommand;
|
|
|
|
import mineplex.core.portal.Commands.ServerCommand;
|
2015-05-25 20:22:06 +02:00
|
|
|
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;
|
2015-05-05 21:33:42 +02:00
|
|
|
import mineplex.game.clans.items.commands.GearCommand;
|
|
|
|
import mineplex.game.clans.items.generation.Weight;
|
|
|
|
import mineplex.game.clans.items.generation.WeightSet;
|
2015-05-25 20:22:06 +02:00
|
|
|
import mineplex.game.clans.items.legendaries.AlligatorsTooth;
|
|
|
|
import mineplex.game.clans.items.legendaries.WindBlade;
|
|
|
|
import mineplex.game.clans.items.smelting.SmeltingListener;
|
2015-05-05 21:33:42 +02:00
|
|
|
import mineplex.serverdata.Region;
|
2015-05-16 18:58:58 +02:00
|
|
|
import mineplex.serverdata.Utility;
|
2015-05-05 21:33:42 +02:00
|
|
|
import mineplex.serverdata.commands.ServerCommandManager;
|
|
|
|
import mineplex.serverdata.commands.TransferCommand;
|
2015-05-25 20:22:06 +02:00
|
|
|
import mineplex.serverdata.serialization.RuntimeTypeAdapterFactory;
|
2015-05-05 21:33:42 +02:00
|
|
|
import mineplex.serverdata.servers.ServerManager;
|
2015-05-25 20:22:06 +02:00
|
|
|
import net.minecraft.server.v1_7_R4.Packet;
|
|
|
|
import net.minecraft.server.v1_7_R4.PacketPlayOutSetSlot;
|
2015-05-05 21:33:42 +02:00
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2015-05-25 20:22:06 +02:00
|
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
2015-05-05 21:33:42 +02:00
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
2015-05-25 20:22:06 +02:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
|
2015-05-05 21:33:42 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2015-05-25 20:22:06 +02:00
|
|
|
public class GearManager extends MiniPlugin implements IPacketHandler
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
2015-05-25 20:22:06 +02:00
|
|
|
private static final String ITEM_SERIALIZATION_TAG = "-JSON-";
|
2015-05-05 21:33:42 +02:00
|
|
|
private static GearManager _instance; // Singleton instance
|
2015-05-25 20:22:06 +02:00
|
|
|
|
2015-05-05 21:33:42 +02:00
|
|
|
|
|
|
|
private Map<String, PlayerGear> playerGears; // Mapping of player names (key) to cached gear set (value).
|
|
|
|
private WeightSet<Integer> _attributeWeights; // Weightings for randomly selecting number of attributes (1, 2, 3)
|
|
|
|
private WeightSet<Boolean> _itemWeights; // Weightings for randomly selecting item type (legendary/rare)
|
|
|
|
private WeightSet<Boolean> _gearWeights; // Weightings for randomly selecting gear type (armour/weapon)
|
2015-05-25 20:22:06 +02:00
|
|
|
private static Gson _gson;
|
2015-05-05 21:33:42 +02:00
|
|
|
|
2015-05-25 20:22:06 +02:00
|
|
|
public GearManager(JavaPlugin plugin, PacketHandler packetHandler)
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
|
|
|
super("CustomGear", plugin);
|
|
|
|
|
|
|
|
_instance = this;
|
|
|
|
|
|
|
|
playerGears = new HashMap<String, PlayerGear>();
|
2015-05-25 20:22:06 +02:00
|
|
|
// TODO: Introduce configurable non-hardcoded values for generation weights?
|
2015-05-05 21:33:42 +02:00
|
|
|
_attributeWeights = new WeightSet<Integer>(new Weight<Integer>(3, 3), new Weight<Integer>(20, 2), new Weight<Integer>(77, 1));
|
|
|
|
_itemWeights = new WeightSet<Boolean>(new Weight<Boolean>(90, true), new Weight<Boolean>(10, false));
|
|
|
|
_itemWeights = new WeightSet<Boolean>(new Weight<Boolean>(50, true), new Weight<Boolean>(50, false));
|
2015-05-16 18:58:58 +02:00
|
|
|
|
|
|
|
System.out.println("-Testting-testing");
|
|
|
|
System.out.println(Utility.currentTimeSeconds());
|
2015-05-25 20:22:06 +02:00
|
|
|
|
|
|
|
// 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<ItemAttribute> typeFactory = RuntimeTypeAdapterFactory
|
|
|
|
.of(ItemAttribute.class, "AttributeType")
|
|
|
|
.registerSubtype(SharpAttribute.class)
|
|
|
|
.registerSubtype(FrostedAttribute.class)
|
|
|
|
.registerSubtype(FlamingAttribute.class); // TODO: Register all item attributes automatically
|
|
|
|
|
|
|
|
RuntimeTypeAdapterFactory<CustomItem> 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);
|
2015-05-05 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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)
|
|
|
|
{
|
2015-05-25 20:22:06 +02:00
|
|
|
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!
|
2015-05-05 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isCustomItem(ItemStack item)
|
|
|
|
{
|
2015-05-25 20:22:06 +02:00
|
|
|
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<String> 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);
|
2015-05-05 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return singleton instance of {@link GearManager}.
|
|
|
|
*/
|
|
|
|
public static GearManager getInstane()
|
|
|
|
{
|
|
|
|
return _instance;
|
|
|
|
}
|
2015-05-25 20:22:06 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handle(PacketInfo packetInfo)
|
|
|
|
{
|
|
|
|
Packet packet = packetInfo.getPacket();
|
|
|
|
|
|
|
|
if (packet instanceof PacketPlayOutSetSlot)
|
|
|
|
{
|
|
|
|
System.out.println("Item slot packet!");
|
|
|
|
PacketPlayOutSetSlot slotPacket = (PacketPlayOutSetSlot) packet;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 21:33:42 +02:00
|
|
|
}
|