MassiveCore - Working on DataItemStack. Not done yet.

This commit is contained in:
Olof Larsson 2016-04-24 08:39:15 +02:00
parent 5308e63282
commit cfff653649
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
120 changed files with 4883 additions and 566 deletions

View File

@ -1,221 +0,0 @@
package com.massivecraft.massivecore;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.block.banner.Pattern;
import org.bukkit.potion.PotionEffect;
import com.massivecraft.massivecore.collections.MassiveListDef;
import com.massivecraft.massivecore.collections.MassiveTreeMapDef;
import com.massivecraft.massivecore.collections.MassiveTreeSetDef;
import com.massivecraft.massivecore.comparator.ComparatorHashCode;
import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName;
/**
* This class makes use of primitives, collections and maps only.
* All Bukkit specific enumerations and classes are avoided.
* That means this class itself is compatible with all Bukkit server versions.
*
* We also make sure to only initialize variables with null as value.
* Null means "default" and this way we save database space as well as CPU power on class construction.
*
* This class acts as a safe intermediary for database storage.
* It is mainly used by the ItemStackAdapter and InventoryAdapter.
* It can also be used directly, for example in maps, since it provides a stable equals and hash code method (as opposed to Bukkit).
*/
public class ItemData
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
// Immutable Defaults
public static final transient int DEFAULT_ID = 0;
public static final transient int DEFAULT_COUNT = 1;
public static final transient int DEFAULT_DAMAGE = 0;
public static final transient boolean DEFAULT_SCALING = false;
public static final transient boolean DEFAULT_UNBREAKABLE = false;
// -------------------------------------------- //
// FIELDS > BASIC
// -------------------------------------------- //
private Integer id = null;
public int getId() { return (this.id == null ? DEFAULT_ID : this.id); }
public ItemData setId(int id) { this.id = (id == DEFAULT_ID ? null : id); return this; }
private Integer count = null;
public int getCount() { return (this.count == null ? DEFAULT_COUNT : this.count); }
public ItemData setCount(int count) { this.count = (count == DEFAULT_COUNT ? null : count); return this; }
private Integer damage = null;
public int getDamage() { return (this.damage == null ? DEFAULT_DAMAGE : this.damage); }
public ItemData setDamage(int damage) { this.damage = (damage == DEFAULT_DAMAGE ? null: damage); return this; }
// -------------------------------------------- //
// FIELDS > UNSPECIFIC
// -------------------------------------------- //
private String name = null;
public String getName() { return this.name; }
public ItemData setName(String name) { this.name = name; return this; }
private MassiveListDef<String> lore = null;
public List<String> getLore() { return this.lore; }
public ItemData setLore(Collection<String> lore) { this.lore = (lore == null ? null : new MassiveListDef<>(lore)); return this;}
// TODO: Can I create a string comparator and use that one instead? HashCode looks ugly.
private MassiveTreeMapDef<String, Integer, ComparatorHashCode> enchants = null;
public Map<String, Integer> getEnchants() { return this.enchants; }
public ItemData setEnchants(Map<String, Integer> enchants) { this.enchants = (enchants == null ? null : new MassiveTreeMapDef<String, Integer, ComparatorHashCode>(ComparatorHashCode.get(), enchants)); return this; }
private Integer repaircost = null;
public Integer getRepaircost() { return this.repaircost; }
public ItemData setRepaircost(int repaircost) { this.repaircost = repaircost; return this; }
// -------------------------------------------- //
// FIELDS > BOOK
// -------------------------------------------- //
private String title = null;
public String getTitle() { return this.title; }
public ItemData setTitle(String title) { this.title = title; return this; }
private String author = null;
public String getAuthor() { return this.author; }
public ItemData setAuthor(String author) { this.author = author; return this; }
private MassiveListDef<String> pages = new MassiveListDef<>();
public List<String> getPages() { return this.pages; }
public ItemData setPages(Collection<String> bookPages) { this.pages = new MassiveListDef<>(bookPages); return this; }
// -------------------------------------------- //
// FIELDS > LEATHER ARMOR
// -------------------------------------------- //
// TODO: Color is not a primitive... convert into using a primitive!
// TODO: We must also figure out the int value of the DefaultLeatherColor!
private Color color = null;
public Color getColor() { return (this.color == null ? Bukkit.getItemFactory().getDefaultLeatherColor() : this.color); }
public ItemData setColor(Color color) { this.color = (Bukkit.getItemFactory().getDefaultLeatherColor().equals(color) ? null : color); return this; }
// -------------------------------------------- //
// FIELDS > MAP
// -------------------------------------------- //
private Boolean scaling = null;
public boolean isScaling() { return (this.scaling == null ? DEFAULT_SCALING : this.scaling); }
public ItemData setScaling(boolean scaling) { this.scaling = (scaling == DEFAULT_SCALING ? null : scaling); return this; }
// -------------------------------------------- //
// FIELDS > POTION
// -------------------------------------------- //
// TODO: Create and use PotionEffectData!
@SerializedName("potion-effects")
private List<PotionEffect> potionEffects = null;
public List<PotionEffect> getPotionEffects() { return this.potionEffects; }
public ItemData setPotionEffects(Collection<PotionEffect> potionEffects) { this.potionEffects = (potionEffects == null ? null : new MassiveListDef<>(potionEffects)); return this; }
// -------------------------------------------- //
// FIELDS > SKULL
// -------------------------------------------- //
private String skull = null;
public String getSkull() { return this.skull; }
public ItemData setSkull(String skull) { this.skull = skull; return this; }
// -------------------------------------------- //
// FIELDS > FIREWORK EFFECT
// -------------------------------------------- //
// TODO
// -------------------------------------------- //
// FIELDS > FIREWORK
// -------------------------------------------- //
// TODO
// -------------------------------------------- //
// FIELDS > STORED ENCHANTS
// -------------------------------------------- //
// TODO: Can I create a string comparator and use that one instead? HashCode looks ugly.
@SerializedName("stored-enchants")
private MassiveTreeMapDef<String, Integer, ComparatorHashCode> storedEnchants = null;
public Map<String, Integer> getStoredEnchants() { return this.storedEnchants; }
public ItemData setStoredEnchants(Map<String, Integer> storedEnchants) { this.storedEnchants = (storedEnchants == null ? null : new MassiveTreeMapDef<String, Integer, ComparatorHashCode>(ComparatorHashCode.get(), storedEnchants)); return this; }
// -------------------------------------------- //
// FIELDS > UNBREAKABLE
// -------------------------------------------- //
// SINCE: 1.8
private Boolean unbreakable = null;
public boolean isUnbreakable() { return (this.unbreakable == null ? DEFAULT_UNBREAKABLE : this.unbreakable); }
public ItemData setUnbreakable(boolean unbreakable) { this.unbreakable = (unbreakable == DEFAULT_UNBREAKABLE ? null : unbreakable); return this; }
// -------------------------------------------- //
// FIELDS > FLAGS
// -------------------------------------------- //
// SINCE: 1.8
// TODO: Can I create a string comparator and use that one instead? HashCode looks ugly.
private MassiveTreeSetDef<String, ComparatorHashCode> flags = null;
public Set<String> getFlags() { return this.flags; }
public ItemData setFlags(Collection<String> flags) { this.flags = (flags == null ? null : new MassiveTreeSetDef<String, ComparatorHashCode>(ComparatorHashCode.get(), flags)); return this; }
// -------------------------------------------- //
// FIELDS > BANNER BASE
// -------------------------------------------- //
// SINCE: 1.8
// The integer is the dye color byte representation.
@SerializedName("banner-base")
private Integer bannerBase = null;
public Integer getBannerBase() { return this.bannerBase; }
public ItemData setBannerBase(Integer bannerBase) { this.bannerBase = bannerBase; return this; }
// -------------------------------------------- //
// FIELDS > BANNER PATTERNS
// -------------------------------------------- //
// SINCE: 1.8
// This should really be a list and not a set.
// The order matters and is explicitly assigned.
// TODO: The Pattern class can not be used here. It breaks 1.8 compatibility.
// TODO: Convert to to use only raw primitiveish data!
// TODO: I actually decided to use a list of integers. That should be mimiced here.
@SerializedName("banner")
private MassiveListDef<Pattern> bannerPatterns = null;
public List<Pattern> getBannerPatterns() { return this.bannerPatterns; }
public ItemData setBannerPatterns(Collection<Pattern> bannerPatterns) { this.bannerPatterns = (bannerPatterns == null ? null : new MassiveListDef<>(bannerPatterns)); return this;}
// -------------------------------------------- //
// FIELDS > POTION
// -------------------------------------------- //
// SINCE: 1.9
private String potion = null;
public String getPotion() { return this.potion; }
public ItemData setPotion(String potion) { this.potion = potion; return this; }
// -------------------------------------------- //
// CONVERT
// -------------------------------------------- //
// TODO: Add in covert methods... they will have to use mixins for transfering!
// -------------------------------------------- //
// HASH CODE & EQUALS
// -------------------------------------------- //
// TODO
}

View File

@ -61,6 +61,10 @@ import com.massivecraft.massivecore.engine.EngineMassiveCoreTeleportMixinCause;
import com.massivecraft.massivecore.engine.EngineMassiveCoreVariable; import com.massivecraft.massivecore.engine.EngineMassiveCoreVariable;
import com.massivecraft.massivecore.engine.EngineMassiveCoreWorldNameSet; import com.massivecraft.massivecore.engine.EngineMassiveCoreWorldNameSet;
import com.massivecraft.massivecore.integration.vault.IntegrationVault; import com.massivecraft.massivecore.integration.vault.IntegrationVault;
import com.massivecraft.massivecore.item.WriterBannerPattern;
import com.massivecraft.massivecore.item.WriterFireworkEffect;
import com.massivecraft.massivecore.item.WriterItemStack;
import com.massivecraft.massivecore.item.WriterPotionEffect;
import com.massivecraft.massivecore.mixin.MixinActionbar; import com.massivecraft.massivecore.mixin.MixinActionbar;
import com.massivecraft.massivecore.mixin.MixinActual; import com.massivecraft.massivecore.mixin.MixinActual;
import com.massivecraft.massivecore.mixin.MixinCommand; import com.massivecraft.massivecore.mixin.MixinCommand;
@ -210,6 +214,12 @@ public class MassiveCore extends MassivePlugin
MassiveCoreMConfColl.class, MassiveCoreMConfColl.class,
MassiveCoreMSponsorInfoColl.class, MassiveCoreMSponsorInfoColl.class,
// Writer,
WriterItemStack.class,
WriterPotionEffect.class,
WriterFireworkEffect.class,
WriterBannerPattern.class,
// Engine // Engine
EngineMassiveCoreChestGui.class, EngineMassiveCoreChestGui.class,
EngineMassiveCoreCollTick.class, EngineMassiveCoreCollTick.class,

View File

@ -131,6 +131,12 @@ public class MassiveCoreMConf extends Entity<MassiveCoreMConf>
public WriteConcern getMongoDbWriteConcernSave() { return getMongoDbWriteConcern(this.catchingMongoDbErrorsOnSave); } public WriteConcern getMongoDbWriteConcernSave() { return getMongoDbWriteConcern(this.catchingMongoDbErrorsOnSave); }
public WriteConcern getMongoDbWriteConcernDelete() { return getMongoDbWriteConcern(this.catchingMongoDbErrorsOnDelete); } public WriteConcern getMongoDbWriteConcernDelete() { return getMongoDbWriteConcern(this.catchingMongoDbErrorsOnDelete); }
// -------------------------------------------- //
// DEBUG
// -------------------------------------------- //
public boolean debugWriters = false;
// -------------------------------------------- // // -------------------------------------------- //
// SPONSOR // SPONSOR
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -4,8 +4,10 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Objects;
import com.massivecraft.massivecore.comparator.ComparatorHashCode; import com.massivecraft.massivecore.comparator.ComparatorIdentity;
import com.massivecraft.massivecore.comparator.ComparatorPrioritized;
import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.MUtil;
public class PriorityLines implements Prioritized, Comparable<PriorityLines> public class PriorityLines implements Prioritized, Comparable<PriorityLines>
@ -55,47 +57,46 @@ public class PriorityLines implements Prioritized, Comparable<PriorityLines>
} }
// -------------------------------------------- // // -------------------------------------------- //
// COMPARABLE // COMPARABLE & EQUALS & HASHCODE
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public int compareTo(PriorityLines that) public int compareTo(PriorityLines that)
{ {
int ret; // Create
int ret = 0;
ret = Integer.compare(this.priority, that.priority); // Fill
ret = ComparatorPrioritized.get().compare(this, that);
if (ret != 0) return ret; if (ret != 0) return ret;
if (MUtil.equals(this.lines, that.lines)) return 0; ret = ComparatorIdentity.get().compare(this, that);
ret = ComparatorHashCode.get().compare(this.lines, that.lines);
if (ret != 0) return ret; if (ret != 0) return ret;
// Return
return ret; return ret;
} }
// -------------------------------------------- // @Override
// HASH CODE & EQUALS public boolean equals(Object object)
// -------------------------------------------- // {
if ( ! (object instanceof PriorityLines)) return false;
PriorityLines that = (PriorityLines)object;
return MUtil.equals(
this.getPriority(), that.getPriority(),
this.getLines(), that.getLines()
);
}
@Override @Override
public int hashCode() public int hashCode()
{ {
final int prime = 31; return Objects.hash(
int result = 1; this.getPriority(),
result = prime * result + ((lines == null) ? 0 : lines.hashCode()); this.getLines()
result = prime * result + priority; );
return result;
} }
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (!(obj instanceof PriorityLines)) return false;
PriorityLines other = (PriorityLines) obj;
if (priority != other.priority) return false;
if ( ! MUtil.equals(this.lines, other.lines)) return false;
return true;
}
} }

View File

@ -22,7 +22,7 @@ import com.massivecraft.massivecore.command.editor.CommandEditAbstract;
import com.massivecraft.massivecore.command.editor.CommandEditSimple; import com.massivecraft.massivecore.command.editor.CommandEditSimple;
import com.massivecraft.massivecore.command.editor.EditSettings; import com.massivecraft.massivecore.command.editor.EditSettings;
import com.massivecraft.massivecore.command.editor.Property; import com.massivecraft.massivecore.command.editor.Property;
import com.massivecraft.massivecore.comparator.ComparatorHashCode; import com.massivecraft.massivecore.comparator.ComparatorSmart;
import com.massivecraft.massivecore.mson.Mson; import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.store.SenderEntity; import com.massivecraft.massivecore.store.SenderEntity;
import com.massivecraft.massivecore.util.ContainerUtil; import com.massivecraft.massivecore.util.ContainerUtil;
@ -518,7 +518,7 @@ public abstract class TypeAbstract<T> implements Type<T>
{ {
if (this.elementComparator != null) return (Comparator<E>) this.elementComparator; if (this.elementComparator != null) return (Comparator<E>) this.elementComparator;
if (this.isContainerIndexed()) return null; if (this.isContainerIndexed()) return null;
return (Comparator<E>) ComparatorHashCode.get().getLenient(); return (Comparator<E>) ComparatorSmart.get();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override

View File

@ -2,92 +2,138 @@ package com.massivecraft.massivecore.comparator;
import java.util.Comparator; import java.util.Comparator;
import com.massivecraft.massivecore.Identified; public abstract class ComparatorAbstract<T> implements Comparator<T>
import com.massivecraft.massivecore.Named;
import com.massivecraft.massivecore.Prioritized;
import com.massivecraft.massivecore.util.MUtil;
public class ComparatorAbstract<T> implements Comparator<T>
{ {
// -------------------------------------------- // // -------------------------------------------- //
// REVERSED // SMART
// -------------------------------------------- // // -------------------------------------------- //
// Smart comparators falls back to generalized Object comparison solutions we have implemented.
// They just try to compare better if the initial comparison does not find a difference.
// Note that they leave equals be. Objects that actually are equal will remain that way.
private ComparatorReversed<T> reversed = null; private boolean smart = false;
public ComparatorAbstract<T> getReversed() public boolean isSmart() { return this.smart; }
{ public ComparatorAbstract<T> setSmart(boolean smart) { this.smart = smart; return this; }
if (this.reversed == null) this.reversed = ComparatorReversed.get(this);
return this.reversed;
}
// -------------------------------------------- // // -------------------------------------------- //
// LENIENT // LENIENT
// -------------------------------------------- // // -------------------------------------------- //
// Lenient comparators will not ever accept 0 as a return value.
// The only common user case is when sorting map entries by value.
private ComparatorLenient<T> lenient = null; private boolean lenient = false;
public ComparatorAbstract<T> getLenient() public boolean isLenient() { return this.lenient; }
{ public ComparatorAbstract<T> setLenient(boolean lenient) { this.lenient = lenient; return this; }
if (this.lenient == null) this.lenient = ComparatorLenient.get(this);
return this.lenient;
}
// -------------------------------------------- // // -------------------------------------------- //
// OVERRIDE // REVERSED
// -------------------------------------------- //
// Reversed comparators multiply the return value with -1.
private boolean reversed = false;
public boolean isReversed() { return this.reversed; }
public ComparatorAbstract<T> setReversed(boolean reversed) { this.reversed = reversed; return this; }
// -------------------------------------------- //
// COMPARE
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public int compare(T type1, T type2) public int compare(T object1, T object2)
{ {
Integer ret; // Create
int ret = compareSystem(object1, object2);
// Null // Lenient
ret = MUtil.compareNulls(type1, type2); if (this.isLenient() && ret == 0)
if (ret != null) return ret;
// Inner
ret = this.compareInner(type1, type2);
if (ret != null) return ret;
// Prioritized
if (type1 instanceof Prioritized)
{ {
Prioritized prioritized1 = (Prioritized)type1; ret = ComparatorIdentity.get().compare(object1, object2);
Prioritized prioritized2 = (Prioritized)type2; if (ret == 0) ret = 1;
ret = Integer.compare(prioritized1.getPriority(), prioritized2.getPriority());
if (ret != null) return ret;
} }
// Named // Reversed
if (type1 instanceof Named) if (this.isReversed())
{ {
Named named1 = (Named)type1; ret *= -1;
Named named2 = (Named)type2;
ret = ComparatorNaturalOrder.get().compare(named1.getName(), named2.getName());
if (ret != null) return ret;
} }
// Identified // Return
if (type1 instanceof Identified) return ret;
{
Identified identified1 = (Identified)type1;
Identified identified2 = (Identified)type2;
ret = MUtil.compare(identified1.getId(), identified2.getId());
if (ret != null) return ret;
}
// Identity
return ComparatorIdentity.get().compare(type1, type2);
} }
// -------------------------------------------- // // -------------------------------------------- //
// INNER // COMPARE > SYSTEM
// -------------------------------------------- // // -------------------------------------------- //
public Integer compareInner(T type1, T type2) private int compareSystem(T object1, T object2)
{ {
return null; // Create
int ret = 0;
// Null
if (object1 == null && object2 == null) return 0;
if (object1 == null) return -1;
if (object2 == null) return +1;
// Inner
ret = this.compareInner(object1, object2);
if (ret != 0) return ret;
// Smart
if (this.isSmart())
{
ret = ComparatorPrioritized.get().compare(object1, object2);
if (ret != 0) return ret;
ret = ComparatorNamed.get().compare(object1, object2);
if (ret != 0) return ret;
ret = ComparatorIdentified.get().compare(object1, object2);
if (ret != 0) return ret;
ret = ComparatorComparable.get().compare(object1, object2);
if (ret != 0) return ret;
ret = ComparatorCollection.get().compare(object1, object2);
if (ret != 0) return ret;
ret = ComparatorMap.get().compare(object1, object2);
if (ret != 0) return ret;
}
// Return
return ret;
}
// -------------------------------------------- //
// COMPARE > INNER
// -------------------------------------------- //
public abstract int compareInner(T type1, T type2);
// -------------------------------------------- //
// UTILITY
// -------------------------------------------- //
@SuppressWarnings("unchecked")
public int compare(T... objects)
{
if (objects == null) throw new NullPointerException("objects");
if (objects.length % 2 != 0) throw new IllegalArgumentException("objects length not even");
int index = 1;
while (index < objects.length)
{
T object1 = objects[index - 1];
T object2 = objects[index];
int ret = this.compare(object1, object2);
if (ret != 0) return ret;
index += 2;
}
return 0;
} }
} }

View File

@ -18,7 +18,7 @@ public abstract class ComparatorAbstractTransformer<T, X> extends ComparatorAbst
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public Integer compareInner(T type1, T type2) public int compareInner(T type1, T type2)
{ {
X x1 = this.transform(type1); X x1 = this.transform(type1);
X x2 = this.transform(type2); X x2 = this.transform(type2);

View File

@ -14,7 +14,7 @@ public class ComparatorCaseInsensitive extends ComparatorAbstract<String>
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public Integer compareInner(String string1, String string2) public int compareInner(String string1, String string2)
{ {
return String.CASE_INSENSITIVE_ORDER.compare(string1, string2); return String.CASE_INSENSITIVE_ORDER.compare(string1, string2);
} }

View File

@ -0,0 +1,68 @@
package com.massivecraft.massivecore.comparator;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import com.massivecraft.massivecore.collections.MassiveList;
public class ComparatorCollection extends ComparatorAbstract<Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ComparatorCollection i = new ComparatorCollection();
public static ComparatorCollection get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings("unchecked")
@Override
public int compareInner(Object object1, Object object2)
{
// Create
int ret = 0;
// Instance Of
Collection<Object> collection1 = null;
Collection<Object> collection2 = null;
if (object1 instanceof Collection<?>) collection1 = (Collection<Object>)object1;
if (object2 instanceof Collection<?>) collection2 = (Collection<Object>)object2;
ret = ComparatorNull.get().compare(collection1, collection2);
if (ret != 0) return ret;
if (collection1 == null && collection2 == null) return ret;
// Size
int size1 = collection1.size();
int size2 = collection2.size();
ret = Integer.compare(size1, size2);
if (ret != 0) return ret;
// Elements
List<Object> elements1 = new MassiveList<>(collection1);
Collections.sort(elements1, ComparatorSmart.get());
List<Object> elements2 = new MassiveList<>(collection2);
Collections.sort(elements2, ComparatorSmart.get());
Iterator<Object> iterator1 = elements1.iterator();
Iterator<Object> iterator2 = elements1.iterator();
while (iterator1.hasNext())
{
Object element1 = iterator1.next();
Object element2 = iterator2.next();
ret = ComparatorSmart.get().compare(element1, element2);
if (ret != 0) return ret;
}
// Return
return ret;
}
}

View File

@ -0,0 +1,55 @@
package com.massivecraft.massivecore.comparator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ComparatorCombined<T> extends ComparatorAbstract<T>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
@SafeVarargs
public static <T> ComparatorCombined<T> get(Comparator<? super T>... comparators) { return new ComparatorCombined<>(comparators); }
public static <T> ComparatorCombined<T> get(List<Comparator<? super T>> comparators) { return new ComparatorCombined<>(comparators); }
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private List<Comparator<? super T>> comparators = null;
public List<Comparator<? super T>> getComparators() { return this.comparators; }
public ComparatorCombined<T> setComparators(List<Comparator<? super T>> comparators) { this.comparators = comparators; return this; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
@SafeVarargs
public ComparatorCombined(Comparator<? super T>... comparators)
{
this(Arrays.asList(comparators));
}
public ComparatorCombined(List<Comparator<? super T>> comparators)
{
this.comparators = comparators;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public int compareInner(T object1, T object2)
{
for (Comparator<? super T> comparator : this.getComparators())
{
int ret = comparator.compare(object1, object2);
if (ret != 0) return ret;
}
return 0;
}
}

View File

@ -0,0 +1,40 @@
package com.massivecraft.massivecore.comparator;
public class ComparatorComparable extends ComparatorAbstract<Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static transient ComparatorComparable i = new ComparatorComparable();
public static ComparatorComparable get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings("unchecked")
@Override
public int compareInner(Object object1, Object object2)
{
// Create
int ret = 0;
// Instance Of
Comparable<Object> comparable1 = null;
Comparable<Object> comparable2 = null;
if (object1 instanceof Comparable) comparable1 = (Comparable<Object>)object1;
if (object2 instanceof Comparable) comparable2 = (Comparable<Object>)object2;
ret = ComparatorNull.get().compare(comparable1, comparable2);
if (ret != 0) return ret;
if (comparable1 == null && comparable2 == null) return ret;
// Compare
ret = comparable1.compareTo(comparable2);
if (ret != 0) return ret;
// Return
return ret;
}
}

View File

@ -1,39 +0,0 @@
package com.massivecraft.massivecore.comparator;
import java.util.Comparator;
import com.massivecraft.massivecore.store.Entity;
public class ComparatorEntityId implements Comparator<Entity<?>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ComparatorEntityId i = new ComparatorEntityId();
public static ComparatorEntityId get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public int compare(Entity<?> e1, Entity<?> e2)
{
if (e1 == null && e2 == null) return 0;
if (e1 == null) return -1;
if (e2 == null) return +1;
if (e1.equals(e2)) return 0;
String id1 = e1.getId();
String id2 = e2.getId();
int ret = ComparatorNaturalOrder.get().compare(id1, id2);
if (ret != 0) return ret;
// We should only return 0 if the items actually are equal.
return e2.hashCode() - e1.hashCode();
}
}

View File

@ -8,7 +8,7 @@ public class ComparatorHashCode extends ComparatorAbstract<Object>
// INSTANCE & CONSTRUCT // INSTANCE & CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
private static transient ComparatorHashCode i = new ComparatorHashCode(); private static ComparatorHashCode i = new ComparatorHashCode();
public static ComparatorHashCode get() { return i; } public static ComparatorHashCode get() { return i; }
// -------------------------------------------- // // -------------------------------------------- //
@ -16,7 +16,7 @@ public class ComparatorHashCode extends ComparatorAbstract<Object>
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public Integer compareInner(Object object1, Object object2) public int compareInner(Object object1, Object object2)
{ {
return Integer.compare(Objects.hashCode(object1), Objects.hashCode(object2)); return Integer.compare(Objects.hashCode(object1), Objects.hashCode(object2));
} }

View File

@ -0,0 +1,43 @@
package com.massivecraft.massivecore.comparator;
import com.massivecraft.massivecore.Identified;
public class ComparatorIdentified extends ComparatorAbstract<Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ComparatorIdentified i = new ComparatorIdentified();
public static ComparatorIdentified get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public int compareInner(Object object1, Object object2)
{
// Create
int ret = 0;
// Instance Of
Identified identified1 = null;
Identified identified2 = null;
if (object1 instanceof Identified) identified1 = (Identified)object1;
if (object2 instanceof Identified) identified2 = (Identified)object2;
ret = ComparatorNull.get().compareInner(identified1, identified2);
if (ret != 0) return ret;
if (identified1 == null && identified2 == null) return ret;
// Id
String id1 = identified1.getId();
String id2 = identified2.getId();
ret = ComparatorComparable.get().compare(id1, id2);
if (ret != 0) return ret;
// Return
return ret;
}
}

View File

@ -0,0 +1,16 @@
package com.massivecraft.massivecore.comparator;
public class ComparatorIdentifiedSmart extends ComparatorIdentified
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ComparatorIdentifiedSmart i = new ComparatorIdentifiedSmart();
public static ComparatorIdentifiedSmart get() { return i; }
public ComparatorIdentifiedSmart()
{
this.setSmart(true);
}
}

View File

@ -6,7 +6,7 @@ public class ComparatorIdentity extends ComparatorAbstract<Object>
// INSTANCE & CONSTRUCT // INSTANCE & CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
private static transient ComparatorIdentity i = new ComparatorIdentity(); private static ComparatorIdentity i = new ComparatorIdentity();
public static ComparatorIdentity get() { return i; } public static ComparatorIdentity get() { return i; }
// -------------------------------------------- // // -------------------------------------------- //
@ -14,7 +14,7 @@ public class ComparatorIdentity extends ComparatorAbstract<Object>
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public Integer compareInner(Object object1, Object object2) public int compareInner(Object object1, Object object2)
{ {
if (object1 == object2) return 0; if (object1 == object2) return 0;
return Integer.compare(System.identityHashCode(object1), System.identityHashCode(object2)); return Integer.compare(System.identityHashCode(object1), System.identityHashCode(object2));

View File

@ -1,44 +0,0 @@
package com.massivecraft.massivecore.comparator;
import java.util.Comparator;
public class ComparatorLenient<T> extends ComparatorAbstractWrapper<T, T>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static <T> ComparatorLenient<T> get(Comparator<T> comparator) { return new ComparatorLenient<T>(comparator); }
public ComparatorLenient(Comparator<T> comparator)
{
super(comparator);
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public int compare(T type1, T type2)
{
int ret;
ret = this.getComparator().compare(type1, type2);
if (ret != 0) return ret;
ret = ComparatorHashCode.get().compare(type1, type2);
if (ret != 0) return ret;
ret = ComparatorIdentity.get().compare(type1, type2);
if (ret != 0) return ret;
return 1;
}
@Override
public ComparatorAbstract<T> getLenient()
{
return this;
}
}

View File

@ -0,0 +1,46 @@
package com.massivecraft.massivecore.comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class ComparatorMap extends ComparatorAbstract<Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ComparatorMap i = new ComparatorMap();
public static ComparatorMap get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings("unchecked")
@Override
public int compareInner(Object object1, Object object2)
{
// Create
int ret = 0;
// Instance Of
Map<Object, Object> map1 = null;
Map<Object, Object> map2 = null;
if (object1 instanceof Map<?, ?>) map1 = (Map<Object, Object>)object1;
if (object2 instanceof Map<?, ?>) map2 = (Map<Object, Object>)object2;
ret = ComparatorNull.get().compare(map1, map2);
if (ret != 0) return ret;
if (map1 == null && map2 == null) return ret;
// Entries
Set<Entry<Object, Object>> entries1 = map1.entrySet();
Set<Entry<Object, Object>> entries2 = map2.entrySet();
ret = ComparatorCollection.get().compare(entries1, entries2);
if (ret != 0) return ret;
// Return
return ret;
}
}

View File

@ -0,0 +1,43 @@
package com.massivecraft.massivecore.comparator;
import com.massivecraft.massivecore.Named;
public class ComparatorNamed extends ComparatorAbstract<Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ComparatorNamed i = new ComparatorNamed();
public static ComparatorNamed get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public int compareInner(Object object1, Object object2)
{
// Create
int ret = 0;
// Instance Of
Named named1 = null;
Named named2 = null;
if (object1 instanceof Named) named1 = (Named)object1;
if (object2 instanceof Named) named2 = (Named)object2;
ret = ComparatorNull.get().compare(named1, named2);
if (ret != 0) return ret;
if (named1 == null && named2 == null) return ret;
// Name
String name1 = named1.getName();
String name2 = named2.getName();
ret = ComparatorNaturalOrder.get().compare(name1, name2);
if (ret != 0) return ret;
// Return
return ret;
}
}

View File

@ -43,7 +43,7 @@ public class ComparatorNaturalOrder extends ComparatorAbstract<Object>
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public Integer compareInner(Object object1, Object object2) public int compareInner(Object object1, Object object2)
{ {
// Martin Pool // Martin Pool
String a = object1.toString(); String a = object1.toString();

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.comparator;
public class ComparatorNull extends ComparatorAbstract<Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ComparatorNull i = new ComparatorNull();
public static ComparatorNull get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public int compareInner(Object object1, Object object2)
{
if (object1 == null && object2 == null) return 0;
if (object1 == null) return -1;
if (object2 == null) return +1;
return 0;
}
}

View File

@ -0,0 +1,44 @@
package com.massivecraft.massivecore.comparator;
import com.massivecraft.massivecore.Prioritized;
public class ComparatorPrioritized extends ComparatorAbstract<Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ComparatorPrioritized i = new ComparatorPrioritized();
public static ComparatorPrioritized get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public int compareInner(Object object1, Object object2)
{
// Create
int ret = 0;
// Instance Of
Prioritized prioritized1 = null;
Prioritized prioritized2 = null;
if (object1 instanceof Prioritized) prioritized1 = (Prioritized)object1;
if (object2 instanceof Prioritized) prioritized2 = (Prioritized)object2;
ret = ComparatorNull.get().compare(prioritized1, prioritized2);
if (ret != 0) return ret;
if (prioritized1 == null && prioritized2 == null) return ret;
// Priority
int priority1 = prioritized1.getPriority();
int priority2 = prioritized2.getPriority();
ret = Integer.compare(priority1, priority2);
if (ret != 0) return ret;
// Return
return ret;
}
}

View File

@ -1,41 +0,0 @@
package com.massivecraft.massivecore.comparator;
import com.massivecraft.massivecore.Prioritized;
import com.massivecraft.massivecore.store.Entity;
public class ComparatorPriority extends ComparatorAbstract<Prioritized>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ComparatorPriority i = new ComparatorPriority();
public static ComparatorPriority get() { return i; }
// -------------------------------------------- //
// OVERRIDE: COMPARATOR
// -------------------------------------------- //
@Override
public Integer compareInner(Prioritized prioritized1, Prioritized prioritized2)
{
// Equals
if (prioritized1.equals(prioritized2)) return 0;
// Priority
Integer ret = Integer.compare(prioritized1.getPriority(), prioritized2.getPriority());
if (ret != 0) return ret;
// Entity Id
if (prioritized1 instanceof Entity<?> && prioritized2 instanceof Entity<?>)
{
Entity<?> entity1 = (Entity<?>)prioritized1;
Entity<?> entity2 = (Entity<?>)prioritized2;
return ComparatorEntityId.get().compare(entity1, entity2);
}
// We should only return 0 if the items actually are equal.
return ComparatorIdentity.get().compare(prioritized1, prioritized2);
}
}

View File

@ -12,6 +12,7 @@ public class ComparatorReversed<T> extends ComparatorAbstractWrapper<T, T>
public ComparatorReversed(Comparator<T> comparator) public ComparatorReversed(Comparator<T> comparator)
{ {
super(comparator); super(comparator);
this.setReversed(true);
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -19,17 +20,9 @@ public class ComparatorReversed<T> extends ComparatorAbstractWrapper<T, T>
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public int compare(T type1, T type2) public int compareInner(T type1, T type2)
{ {
return - this.getComparator().compare(type1, type2); return - this.getComparator().compare(type1, type2);
} }
@Override
public ComparatorAbstract<T> getReversed()
{
Comparator<T> comparator = this.getComparator();
if (comparator instanceof ComparatorAbstract<?>) return (ComparatorAbstract<T>) comparator;
return super.getReversed();
}
} }

View File

@ -0,0 +1,26 @@
package com.massivecraft.massivecore.comparator;
public class ComparatorSmart extends ComparatorAbstract<Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ComparatorSmart i = new ComparatorSmart();
public static ComparatorSmart get() { return i; }
public ComparatorSmart()
{
this.setSmart(true);
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public int compareInner(Object type1, Object type2)
{
return 0;
}
}

View File

@ -0,0 +1,43 @@
package com.massivecraft.massivecore.comparator;
import java.util.List;
public class ComparatorWithList<T> extends ComparatorAbstract<T>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static <T> ComparatorWithList<T> get(List<T> list) { return new ComparatorWithList<>(list); }
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private List<T> list = null;
public List<T> getList() { return this.list; }
public ComparatorWithList<T> setList(List<T> list) { this.list = list; return this; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public ComparatorWithList(List<T> list)
{
this.list = list;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public int compareInner(T object1, T object2)
{
int index1 = this.getList().indexOf(object1);
int index2 = this.getList().indexOf(object2);
return Integer.compare(index1, index2);
}
}

View File

@ -0,0 +1,11 @@
package com.massivecraft.massivecore.item;
public abstract class Converter<X, Y>
{
// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
public abstract Y convert(X x);
}

View File

@ -0,0 +1,26 @@
package com.massivecraft.massivecore.item;
public class ConverterDefault<X, Y> extends Converter<X, Y>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterDefault<Object, Object> i = new ConverterDefault<Object, Object>();
@SuppressWarnings("unchecked")
public static <X, Y> ConverterDefault<X, Y> get() { return (ConverterDefault<X, Y>) i; }
@SuppressWarnings("unchecked")
public static <X, Y> ConverterDefault<X, Y> get(Class<X> cx, Class<Y> cy) { return (ConverterDefault<X, Y>) i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings("unchecked")
@Override
public Y convert(X x)
{
return (Y)x;
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.block.banner.Pattern;
public class ConverterFromBannerPattern extends Converter<Pattern, DataBannerPattern>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromBannerPattern i = new ConverterFromBannerPattern();
public static ConverterFromBannerPattern get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public DataBannerPattern convert(Pattern x)
{
if (x == null) return null;
return new DataBannerPattern(x);
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.block.banner.PatternType;
public class ConverterFromBannerPatternType extends Converter<PatternType, String>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromBannerPatternType i = new ConverterFromBannerPatternType();
public static ConverterFromBannerPatternType get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String convert(PatternType x)
{
if (x == null) return null;
return x.getIdentifier();
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.block.banner.Pattern;
public class ConverterFromBannerPatterns extends ConverterList<Pattern, DataBannerPattern>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromBannerPatterns i = new ConverterFromBannerPatterns();
public static ConverterFromBannerPatterns get() { return i; }
public ConverterFromBannerPatterns()
{
super(ConverterFromBannerPattern.get());
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.Color;
public class ConverterFromColor extends Converter<Color, Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromColor i = new ConverterFromColor();
public static ConverterFromColor get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Integer convert(Color x)
{
if (x == null) return null;
return x.asRGB();
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.Color;
public class ConverterFromColors extends ConverterList<Color, Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromColors i = new ConverterFromColors();
public static ConverterFromColors get() { return i; }
public ConverterFromColors()
{
super(ConverterFromColor.get());
}
}

View File

@ -0,0 +1,26 @@
package com.massivecraft.massivecore.item;
import org.bukkit.DyeColor;
public class ConverterFromDyeColor extends Converter<DyeColor, Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromDyeColor i = new ConverterFromDyeColor();
public static ConverterFromDyeColor get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings("deprecation")
@Override
public Integer convert(DyeColor x)
{
if (x == null) return null;
return Integer.valueOf(x.getDyeData());
}
}

View File

@ -0,0 +1,26 @@
package com.massivecraft.massivecore.item;
import org.bukkit.enchantments.Enchantment;
public class ConverterFromEnchant extends Converter<Enchantment, Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromEnchant i = new ConverterFromEnchant();
public static ConverterFromEnchant get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings("deprecation")
@Override
public Integer convert(Enchantment x)
{
if (x == null) return null;
return x.getId();
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.enchantments.Enchantment;
public class ConverterFromEnchants extends ConverterMap<Enchantment, Integer, Integer, Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromEnchants i = new ConverterFromEnchants();
public static ConverterFromEnchants get() { return i; }
public ConverterFromEnchants()
{
super(ConverterFromEnchant.get(), ConverterDefault.get(Integer.class, Integer.class));
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.FireworkEffect;
public class ConverterFromFireworkEffect extends Converter<FireworkEffect, DataFireworkEffect>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromFireworkEffect i = new ConverterFromFireworkEffect();
public static ConverterFromFireworkEffect get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public DataFireworkEffect convert(FireworkEffect x)
{
if (x == null) return null;
return new DataFireworkEffect(x);
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.FireworkEffect.Type;
public class ConverterFromFireworkEffectType extends Converter<Type, String>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromFireworkEffectType i = new ConverterFromFireworkEffectType();
public static ConverterFromFireworkEffectType get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String convert(Type x)
{
if (x == null) return null;
return x.name();
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.FireworkEffect;
public class ConverterFromFireworkEffects extends ConverterList<FireworkEffect, DataFireworkEffect>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromFireworkEffects i = new ConverterFromFireworkEffects();
public static ConverterFromFireworkEffects get() { return i; }
public ConverterFromFireworkEffects()
{
super(ConverterFromFireworkEffect.get());
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.inventory.ItemFlag;
public class ConverterFromItemFlag extends Converter<ItemFlag, String>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromItemFlag i = new ConverterFromItemFlag();
public static ConverterFromItemFlag get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String convert(ItemFlag x)
{
if (x == null) return null;
return x.name();
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.inventory.ItemFlag;
public class ConverterFromItemFlags extends ConverterSet<ItemFlag, String>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromItemFlags i = new ConverterFromItemFlags();
public static ConverterFromItemFlags get() { return i; }
public ConverterFromItemFlags()
{
super(ConverterFromItemFlag.get());
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.potion.PotionEffect;
public class ConverterFromPotionEffect extends Converter<PotionEffect, DataPotionEffect>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromPotionEffect i = new ConverterFromPotionEffect();
public static ConverterFromPotionEffect get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public DataPotionEffect convert(PotionEffect x)
{
if (x == null) return null;
return new DataPotionEffect(x);
}
}

View File

@ -0,0 +1,26 @@
package com.massivecraft.massivecore.item;
import org.bukkit.potion.PotionEffectType;
public class ConverterFromPotionEffectType extends Converter<PotionEffectType, Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromPotionEffectType i = new ConverterFromPotionEffectType();
public static ConverterFromPotionEffectType get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings("deprecation")
@Override
public Integer convert(PotionEffectType x)
{
if (x == null) return null;
return x.getId();
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.potion.PotionEffect;
public class ConverterFromPotionEffects extends ConverterList<PotionEffect, DataPotionEffect>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterFromPotionEffects i = new ConverterFromPotionEffects();
public static ConverterFromPotionEffects get() { return i; }
public ConverterFromPotionEffects()
{
super(ConverterFromPotionEffect.get());
}
}

View File

@ -0,0 +1,59 @@
package com.massivecraft.massivecore.item;
import java.util.List;
import com.massivecraft.massivecore.collections.MassiveList;
public class ConverterList<EX, EY> extends Converter<Iterable<EX>, List<EY>>
{
// -------------------------------------------- //
// FIELD
// -------------------------------------------- //
private final Converter<EX, EY> converterElement;
public Converter<EX, EY> getConverterElement() { return this.converterElement; }
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
public ConverterList(Converter<EX, EY> converterElement)
{
this.converterElement = converterElement;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public List<EY> convert(Iterable<EX> exs)
{
// Null
if (exs == null) return null;
// Create
List<EY> eys = new MassiveList<>();
// Fill
for (EX ex : exs)
{
EY ey = null;
try
{
ey = this.getConverterElement().convert(ex);
}
catch (Throwable t)
{
t.printStackTrace();
continue;
}
eys.add(ey);
}
// Return
return eys;
}
}

View File

@ -0,0 +1,58 @@
package com.massivecraft.massivecore.item;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
public class ConverterListImmutable<EX, EY> extends Converter<Iterable<EX>, ImmutableList<EY>>
{
// -------------------------------------------- //
// FIELD
// -------------------------------------------- //
private final Converter<EX, EY> converterElement;
public Converter<EX, EY> getConverterElement() { return this.converterElement; }
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
public ConverterListImmutable(Converter<EX, EY> converterElement)
{
this.converterElement = converterElement;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public ImmutableList<EY> convert(Iterable<EX> exs)
{
// Null
if (exs == null) return null;
// Create
Builder<EY> eys = ImmutableList.builder();
// Fill
for (EX ex : exs)
{
EY ey = null;
try
{
ey = this.getConverterElement().convert(ex);
}
catch (Throwable t)
{
t.printStackTrace();
continue;
}
eys.add(ey);
}
// Return
return eys.build();
}
}

View File

@ -0,0 +1,77 @@
package com.massivecraft.massivecore.item;
import java.util.Map;
import java.util.Map.Entry;
import com.massivecraft.massivecore.collections.MassiveMap;
public class ConverterMap<KX, VX, KY, VY> extends Converter<Map<KX, VX>, Map<KY, VY>>
{
// -------------------------------------------- //
// FIELD
// -------------------------------------------- //
private final Converter<KX, KY> converterKey;
public Converter<KX, KY> getConverterKey() { return this.converterKey; }
private final Converter<VX, VY> converterValue;
public Converter<VX, VY> getConverterValue() { return this.converterValue; }
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
public ConverterMap(Converter<KX, KY> converterKey, Converter<VX, VY> converterValue)
{
this.converterKey = converterKey;
this.converterValue = converterValue;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Map<KY, VY> convert(Map<KX, VX> mapx)
{
// Null
if (mapx == null) return null;
// Create
Map<KY, VY> mapy = new MassiveMap<>();
// Fill
for (Entry<KX, VX> entry : mapx.entrySet())
{
KY ky = null;
KX kx = entry.getKey();
try
{
ky = this.getConverterKey().convert(kx);
}
catch (Throwable t)
{
t.printStackTrace();
continue;
}
VY vy = null;
VX vx = entry.getValue();
try
{
vy = this.getConverterValue().convert(vx);
}
catch (Throwable t)
{
t.printStackTrace();
continue;
}
mapy.put(ky, vy);
}
// Return
return mapy;
}
}

View File

@ -0,0 +1,59 @@
package com.massivecraft.massivecore.item;
import java.util.Set;
import com.massivecraft.massivecore.collections.MassiveSet;
public class ConverterSet<EX, EY> extends Converter<Iterable<EX>, Set<EY>>
{
// -------------------------------------------- //
// FIELD
// -------------------------------------------- //
private final Converter<EX, EY> converterElement;
public Converter<EX, EY> getConverterElement() { return this.converterElement; }
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
public ConverterSet(Converter<EX, EY> converterElement)
{
this.converterElement = converterElement;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Set<EY> convert(Iterable<EX> exs)
{
// Null
if (exs == null) return null;
// Create
Set<EY> eys = new MassiveSet<>();
// Fill
for (EX ex : exs)
{
EY ey = null;
try
{
ey = this.getConverterElement().convert(ex);
}
catch (Throwable t)
{
t.printStackTrace();
continue;
}
eys.add(ey);
}
// Return
return eys;
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.block.banner.Pattern;
public class ConverterToBannerPattern extends Converter<DataBannerPattern, Pattern>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToBannerPattern i = new ConverterToBannerPattern();
public static ConverterToBannerPattern get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Pattern convert(DataBannerPattern x)
{
if (x == null) return null;
return x.toBukkit();
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.block.banner.PatternType;
public class ConverterToBannerPatternType extends Converter<String, PatternType>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToBannerPatternType i = new ConverterToBannerPatternType();
public static ConverterToBannerPatternType get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public PatternType convert(String x)
{
if (x == null) return null;
return PatternType.getByIdentifier(x);
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.block.banner.Pattern;
public class ConverterToBannerPatterns extends ConverterList<DataBannerPattern, Pattern>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToBannerPatterns i = new ConverterToBannerPatterns();
public static ConverterToBannerPatterns get() { return i; }
public ConverterToBannerPatterns()
{
super(ConverterToBannerPattern.get());
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.Color;
public class ConverterToColor extends Converter<Integer, Color>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToColor i = new ConverterToColor();
public static ConverterToColor get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Color convert(Integer x)
{
if (x == null) return null;
return Color.fromRGB(x);
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.Color;
public class ConverterToColors extends ConverterListImmutable<Integer, Color>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToColors i = new ConverterToColors();
public static ConverterToColors get() { return i; }
public ConverterToColors()
{
super(ConverterToColor.get());
}
}

View File

@ -0,0 +1,26 @@
package com.massivecraft.massivecore.item;
import org.bukkit.DyeColor;
public class ConverterToDyeColor extends Converter<Integer, DyeColor>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToDyeColor i = new ConverterToDyeColor();
public static ConverterToDyeColor get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings("deprecation")
@Override
public DyeColor convert(Integer x)
{
if (x == null) return null;
return DyeColor.getByDyeData(x.byteValue());
}
}

View File

@ -0,0 +1,26 @@
package com.massivecraft.massivecore.item;
import org.bukkit.enchantments.Enchantment;
public class ConverterToEnchant extends Converter<Integer, Enchantment>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToEnchant i = new ConverterToEnchant();
public static ConverterToEnchant get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings("deprecation")
@Override
public Enchantment convert(Integer x)
{
if (x == null) return null;
return Enchantment.getById(x);
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.enchantments.Enchantment;
public class ConverterToEnchants extends ConverterMap<Integer, Integer, Enchantment, Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToEnchants i = new ConverterToEnchants();
public static ConverterToEnchants get() { return i; }
public ConverterToEnchants()
{
super(ConverterToEnchant.get(), ConverterDefault.get(Integer.class, Integer.class));
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.FireworkEffect;
public class ConverterToFireworkEffect extends Converter<DataFireworkEffect, FireworkEffect>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToFireworkEffect i = new ConverterToFireworkEffect();
public static ConverterToFireworkEffect get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public FireworkEffect convert(DataFireworkEffect x)
{
if (x == null) return null;
return x.toBukkit();
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.FireworkEffect.Type;
public class ConverterToFireworkEffectType extends Converter<String, Type>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToFireworkEffectType i = new ConverterToFireworkEffectType();
public static ConverterToFireworkEffectType get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Type convert(String x)
{
if (x == null) return null;
return Type.valueOf(x);
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.FireworkEffect;
public class ConverterToFireworkEffects extends ConverterList<DataFireworkEffect, FireworkEffect>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToFireworkEffects i = new ConverterToFireworkEffects();
public static ConverterToFireworkEffects get() { return i; }
public ConverterToFireworkEffects()
{
super(ConverterToFireworkEffect.get());
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.inventory.ItemFlag;
public class ConverterToItemFlag extends Converter<String, ItemFlag>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToItemFlag i = new ConverterToItemFlag();
public static ConverterToItemFlag get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public ItemFlag convert(String x)
{
if (x == null) return null;
return ItemFlag.valueOf(x);
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.inventory.ItemFlag;
public class ConverterToItemFlags extends ConverterSet<String, ItemFlag>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToItemFlags i = new ConverterToItemFlags();
public static ConverterToItemFlags get() { return i; }
public ConverterToItemFlags()
{
super(ConverterToItemFlag.get());
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.potion.PotionEffect;
public class ConverterToPotionEffect extends Converter<DataPotionEffect, PotionEffect>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToPotionEffect i = new ConverterToPotionEffect();
public static ConverterToPotionEffect get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public PotionEffect convert(DataPotionEffect x)
{
if (x == null) return null;
return x.toBukkit();
}
}

View File

@ -0,0 +1,26 @@
package com.massivecraft.massivecore.item;
import org.bukkit.potion.PotionEffectType;
public class ConverterToPotionEffectType extends Converter<Integer, PotionEffectType>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToPotionEffectType i = new ConverterToPotionEffectType();
public static ConverterToPotionEffectType get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings("deprecation")
@Override
public PotionEffectType convert(Integer x)
{
if (x == null) return null;
return PotionEffectType.getById(x);
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.item;
import org.bukkit.potion.PotionEffect;
public class ConverterToPotionEffects extends ConverterList<DataPotionEffect, PotionEffect>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final ConverterToPotionEffects i = new ConverterToPotionEffects();
public static ConverterToPotionEffects get() { return i; }
public ConverterToPotionEffects()
{
super(ConverterToPotionEffect.get());
}
}

View File

@ -0,0 +1,107 @@
package com.massivecraft.massivecore.item;
import static com.massivecraft.massivecore.item.DataItemStack.get;
import static com.massivecraft.massivecore.item.DataItemStack.set;
import java.util.Objects;
import org.bukkit.block.banner.Pattern;
import com.massivecraft.massivecore.comparator.ComparatorSmart;
import com.massivecraft.massivecore.util.MUtil;
public class DataBannerPattern implements Comparable<DataBannerPattern>
{
// -------------------------------------------- //
// DEFAULTS
// -------------------------------------------- //
public static final transient String DEFAULT_ID = null;
public static final transient Integer DEFAULT_COLOR = null;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private String id = null;
public String getId() { return get(this.id, DEFAULT_ID); }
public DataBannerPattern setId(String id) { this.id = set(id, DEFAULT_ID); return this; }
private Integer color = null;
public Integer getColor() { return get(this.color, DEFAULT_COLOR); }
public DataBannerPattern setColor(Integer color) { this.color = set(color, DEFAULT_ID); return this; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public DataBannerPattern()
{
}
public DataBannerPattern(Pattern pattern)
{
this.write(pattern, false);
}
// -------------------------------------------- //
// WRITE
// -------------------------------------------- //
public void write(Pattern pattern, boolean a2b)
{
WriterBannerPattern.get().write(this, pattern, a2b);
}
// -------------------------------------------- //
// TO BUKKIT
// -------------------------------------------- //
public Pattern toBukkit()
{
// Create
Pattern ret = WriterBannerPattern.get().createB();
// Fill
this.write(ret, true);
// Return
return ret;
}
// -------------------------------------------- //
// COMPARE & EQUALS & HASHCODE
// -------------------------------------------- //
@Override
public int compareTo(DataBannerPattern that)
{
return ComparatorSmart.get().compare(
this.getId(), that.getId(),
this.getColor(), that.getColor()
);
}
// TODO: Use compare instead to avoid bugs?
@Override
public boolean equals(Object object)
{
if ( ! (object instanceof DataBannerPattern)) return false;
DataBannerPattern that = (DataBannerPattern)object;
return MUtil.equals(
this.getId(), that.getId(),
this.getColor(), that.getColor()
);
}
@Override
public int hashCode()
{
return Objects.hash(
this.getId(),
this.getColor()
);
}
}

View File

@ -0,0 +1,143 @@
package com.massivecraft.massivecore.item;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.bukkit.FireworkEffect;
import static com.massivecraft.massivecore.item.DataItemStack.get;
import static com.massivecraft.massivecore.item.DataItemStack.set;
import com.massivecraft.massivecore.collections.MassiveListDef;
import com.massivecraft.massivecore.comparator.ComparatorSmart;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName;
public class DataFireworkEffect implements Comparable<DataFireworkEffect>
{
// -------------------------------------------- //
// DEFAULTS
// -------------------------------------------- //
public static final transient Boolean DEFAULT_FLICKER = false;
public static final transient Boolean DEFAULT_TRAIL = false;
public static final transient List<Integer> DEFAULT_COLORS = Collections.emptyList();
public static final transient List<Integer> DEFAULT_FADE_COLORS = Collections.emptyList();
public static final transient String DEFAULT_TYPE = "BALL_LARGE";
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
// According to Bukkit the colors are indeed lists with explicit order.
// I have not researched if that is true. For now I am assuming it is.
private Boolean flicker = null;
public boolean hasFlicker() { return get(this.flicker, DEFAULT_FLICKER); }
public DataFireworkEffect setFlicker(boolean flicker) { this.flicker = set(flicker, DEFAULT_FLICKER); return this; }
private Boolean trail = null;
public boolean hasTrail() { return get(this.trail, DEFAULT_TRAIL); }
public DataFireworkEffect setTrail(boolean trail) { this.trail = set(trail, DEFAULT_TRAIL); return this; }
private MassiveListDef<Integer> colors = null;
public List<Integer> getColors() { return get(this.colors, DEFAULT_COLORS); }
public DataFireworkEffect setColors(List<Integer> colors) { this.colors = set(colors, DEFAULT_COLORS); return this; }
@SerializedName("fade-colors")
private MassiveListDef<Integer> fadeColors = null;
public List<Integer> getFadeColors() { return get(this.fadeColors, DEFAULT_FADE_COLORS); }
public DataFireworkEffect setFadeColors(List<Integer> fadeColors) { this.fadeColors = set(fadeColors, DEFAULT_FADE_COLORS); return this; }
private String type = null;
public String getType() { return get(this.type, DEFAULT_TYPE); }
public DataFireworkEffect setType(String type) { this.type = set(type, DEFAULT_TYPE); return this; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public DataFireworkEffect()
{
}
public DataFireworkEffect(FireworkEffect fireworkEffect)
{
this.write(fireworkEffect, false);
}
// -------------------------------------------- //
// WRITE
// -------------------------------------------- //
public void write(FireworkEffect fireworkEffect, boolean a2b)
{
WriterFireworkEffect.get().write(this, fireworkEffect, a2b);
}
// -------------------------------------------- //
// TO BUKKIT
// -------------------------------------------- //
public FireworkEffect toBukkit()
{
// Create
FireworkEffect ret = WriterFireworkEffect.get().createB();
// Fill
this.write(ret, true);
// Return
return ret;
}
// -------------------------------------------- //
// COMPARE & EQUALS & HASHCODE
// -------------------------------------------- //
@Override
public int compareTo(DataFireworkEffect that)
{
return ComparatorSmart.get().compare(
this.hasFlicker(), that.hasFlicker(),
this.hasTrail(), that.hasTrail(),
this.getColors(), that.getColors(),
this.getColors(), that.getColors(),
this.getFadeColors(), that.getFadeColors(),
this.getType(), that.getType()
);
}
// TODO: Use compare instead to avoid bugs?
@Override
public boolean equals(Object object)
{
if ( ! (object instanceof DataFireworkEffect)) return false;
DataFireworkEffect that = (DataFireworkEffect)object;
return MUtil.equals(
this.hasFlicker(), that.hasFlicker(),
this.hasTrail(), that.hasTrail(),
this.getColors(), that.getColors(),
this.getColors(), that.getColors(),
this.getFadeColors(), that.getFadeColors(),
this.getType(), that.getType()
);
}
@Override
public int hashCode()
{
return Objects.hash(
this.hasFlicker(),
this.hasTrail(),
this.getColors(),
this.getColors(),
this.getFadeColors(),
this.getType()
);
}
}

View File

@ -0,0 +1,428 @@
package com.massivecraft.massivecore.item;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import com.massivecraft.massivecore.collections.MassiveListDef;
import com.massivecraft.massivecore.collections.MassiveTreeMapDef;
import com.massivecraft.massivecore.collections.MassiveTreeSetDef;
import com.massivecraft.massivecore.comparator.ComparatorSmart;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName;
/**
* This class makes use of primitives, collections and maps only.
* All Bukkit specific enumerations and classes are avoided.
* That means this class itself is compatible with all Bukkit server versions.
*
* We also make sure to only initialize variables with null as value.
* Null means "default" and this way we save database space as well as CPU power on class construction.
*
* This class acts as a safe intermediary for database storage.
* It is mainly used by the ItemStackAdapter and InventoryAdapter.
* It can also be used directly, for example in maps, since it provides a stable equals and hash code method (as opposed to Bukkit).
*/
public class DataItemStack implements Comparable<DataItemStack>
{
// -------------------------------------------- //
// DEFAULTS
// -------------------------------------------- //
// The default values are used in the logic of both getters and setters.
// For that reason they are immutable.
//
// We avoid null in all locations except where Bukkit makes use of null.
// Since Bukkit doesn't NPE evade much we save ourselves a lot of trouble that way.
// Especially note how all collections default to empty immutables instead of null.
public static final transient Integer DEFAULT_ID = 0;
public static final transient Integer DEFAULT_COUNT = 1;
public static final transient Integer DEFAULT_DAMAGE = 0;
public static final transient String DEFAULT_NAME = null;
public static final transient List<String> DEFAULT_LORE = Collections.emptyList();
public static final transient Map<Integer, Integer> DEFAULT_ENCHANTS = Collections.emptyMap();
public static final transient Integer DEFAULT_REPAIRCOST = 0;
public static final transient String DEFAULT_TITLE = null;
public static final transient String DEFAULT_AUTHOR = null;
public static final transient List<String> DEFAULT_PAGES = Collections.emptyList();
public static final transient Integer DEFAULT_COLOR = Bukkit.getItemFactory().getDefaultLeatherColor().asRGB();
public static final transient Boolean DEFAULT_SCALING = false;
public static final transient List<DataPotionEffect> DEFAULT_POTION_EFFECTS = Collections.emptyList();
public static final transient String DEFAULT_SKULL = null;
public static final transient DataFireworkEffect DEFAULT_FIREWORK_EFFECT = null;
public static final transient List<DataFireworkEffect> DEFAULT_FIREWORK_EFFECTS = Collections.emptyList();
public static final transient Integer DEFAULT_FIREWORK_FLIGHT = 0;
public static final transient Map<Integer, Integer> DEFAULT_STORED_ENCHANTS = Collections.emptyMap();
public static final transient Boolean DEFAULT_UNBREAKABLE = false;
public static final transient Set<String> DEFAULT_FLAGS = Collections.emptySet();
public static final transient Integer DEFAULT_BANNER_BASE = null;
public static final transient List<DataBannerPattern> DEFAULT_BANNER_PATTERNS = Collections.emptyList();
public static final transient String DEFAULT_POTION = "water";
// -------------------------------------------- //
// FIELDS > BASIC
// -------------------------------------------- //
private Integer id = null;
public int getId() { return get(this.id, DEFAULT_ID); }
public DataItemStack setId(int id) { this.id = set(id, DEFAULT_ID); return this; }
private Integer count = null;
public int getCount() { return get(this.count, DEFAULT_COUNT); }
public DataItemStack setCount(int count) { this.count = set(count, DEFAULT_COUNT); return this; }
private Integer damage = null;
public int getDamage() { return get(this.damage, DEFAULT_DAMAGE); }
public DataItemStack setDamage(int damage) { this.damage = set(damage, DEFAULT_DAMAGE); return this; }
// -------------------------------------------- //
// FIELDS > UNSPECIFIC
// -------------------------------------------- //
private String name = null;
public String getName() { return get(this.name, DEFAULT_NAME); }
public DataItemStack setName(String name) { this.name = set(name, DEFAULT_NAME); return this; }
private MassiveListDef<String> lore = null;
public List<String> getLore() { return get(this.lore, DEFAULT_LORE); }
public DataItemStack setLore(List<String> lore) { this.lore = set(lore, DEFAULT_LORE); return this; }
// The Bukkit ItemMeta#getEnchants() is not sorted by the enchant id.
// There may be some sort of custom sorting order, I'm not sure.
// We are however enforcing sorting by the enchant id ourselves to be sure.
private MassiveTreeMapDef<Integer, Integer, ComparatorSmart> enchants = null;
public Map<Integer, Integer> getEnchants() { return get(this.enchants, DEFAULT_ENCHANTS); }
public DataItemStack setEnchants(Map<Integer, Integer> enchants) { this.enchants = set(enchants, DEFAULT_ENCHANTS); return this; }
private Integer repaircost = null;
public int getRepaircost() { return get(this.repaircost, DEFAULT_REPAIRCOST); }
public DataItemStack setRepaircost(int repaircost) { this.repaircost = set(repaircost, DEFAULT_REPAIRCOST); return this; }
// -------------------------------------------- //
// FIELDS > BOOK
// -------------------------------------------- //
private String title = null;
public String getTitle() { return get(this.title, DEFAULT_TITLE); }
public DataItemStack setTitle(String title) { this.title = set(title, DEFAULT_TITLE); return this; }
private String author = null;
public String getAuthor() { return get(this.author, DEFAULT_AUTHOR); }
public DataItemStack setAuthor(String author) { this.author = set(author, DEFAULT_AUTHOR); return this; }
private MassiveListDef<String> pages = null;
public List<String> getPages() { return get(this.pages, DEFAULT_PAGES); }
public DataItemStack setPages(Collection<String> pages) { this.pages = set(pages, DEFAULT_PAGES); return this; }
// -------------------------------------------- //
// FIELDS > LEATHER ARMOR
// -------------------------------------------- //
private Integer color = null;
public Integer getColor() { return get(this.color, DEFAULT_COLOR); }
public DataItemStack setColor(int color) { this.color = set(color, DEFAULT_COLOR); return this; }
// -------------------------------------------- //
// FIELDS > MAP
// -------------------------------------------- //
private Boolean scaling = null;
public boolean isScaling() { return get(this.scaling, DEFAULT_SCALING); }
public DataItemStack setScaling(boolean scaling) { this.scaling = set(scaling, DEFAULT_SCALING); return this; }
// -------------------------------------------- //
// FIELDS > POTION EFFECTS
// -------------------------------------------- //
// TODO: Sorting?
@SerializedName("potion-effects")
private MassiveListDef<DataPotionEffect> potionEffects = null;
public List<DataPotionEffect> getPotionEffects() { return get(this.potionEffects, DEFAULT_POTION_EFFECTS); }
public DataItemStack setPotionEffects(List<DataPotionEffect> potionEffects) { this.potionEffects = set(potionEffects, DEFAULT_POTION_EFFECTS); return this; }
// -------------------------------------------- //
// FIELDS > SKULL
// -------------------------------------------- //
private String skull = null;
public String getSkull() { return get(this.skull, DEFAULT_SKULL); }
public DataItemStack setSkull(String skull) { this.skull = set(skull, DEFAULT_SKULL); return this; }
// -------------------------------------------- //
// FIELDS > FIREWORK EFFECT
// -------------------------------------------- //
@SerializedName("firework-effect")
private DataFireworkEffect fireworkEffect = null;
public DataFireworkEffect getFireworkEffect() { return get(this.fireworkEffect, DEFAULT_FIREWORK_EFFECT); }
public DataItemStack setFireworkEffect(DataFireworkEffect fireworkEffect) { this.fireworkEffect = set(fireworkEffect, DEFAULT_FIREWORK_EFFECT); return this; }
// -------------------------------------------- //
// FIELDS > FIREWORK
// -------------------------------------------- //
// TODO: Sorting?
@SerializedName("firework-effects")
private MassiveListDef<DataFireworkEffect> fireworkEffects = null;
public List<DataFireworkEffect> getFireworkEffects() { return get(this.fireworkEffects, DEFAULT_FIREWORK_EFFECTS); }
public DataItemStack setFireworkEffects(List<DataFireworkEffect> fireworkEffects) { this.fireworkEffects = set(fireworkEffects, DEFAULT_FIREWORK_EFFECTS); return this; }
// NOTE: Did not have a default specified.
@SerializedName("firework-flight")
private Integer fireworkFlight = null;
public int getFireworkFlight() { return get(this.fireworkFlight, DEFAULT_FIREWORK_FLIGHT); }
public DataItemStack setFireworkFlight(int fireworkFlight) { this.fireworkFlight = set(fireworkFlight, DEFAULT_FIREWORK_FLIGHT); return this; }
// -------------------------------------------- //
// FIELDS > STORED ENCHANTS
// -------------------------------------------- //
@SerializedName("stored-enchants")
private MassiveTreeMapDef<Integer, Integer, ComparatorSmart> storedEnchants = null;
public Map<Integer, Integer> getStoredEnchants() { return get(this.storedEnchants, DEFAULT_STORED_ENCHANTS); }
public DataItemStack setStoredEnchants(Map<Integer, Integer> storedEnchants) { this.storedEnchants = set(storedEnchants, DEFAULT_STORED_ENCHANTS); return this; }
// -------------------------------------------- //
// FIELDS > UNBREAKABLE
// -------------------------------------------- //
// SINCE: 1.8
private Boolean unbreakable = null;
public boolean isUnbreakable() { return get(this.unbreakable, DEFAULT_UNBREAKABLE); }
public DataItemStack setUnbreakable(boolean unbreakable) { this.unbreakable = set(unbreakable, DEFAULT_UNBREAKABLE); return this; }
// -------------------------------------------- //
// FIELDS > FLAGS
// -------------------------------------------- //
// SINCE: 1.8
private MassiveTreeSetDef<String, ComparatorSmart> flags = null;
public Set<String> getFlags() { return get(this.flags, DEFAULT_FLAGS); }
public DataItemStack setFlags(Set<String> flags) { this.flags = set(flags, DEFAULT_FLAGS); return this; }
// -------------------------------------------- //
// FIELDS > BANNER BASE
// -------------------------------------------- //
// SINCE: 1.8
// The integer is the dye color byte representation.
// Is actually nullable in Bukkit.
@SerializedName("banner-base")
private Integer bannerBase = null;
public Integer getBannerBase() { return get(this.bannerBase, DEFAULT_BANNER_BASE); }
public DataItemStack setBannerBase(Integer bannerBase) { this.bannerBase = set(bannerBase, DEFAULT_BANNER_BASE); return this; }
// -------------------------------------------- //
// FIELDS > BANNER PATTERNS
// -------------------------------------------- //
// SINCE: 1.8
// This should really be a list and not a set.
// The order matters and is explicitly assigned.
// String, Number, String, Number ...
// TODO: Make sure the special adapter for upgrading the format is implemented!
@SerializedName("banner")
private MassiveListDef<DataBannerPattern> bannerPatterns = null;
public List<DataBannerPattern> getBannerPatterns() { return get(this.bannerPatterns, DEFAULT_BANNER_PATTERNS); }
public DataItemStack setBannerPatterns(List<DataBannerPattern> bannerPatterns) { this.bannerPatterns = set(bannerPatterns, DEFAULT_BANNER_PATTERNS); return this;}
// -------------------------------------------- //
// FIELDS > POTION
// -------------------------------------------- //
// SINCE: 1.9
private String potion = null;
public String getPotion() { return get(this.potion, DEFAULT_POTION); }
public DataItemStack setPotion(String potion) { this.potion = set(potion, DEFAULT_POTION); return this; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public DataItemStack()
{
}
public DataItemStack(ItemStack itemStack)
{
this.write(itemStack, false);
}
// -------------------------------------------- //
// WRITE
// -------------------------------------------- //
public void write(ItemStack itemStack, boolean a2b)
{
WriterItemStack.get().write(this, itemStack, a2b);
}
// -------------------------------------------- //
// TO BUKKIT
// -------------------------------------------- //
public ItemStack toBukkit()
{
// Create
ItemStack ret = WriterItemStack.get().createB();
// Fill
this.write(ret, true);
// Return
return ret;
}
// -------------------------------------------- //
// COMPARE & EQUALS & HASHCODE
// -------------------------------------------- //
@Override
public int compareTo(DataItemStack that)
{
return ComparatorSmart.get().compare(
this.getId(), that.getId(),
this.getCount(), that.getCount(),
this.getDamage(), that.getDamage(),
this.getName(), that.getName(),
this.getLore(), that.getLore(),
this.getEnchants(), that.getEnchants(),
this.getRepaircost(), that.getRepaircost(),
this.getTitle(), that.getTitle(),
this.getAuthor(), that.getAuthor(),
this.getPages(), that.getPages(),
this.getColor(), that.getColor(),
this.isScaling(), that.isScaling(),
this.getPotionEffects(), that.getPotionEffects(),
this.getSkull(), that.getSkull(),
this.getFireworkEffect(), that.getFireworkEffect(),
this.getFireworkEffects(), that.getFireworkEffects(),
this.getFireworkFlight(), that.getFireworkFlight(),
this.getStoredEnchants(), that.getStoredEnchants(),
this.isUnbreakable(), that.isUnbreakable(),
this.getFlags(), that.getFlags(),
this.getBannerBase(), that.getBannerBase(),
this.getBannerPatterns(), that.getBannerPatterns(),
this.getPotion(), that.getPotion()
);
}
@Override
public boolean equals(Object object)
{
if ( ! (object instanceof DataItemStack)) return false;
DataItemStack that = (DataItemStack)object;
// TODO: Use compare instead to avoid bugs?
return MUtil.equals(
this.getId(), that.getId(),
this.getCount(), that.getCount(),
this.getDamage(), that.getDamage(),
this.getName(), that.getName(),
this.getLore(), that.getLore(),
this.getEnchants(), that.getEnchants(),
this.getRepaircost(), that.getRepaircost(),
this.getTitle(), that.getTitle(),
this.getAuthor(), that.getAuthor(),
this.getPages(), that.getPages(),
this.getColor(), that.getColor(),
this.isScaling(), that.isScaling(),
this.getPotionEffects(), that.getPotionEffects(),
this.getSkull(), that.getSkull(),
this.getFireworkEffect(), that.getFireworkEffect(),
this.getFireworkEffects(), that.getFireworkEffects(),
this.getFireworkFlight(), that.getFireworkFlight(),
this.getStoredEnchants(), that.getStoredEnchants(),
this.isUnbreakable(), that.isUnbreakable(),
this.getFlags(), that.getFlags(),
this.getBannerBase(), that.getBannerBase(),
this.getBannerPatterns(), that.getBannerPatterns(),
this.getPotion(), that.getPotion()
);
}
@Override
public int hashCode()
{
return Objects.hash(
this.getId(),
this.getCount(),
this.getDamage(),
this.getName(),
this.getLore(),
this.getEnchants(),
this.getRepaircost(),
this.getTitle(),
this.getAuthor(),
this.getPages(),
this.getColor(),
this.isScaling(),
this.getPotionEffects(),
this.getSkull(),
this.getFireworkEffect(),
this.getFireworkEffects(),
this.getFireworkFlight(),
this.getStoredEnchants(),
this.isUnbreakable(),
this.getFlags(),
this.getBannerBase(),
this.getBannerPatterns(),
this.getPotion()
);
}
// -------------------------------------------- //
// GET & SET & NOTHING
// -------------------------------------------- //
// We treat null and empty collections the same.
public static boolean isNothing(Object object)
{
if (object == null) return true;
if (object instanceof Collection<?>) return ((Collection<?>)object).isEmpty();
if (object instanceof Map<?, ?>) return ((Map<?, ?>)object).isEmpty();
return false;
}
// Return the value unless the value is nothing then return standard instead.
public static <T> T get(T value, T standard)
{
if (isNothing(value)) return standard;
return value;
}
// Return the value unless the value is nothing or standard then return null instead.
// Perform shallow copy on supported collections.
@SuppressWarnings("unchecked")
public static <R, T> R set(T value, T standard)
{
if (isNothing(value)) return null;
if (value.equals(standard)) return null;
if (value instanceof List<?>)
{
List<Object> list = (List<Object>)value;
return (R) new MassiveListDef<>(list);
}
else if (value instanceof Set<?>)
{
Set<Object> set = (Set<Object>)value;
return (R) new MassiveTreeSetDef<>(ComparatorSmart.get(), set);
}
else if (value instanceof Map<?, ?>)
{
Map<Object, Object> map = (Map<Object, Object>)value;
return (R) new MassiveTreeMapDef<>(ComparatorSmart.get(), map);
}
return (R) value;
}
}

View File

@ -0,0 +1,142 @@
package com.massivecraft.massivecore.item;
import static com.massivecraft.massivecore.item.DataItemStack.get;
import static com.massivecraft.massivecore.item.DataItemStack.set;
import java.util.Objects;
import org.bukkit.potion.PotionEffect;
import com.massivecraft.massivecore.comparator.ComparatorSmart;
import com.massivecraft.massivecore.util.MUtil;
public class DataPotionEffect implements Comparable<DataPotionEffect>
{
// -------------------------------------------- //
// DEFAULTS
// -------------------------------------------- //
public static final transient Integer DEFAULT_ID = null;
public static final transient Integer DEFAULT_DURATION = 20 * 3 * 60;
public static final transient Integer DEFAULT_AMPLIFIER = 0;
public static final transient Boolean DEFAULT_AMBIENT = false;
public static final transient Boolean DEFAULT_PARTICLES = true;
public static final transient Integer DEFAULT_COLOR = null;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private Integer id = null;
public Integer getId() { return get(this.id, DEFAULT_ID); }
public DataPotionEffect setId(Integer id) { this.id = set(id, DEFAULT_ID); return this; }
private Integer duration = null;
public int getDuration() { return get(this.duration, DEFAULT_DURATION); }
public DataPotionEffect setDuration(int duration) { this.duration = set(duration, DEFAULT_DURATION); return this; }
private Integer amplifier = null;
public int getAmplifier() { return get(this.amplifier, DEFAULT_AMPLIFIER); }
public DataPotionEffect setAmplifier(int amplifier) { this.amplifier = set(amplifier, DEFAULT_AMPLIFIER); return this; }
private Boolean ambient = null;
public boolean isAmbient() { return get(this.ambient, DEFAULT_AMBIENT); }
public DataPotionEffect setAmbient(boolean ambient) { this.ambient = set(ambient, DEFAULT_AMBIENT); return this; }
// SINCE: 1.8
private Boolean particles = null;
public boolean isParticles() { return get(this.particles, DEFAULT_PARTICLES); }
public DataPotionEffect setParticles(boolean particles) { this.particles = set(particles, DEFAULT_PARTICLES); return this; }
// SINCE: 1.9
private Integer color = null;
public Integer getColor() { return get(this.color, DEFAULT_COLOR); }
public DataPotionEffect setColor(Integer color) { this.color = set(color, DEFAULT_COLOR); return this; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public DataPotionEffect()
{
}
public DataPotionEffect(PotionEffect potionEffect)
{
this.write(potionEffect, false);
}
// -------------------------------------------- //
// WRITE
// -------------------------------------------- //
public void write(PotionEffect potionEffect, boolean a2b)
{
WriterPotionEffect.get().write(this, potionEffect, a2b);
}
// -------------------------------------------- //
// TO BUKKIT
// -------------------------------------------- //
public PotionEffect toBukkit()
{
// Create
PotionEffect ret = WriterPotionEffect.get().createB();
// Fill
this.write(ret, true);
// Return
return ret;
}
// -------------------------------------------- //
// COMPARE & EQUALS & HASHCODE
// -------------------------------------------- //
@Override
public int compareTo(DataPotionEffect that)
{
return ComparatorSmart.get().compare(
this.getId(), that.getId(),
this.getDuration(), that.getDuration(),
this.getAmplifier(), that.getAmplifier(),
this.isAmbient(), that.isAmbient(),
this.isParticles(), that.isParticles(),
this.getColor(), that.getColor()
);
}
// TODO: Use compare instead to avoid bugs?
@Override
public boolean equals(Object object)
{
if ( ! (object instanceof DataPotionEffect)) return false;
DataPotionEffect that = (DataPotionEffect)object;
return MUtil.equals(
this.getId(), that.getId(),
this.getDuration(), that.getDuration(),
this.getAmplifier(), that.getAmplifier(),
this.isAmbient(), that.isAmbient(),
this.isParticles(), that.isParticles(),
this.getColor(), that.getColor()
);
}
@Override
public int hashCode()
{
return Objects.hash(
this.getId(),
this.getDuration(),
this.getAmplifier(),
this.isAmbient(),
this.isParticles(),
this.getColor()
);
}
}

View File

@ -0,0 +1,251 @@
package com.massivecraft.massivecore.item;
import java.util.List;
import com.massivecraft.massivecore.Engine;
import com.massivecraft.massivecore.MassiveCoreMConf;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.type.primitive.TypeBoolean;
import com.massivecraft.massivecore.util.ReflectionUtil;
import com.massivecraft.massivecore.util.Txt;
public abstract class WriterAbstract<OA, OB, CA, CB, FA, FB> extends Engine
{
// -------------------------------------------- //
// WRITERS
// -------------------------------------------- //
// A writer may contain subwriters.
private List<WriterAbstract<CA, CB, ?, ?, ?, ?>> writers = new MassiveList<>();
public List<WriterAbstract<CA, CB, ?, ?, ?, ?>> getWriters()
{
return this.writers;
}
public void clearWriters()
{
this.writers.clear();
}
@SuppressWarnings("unchecked")
public void addWriter(Class<?> clazz)
{
boolean success = false;
try
{
Class<WriterAbstract<CA, CB, ?, ?, ?, ?>> clazzInner = (Class<WriterAbstract<CA, CB, ?, ?, ?, ?>>) clazz;
WriterAbstract<CA, CB, ?, ?, ?, ?> writer = ReflectionUtil.getSingletonInstance(clazzInner);
writer.setActive(this.getActivePlugin());
this.getWriters().add(writer);
success = true;
}
catch (Throwable t)
{
if (MassiveCoreMConf.get().debugWriters)
{
t.printStackTrace();
}
}
if (MassiveCoreMConf.get().debugWriters)
{
String message = Txt.parse("<h>%s %s", clazz.getSimpleName(), TypeBoolean.getOn().getVisual(success));
this.getActivePlugin().log(message);
}
}
public void addWriters(Class<?>... clazzs)
{
for (Class<?> clazz : clazzs)
{
this.addWriter(clazz);
}
}
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
public abstract CA createA();
public abstract CB createB();
// -------------------------------------------- //
// CLASSES
// -------------------------------------------- //
private Class<CA> classA = null;
public Class<CA> getClassA()
{
return this.classA;
}
public void setClassA(Class<CA> classA)
{
this.classA = classA;
}
private Class<CB> classB = null;
public Class<CB> getClassB()
{
return this.classB;
}
public void setClassB(Class<CB> classB)
{
this.classB = classB;
}
// -------------------------------------------- //
// MORPH
// -------------------------------------------- //
@SuppressWarnings("unchecked")
public CA morphA(OA oa)
{
Class<CA> classA = this.getClassA();
if (classA != null && !classA.isAssignableFrom(oa.getClass())) return null;
CA ca = (CA) oa;
return ca;
}
@SuppressWarnings("unchecked")
public CB morphB(OB ob)
{
Class<CB> classB = this.getClassB();
if (classB != null && !classB.isAssignableFrom(ob.getClass())) return null;
CB cb = (CB) ob;
return cb;
}
// -------------------------------------------- //
// ACTIVE
// -------------------------------------------- //
@Override
public void setActive(boolean active)
{
this.provoke();
super.setActive(active);
}
// -------------------------------------------- //
// PROVOKE
// -------------------------------------------- //
public Object provoke()
{
// Create Instances
CA ia = this.createA();
CB ib = this.createB();
// Demand Set
if (ia == null) throw new NullPointerException("Couldn't Create A");
if (ib == null) throw new NullPointerException("Couldn't Create B");
// Use Access
FA fa = this.getA(ia);
this.setA(ia, fa);
FB fb = this.getB(ib);
this.setB(ib, fb);
// Use To
this.toA(fb);
this.toB(fa);
// Return
return null;
}
// -------------------------------------------- //
// WRITE
// -------------------------------------------- //
public void write(OA oa, OB ob, boolean a2b)
{
if (!this.isActive()) throw new IllegalStateException("not active " + this.getClass().getName());
if (oa == null) throw new NullPointerException("oa");
if (ob == null) throw new NullPointerException("ob");
CA ca = this.morphA(oa);
if (ca == null) return;
CB cb = this.morphB(ob);
if (cb == null) return;
this.writeInner(oa, ob, ca, cb, a2b);
}
public void writeInner(OA oa, OB ob, CA ca, CB cb, boolean a2b)
{
for (WriterAbstract<CA, CB, ?, ?, ?, ?> writer : this.getWriters())
{
writer.write(ca, cb, a2b);
}
if (a2b)
{
FA fa = getA(ca);
FB fb = toB(fa);
setB(cb, fb);
}
else
{
FB fb = getB(cb);
FA fa = toA(fb);
setA(ca, fa);
}
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
public FA getA(CA ca)
{
return null;
}
public void setA(CA ca, FA fa)
{
}
public FB getB(CB cb)
{
return null;
}
public void setB(CB cb, FB fb)
{
}
// -------------------------------------------- //
// CONVERT
// -------------------------------------------- //
private Converter<? super FA, FB> converterTo = ConverterDefault.get();
public Converter<? super FA, FB> getConverterTo() { return this.converterTo; }
public void setConverterTo(Converter<? super FA, FB> converterTo) { this.converterTo = converterTo; }
private Converter<? super FB, FA> converterFrom = ConverterDefault.get();
public Converter<? super FB, FA> getConverterFrom() { return this.converterFrom; }
public void setConverterFrom(Converter<? super FB, FA> converterFrom) { this.converterFrom = converterFrom; }
public FA toA(FB fb)
{
return this.getConverterFrom().convert(fb);
}
public FB toB(FA fa)
{
return this.getConverterTo().convert(fa);
}
}

View File

@ -0,0 +1,40 @@
package com.massivecraft.massivecore.item;
import org.bukkit.DyeColor;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
public abstract class WriterAbstractBannerPattern<FA, FB> extends WriterAbstractReflect<DataBannerPattern, Pattern, DataBannerPattern, Pattern, FA, FB>
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public WriterAbstractBannerPattern(String fieldName)
{
super(Pattern.class, fieldName);
}
public WriterAbstractBannerPattern()
{
this(null);
}
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
@Override
public DataBannerPattern createA()
{
return new DataBannerPattern();
}
@Override
public Pattern createB()
{
return new Pattern(DyeColor.WHITE, PatternType.BASE);
}
}

View File

@ -0,0 +1,39 @@
package com.massivecraft.massivecore.item;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
public abstract class WriterAbstractFireworkEffect<FA, FB> extends WriterAbstractReflect<DataFireworkEffect, FireworkEffect, DataFireworkEffect, FireworkEffect, FA, FB>
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public WriterAbstractFireworkEffect(String fieldName)
{
super(FireworkEffect.class, fieldName);
}
public WriterAbstractFireworkEffect()
{
this(null);
}
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
@Override
public DataFireworkEffect createA()
{
return new DataFireworkEffect();
}
@Override
public FireworkEffect createB()
{
return FireworkEffect.builder().withColor(Color.GREEN).build();
}
}

View File

@ -0,0 +1,8 @@
package com.massivecraft.massivecore.item;
import org.bukkit.inventory.meta.ItemMeta;
public abstract class WriterAbstractItemMeta<M, FA, FB> extends WriterAbstractMeta<ItemMeta, M, FA, FB>
{
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.massivecore.item;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public abstract class WriterAbstractItemStack<FA, FB> extends WriterAbstract<DataItemStack, ItemStack, DataItemStack, ItemStack, FA, FB>
{
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
@Override
public DataItemStack createA()
{
return new DataItemStack();
}
@Override
public ItemStack createB()
{
return new ItemStack(Material.AIR);
}
}

View File

@ -0,0 +1,31 @@
package com.massivecraft.massivecore.item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public abstract class WriterAbstractItemStackMeta<FA, FB> extends WriterAbstractMeta<ItemStack, ItemMeta, FA, FB>
{
// -------------------------------------------- //
// MORPH
// -------------------------------------------- //
@Override
public ItemMeta morphB(ItemStack ob)
{
return ob.getItemMeta();
}
// -------------------------------------------- //
// WRITE
// -------------------------------------------- //
@Override
public void writeInner(DataItemStack oa, ItemStack ob, DataItemStack ca, ItemMeta cb, boolean a2b)
{
super.writeInner(oa, ob, ca, cb, a2b);
// Write back the meta
if (a2b) ob.setItemMeta(cb);
}
}

View File

@ -0,0 +1,39 @@
package com.massivecraft.massivecore.item;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public abstract class WriterAbstractMeta<OB, CB, FA, FB> extends WriterAbstract<DataItemStack, OB, DataItemStack, CB, FA, FB>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private Material material = Material.STONE;
public Material getMaterial() { return this.material; }
@SuppressWarnings("unchecked")
public void setMaterial(Material material)
{
this.material = material;
CB cb = this.createB();
this.setClassB((Class<CB>) cb.getClass());
}
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
@Override
public DataItemStack createA()
{
return new DataItemStack();
}
@SuppressWarnings("unchecked")
@Override
public CB createB()
{
return (CB) new ItemStack(this.getMaterial()).getItemMeta();
}
}

View File

@ -0,0 +1,38 @@
package com.massivecraft.massivecore.item;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public abstract class WriterAbstractPotionEffect<FA, FB> extends WriterAbstractReflect<DataPotionEffect, PotionEffect, DataPotionEffect, PotionEffect, FA, FB>
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public WriterAbstractPotionEffect(String fieldName)
{
super(PotionEffect.class, fieldName);
}
public WriterAbstractPotionEffect()
{
this(null);
}
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
@Override
public DataPotionEffect createA()
{
return new DataPotionEffect();
}
@Override
public PotionEffect createB()
{
return new PotionEffect(PotionEffectType.SPEED, 1, 1);
}
}

View File

@ -0,0 +1,35 @@
package com.massivecraft.massivecore.item;
import java.lang.reflect.Field;
import com.massivecraft.massivecore.util.ReflectionUtil;
public abstract class WriterAbstractReflect<OA, OB, CA, CB, FA, FB> extends WriterAbstract<OA, OB, CA, CB, FA, FB>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final Field field;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public WriterAbstractReflect(Class<?> clazz, String fieldName)
{
this.field = (fieldName == null ? null : ReflectionUtil.getField(clazz, fieldName));
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public void setB(CB cb, FB fb)
{
if (this.field == null) return;
ReflectionUtil.setField(this.field, cb, fb);
}
}

View File

@ -0,0 +1,29 @@
package com.massivecraft.massivecore.item;
public class WriterBannerPattern extends WriterAbstractBannerPattern<Object, Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterBannerPattern i = new WriterBannerPattern();
public static WriterBannerPattern get() { return i; }
// -------------------------------------------- //
// ACTIVE
// -------------------------------------------- //
@Override
public void setActiveInner(boolean active)
{
if ( ! active) return;
this.clearWriters();
this.addWriters(
WriterBannerPatternId.class,
WriterBannerPatternColor.class
);
}
}

View File

@ -0,0 +1,43 @@
package com.massivecraft.massivecore.item;
import org.bukkit.DyeColor;
import org.bukkit.block.banner.Pattern;
public class WriterBannerPatternColor extends WriterAbstractBannerPattern<Integer, DyeColor>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterBannerPatternColor i = new WriterBannerPatternColor();
public static WriterBannerPatternColor get() { return i; }
public WriterBannerPatternColor()
{
super("color");
this.setConverterTo(ConverterToDyeColor.get());
this.setConverterFrom(ConverterFromDyeColor.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Integer getA(DataBannerPattern ca)
{
return ca.getColor();
}
@Override
public void setA(DataBannerPattern ca, Integer fa)
{
ca.setColor(fa);
}
@Override
public DyeColor getB(Pattern cb)
{
return cb.getColor();
}
}

View File

@ -0,0 +1,43 @@
package com.massivecraft.massivecore.item;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
public class WriterBannerPatternId extends WriterAbstractBannerPattern<String, PatternType>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterBannerPatternId i = new WriterBannerPatternId();
public static WriterBannerPatternId get() { return i; }
public WriterBannerPatternId()
{
super("pattern");
this.setConverterTo(ConverterToBannerPatternType.get());
this.setConverterFrom(ConverterFromBannerPatternType.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public String getA(DataBannerPattern ca)
{
return ca.getId();
}
@Override
public void setA(DataBannerPattern ca, String fa)
{
ca.setId(fa);
}
@Override
public PatternType getB(Pattern cb)
{
return cb.getPattern();
}
}

View File

@ -0,0 +1,32 @@
package com.massivecraft.massivecore.item;
public class WriterFireworkEffect extends WriterAbstractFireworkEffect<Object, Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterFireworkEffect i = new WriterFireworkEffect();
public static WriterFireworkEffect get() { return i; }
// -------------------------------------------- //
// ACTIVE
// -------------------------------------------- //
@Override
public void setActiveInner(boolean active)
{
if ( ! active) return;
this.clearWriters();
this.addWriters(
WriterFireworkEffectFlicker.class,
WriterFireworkEffectTrail.class,
WriterFireworkEffectColors.class,
WriterFireworkEffectFadeColors.class,
WriterFireworkEffectType.class
);
}
}

View File

@ -0,0 +1,47 @@
package com.massivecraft.massivecore.item;
import java.util.List;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import com.google.common.collect.ImmutableList;
public class WriterFireworkEffectColors extends WriterAbstractFireworkEffect<List<Integer>, ImmutableList<Color>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterFireworkEffectColors i = new WriterFireworkEffectColors();
public static WriterFireworkEffectColors get() { return i; }
public WriterFireworkEffectColors()
{
super("colors");
this.setConverterTo(ConverterToColors.get());
this.setConverterFrom(ConverterFromColors.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public List<Integer> getA(DataFireworkEffect ca)
{
return ca.getColors();
}
@Override
public void setA(DataFireworkEffect ca, List<Integer> fa)
{
ca.setColors(fa);
}
@Override
public ImmutableList<Color> getB(FireworkEffect cb)
{
return (ImmutableList<Color>) cb.getColors();
}
}

View File

@ -0,0 +1,47 @@
package com.massivecraft.massivecore.item;
import java.util.List;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import com.google.common.collect.ImmutableList;
public class WriterFireworkEffectFadeColors extends WriterAbstractFireworkEffect<List<Integer>, ImmutableList<Color>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterFireworkEffectFadeColors i = new WriterFireworkEffectFadeColors();
public static WriterFireworkEffectFadeColors get() { return i; }
public WriterFireworkEffectFadeColors()
{
super("fadeColors");
this.setConverterTo(ConverterToColors.get());
this.setConverterFrom(ConverterFromColors.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public List<Integer> getA(DataFireworkEffect ca)
{
return ca.getFadeColors();
}
@Override
public void setA(DataFireworkEffect ca, List<Integer> fa)
{
ca.setFadeColors(fa);
}
@Override
public ImmutableList<Color> getB(FireworkEffect cb)
{
return (ImmutableList<Color>) cb.getFadeColors();
}
}

View File

@ -0,0 +1,40 @@
package com.massivecraft.massivecore.item;
import org.bukkit.FireworkEffect;
public class WriterFireworkEffectFlicker extends WriterAbstractFireworkEffect<Boolean, Boolean>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterFireworkEffectFlicker i = new WriterFireworkEffectFlicker();
public static WriterFireworkEffectFlicker get() { return i; }
public WriterFireworkEffectFlicker()
{
super("flicker");
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Boolean getA(DataFireworkEffect ca)
{
return ca.hasFlicker();
}
@Override
public void setA(DataFireworkEffect ca, Boolean fa)
{
ca.setFlicker(fa);
}
@Override
public Boolean getB(FireworkEffect cb)
{
return cb.hasFlicker();
}
}

View File

@ -0,0 +1,40 @@
package com.massivecraft.massivecore.item;
import org.bukkit.FireworkEffect;
public class WriterFireworkEffectTrail extends WriterAbstractFireworkEffect<Boolean, Boolean>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterFireworkEffectTrail i = new WriterFireworkEffectTrail();
public static WriterFireworkEffectTrail get() { return i; }
public WriterFireworkEffectTrail()
{
super("trail");
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Boolean getA(DataFireworkEffect ca)
{
return ca.hasTrail();
}
@Override
public void setA(DataFireworkEffect ca, Boolean fa)
{
ca.setTrail(fa);
}
@Override
public Boolean getB(FireworkEffect cb)
{
return cb.hasTrail();
}
}

View File

@ -0,0 +1,43 @@
package com.massivecraft.massivecore.item;
import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Type;
public class WriterFireworkEffectType extends WriterAbstractFireworkEffect<String, Type>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterFireworkEffectType i = new WriterFireworkEffectType();
public static WriterFireworkEffectType get() { return i; }
public WriterFireworkEffectType()
{
super("type");
this.setConverterTo(ConverterToFireworkEffectType.get());
this.setConverterFrom(ConverterFromFireworkEffectType.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public String getA(DataFireworkEffect ca)
{
return ca.getType();
}
@Override
public void setA(DataFireworkEffect ca, String fa)
{
ca.setType(fa);
}
@Override
public Type getB(FireworkEffect cb)
{
return cb.getType();
}
}

View File

@ -0,0 +1,34 @@
package com.massivecraft.massivecore.item;
public class WriterItemStack extends WriterAbstractItemStack<Object, Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStack i = new WriterItemStack();
public static WriterItemStack get() { return i; }
// -------------------------------------------- //
// ACTIVE
// -------------------------------------------- //
@Override
public void setActiveInner(boolean active)
{
if ( ! active) return;
this.clearWriters();
this.addWriters(
// BASIC
WriterItemStackId.class,
WriterItemStackCount.class,
WriterItemStackDamage.class,
// META
WriterItemStackMeta.class
);
}
}

View File

@ -0,0 +1,42 @@
package com.massivecraft.massivecore.item;
import org.bukkit.inventory.ItemStack;
public class WriterItemStackCount extends WriterAbstractItemStack<Integer, Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackCount i = new WriterItemStackCount();
public static WriterItemStackCount get() { return i; }
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Integer getA(DataItemStack ca)
{
return ca.getCount();
}
@Override
public void setA(DataItemStack ca, Integer fa)
{
ca.setCount(fa);
}
@Override
public Integer getB(ItemStack cb)
{
return cb.getAmount();
}
@Override
public void setB(ItemStack cb, Integer fb)
{
cb.setAmount(fb);
}
}

View File

@ -0,0 +1,42 @@
package com.massivecraft.massivecore.item;
import org.bukkit.inventory.ItemStack;
public class WriterItemStackDamage extends WriterAbstractItemStack<Integer, Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackDamage i = new WriterItemStackDamage();
public static WriterItemStackDamage get() { return i; }
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Integer getA(DataItemStack ca)
{
return ca.getDamage();
}
@Override
public void setA(DataItemStack ca, Integer fa)
{
ca.setDamage(fa);
}
@Override
public Integer getB(ItemStack cb)
{
return Integer.valueOf(cb.getDurability());
}
@Override
public void setB(ItemStack cb, Integer fb)
{
cb.setDurability(fb.shortValue());
}
}

View File

@ -0,0 +1,44 @@
package com.massivecraft.massivecore.item;
import org.bukkit.inventory.ItemStack;
public class WriterItemStackId extends WriterAbstractItemStack<Integer, Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackId i = new WriterItemStackId();
public static WriterItemStackId get() { return i; }
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Integer getA(DataItemStack ca)
{
return ca.getId();
}
@Override
public void setA(DataItemStack ca, Integer fa)
{
ca.setId(fa);
}
@SuppressWarnings("deprecation")
@Override
public Integer getB(ItemStack cb)
{
return cb.getTypeId();
}
@SuppressWarnings("deprecation")
@Override
public void setB(ItemStack cb, Integer fb)
{
cb.setTypeId(fb);
}
}

View File

@ -0,0 +1,77 @@
package com.massivecraft.massivecore.item;
public class WriterItemStackMeta extends WriterAbstractItemStackMeta<Object, Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMeta i = new WriterItemStackMeta();
public static WriterItemStackMeta get() { return i; }
// -------------------------------------------- //
// ACTIVE
// -------------------------------------------- //
@Override
public void setActiveInner(boolean active)
{
if ( ! active) return;
this.clearWriters();
this.addWriters(
// UNSPECIFIC
WriterItemStackMetaName.class,
WriterItemStackMetaLore.class,
WriterItemStackMetaEnchants.class,
WriterItemStackMetaRepaircost.class,
// BOOK
WriterItemStackMetaTitle.class,
WriterItemStackMetaAuthor.class,
WriterItemStackMetaPages.class,
// LEATHER ARMOR
WriterItemStackMetaColor.class,
// MAP
WriterItemStackMetaScaling.class,
// POTION EFFECTS
WriterItemStackMetaPotionEffects.class,
// SKULL
WriterItemStackMetaSkull17.class,
WriterItemStackMetaSkull18.class,
// FIREWORK EFFECT
WriterItemStackMetaFireworkEffect.class,
// FIREWORK
WriterItemStackMetaFireworkEffects.class,
WriterItemStackMetaFireworkFlight.class,
// STORED ENCHANTS
WriterItemStackMetaStoredEnchants.class,
// UNBREAKABLE
WriterItemStackMetaUnbreakable.class,
// FLAGS
WriterItemStackMetaFlags.class,
// BANNER BASE
WriterItemStackMetaBannerBase.class,
// TODO: Shield?
// BANNER PATTERNS
WriterItemStackMetaBannerPatterns.class
// TODO
// TODO: Shield?
// POTION
// TODO
);
}
}

View File

@ -0,0 +1,46 @@
package com.massivecraft.massivecore.item;
import org.bukkit.Material;
import org.bukkit.inventory.meta.BookMeta;
public class WriterItemStackMetaAuthor extends WriterAbstractItemMeta<BookMeta, String, String>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaAuthor i = new WriterItemStackMetaAuthor();
public static WriterItemStackMetaAuthor get() { return i; }
{
this.setMaterial(Material.WRITTEN_BOOK);
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public String getA(DataItemStack ca)
{
return ca.getAuthor();
}
@Override
public void setA(DataItemStack ca, String fa)
{
ca.setAuthor(fa);
}
@Override
public String getB(BookMeta cb)
{
return cb.getAuthor();
}
@Override
public void setB(BookMeta cb, String fb)
{
cb.setAuthor(fb);
}
}

View File

@ -0,0 +1,48 @@
package com.massivecraft.massivecore.item;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.inventory.meta.BannerMeta;
public class WriterItemStackMetaBannerBase extends WriterAbstractItemMeta<BannerMeta, Integer, DyeColor>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaBannerBase i = new WriterItemStackMetaBannerBase();
public static WriterItemStackMetaBannerBase get() { return i; }
public WriterItemStackMetaBannerBase()
{
this.setMaterial(Material.BANNER);
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Integer getA(DataItemStack ca)
{
return ca.getBannerBase();
}
@Override
public void setA(DataItemStack ca, Integer fa)
{
ca.setBannerBase(fa);
}
@Override
public DyeColor getB(BannerMeta cb)
{
return cb.getBaseColor();
}
@Override
public void setB(BannerMeta cb, DyeColor fb)
{
cb.setBaseColor(fb);
}
}

View File

@ -0,0 +1,51 @@
package com.massivecraft.massivecore.item;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.block.banner.Pattern;
import org.bukkit.inventory.meta.BannerMeta;
public class WriterItemStackMetaBannerPatterns extends WriterAbstractItemMeta<BannerMeta, List<DataBannerPattern>, List<Pattern>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaBannerPatterns i = new WriterItemStackMetaBannerPatterns();
public static WriterItemStackMetaBannerPatterns get() { return i; }
{
this.setMaterial(Material.BANNER);
this.setConverterTo(ConverterToBannerPatterns.get());
this.setConverterFrom(ConverterFromBannerPatterns.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public List<DataBannerPattern> getA(DataItemStack ca)
{
return ca.getBannerPatterns();
}
@Override
public void setA(DataItemStack ca, List<DataBannerPattern> fa)
{
ca.setBannerPatterns(fa);
}
@Override
public List<Pattern> getB(BannerMeta cb)
{
return cb.getPatterns();
}
@Override
public void setB(BannerMeta cb, List<Pattern> fb)
{
cb.setPatterns(fb);
}
}

View File

@ -0,0 +1,49 @@
package com.massivecraft.massivecore.item;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.inventory.meta.LeatherArmorMeta;
public class WriterItemStackMetaColor extends WriterAbstractItemMeta<LeatherArmorMeta, Integer, Color>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaColor i = new WriterItemStackMetaColor();
public static WriterItemStackMetaColor get() { return i; }
{
this.setMaterial(Material.LEATHER_HELMET);
this.setConverterTo(ConverterToColor.get());
this.setConverterFrom(ConverterFromColor.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Integer getA(DataItemStack ca)
{
return ca.getColor();
}
@Override
public void setA(DataItemStack ca, Integer fa)
{
ca.setColor(fa);
}
@Override
public Color getB(LeatherArmorMeta cb)
{
return cb.getColor();
}
@Override
public void setB(LeatherArmorMeta cb, Color fb)
{
cb.setColor(fb);
}
}

View File

@ -0,0 +1,54 @@
package com.massivecraft.massivecore.item;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.meta.ItemMeta;
public class WriterItemStackMetaEnchants extends WriterAbstractItemMeta<ItemMeta, Map<Integer, Integer>, Map<Enchantment, Integer>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaEnchants i = new WriterItemStackMetaEnchants();
public static WriterItemStackMetaEnchants get() { return i; }
public WriterItemStackMetaEnchants()
{
this.setConverterTo(ConverterToEnchants.get());
this.setConverterFrom(ConverterFromEnchants.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Map<Integer, Integer> getA(DataItemStack ca)
{
return ca.getEnchants();
}
@Override
public void setA(DataItemStack ca, Map<Integer, Integer> fa)
{
ca.setEnchants(fa);
}
@Override
public Map<Enchantment, Integer> getB(ItemMeta cb)
{
return cb.getEnchants();
}
@Override
public void setB(ItemMeta cb, Map<Enchantment, Integer> fb)
{
for (Entry<Enchantment, Integer> entry : fb.entrySet())
{
cb.addEnchant(entry.getKey(), entry.getValue(), true);
}
}
}

View File

@ -0,0 +1,49 @@
package com.massivecraft.massivecore.item;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.inventory.meta.FireworkEffectMeta;
public class WriterItemStackMetaFireworkEffect extends WriterAbstractItemMeta<FireworkEffectMeta, DataFireworkEffect, FireworkEffect>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaFireworkEffect i = new WriterItemStackMetaFireworkEffect();
public static WriterItemStackMetaFireworkEffect get() { return i; }
{
this.setMaterial(Material.FIREWORK_CHARGE);
this.setConverterTo(ConverterToFireworkEffect.get());
this.setConverterFrom(ConverterFromFireworkEffect.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public DataFireworkEffect getA(DataItemStack ca)
{
return ca.getFireworkEffect();
}
@Override
public void setA(DataItemStack ca, DataFireworkEffect fa)
{
ca.setFireworkEffect(fa);
}
@Override
public FireworkEffect getB(FireworkEffectMeta cb)
{
return cb.getEffect();
}
@Override
public void setB(FireworkEffectMeta cb, FireworkEffect fb)
{
cb.setEffect(fb);
}
}

View File

@ -0,0 +1,51 @@
package com.massivecraft.massivecore.item;
import java.util.List;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.inventory.meta.FireworkMeta;
public class WriterItemStackMetaFireworkEffects extends WriterAbstractItemMeta<FireworkMeta, List<DataFireworkEffect>, List<FireworkEffect>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaFireworkEffects i = new WriterItemStackMetaFireworkEffects();
public static WriterItemStackMetaFireworkEffects get() { return i; }
{
this.setMaterial(Material.FIREWORK);
this.setConverterTo(ConverterToFireworkEffects.get());
this.setConverterFrom(ConverterFromFireworkEffects.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public List<DataFireworkEffect> getA(DataItemStack ca)
{
return ca.getFireworkEffects();
}
@Override
public void setA(DataItemStack ca, List<DataFireworkEffect> fa)
{
ca.setFireworkEffects(fa);
}
@Override
public List<FireworkEffect> getB(FireworkMeta cb)
{
return cb.getEffects();
}
@Override
public void setB(FireworkMeta cb, List<FireworkEffect> fb)
{
cb.addEffects(fb);
}
}

View File

@ -0,0 +1,46 @@
package com.massivecraft.massivecore.item;
import org.bukkit.Material;
import org.bukkit.inventory.meta.FireworkMeta;
public class WriterItemStackMetaFireworkFlight extends WriterAbstractItemMeta<FireworkMeta, Integer, Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaFireworkFlight i = new WriterItemStackMetaFireworkFlight();
public static WriterItemStackMetaFireworkFlight get() { return i; }
{
this.setMaterial(Material.FIREWORK);
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Integer getA(DataItemStack ca)
{
return ca.getFireworkFlight();
}
@Override
public void setA(DataItemStack ca, Integer fa)
{
ca.setFireworkFlight(fa);
}
@Override
public Integer getB(FireworkMeta cb)
{
return cb.getPower();
}
@Override
public void setB(FireworkMeta cb, Integer fb)
{
cb.setPower(fb);
}
}

View File

@ -0,0 +1,50 @@
package com.massivecraft.massivecore.item;
import java.util.Set;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.meta.ItemMeta;
public class WriterItemStackMetaFlags extends WriterAbstractItemMeta<ItemMeta, Set<String>, Set<ItemFlag>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaFlags i = new WriterItemStackMetaFlags();
public static WriterItemStackMetaFlags get() { return i; }
public WriterItemStackMetaFlags()
{
this.setConverterTo(ConverterToItemFlags.get());
this.setConverterFrom(ConverterFromItemFlags.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Set<String> getA(DataItemStack ca)
{
return ca.getFlags();
}
@Override
public void setA(DataItemStack ca, Set<String> fa)
{
ca.setFlags(fa);
}
@Override
public Set<ItemFlag> getB(ItemMeta cb)
{
return cb.getItemFlags();
}
@Override
public void setB(ItemMeta cb, Set<ItemFlag> fb)
{
cb.addItemFlags(fb.toArray(new ItemFlag[0]));
}
}

Some files were not shown because too many files have changed in this diff Show More