formatted some classes

This commit is contained in:
NewGarbo 2015-11-22 11:23:19 +00:00
parent 17e62f1f5a
commit 0cdee95b87
2 changed files with 112 additions and 88 deletions

View File

@ -8,18 +8,18 @@ import mineplex.game.clans.items.GearManager;
public class GearLoot implements ILoot
{
private GearManager _gearManager;
public GearLoot(GearManager gearManager)
{
_gearManager = gearManager;
}
@Override
public void dropLoot(Location location)
{
_gearManager.spawnItem(location);
}
@Override
public ItemStack getItemStack()
{

View File

@ -68,22 +68,30 @@ import net.minecraft.server.v1_8_R3.PacketPlayOutSetSlot;
import net.minecraft.server.v1_8_R3.PacketPlayOutWindowItems;
/**
* Manages creation and retrieval of associated {@link PlayerGear}s with online players, as well
* as offering methods for parsing and handling {@link CustomItem}s.
* 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, Runnable
{
private static final String ITEM_SERIALIZATION_TAG = "-JSON-";
private static Gson _gson;
private static GearManager _instance; // Singleton instance
private static GearManager _instance; // Singleton instance
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<ItemType> _typeWeights; // Weightings for randomly selecting item type (legendary/weapon/armor/bow)
private Set<String> _creativePlayers; // Set of names for all players currently in Creative gamemode
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<ItemType> _typeWeights; // Weightings for randomly
// selecting item type
// (legendary/weapon/armor/bow)
private Set<String> _creativePlayers; // Set of names for all players
// currently in Creative gamemode
// Legendary generation
private WeightSet<Class<? extends LegendaryItem>> _legendaryWeights;
@ -98,7 +106,7 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
private WeightSet<Class<? extends ItemAttribute>> _weaponAttributes;
private WeightSet<Class<? extends ItemAttribute>> _armorAttributes;
private WeightSet<Class<? extends ItemAttribute>> _bowAttributes;
private GearShop _shop;
public GearManager(JavaPlugin plugin, PacketHandler packetHandler, CoreClientManager clientManager, DonationManager donationManager)
@ -111,39 +119,30 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
_creativePlayers = new HashSet<String>();
_playerGears = new HashMap<String, PlayerGear>();
// TODO: Introduce configurable non-hardcoded values for generation weights?
// TODO: Introduce configurable non-hardcoded values for generation
// weights?
_attributeWeights = new WeightSet<Integer>(new Weight<Integer>(3, 3), new Weight<Integer>(20, 2), new Weight<Integer>(77, 1));
_typeWeights = new WeightSet<ItemType>(new Weight<ItemType>(10, ItemType.LEGENDARY),
new Weight<ItemType>(45, ItemType.ARMOR),
new Weight<ItemType>(23, ItemType.WEAPON),
new Weight<ItemType>(22, ItemType.BOW));
_typeWeights = new WeightSet<ItemType>(new Weight<ItemType>(10, ItemType.LEGENDARY), new Weight<ItemType>(45, ItemType.ARMOR), new Weight<ItemType>(23, ItemType.WEAPON), new Weight<ItemType>(22, ItemType.BOW));
// Weapon-based attributes
_weaponAttributes = new WeightSet<Class<? extends ItemAttribute>>(FrostedAttribute.class, SharpAttribute.class,
JaggedAttribute.class, HasteAttribute.class, FlamingAttribute.class, ConqueringAttribute.class);
_weaponAttributes = new WeightSet<Class<? extends ItemAttribute>>(FrostedAttribute.class, SharpAttribute.class, JaggedAttribute.class, HasteAttribute.class, FlamingAttribute.class, ConqueringAttribute.class);
// Armor-based attributes
_armorAttributes = new WeightSet<Class<? extends ItemAttribute>>(SlantedAttribute.class, ReinforcedAttribute.class,
ConqueringArmorAttribute.class, PaddedAttribute.class, LavaAttribute.class);
// Armor-based attributes
_armorAttributes = new WeightSet<Class<? extends ItemAttribute>>(SlantedAttribute.class, ReinforcedAttribute.class, ConqueringArmorAttribute.class, PaddedAttribute.class, LavaAttribute.class);
// Bow-based attributes
_bowAttributes = new WeightSet<Class<? extends ItemAttribute>>(HeavyArrowsAttribute.class, HuntingAttribute.class, InverseAttribute.class,
LeechingAttribute.class, RecursiveAttribute.class, ScorchingAttribute.class, SlayingAttribute.class);
_bowAttributes = new WeightSet<Class<? extends ItemAttribute>>(HeavyArrowsAttribute.class, HuntingAttribute.class, InverseAttribute.class, LeechingAttribute.class, RecursiveAttribute.class, ScorchingAttribute.class, SlayingAttribute.class);
// Weapon material types
_weaponTypes = new WeightSet<Material>(Material.DIAMOND_SWORD, Material.GOLD_SWORD, Material.IRON_SWORD, Material.STONE_SWORD,
Material.DIAMOND_AXE, Material.GOLD_AXE, Material.IRON_AXE, Material.STONE_AXE);
_weaponTypes = new WeightSet<Material>(Material.DIAMOND_SWORD, Material.GOLD_SWORD, Material.IRON_SWORD, Material.STONE_SWORD, Material.DIAMOND_AXE, Material.GOLD_AXE, Material.IRON_AXE, Material.STONE_AXE);
// Armor material types
_armorTypes = new WeightSet<Material>(Material.DIAMOND_HELMET, Material.DIAMOND_CHESTPLATE, Material.DIAMOND_LEGGINGS, Material.DIAMOND_BOOTS,
Material.IRON_HELMET, Material.IRON_CHESTPLATE, Material.IRON_LEGGINGS, Material.IRON_BOOTS,
Material.GOLD_HELMET, Material.GOLD_CHESTPLATE, Material.GOLD_LEGGINGS, Material.GOLD_BOOTS);
_armorTypes = new WeightSet<Material>(Material.DIAMOND_HELMET, Material.DIAMOND_CHESTPLATE, Material.DIAMOND_LEGGINGS, Material.DIAMOND_BOOTS, Material.IRON_HELMET, Material.IRON_CHESTPLATE, Material.IRON_LEGGINGS, Material.IRON_BOOTS, Material.GOLD_HELMET, Material.GOLD_CHESTPLATE, Material.GOLD_LEGGINGS, Material.GOLD_BOOTS);
// TODO: Initialize list of attributes and types
// Initialize various LegendaryItem types
_legendaryWeights = new WeightSet<Class<? extends LegendaryItem>>(AlligatorsTooth.class, WindBlade.class,
GiantsBroadsword.class, HyperBlade.class, MagneticBlade.class);
_legendaryWeights = new WeightSet<Class<? extends LegendaryItem>>(AlligatorsTooth.class, WindBlade.class, GiantsBroadsword.class, HyperBlade.class, MagneticBlade.class);
// TODO: Add rest of legendaries, find better way?
// Register listeners
@ -151,8 +150,7 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
UtilServer.getServer().getPluginManager().registerEvents(new SmeltingListener(), getPlugin());
// Initialize attribute types factory for JSON handling of polymorphism.
RuntimeTypeAdapterFactory<ItemAttribute> attributeFactory = RuntimeTypeAdapterFactory
.of(ItemAttribute.class);
RuntimeTypeAdapterFactory<ItemAttribute> attributeFactory = RuntimeTypeAdapterFactory.of(ItemAttribute.class);
for (Class<? extends ItemAttribute> attributeType : _armorAttributes.elements())
{
@ -167,9 +165,9 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
attributeFactory.registerSubtype(attributeType);
}
// Initialize legendary item type factory for JSON handling of polymorphism.
RuntimeTypeAdapterFactory<CustomItem> customItemType = RuntimeTypeAdapterFactory
.of(CustomItem.class);
// Initialize legendary item type factory for JSON handling of
// polymorphism.
RuntimeTypeAdapterFactory<CustomItem> customItemType = RuntimeTypeAdapterFactory.of(CustomItem.class);
customItemType.registerSubtype(CustomItem.class);
customItemType.registerSubtype(LegendaryItem.class);
customItemType.registerSubtype(GoldToken.class);
@ -179,11 +177,8 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
}
// Build GSON instance off factories for future serialization of items.
_gson = new GsonBuilder()
.registerTypeAdapterFactory(attributeFactory)
.registerTypeAdapterFactory(customItemType)
.create();
_gson = new GsonBuilder().registerTypeAdapterFactory(attributeFactory).registerTypeAdapterFactory(customItemType).create();
packetHandler.addPacketHandler(this, PacketPlayOutSetSlot.class, PacketPlayOutWindowItems.class);
plugin.getServer().getScheduler().runTaskTimer(plugin, this, 1l, 1l);
@ -208,7 +203,8 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
}
/**
* Tick & update internal logic for {@link GearManager}. Called once per tick.
* Tick & update internal logic for {@link GearManager}. Called once per
* tick.
*/
@Override
public void run()
@ -230,7 +226,8 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
/**
* @param player - the player whose {@link PlayerGear} set is to be fetched.
* @return the cached or newly instantiated {@link PlayerGear} associated with {@code player}.
* @return the cached or newly instantiated {@link PlayerGear} associated
* with {@code player}.
*/
public PlayerGear getPlayerGear(Player player)
{
@ -268,20 +265,26 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
factory.setType(Material.BOW);
}
if (itemType != ItemType.LEGENDARY) // Only non-legendaries have attributes
if (itemType != ItemType.LEGENDARY) // Only non-legendaries have
// attributes
{
AttributeContainer attributes = new AttributeContainer();
generateAttributes(attributes, itemType, attributeCount);
if (attributes.getSuperPrefix()!= null){
if (attributes.getSuperPrefix() != null)
{
factory.setSuperPrefix(attributes.getSuperPrefix().getClass());
}
if (attributes.getPrefix() != null){
if (attributes.getPrefix() != null)
{
factory.setPrefix(attributes.getPrefix().getClass());
}if (attributes.getSuffix() != null){
factory.setSuffix(attributes.getSuffix().getClass());
} }
}
if (attributes.getSuffix() != null)
{
factory.setSuffix(attributes.getSuffix().getClass());
}
}
return factory.getWrapper();
}
@ -299,22 +302,23 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
switch (type)
{
case ARMOR:
sampleAttribute = instantiate(_armorAttributes.generateRandom());
break;
case WEAPON:
sampleAttribute = instantiate(_weaponAttributes.generateRandom());
break;
case BOW:
sampleAttribute = instantiate(_bowAttributes.generateRandom());
break;
default:
break;
case ARMOR:
sampleAttribute = instantiate(_armorAttributes.generateRandom());
break;
case WEAPON:
sampleAttribute = instantiate(_weaponAttributes.generateRandom());
break;
case BOW:
sampleAttribute = instantiate(_bowAttributes.generateRandom());
break;
default:
break;
}
if (sampleAttribute != null && remaining.contains(sampleAttribute.getType()))
{
attribute = sampleAttribute; // Select valid attribute to add
attribute = sampleAttribute; // Select valid attribute to
// add
}
}
@ -337,7 +341,7 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
CustomItem customItem = null;
try
try
{
customItem = deserialize(serialization);
}
@ -352,7 +356,8 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
return customItem;
}
return null; // No serialization found in item's lore, not a custom item!
return null; // No serialization found in item's lore, not a custom
// item!
}
public static boolean isCustomItem(ItemStack item)
@ -368,31 +373,33 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
}
/**
* @param type - the class-type of the object to be instantiated. (must have zero-argument constructor)
* @return a newly instantiated instance of {@code type} class-type. Instantied with zero argument constructor.
* @param type - the class-type of the object to be instantiated. (must have
* zero-argument constructor)
* @return a newly instantiated instance of {@code type} class-type.
* Instantied with zero argument constructor.
*/
private static <T> T instantiate(Class<T> type)
{
try
try
{
return type.newInstance();
}
catch (Exception e)
}
catch (Exception e)
{
return null;
}
}
}
private static String getItemSerialization(ItemStack item)
{
if (item == null || item.getItemMeta() == null
|| item.getItemMeta().getLore() == null) return null;
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
if (lore.startsWith(ITEM_SERIALIZATION_TAG)) // Found serialization
// lore-line
{
int tagLength = ITEM_SERIALIZATION_TAG.length();
String serialization = lore.substring(tagLength);
@ -401,7 +408,8 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
}
}
return null; // Unable to find any serialized lore lines, hence not a CustomItem.
return null; // Unable to find any serialized lore lines, hence not a
// CustomItem.
}
public static String serialize(CustomItem customItem)
@ -438,27 +446,30 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
}
/**
* @param player - the player to see if they should have their out-going packets
* masked on CustomGear items.
* @return true, if the player should have their gear lore masked, false otherwise.
* @param player - the player to see if they should have their out-going
* packets masked on CustomGear items.
* @return true, if the player should have their gear lore masked, false
* otherwise.
*/
private boolean maskGearPacket(Player player)
{
return player.getGameMode() != GameMode.CREATIVE && !_creativePlayers.contains(player.getName());
}
@Override
public void handle(PacketInfo packetInfo)
{
// Don't mask custom gear lore for creative players, as this will break them.
// Don't mask custom gear lore for creative players, as this will break
// them.
if (!maskGearPacket(packetInfo.getPlayer())) return;
Packet packet = packetInfo.getPacket();
if (packet instanceof PacketPlayOutSetSlot)
{
{
PacketPlayOutSetSlot slotPacket = (PacketPlayOutSetSlot) packet;
slotPacket.c = maskItem(slotPacket.c); // Mask all out-going item packets
slotPacket.c = maskItem(slotPacket.c); // Mask all out-going item
// packets
}
else if (packet instanceof PacketPlayOutWindowItems)
{
@ -466,25 +477,38 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
for (int i = 0; i < itemsPacket.b.length; i++)
{
itemsPacket.b[i] = maskItem(itemsPacket.b[i]); // Mask all out-going item packets
itemsPacket.b[i] = maskItem(itemsPacket.b[i]); // Mask all
// out-going
// item packets
}
}
}
}
private net.minecraft.server.v1_8_R3.ItemStack maskItem(net.minecraft.server.v1_8_R3.ItemStack item)
{
if (item == null) return null; // Cannot mask a null item
if (item == null) return null; // Cannot mask a null item
CraftItemStack originalItem = CraftItemStack.asCraftMirror(item);
ItemMeta originalMeta = originalItem.getItemMeta();
if (originalMeta == null || originalMeta.getLore() == null) return item; // No need to modify item packets with no lore
if (originalMeta == null || originalMeta.getLore() == null) return item; // No
// need
// to
// modify
// item
// packets
// with
// no
// lore
List<String> lore = new ArrayList<String>();
for (String line : originalMeta.getLore())
{
if (!line.startsWith(ITEM_SERIALIZATION_TAG)) // Remove serialization lines from out-going lore
if (!line.startsWith(ITEM_SERIALIZATION_TAG)) // Remove
// serialization
// lines from
// out-going lore
{
lore.add(line);
}
@ -497,7 +521,7 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
newCopy.setItemMeta(newMeta);
return newItem;
}
public void openShop(Player player)
{
_shop.attemptShopOpen(player);