Last active
August 29, 2015 14:19
-
-
Save GotoFinal/847957744fc630fbe542 to your computer and use it in GitHub Desktop.
Full Item builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
public final class BlockColors | |
{ | |
public static final byte WHITE = 0x0; | |
public static final byte ORANGE = 0x1; | |
public static final byte MAGENTA = 0x2; | |
public static final byte LIGHT_BLUE = 0x3; | |
public static final byte YELLOW = 0x4; | |
public static final byte LIME = 0x5; | |
public static final byte PINK = 0x6; | |
public static final byte GRAY = 0x7; | |
public static final byte LIGHT_GRAY = 0x8; | |
public static final byte CYAN = 0x9; | |
public static final byte PURPLE = 0xA; | |
public static final byte BLUE = 0xB; | |
public static final byte BROWN = 0xC; | |
public static final byte GREEN = 0xD; | |
public static final byte RED = 0xE; | |
public static final byte BLACK = 0xF; | |
private BlockColors() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Map; | |
import org.bukkit.configuration.serialization.SerializableAs; | |
import org.bukkit.inventory.meta.BookMeta; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
import utils.DeserializationWorker; | |
import utils.SerializationBuilder; | |
import utils.Utils; | |
@SerializableAs("GF_BookMeta") | |
public class BookDataBuilder implements DataBuilder | |
{ | |
private String title; | |
private String author; | |
private List<String> pages = new ArrayList<>(10); | |
public BookDataBuilder() | |
{ | |
} | |
@SuppressWarnings("unchecked") | |
public BookDataBuilder(final Map<String, Object> map) | |
{ | |
final DeserializationWorker w = DeserializationWorker.start(map); | |
this.title = w.getString("title"); | |
this.author = w.getString("author"); | |
this.pages = w.getTypedObject("pages", new ArrayList<String>(3)); | |
} | |
public BookDataBuilder title(final String title) | |
{ | |
this.title = title; | |
return this; | |
} | |
public BookDataBuilder author(final String author) | |
{ | |
this.author = author; | |
return this; | |
} | |
public BookDataBuilder newPage(final String page) | |
{ | |
this.pages.add(page); | |
return this; | |
} | |
public BookDataBuilder newPage(final String... pages) | |
{ | |
Collections.addAll(this.pages, pages); | |
return this; | |
} | |
public BookDataBuilder newPage(final Collection<String> pages) | |
{ | |
this.pages.addAll(pages); | |
return this; | |
} | |
public BookDataBuilder newPage(final int index, final String page) | |
{ | |
this.pages.add(index, page); | |
return this; | |
} | |
public BookDataBuilder newPage(final int index, final String... pages) | |
{ | |
return this.newPage(index, Arrays.asList(pages)); | |
} | |
public BookDataBuilder newPage(final int index, final Collection<String> pages) | |
{ | |
this.pages.addAll(index, pages); | |
return this; | |
} | |
public BookDataBuilder setPage(final int index, final String page) | |
{ | |
this.pages.set(index, page); | |
return this; | |
} | |
public BookDataBuilder removePage(final int index) | |
{ | |
this.pages.remove(index); | |
return this; | |
} | |
public BookDataBuilder removePage(final String page) | |
{ | |
this.pages.remove(page); | |
return this; | |
} | |
public BookDataBuilder removePage(final String... pages) | |
{ | |
return this.removePage(Arrays.asList(pages)); | |
} | |
public BookDataBuilder removePage(final Collection<String> pages) | |
{ | |
this.pages.removeAll(pages); | |
return this; | |
} | |
public BookDataBuilder clearPages() | |
{ | |
this.pages.clear(); | |
return this; | |
} | |
public BookDataBuilder clearTitle() | |
{ | |
this.title = null; | |
return this; | |
} | |
public BookDataBuilder clearAuthor() | |
{ | |
this.author = null; | |
return this; | |
} | |
@Override | |
public void apply(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof BookMeta)) | |
{ | |
return; | |
} | |
final BookMeta meta = (BookMeta) itemMeta; | |
meta.setPages(Utils.fixColors(this.pages)); | |
meta.setAuthor(Utils.fixColors(this.author)); | |
meta.setTitle(Utils.fixColors(this.title)); | |
} | |
@Override | |
public BookDataBuilder use(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof BookMeta)) | |
{ | |
return null; | |
} | |
final BookMeta meta = (BookMeta) itemMeta; | |
this.title = Utils.removeColors(meta.getTitle()); | |
this.author = Utils.removeColors(meta.getAuthor()); | |
this.pages = Utils.removeColors(new ArrayList<>(meta.getPages())); | |
return this; | |
} | |
// | |
// @Override | |
// public BookDataBuilder applyFunc(final UnaryOperator<String> func) | |
// { | |
// if (this.title != null) | |
// { | |
// this.title = func.apply(this.title); | |
// } | |
// if (this.author != null) | |
// { | |
// this.author = func.apply(this.author); | |
// } | |
// if ((this.pages != null) && ! this.pages.isEmpty()) | |
// { | |
// this.pages = Stream.of(func.apply(StringUtils.join(this.pages, '\n')).split("\n")).filter(s -> ! s.equals("<NO-LINE>")).collect(Collectors.toList()); | |
// } | |
// return this; | |
// } | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("title", this.title).append("author", this.author).append("pages", this.pages).toString(); | |
} | |
@Override | |
public Map<String, Object> serialize() | |
{ | |
final SerializationBuilder b = SerializationBuilder.start(3); | |
b.append("title", this.title); | |
b.append("author", this.author); | |
b.append("pages", this.pages); | |
return b.build(); | |
} | |
public static BookDataBuilder start() | |
{ | |
return new BookDataBuilder(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
import org.bukkit.configuration.serialization.ConfigurationSerializable; | |
import org.bukkit.inventory.meta.ItemMeta; | |
public interface DataBuilder extends ConfigurationSerializable | |
{ | |
void apply(ItemMeta itemMeta); | |
DataBuilder use(ItemMeta itemMeta); | |
// default DataBuilder applyFunc(final UnaryOperator<String> func) | |
// { | |
// return this; | |
// } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.UUID; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Location; | |
import org.bukkit.enchantments.Enchantment; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
@SuppressWarnings({"rawtypes", "unchecked"}) | |
public class DeserializationWorker | |
{ | |
private final Map data; | |
private DeserializationWorker(final Map data) | |
{ | |
this.data = data; | |
} | |
@SuppressWarnings("unchecked") | |
public <T> T getTypedObject(final String name) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return null; | |
} | |
return (T) this.data.get(name); | |
} | |
public <T> T getTypedObject(final String name, final Class<T> clazz) | |
{ | |
return this.getTypedObject(name); | |
} | |
@SuppressWarnings("unchecked") | |
public <T> T getTypedObject(final String name, final T def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
return (T) this.data.get(name); | |
} | |
public Object getObject(final String name) | |
{ | |
return this.getObject(name, null); | |
} | |
public Object getObject(final String name, final Object def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
return this.data.get(name); | |
} | |
public String getString(final String name) | |
{ | |
return this.getString(name, null); | |
} | |
public String getString(final String name, final String def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
return this.data.get(name).toString(); | |
} | |
public UUID getUUID(final String name) | |
{ | |
return this.getUUID(name, null); | |
} | |
public UUID getUUID(final String name, final UUID def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
return UUID.fromString(this.data.get(name).toString()); | |
} | |
public float getFloat(final String name) | |
{ | |
return this.getFloat(name, 0f); | |
} | |
public float getFloat(final String name, final float def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
return ((Number) this.data.get(name)).floatValue(); | |
} | |
public int getInt(final String name) | |
{ | |
return this.getInt(name, 0); | |
} | |
public int getInt(final String name, final int def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
return ((Number) this.data.get(name)).intValue(); | |
} | |
public boolean getBoolean(final String name) | |
{ | |
return this.getBoolean(name, false); | |
} | |
public boolean getBoolean(final String name, final boolean def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
final Object obj = this.data.get(name); | |
return (obj instanceof Boolean) ? (Boolean) obj : Boolean.parseBoolean(obj.toString()); | |
} | |
public byte getByte(final String name) | |
{ | |
return this.getByte(name, (byte) 0); | |
} | |
public byte getByte(final String name, final byte def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
return ((Number) this.data.get(name)).byteValue(); | |
} | |
public short getShort(final String name) | |
{ | |
return this.getShort(name, (short) 0); | |
} | |
public short getShort(final String name, final short def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
return ((Number) this.data.get(name)).shortValue(); | |
} | |
public <T extends Enum<T>> T getEnum(final String name, final T def) | |
{ | |
return this.getEnum(name, def.getDeclaringClass(), def); | |
} | |
public <T extends Enum<T>> T getEnum(final String name, final Class<T> enumClass) | |
{ | |
return this.getEnum(name, enumClass, null); | |
} | |
public <T extends Enum<T>> T getEnum(final String name, final Class<T> enumClass, final T def) | |
{ | |
final String constName = this.getString(name, (def == null) ? null : def.name()); | |
if (constName == null) | |
{ | |
return def; | |
} | |
final T[] consts = enumClass.getEnumConstants(); | |
for (final T enumConst : consts) | |
{ | |
if (enumConst.name().equals(constName)) | |
{ | |
return enumConst; | |
} | |
} | |
return def; | |
} | |
public Enchantment getEnchantment(final String name) | |
{ | |
return this.getEnchantment(name, null); | |
} | |
public Enchantment getEnchantment(final String name, final Enchantment def) | |
{ | |
final String enchName = this.getString(name, null); | |
if (enchName == null) | |
{ | |
return null; | |
} | |
return Enchantment.getByName(enchName); | |
} | |
public Location getLoc(final String name) | |
{ | |
return this.getLoc(name, null); | |
} | |
public Location getLoc(final String name, final Location def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
@SuppressWarnings("unchecked") | |
final DeserializationWorker dw = DeserializationWorker.start((Map<String, Object>) this.data.get(name)); | |
final String worldName = dw.getString("world"); | |
return new Location((worldName == null) ? null : Bukkit.getWorld(worldName), dw.getInt("x"), dw.getInt("y"), dw.getInt("z"), dw.getFloat("yaw"), dw.getFloat("pitch")); | |
} | |
public List<String> getStringListSafe(final String name) | |
{ | |
return this.getStringList(name, new ArrayList<String>(1)); | |
} | |
public List<String> getStringList(final String name) | |
{ | |
return this.getStringList(name, null); | |
} | |
public List<String> getStringList(final String name, final List<String> def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
return new ArrayList<>((Collection<String>) this.data.get(name)); | |
} | |
public <T> Set<T> getHashSet(final String name) | |
{ | |
return this.getHashSet(name, null); | |
} | |
@SuppressWarnings("unchecked") | |
public <T> Set<T> getHashSet(final String name, final Set<T> def) | |
{ | |
if (! this.data.containsKey(name)) | |
{ | |
return def; | |
} | |
return new HashSet<>((Collection<T>) this.data.get(name)); | |
} | |
public static DeserializationWorker startUnsafe(final Map data) | |
{ | |
try | |
{ | |
data.remove("=="); | |
} catch (final Exception ignored) | |
{ | |
} | |
return new DeserializationWorker(data); | |
} | |
public static DeserializationWorker start(final Map<String, Object> data) | |
{ | |
try | |
{ | |
data.remove("=="); | |
} catch (final Exception ignored) | |
{ | |
} | |
return new DeserializationWorker(data); | |
} | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("data", this.data).toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
import java.util.HashSet; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import org.bukkit.configuration.serialization.SerializableAs; | |
import org.bukkit.enchantments.Enchantment; | |
import org.bukkit.inventory.meta.EnchantmentStorageMeta; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
import utils.SerializationBuilder; | |
@SerializableAs("GF_EnchantmentStorageMeta") | |
public class EnchantmentStorageBuilder implements DataBuilder | |
{ | |
private Map<Enchantment, Integer> enchants = new LinkedHashMap<>(3); | |
public EnchantmentStorageBuilder() | |
{ | |
} | |
public EnchantmentStorageBuilder(final Map<String, Object> map) | |
{ | |
for (final Map.Entry<String, Object> entry : map.entrySet()) | |
{ | |
if ("==".equals(entry.getKey())) | |
{ | |
continue; | |
} | |
this.enchants.put(Enchantment.getByName(entry.getKey()), ((Number) entry.getValue()).intValue()); | |
} | |
} | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("enchants", this.enchants).toString(); | |
} | |
public Map<Enchantment, Integer> getEnchants() | |
{ | |
return this.enchants; | |
} | |
public EnchantmentStorageBuilder enchant(final Map<Enchantment, Integer> enchants) | |
{ | |
this.enchants = enchants; | |
return this; | |
} | |
public EnchantmentStorageBuilder enchant(final Enchantment enchantment, final int power) | |
{ | |
this.enchants.put(enchantment, power); | |
return this; | |
} | |
public EnchantmentStorageBuilder enchant(final Enchantment enchantment) | |
{ | |
this.enchant(enchantment, 1); | |
return this; | |
} | |
public EnchantmentStorageBuilder unEnchant(final Enchantment enchantment) | |
{ | |
this.enchants.remove(enchantment); | |
return this; | |
} | |
public EnchantmentStorageBuilder clear() | |
{ | |
this.enchants.clear(); | |
return this; | |
} | |
@Override | |
public void apply(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof EnchantmentStorageMeta)) | |
{ | |
return; | |
} | |
final EnchantmentStorageMeta meta = (EnchantmentStorageMeta) itemMeta; | |
if (meta.hasStoredEnchants()) | |
{ | |
final Iterable<Enchantment> enchs = new HashSet<>(meta.getStoredEnchants().keySet()); | |
for (final Enchantment ench : enchs) | |
{ | |
meta.removeStoredEnchant(ench); | |
} | |
} | |
for (final Map.Entry<Enchantment, Integer> entry : this.enchants.entrySet()) | |
{ | |
meta.addStoredEnchant(entry.getKey(), entry.getValue(), true); | |
} | |
} | |
@Override | |
public EnchantmentStorageBuilder use(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof EnchantmentStorageMeta)) | |
{ | |
return null; | |
} | |
final EnchantmentStorageMeta meta = (EnchantmentStorageMeta) itemMeta; | |
this.enchants = new LinkedHashMap<>(meta.getStoredEnchants()); | |
return this; | |
} | |
@Override | |
public Map<String, Object> serialize() | |
{ | |
final SerializationBuilder b = SerializationBuilder.start(this.enchants.size()); | |
for (final Map.Entry<Enchantment, Integer> entry : this.enchants.entrySet()) | |
{ | |
b.append(entry.getKey(), entry.getValue()); | |
} | |
return b.build(); | |
} | |
public static EnchantmentStorageBuilder start() | |
{ | |
return new EnchantmentStorageBuilder(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Map; | |
import org.bukkit.FireworkEffect; | |
import org.bukkit.configuration.serialization.SerializableAs; | |
import org.bukkit.inventory.meta.FireworkMeta; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
import utils.DeserializationWorker; | |
import utils.SerializationBuilder; | |
import utils.Utils; | |
@SerializableAs("GF_FireworkMeta") | |
public class FireworkBuilder implements DataBuilder | |
{ | |
private List<FireworkEffect> effects = new ArrayList<>(10); | |
private int power; | |
public FireworkBuilder() | |
{ | |
} | |
@SuppressWarnings("unchecked") | |
public FireworkBuilder(final Map<String, Object> map) | |
{ | |
final DeserializationWorker w = DeserializationWorker.start(map); | |
this.power = w.getInt("power", 0); | |
this.effects = Utils.simpleDeserializeEffects((Collection<Map<Object, Object>>) map.get("effects")); | |
} | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("effects", this.effects).append("power", this.power).toString(); | |
} | |
public FireworkBuilder power(final int power) | |
{ | |
this.power = power; | |
return this; | |
} | |
@SuppressWarnings("TypeMayBeWeakened") | |
public FireworkBuilder effect(final FireworkEffect effect) | |
{ | |
this.effects.add(effect); | |
return this; | |
} | |
public FireworkBuilder effect(final FireworkEffect.Builder effect) | |
{ | |
this.effects.add(effect.build()); | |
return this; | |
} | |
public FireworkBuilder effect(final FireworkEffectBuilder effect) | |
{ | |
this.effects.add(effect.getEffect()); | |
return this; | |
} | |
public FireworkBuilder effect(final FireworkEffect... effects) | |
{ | |
Collections.addAll(this.effects, effects); | |
return this; | |
} | |
public FireworkBuilder effect(final FireworkEffect.Builder... effects) | |
{ | |
for (final FireworkEffect.Builder effect : effects) | |
{ | |
this.effects.add(effect.build()); | |
} | |
return this; | |
} | |
public FireworkBuilder effect(final FireworkEffectBuilder... effects) | |
{ | |
for (final FireworkEffectBuilder effect : effects) | |
{ | |
this.effects.add(effect.getEffect()); | |
} | |
return this; | |
} | |
public FireworkBuilder effect(final Collection<FireworkEffect> effects) | |
{ | |
this.effects.addAll(effects); | |
return this; | |
} | |
public FireworkBuilder remove(final FireworkEffect effect) | |
{ | |
this.effects.remove(effect); | |
return this; | |
} | |
public FireworkBuilder remove(final FireworkEffect.Builder effect) | |
{ | |
this.effects.remove(effect.build()); | |
return this; | |
} | |
public FireworkBuilder remove(final FireworkEffectBuilder effect) | |
{ | |
this.effects.remove(effect.getEffect()); | |
return this; | |
} | |
public FireworkBuilder remove(final int effect) | |
{ | |
this.effects.remove(effect); | |
return this; | |
} | |
public FireworkBuilder clear() | |
{ | |
this.effects.clear(); | |
return this; | |
} | |
@Override | |
public void apply(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof FireworkMeta)) | |
{ | |
return; | |
} | |
final FireworkMeta meta = (FireworkMeta) itemMeta; | |
meta.setPower(this.power); | |
meta.clearEffects(); | |
meta.addEffects(this.effects); | |
} | |
@Override | |
public FireworkBuilder use(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof FireworkMeta)) | |
{ | |
return null; | |
} | |
final FireworkMeta meta = (FireworkMeta) itemMeta; | |
this.power = meta.getPower(); | |
this.effects = new ArrayList<>(meta.getEffects()); | |
return this; | |
} | |
@Override | |
public Map<String, Object> serialize() | |
{ | |
final SerializationBuilder b = SerializationBuilder.start(2); | |
b.append("power", this.power); | |
b.append("effects", Utils.simpleSerializeEffects(this.effects)); | |
return b.build(); | |
} | |
public static FireworkBuilder start() | |
{ | |
return new FireworkBuilder(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
import java.util.Map; | |
import org.bukkit.FireworkEffect; | |
import org.bukkit.configuration.serialization.SerializableAs; | |
import org.bukkit.inventory.meta.FireworkEffectMeta; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
import utils.DeserializationWorker; | |
import utils.SerializationBuilder; | |
import utils.Utils; | |
@SerializableAs("GF_FireworkEffectMeta") | |
public class FireworkEffectBuilder implements DataBuilder | |
{ | |
private FireworkEffect effect; | |
public FireworkEffectBuilder() | |
{ | |
} | |
@SuppressWarnings("unchecked") | |
public FireworkEffectBuilder(final Map<String, Object> map) | |
{ | |
final DeserializationWorker w = DeserializationWorker.start(map); | |
this.effect = Utils.simpleDeserializeEffect(w.<Map<Object, Object>>getTypedObject("effect")); | |
} | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("effect", this.effect).toString(); | |
} | |
public FireworkEffect getEffect() | |
{ | |
return this.effect; | |
} | |
public FireworkEffectBuilder effect(final FireworkEffect effect) | |
{ | |
this.effect = effect; | |
return this; | |
} | |
public FireworkEffectBuilder effect(final FireworkEffect.Builder effect) | |
{ | |
this.effect = effect.build(); | |
return this; | |
} | |
@Override | |
public void apply(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof FireworkEffectMeta)) | |
{ | |
return; | |
} | |
final FireworkEffectMeta meta = (FireworkEffectMeta) itemMeta; | |
meta.setEffect(this.effect); | |
} | |
@Override | |
public FireworkEffectBuilder use(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof FireworkEffectMeta)) | |
{ | |
return null; | |
} | |
final FireworkEffectMeta meta = (FireworkEffectMeta) itemMeta; | |
this.effect = meta.getEffect(); | |
return this; | |
} | |
@Override | |
public Map<String, Object> serialize() | |
{ | |
final SerializationBuilder b = SerializationBuilder.start(1); | |
b.append("effect", Utils.simpleSerializeEffect(this.effect)); | |
return b.build(); | |
} | |
public static FireworkEffectBuilder start() | |
{ | |
return new FireworkEffectBuilder(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.LinkedHashMap; | |
import java.util.List; | |
import java.util.Map; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Material; | |
import org.bukkit.configuration.serialization.ConfigurationSerializable; | |
import org.bukkit.configuration.serialization.ConfigurationSerialization; | |
import org.bukkit.configuration.serialization.SerializableAs; | |
import org.bukkit.enchantments.Enchantment; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.BookMeta; | |
import org.bukkit.inventory.meta.EnchantmentStorageMeta; | |
import org.bukkit.inventory.meta.FireworkEffectMeta; | |
import org.bukkit.inventory.meta.FireworkMeta; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.bukkit.inventory.meta.LeatherArmorMeta; | |
import org.bukkit.inventory.meta.MapMeta; | |
import org.bukkit.inventory.meta.PotionMeta; | |
import org.bukkit.inventory.meta.SkullMeta; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
import utils.DeserializationWorker; | |
import utils.SerializationBuilder; | |
import utils.Utils; | |
@SerializableAs("GF_Item") | |
public class ItemBuilder implements ConfigurationSerializable | |
{ | |
static | |
{ | |
ConfigurationSerialization.registerClass(EnchantmentStorageBuilder.class, "GF_EnchantmentStorageMeta"); | |
ConfigurationSerialization.registerClass(FireworkEffectBuilder.class, "GF_FireworkEffectMeta"); | |
ConfigurationSerialization.registerClass(LeatherArmorBuilder.class, "GF_LeatherArmorMeta"); | |
ConfigurationSerialization.registerClass(PotionDataBuilder.class, "GF_PotionMeta"); | |
ConfigurationSerialization.registerClass(FireworkBuilder.class, "GF_FireworkMeta"); | |
ConfigurationSerialization.registerClass(BookDataBuilder.class, "GF_BookMeta"); | |
ConfigurationSerialization.registerClass(SkullBuilder.class, "GF_SkullMeta"); | |
ConfigurationSerialization.registerClass(MapBuilder.class, "GF_MapMeta"); | |
ConfigurationSerialization.registerClass(ItemBuilder.class, "GF_Item"); | |
// + można tego uzywać w ten sposób | |
// ItemStack item = ItemBuilder.newItem(Material.POTION).amount(5).name("&3Magiczny potek").newLoreLine("&7Daje siłę niszczenia", "&7skryptów i innego zła.").data(PotionDataBuilder.start().main(PotionEffectType.ABSORPTION).add(PotionEffectType.ABSORPTION, 4,100).add(PotionEffectType.DAMAGE_RESISTANCE, 1, 100)).build(); | |
} | |
protected Material material = Material.AIR; | |
protected int amount = 1; | |
protected short durability = 0; | |
protected String name; | |
protected List<String> lore = new ArrayList<>(5); | |
protected Map<Enchantment, Integer> enchants = new LinkedHashMap<>(3); | |
protected DataBuilder dataBuilder = null; | |
// protected UnaryOperator<String> func; | |
public ItemBuilder() | |
{ | |
} | |
@SuppressWarnings("unchecked") | |
public ItemBuilder(final Map<String, Object> map) | |
{ | |
final DeserializationWorker w = DeserializationWorker.start(map); | |
this.material = Utils.getMaterial(w.getString("material", "AIR")); | |
this.amount = w.getInt("amount", 1); | |
this.durability = w.getShort("durability"); | |
this.name = w.getString("name"); | |
this.lore = w.getStringList("lore"); | |
final Map<Object, Object> enchantsMap = w.getTypedObject("enchants"); | |
for (final Map.Entry<Object, Object> entry : enchantsMap.entrySet()) | |
{ | |
this.enchants.put(Enchantment.getByName(entry.getKey().toString()), ((Number) entry.getValue()).intValue()); | |
} | |
this.dataBuilder = w.getTypedObject("data"); | |
} | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("material", this.material).append("amount", this.amount).append("durability", this.durability).append("name", this.name).append("lore", this.lore).append("enchants", this.enchants).append("dataBuilder", this.dataBuilder).toString(); | |
} | |
public Material getMaterial() | |
{ | |
return this.material; | |
} | |
public int getAmount() | |
{ | |
return this.amount; | |
} | |
public short getDurability() | |
{ | |
return this.durability; | |
} | |
public List<String> getLore() | |
{ | |
return this.lore; | |
} | |
public String getName() | |
{ | |
return this.name; | |
} | |
public Map<Enchantment, Integer> getEnchants() | |
{ | |
return this.enchants; | |
} | |
public DataBuilder getDataBuilder() | |
{ | |
return this.dataBuilder; | |
} | |
public ItemBuilder material(final Material material) | |
{ | |
this.material = material; | |
return this; | |
} | |
public ItemBuilder material(final ItemStack source) | |
{ | |
this.material = source.getType(); | |
return this; | |
} | |
public ItemBuilder durability(final int damage) | |
{ | |
return this.durability((short) damage); | |
} | |
public ItemBuilder durability(final short damage) | |
{ | |
this.durability = damage; | |
return this; | |
} | |
public ItemBuilder durability(final ItemStack source) | |
{ | |
this.durability = source.getDurability(); | |
return this; | |
} | |
public ItemBuilder amount(final int amount) | |
{ | |
this.amount = amount; | |
return this; | |
} | |
public ItemBuilder amount(final ItemStack source) | |
{ | |
this.amount = source.getAmount(); | |
return this; | |
} | |
// | |
// public ItemBuilder apply(final UnaryOperator<String> func) | |
// { | |
// this.func = func; | |
// return this; | |
// } | |
public ItemBuilder name(final String name) | |
{ | |
this.name = name; | |
return this; | |
} | |
public ItemBuilder name(final ItemMeta source) | |
{ | |
this.name = Utils.removeColors(source.getDisplayName()); | |
return this; | |
} | |
public ItemBuilder clearName() | |
{ | |
this.name = null; | |
return this; | |
} | |
public ItemBuilder lore(final List<String> lore) | |
{ | |
this.lore = (lore == null) ? new ArrayList<String>(5) : new ArrayList<>(lore); | |
return this; | |
} | |
public ItemBuilder lore(final ItemMeta source) | |
{ | |
this.lore = source.hasLore() ? Utils.removeColors(new ArrayList<>(source.getLore())) : new ArrayList<String>(5); | |
return this; | |
} | |
public ItemBuilder newLoreLine(final String lore) | |
{ | |
this.lore.add(lore); | |
return this; | |
} | |
public ItemBuilder newLoreLine(final Object lore) | |
{ | |
this.lore.add(lore.toString()); | |
return this; | |
} | |
public ItemBuilder newLoreLine(final Collection<String> lore) | |
{ | |
this.lore.addAll(lore); | |
return this; | |
} | |
public ItemBuilder newLoreLine(final String... lore) | |
{ | |
this.newLoreLine(Arrays.asList(lore)); | |
return this; | |
} | |
public ItemBuilder insertLoreLine(final int index, final String lore) | |
{ | |
this.lore.add(index, lore); | |
return this; | |
} | |
public ItemBuilder insertLoreLine(final int index, final Collection<String> lore) | |
{ | |
this.lore.addAll(index, lore); | |
return this; | |
} | |
public ItemBuilder insertLoreLine(final int index, final String... lore) | |
{ | |
this.insertLoreLine(index, Arrays.asList(lore)); | |
return this; | |
} | |
public ItemBuilder removeLoreLine(final String lore) | |
{ | |
this.lore.remove(lore); | |
return this; | |
} | |
public ItemBuilder removeLoreLine(final Collection<String> lore) | |
{ | |
this.lore.removeAll(lore); | |
return this; | |
} | |
public ItemBuilder removeLoreLine(final String... lore) | |
{ | |
this.removeLoreLine(Arrays.asList(lore)); | |
return this; | |
} | |
public ItemBuilder removeLoreLine(final int lore) | |
{ | |
this.lore.remove(lore); | |
return this; | |
} | |
public ItemBuilder setLoreLine(final int index, final String lore) | |
{ | |
this.lore.set(index, lore); | |
return this; | |
} | |
public ItemBuilder clearLore() | |
{ | |
this.lore.clear(); | |
return this; | |
} | |
public ItemBuilder enchant(final Map<Enchantment, Integer> enchants) | |
{ | |
this.enchants = new LinkedHashMap<>(enchants); | |
return this; | |
} | |
public ItemBuilder enchant(final ItemMeta source) | |
{ | |
this.enchants = source.hasEnchants() ? new LinkedHashMap<>(source.getEnchants()) : new LinkedHashMap<Enchantment, Integer>(3); | |
return this; | |
} | |
public ItemBuilder enchant(final Enchantment enchantment, final int power) | |
{ | |
this.enchants.put(enchantment, power); | |
return this; | |
} | |
public ItemBuilder enchant(final Enchantment enchantment) | |
{ | |
this.enchant(enchantment, 1); | |
return this; | |
} | |
public ItemBuilder unEnchant(final Enchantment enchantment) | |
{ | |
this.enchants.remove(enchantment); | |
return this; | |
} | |
public ItemBuilder clearEnchants() | |
{ | |
this.enchants.clear(); | |
return this; | |
} | |
public ItemBuilder data(final DataBuilder dataBuilder) | |
{ | |
this.dataBuilder = dataBuilder; | |
return this; | |
} | |
public ItemBuilder data(final ItemMeta meta) | |
{ | |
// TODO: maybe find some way to do that more OOP | |
if (meta instanceof BookMeta) | |
{ | |
this.dataBuilder = new BookDataBuilder(); | |
} | |
if (meta instanceof EnchantmentStorageMeta) | |
{ | |
this.dataBuilder = new EnchantmentStorageBuilder(); | |
} | |
if (meta instanceof FireworkEffectMeta) | |
{ | |
this.dataBuilder = new FireworkEffectBuilder(); | |
} | |
if (meta instanceof FireworkMeta) | |
{ | |
this.dataBuilder = new FireworkBuilder(); | |
} | |
if (meta instanceof LeatherArmorMeta) | |
{ | |
this.dataBuilder = new LeatherArmorBuilder(); | |
} | |
if (meta instanceof MapMeta) | |
{ | |
this.dataBuilder = new MapBuilder(); | |
} | |
if (meta instanceof PotionMeta) | |
{ | |
this.dataBuilder = new PotionDataBuilder(); | |
} | |
if (meta instanceof SkullMeta) | |
{ | |
this.dataBuilder = new SkullBuilder(); | |
} | |
if (this.dataBuilder != null) | |
{ | |
this.dataBuilder.use(meta); | |
} | |
return this; | |
} | |
public ItemStack build() | |
{ | |
final ItemStack item = new ItemStack(this.material, this.amount, this.durability); | |
// this.applyFunc(); | |
final ItemMeta meta = Bukkit.getItemFactory().getItemMeta(this.material); | |
if (this.name != null) | |
{ | |
meta.setDisplayName(Utils.fixColors(this.name)); | |
} | |
if ((this.lore != null) && ! this.lore.isEmpty()) | |
{ | |
final List<String> lore = new ArrayList<>(this.lore.size() + 5); | |
for (final String loreLine : this.lore) | |
{ | |
Collections.addAll(lore, loreLine.split("\n")); | |
} | |
// this.lore.stream().forEach(str -> Collections.addAll(lore, str.split("\n"))); | |
meta.setLore(Utils.fixColors(lore)); | |
} | |
if (this.enchants != null) | |
{ | |
for (final Map.Entry<Enchantment, Integer> entry : this.enchants.entrySet()) | |
{ | |
meta.addEnchant(entry.getKey(), entry.getValue(), true); | |
} | |
} | |
if (this.dataBuilder != null) | |
{ | |
this.dataBuilder.apply(meta); | |
} | |
item.setItemMeta(meta); | |
return item; | |
} | |
// private void applyFunc() | |
// { | |
// if (this.func == null) | |
// { | |
// return; | |
// } | |
// if (this.name != null) | |
// { | |
// this.name = this.func.apply(this.name); | |
// } | |
// if ((this.lore != null) && ! this.lore.isEmpty()) | |
// { | |
// this.lore = Stream.of(this.func.apply(StringUtils.join(this.lore, '\n')).split("\n")).filter(s -> ! s.equals("<NO-LINE>")).collect(Collectors.toList()); | |
// } | |
// if (this.dataBuilder != null) | |
// { | |
// this.dataBuilder.applyFunc(this.func); | |
// } | |
// } | |
public ItemBuilder reset() | |
{ | |
this.material = Material.AIR; | |
this.amount = 1; | |
this.durability = 0; | |
this.name = null; | |
this.lore.clear(); | |
this.enchants.clear(); | |
this.dataBuilder = null; | |
return this; | |
} | |
@Override | |
public Map<String, Object> serialize() | |
{ | |
final SerializationBuilder b = SerializationBuilder.start(7); | |
b.append("material", this.material); | |
b.append("amount", this.amount); | |
b.append("durability", this.durability); | |
b.append("name", this.name); | |
b.append("lore", this.lore); | |
final SerializationBuilder enchant = SerializationBuilder.start(this.enchants.size()); | |
for (final Map.Entry<Enchantment, Integer> entry : this.enchants.entrySet()) | |
{ | |
enchant.append(entry.getKey(), entry.getValue()); | |
} | |
b.append("enchants", enchant); | |
b.append("data", this.dataBuilder); | |
return b.build(); | |
} | |
public static ItemBuilder newItem(final Material material) | |
{ | |
return new ItemBuilder().material(material); | |
} | |
public static ItemBuilder newItem(final ItemStack itemStack) | |
{ | |
if (itemStack == null) | |
{ | |
return new ItemBuilder(); | |
} | |
final ItemBuilder itemBuilder = new ItemBuilder().material(itemStack).amount(itemStack).durability(itemStack); | |
final ItemMeta meta = Utils.getItemMeta(itemStack); | |
if (meta == null) | |
{ | |
return itemBuilder; | |
} | |
return itemBuilder.name(meta).lore(meta).enchant(meta).data(meta); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
public final class ItemColors | |
{ | |
public static final byte WHITE = 0xF; | |
public static final byte ORANGE = 0xE; | |
public static final byte MAGENTA = 0xD; | |
public static final byte LIGHT_BLUE = 0xC; | |
public static final byte YELLOW = 0xB; | |
public static final byte LIME = 0xA; | |
public static final byte PINK = 0x9; | |
public static final byte GRAY = 0x8; | |
public static final byte LIGHT_GRAY = 0x7; | |
public static final byte CYAN = 0x6; | |
public static final byte PURPLE = 0x5; | |
public static final byte BLUE = 0x4; | |
public static final byte BROWN = 0x3; | |
public static final byte GREEN = 0x2; | |
public static final byte RED = 0x1; | |
public static final byte BLACK = 0x0; | |
private ItemColors() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
import java.util.Map; | |
import org.bukkit.Color; | |
import org.bukkit.configuration.serialization.SerializableAs; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.bukkit.inventory.meta.LeatherArmorMeta; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
import utils.DeserializationWorker; | |
import utils.SerializationBuilder; | |
import utils.Utils; | |
@SerializableAs("GF_LeatherArmorMeta") | |
public class LeatherArmorBuilder implements DataBuilder | |
{ | |
private int red, green, blue; | |
public LeatherArmorBuilder() | |
{ | |
} | |
public LeatherArmorBuilder(final Map<String, Object> map) | |
{ | |
final DeserializationWorker w = DeserializationWorker.start(map); | |
final Color color = Utils.simpleDeserializeColor(w.getString("color")); | |
if (color == null) | |
{ | |
return; | |
} | |
this.color(color); | |
} | |
public int getRed() | |
{ | |
return this.red; | |
} | |
public int getBlue() | |
{ | |
return this.blue; | |
} | |
public int getGreen() | |
{ | |
return this.green; | |
} | |
public LeatherArmorBuilder red(final int red) | |
{ | |
this.red = red; | |
return this; | |
} | |
public LeatherArmorBuilder green(final int green) | |
{ | |
this.green = green; | |
return this; | |
} | |
public LeatherArmorBuilder blue(final int blue) | |
{ | |
this.blue = blue; | |
return this; | |
} | |
public LeatherArmorBuilder color(final int red, final int green, final int blue) | |
{ | |
this.red = red; | |
this.green = green; | |
this.blue = blue; | |
return this; | |
} | |
public LeatherArmorBuilder color(final int[] rgb) | |
{ | |
if (rgb.length != 3) | |
{ | |
throw new IllegalArgumentException("size of rgb array must be: 3"); | |
} | |
this.red = rgb[0]; | |
this.green = rgb[1]; | |
this.blue = rgb[2]; | |
return this; | |
} | |
public LeatherArmorBuilder color(final Color color) | |
{ | |
this.red = color.getRed(); | |
this.green = color.getGreen(); | |
this.blue = color.getBlue(); | |
return this; | |
} | |
@Override | |
public void apply(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof LeatherArmorMeta)) | |
{ | |
return; | |
} | |
final LeatherArmorMeta meta = (LeatherArmorMeta) itemMeta; | |
meta.setColor(Color.fromRGB(this.red, this.green, this.blue)); | |
} | |
@Override | |
public LeatherArmorBuilder use(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof LeatherArmorMeta)) | |
{ | |
return null; | |
} | |
final LeatherArmorMeta meta = (LeatherArmorMeta) itemMeta; | |
return (meta.getColor() != null) ? this.color(meta.getColor()) : this; | |
} | |
@Override | |
public Map<String, Object> serialize() | |
{ | |
final SerializationBuilder b = SerializationBuilder.start(1); | |
b.append("color", Utils.simpleSerializeColor(Color.fromRGB(this.red, this.green, this.blue))); | |
return b.build(); | |
} | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("red", this.red).append("green", this.green).append("blue", this.blue).toString(); | |
} | |
public static LeatherArmorBuilder start() | |
{ | |
return new LeatherArmorBuilder(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
import java.util.Map; | |
import org.bukkit.configuration.serialization.SerializableAs; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.bukkit.inventory.meta.MapMeta; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
import utils.DeserializationWorker; | |
import utils.SerializationBuilder; | |
@SerializableAs("GF_MapMeta") | |
public class MapBuilder implements DataBuilder | |
{ | |
private boolean scaling; | |
public MapBuilder() | |
{ | |
} | |
public MapBuilder(final Map<String, Object> map) | |
{ | |
final DeserializationWorker w = DeserializationWorker.start(map); | |
this.scaling = w.getBoolean("scaling"); | |
} | |
public boolean isScaling() | |
{ | |
return this.scaling; | |
} | |
public MapBuilder scaling(final boolean scaling) | |
{ | |
this.scaling = scaling; | |
return this; | |
} | |
public MapBuilder enableScaling() | |
{ | |
return this.scaling(true); | |
} | |
public MapBuilder disableScaling() | |
{ | |
return this.scaling(false); | |
} | |
@Override | |
public void apply(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof MapMeta)) | |
{ | |
return; | |
} | |
final MapMeta meta = (MapMeta) itemMeta; | |
meta.setScaling(this.scaling); | |
} | |
@Override | |
public MapBuilder use(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof MapMeta)) | |
{ | |
return null; | |
} | |
final MapMeta meta = (MapMeta) itemMeta; | |
this.scaling = meta.isScaling(); | |
return this; | |
} | |
@Override | |
public Map<String, Object> serialize() | |
{ | |
final SerializationBuilder b = SerializationBuilder.start(1); | |
b.append("scaling", this.scaling); | |
return b.build(); | |
} | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("scaling", this.scaling).toString(); | |
} | |
public static MapBuilder start() | |
{ | |
return new MapBuilder(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import org.bukkit.configuration.serialization.SerializableAs; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.bukkit.inventory.meta.PotionMeta; | |
import org.bukkit.potion.PotionEffect; | |
import org.bukkit.potion.PotionEffectType; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
import utils.DeserializationWorker; | |
import utils.SerializationBuilder; | |
@SerializableAs("GF_PotionMeta") | |
public class PotionDataBuilder implements DataBuilder | |
{ | |
protected Map<PotionEffectType, PotionData> potions = new LinkedHashMap<>(5); | |
protected PotionEffectType main; | |
public PotionDataBuilder() | |
{ | |
} | |
@SuppressWarnings("unchecked") | |
public PotionDataBuilder(final Map<String, Object> map) | |
{ | |
final DeserializationWorker w = DeserializationWorker.start(map); | |
this.main = PotionEffectType.getByName(w.getString("main", "SPEED")); | |
final Map<Object, Map<Object, Object>> effects = w.getTypedObject("effects"); | |
for (final Map.Entry<Object, Map<Object, Object>> entry : effects.entrySet()) | |
{ | |
final PotionEffectType type = PotionEffectType.getByName(entry.getKey().toString()); | |
final DeserializationWorker effect = DeserializationWorker.startUnsafe(entry.getValue()); | |
final byte power = effect.getByte("power"); | |
final int time = effect.getInt("time"); | |
final boolean ambient = effect.getBoolean("ambient"); | |
this.potions.put(type, new PotionData(power, time, ambient)); | |
} | |
} | |
public Map<PotionEffectType, PotionData> getPotions() | |
{ | |
return this.potions; | |
} | |
public PotionEffectType getMain() | |
{ | |
return this.main; | |
} | |
public PotionDataBuilder potions(final Map<PotionEffectType, PotionData> potions) | |
{ | |
this.potions = potions; | |
return this; | |
} | |
public PotionDataBuilder add(final PotionEffectType type, final byte power, final int time, final boolean ambient) | |
{ | |
this.potions.put(type, new PotionData(power, time, ambient)); | |
return this; | |
} | |
public PotionDataBuilder add(final PotionEffectType type, final int power, final int time, final boolean ambient) | |
{ | |
return this.add(type, (byte) power, time, ambient); | |
} | |
public PotionDataBuilder add(final PotionEffectType type, final byte power, final int time) | |
{ | |
return this.add(type, power, time, false); | |
} | |
public PotionDataBuilder add(final PotionEffectType type, final int power, final int time) | |
{ | |
return this.add(type, (byte) power, time); | |
} | |
public PotionDataBuilder add(final PotionEffectType type, final byte power) | |
{ | |
return this.add(type, power, 0); | |
} | |
public PotionDataBuilder add(final PotionEffectType type, final int time) | |
{ | |
return this.add(type, 0, time); | |
} | |
public PotionDataBuilder remove(final PotionEffectType type) | |
{ | |
this.potions.remove(type); | |
return this; | |
} | |
public PotionDataBuilder clear() | |
{ | |
this.potions.clear(); | |
return this; | |
} | |
public PotionDataBuilder main(final PotionEffectType type) | |
{ | |
this.main = type; | |
return this; | |
} | |
@Override | |
public void apply(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof PotionMeta)) | |
{ | |
return; | |
} | |
final PotionMeta meta = (PotionMeta) itemMeta; | |
meta.clearCustomEffects(); | |
for (final Map.Entry<PotionEffectType, PotionData> entry : this.potions.entrySet()) | |
{ | |
final PotionData data = entry.getValue(); | |
meta.addCustomEffect(new PotionEffect(entry.getKey(), data.time, data.power, data.ambient), true); | |
} | |
if (this.main != null) | |
{ | |
meta.setMainEffect(this.main); | |
} | |
} | |
@Override | |
public PotionDataBuilder use(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof PotionMeta)) | |
{ | |
return null; | |
} | |
final PotionMeta meta = (PotionMeta) itemMeta; | |
if (meta.hasCustomEffects()) | |
{ | |
this.main = meta.getCustomEffects().get(0).getType(); | |
} | |
for (final PotionEffect effect : meta.getCustomEffects()) | |
{ | |
this.potions.put(effect.getType(), new PotionData((byte) effect.getAmplifier(), effect.getDuration(), effect.isAmbient())); | |
} | |
return this; | |
} | |
@Override | |
public Map<String, Object> serialize() | |
{ | |
final SerializationBuilder b = SerializationBuilder.start(2); | |
b.append("main", this.main.getName()); | |
final SerializationBuilder effects = SerializationBuilder.start(this.potions.size()); | |
for (final Map.Entry<PotionEffectType, PotionData> entry : this.potions.entrySet()) | |
{ | |
final SerializationBuilder effect = SerializationBuilder.start(3); | |
final PotionData potionData = entry.getValue(); | |
effect.append("power", potionData.power); | |
effect.append("time", potionData.time); | |
effect.append("ambient", potionData.ambient); | |
effects.append(entry.getKey(), effect); | |
} | |
b.append("effects", effects); | |
return b.build(); | |
} | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("potions", this.potions).append("main", this.main).toString(); | |
} | |
public class PotionData | |
{ | |
private final byte power; | |
private final int time; | |
private final boolean ambient; | |
public PotionData(final byte power, final int time, final boolean ambient) | |
{ | |
this.power = power; | |
this.time = time; | |
this.ambient = ambient; | |
} | |
public byte getPower() | |
{ | |
return this.power; | |
} | |
public int getTime() | |
{ | |
return this.time; | |
} | |
public boolean isAmbient() | |
{ | |
return this.ambient; | |
} | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("power", this.power).append("time", this.time).append("ambient", this.ambient).toString(); | |
} | |
} | |
public static PotionDataBuilder start() | |
{ | |
return new PotionDataBuilder(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import org.bukkit.Location; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
public class SerializationBuilder | |
{ | |
private final Map<String, Object> data; | |
private SerializationBuilder(final int size) | |
{ | |
this.data = new LinkedHashMap<>(size); | |
} | |
public SerializationBuilder append(final Object str, final Enum<?> object) | |
{ | |
this.data.put(str.toString(), object.name()); | |
return this; | |
} | |
public SerializationBuilder append(final String str, final Enum<?> object) | |
{ | |
this.data.put(str, object.name()); | |
return this; | |
} | |
public SerializationBuilder append(final Object str, final Object object) | |
{ | |
if (object instanceof Enum) | |
{ | |
return this.append(str, (Enum<?>) object); | |
} | |
this.data.put(str.toString(), object); | |
return this; | |
} | |
public SerializationBuilder append(final String str, final Object object) | |
{ | |
if (object instanceof Enum) | |
{ | |
return this.append(str, (Enum<?>) object); | |
} | |
this.data.put(str, object); | |
return this; | |
} | |
public SerializationBuilder append(final String str, final SerializationBuilder object) | |
{ | |
this.data.put(str, object.data); | |
return this; | |
} | |
public SerializationBuilder append(final Object str, final SerializationBuilder object) | |
{ | |
this.data.put(str.toString(), object.data); | |
return this; | |
} | |
public SerializationBuilder appendLoc(final String str, final Location location) | |
{ | |
return this.append(str, start(6).append("x", location.getX()).append("y", location.getY()).append("z", location.getZ()).append("world", (location.getWorld() == null) ? null : location.getWorld().getName()).append("pitch", location.getPitch()).append("yaw", location.getYaw()).build()); | |
} | |
public Map<String, Object> build() | |
{ | |
return this.data; | |
} | |
public static SerializationBuilder start(final int size) | |
{ | |
return new SerializationBuilder(size); | |
} | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("data", this.data).toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
import java.util.Map; | |
import org.bukkit.configuration.serialization.SerializableAs; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.bukkit.inventory.meta.SkullMeta; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
import utils.DeserializationWorker; | |
import utils.SerializationBuilder; | |
import utils.Utils; | |
@SerializableAs("GF_SkullMeta") | |
public class SkullBuilder implements DataBuilder | |
{ | |
private String owner; | |
public SkullBuilder() | |
{ | |
} | |
public SkullBuilder(final Map<String, Object> map) | |
{ | |
final DeserializationWorker w = DeserializationWorker.start(map); | |
this.owner = w.getString("owner"); | |
} | |
public String getOwner() | |
{ | |
return this.owner; | |
} | |
@SuppressWarnings("TypeMayBeWeakened") | |
public SkullBuilder owner(final String owner) | |
{ | |
this.owner = owner; | |
return this; | |
} | |
public SkullBuilder clear() | |
{ | |
this.owner = null; | |
return this; | |
} | |
@Override | |
public void apply(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof SkullMeta)) | |
{ | |
return; | |
} | |
final SkullMeta meta = (SkullMeta) itemMeta; | |
meta.setOwner(Utils.fixColors(this.owner)); | |
} | |
@Override | |
public SkullBuilder use(final ItemMeta itemMeta) | |
{ | |
if (! (itemMeta instanceof SkullMeta)) | |
{ | |
return null; | |
} | |
final SkullMeta meta = (SkullMeta) itemMeta; | |
this.owner = Utils.removeColors(meta.getOwner()); | |
return this; | |
} | |
// @Override | |
// public DataBuilder applyFunc(final UnaryOperator<String> func) | |
// { | |
// if (this.owner != null) | |
// { | |
// this.owner = func.apply(this.owner); | |
// } | |
// return this; | |
// } | |
@Override | |
public Map<String, Object> serialize() | |
{ | |
final SerializationBuilder b = SerializationBuilder.start(1); | |
b.append("owner", this.owner); | |
return b.build(); | |
} | |
@Override | |
public String toString() | |
{ | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("owner", this.owner).toString(); | |
} | |
public static SkullBuilder start() | |
{ | |
return new SkullBuilder(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.builder; | |
public final class SkullType | |
{ | |
public static final byte SKELETON = 0x0; | |
public static final byte WITHER_SKELETON = 0x1; | |
public static final byte ZOMBIE = 0x2; | |
public static final byte PLAYER = 0x3; | |
public static final byte CREEPER = 0x4; | |
private SkullType() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.Map; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Color; | |
import org.bukkit.FireworkEffect; | |
import org.bukkit.FireworkEffect.Type; | |
import org.bukkit.Material; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.ItemMeta; | |
public final class Utils | |
{ | |
private static final String[] EMPTY_STRING_ARRAY = new String[0]; | |
private Utils() | |
{ | |
} | |
public static Integer parseInt(final String num) | |
{ | |
try | |
{ | |
return Integer.valueOf(num); | |
} catch (final Exception ignored) | |
{ | |
return null; | |
} | |
} | |
public static String fixColors(final String string) | |
{ | |
if (string == null) | |
{ | |
return null; | |
} | |
return ChatColor.translateAlternateColorCodes('&', string); | |
} | |
public static String[] fixColors(final String... strings) | |
{ | |
if (strings == null) | |
{ | |
return EMPTY_STRING_ARRAY; | |
} | |
for (int i = 0; i < strings.length; i++) | |
{ | |
strings[i] = fixColors(strings[i]); | |
} | |
return strings; | |
} | |
public static List<String> fixColors(final List<String> list) | |
{ | |
if (list == null) | |
{ | |
return null; | |
} | |
for (int i = 0; i < list.size(); i++) | |
{ | |
list.set(i, fixColors(list.get(i))); | |
} | |
return list; | |
} | |
public static List<String> removeColors(final List<String> list) | |
{ | |
if (list == null) | |
{ | |
return null; | |
} | |
for (int i = 0; i < list.size(); i++) | |
{ | |
list.set(i, removeColors(list.get(i))); | |
} | |
return list; | |
} | |
public static String[] removeColors(final String... strings) | |
{ | |
if (strings == null) | |
{ | |
return EMPTY_STRING_ARRAY; | |
} | |
for (int i = 0; i < strings.length; i++) | |
{ | |
strings[i] = removeColors(strings[i]); | |
} | |
return strings; | |
} | |
public static String removeColors(final String string) | |
{ | |
if (string == null) | |
{ | |
return null; | |
} | |
final char[] b = string.toCharArray(); | |
for (int i = 0; i < (b.length - 1); i++) | |
{ | |
if ((b[i] == ChatColor.COLOR_CHAR) && ("0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i + 1]) > - 1)) | |
{ | |
b[i] = '&'; | |
b[i + 1] = Character.toLowerCase(b[i + 1]); | |
} | |
} | |
return new String(b); | |
} | |
public static Integer toInt(final String str) | |
{ | |
try | |
{ | |
return Integer.parseInt(str); | |
} catch (final Exception e) | |
{ | |
return null; | |
} | |
} | |
@SuppressWarnings("deprecation") | |
public static Material getMaterial(final String mat) | |
{ | |
Material material = Material.getMaterial(mat); | |
if (material == null) | |
{ | |
material = Material.matchMaterial(mat); | |
if (material == null) | |
{ | |
try | |
{ | |
final int id = Integer.parseInt(mat); | |
material = Material.getMaterial(id); | |
} catch (final Exception ignored) | |
{ | |
return Material.AIR; | |
} | |
} | |
} | |
return material; | |
} | |
@SuppressWarnings("MagicNumber") | |
public static Color simpleDeserializeColor(final String string) | |
{ | |
if (string == null) | |
{ | |
return null; | |
} | |
return Color.fromRGB(Integer.parseInt(string, 16)); | |
} | |
public static List<Color> simpleDeserializeColors(final Collection<String> strings) | |
{ | |
if (strings == null) | |
{ | |
return new ArrayList<>(1); | |
} | |
final List<Color> result = new ArrayList<>(strings.size()); | |
for (final String str : strings) | |
{ | |
result.add(simpleDeserializeColor(str)); | |
} | |
return result; | |
} | |
@SuppressWarnings("unchecked") | |
public static FireworkEffect simpleDeserializeEffect(final Map<Object, Object> map) | |
{ | |
if (map == null) | |
{ | |
return null; | |
} | |
final DeserializationWorker w = DeserializationWorker.startUnsafe(map); | |
final FireworkEffect.Type type = w.getEnum("type", Type.BALL); | |
final boolean trail = w.getBoolean("trail"); | |
final boolean flicker = w.getBoolean("flicker"); | |
final List<Color> colors = simpleDeserializeColors(w.<Collection<String>>getTypedObject("colors")); | |
final List<Color> fadeColors = simpleDeserializeColors(w.<Collection<String>>getTypedObject("fadeColors")); | |
return FireworkEffect.builder().with(type).trail(trail).flicker(flicker).withColor(colors).withFade(fadeColors).build(); | |
} | |
public static List<FireworkEffect> simpleDeserializeEffects(final Collection<Map<Object, Object>> list) | |
{ | |
if (list == null) | |
{ | |
return new ArrayList<>(1); | |
} | |
final List<FireworkEffect> result = new ArrayList<>(list.size()); | |
for (final Map<Object, Object> map : list) | |
{ | |
result.add(simpleDeserializeEffect(map)); | |
} | |
return result; | |
} | |
@SuppressWarnings("MagicNumber") | |
public static String simpleSerializeColor(final Color color) | |
{ | |
if (color == null) | |
{ | |
return null; | |
} | |
return Integer.toString(color.asRGB(), 16); | |
} | |
public static List<String> simpleSerializeColors(final Collection<Color> colors) | |
{ | |
if (colors == null) | |
{ | |
return new ArrayList<>(1); | |
} | |
final List<String> result = new ArrayList<>(colors.size()); | |
for (final Color color : colors) | |
{ | |
result.add(simpleSerializeColor(color)); | |
} | |
return result; | |
} | |
public static Map<String, Object> simpleSerializeEffect(final FireworkEffect effect) | |
{ | |
if (effect == null) | |
{ | |
return null; | |
} | |
final SerializationBuilder b = SerializationBuilder.start(5); | |
b.append("type", effect.getType()); | |
b.append("trail", effect.hasTrail()); | |
b.append("flicker", effect.hasFlicker()); | |
b.append("colors", simpleSerializeColors(effect.getColors())); | |
b.append("fadeColors", simpleSerializeColors(effect.getFadeColors())); | |
return b.build(); | |
} | |
public static List<Map<String, Object>> simpleSerializeEffects(final Collection<FireworkEffect> effects) | |
{ | |
if (effects == null) | |
{ | |
return new ArrayList<>(1); | |
} | |
final List<Map<String, Object>> result = new ArrayList<>(effects.size()); | |
for (final FireworkEffect effect : effects) | |
{ | |
result.add(simpleSerializeEffect(effect)); | |
} | |
return result; | |
} | |
public static ItemMeta getItemMeta(final ItemStack itemStack) | |
{ | |
final ItemMeta meta = itemStack.getItemMeta(); | |
if (meta == null) | |
{ | |
return Bukkit.getItemFactory().getItemMeta(itemStack.getType()); | |
} | |
return meta; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment