From a144e160e86d0460538e970d59c027e2e0ccb675 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 30 Mar 2015 11:02:18 +0200 Subject: [PATCH] Add DyeColor --> ChatColor method in MUtil. Add PotionEffectWrap. Add ARNullable. --- .../massivecore/PotionEffectWrap.java | 142 ++++++++++++++++++ .../massivecore/cmd/MassiveCommand.java | 2 +- .../cmd/arg/ARAbstractPrimitive.java | 2 +- .../massivecore/cmd/arg/ARNullable.java | 43 ++++++ .../massivecraft/massivecore/util/MUtil.java | 60 +++++++- 5 files changed, 246 insertions(+), 3 deletions(-) create mode 100644 src/com/massivecraft/massivecore/PotionEffectWrap.java create mode 100644 src/com/massivecraft/massivecore/cmd/arg/ARNullable.java diff --git a/src/com/massivecraft/massivecore/PotionEffectWrap.java b/src/com/massivecraft/massivecore/PotionEffectWrap.java new file mode 100644 index 00000000..4f7a5bbd --- /dev/null +++ b/src/com/massivecraft/massivecore/PotionEffectWrap.java @@ -0,0 +1,142 @@ +package com.massivecraft.massivecore; + +import java.util.Collection; +import java.util.List; + +import org.bukkit.entity.LivingEntity; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +import com.massivecraft.massivecore.collections.MassiveList; + +/** + * This class wraps the Bukkit PotionEffect class by reimplementing storage of the data. + * The purpose of this class is to allow for serialization using GSON. + * You can not serialize the Bukkit PotionEffect due to some strange GSON bug. + * Also we get the opportunity to add in some nice utility methods. + */ +public class PotionEffectWrap +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + protected int id; + public int getId() { return this.id; } + public void setId(int id) { this.id = id; } + + protected int amplifier; + public int getAmplifier() { return this.amplifier; } + public void setAmplifier(int amplifier) { this.amplifier = amplifier; } + + protected int duration; + public int getDuration() { return this.duration; } + public void setDuration(int duration) { this.duration = duration; } + + protected boolean ambient; + public boolean isAmbient() { return this.ambient; } + public void setAmbient(boolean ambient) { this.ambient = ambient; } + + // Since Minecraft 1.8 + protected boolean particles; + public boolean isParticles() { return this.particles; } + public void setParticles(boolean particles) { this.particles = particles; } + // TODO: How to backwards compat? + // TODO: For now we just don't support this 1.8 option... + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public PotionEffectWrap(int id, int amplifier, int duration, boolean ambient, boolean particles) + { + this.id = id; + this.amplifier = amplifier; + this.duration = duration; + this.ambient = ambient; + this.particles = particles; + } + + public PotionEffectWrap() + { + this.id = 0; + this.amplifier = 0; + this.duration = 0; + this.ambient = false; + this.particles = true; + } + + // -------------------------------------------- // + // FROM BUKKIT + // -------------------------------------------- // + + @SuppressWarnings("deprecation") + public static PotionEffectWrap valueOf(PotionEffect potionEffect) + { + return new PotionEffectWrap(potionEffect.getType().getId(), potionEffect.getAmplifier(), potionEffect.getDuration(), potionEffect.isAmbient(), true); + } + + // -------------------------------------------- // + // TO BUKKIT + // -------------------------------------------- // + + @SuppressWarnings("deprecation") + public PotionEffect asPotionEffect() + { + return new PotionEffect(PotionEffectType.getById(id), this.duration, this.amplifier, this.ambient); + } + + public boolean addTo(LivingEntity entity) + { + return entity.addPotionEffect(this.asPotionEffect(), true); + } + + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + public static List getEffects(LivingEntity entity) + { + // Create Ret + List ret = new MassiveList(); + + // Fill Ret + for (PotionEffect potionEffect : entity.getActivePotionEffects()) + { + ret.add(PotionEffectWrap.valueOf(potionEffect)); + } + + // Return Ret + return ret; + } + + public static void removeEffects(LivingEntity entity) + { + // For each active potion effect ... + for (PotionEffect potionEffect : entity.getActivePotionEffects()) + { + // ... remove that type. + entity.removePotionEffect(potionEffect.getType()); + } + } + + public static void addEffects(LivingEntity entity, Iterable potionEffectWraps) + { + // For each supplied potion effect wrap ... + for (PotionEffectWrap potionEffectWrap : potionEffectWraps) + { + // ... add it to the entity. + potionEffectWrap.addTo(entity); + } + } + + public static void setEffects(LivingEntity entity, Collection potionEffectWraps) + { + // Remove ... + removeEffects(entity); + + // ... then add. + addEffects(entity, potionEffectWraps); + } + +} diff --git a/src/com/massivecraft/massivecore/cmd/MassiveCommand.java b/src/com/massivecraft/massivecore/cmd/MassiveCommand.java index 5ceebbcb..51ccc174 100644 --- a/src/com/massivecraft/massivecore/cmd/MassiveCommand.java +++ b/src/com/massivecraft/massivecore/cmd/MassiveCommand.java @@ -168,7 +168,7 @@ public class MassiveCommand public void setAliases(List aliases) { this.aliases = aliases; } public void addAliases(String... aliases) { this.aliases.addAll(Arrays.asList(aliases)); } - public void addAliases(List aliases) { this.aliases.addAll(aliases); } + public void addAliases(Collection aliases) { this.aliases.addAll(aliases); } // FIELD: requiredArgs // These args must always be sent diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARAbstractPrimitive.java b/src/com/massivecraft/massivecore/cmd/arg/ARAbstractPrimitive.java index 2288e5a3..64f71c46 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARAbstractPrimitive.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARAbstractPrimitive.java @@ -28,7 +28,7 @@ public abstract class ARAbstractPrimitive extends ArgReaderAbstract } catch (Exception e) { - throw new MassiveException().addMsg("Invalid %s \"%s\".", this.typename(), arg); + throw new MassiveException().addMsg("Invalid %s \"%s\".", this.typename(), arg); } return result; diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARNullable.java b/src/com/massivecraft/massivecore/cmd/arg/ARNullable.java new file mode 100644 index 00000000..91a23b6b --- /dev/null +++ b/src/com/massivecraft/massivecore/cmd/arg/ARNullable.java @@ -0,0 +1,43 @@ +package com.massivecraft.massivecore.cmd.arg; + +import org.bukkit.command.CommandSender; + +import com.massivecraft.massivecore.MassiveCore; +import com.massivecraft.massivecore.MassiveException; + +public class ARNullable extends ArgReaderAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + public static ARNullable get(ArgReader inner) + { + return new ARNullable(inner); + } + + public ARNullable(ArgReader inner) + { + this.inner = inner; + } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + protected ArgReader inner; + public ArgReader getInner() { return this.inner; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public T read(String arg, CommandSender sender) throws MassiveException + { + if (MassiveCore.NOTHING_REMOVE.contains(arg)) return null; + + return this.getInner().read(arg, sender); + } + +} diff --git a/src/com/massivecraft/massivecore/util/MUtil.java b/src/com/massivecraft/massivecore/util/MUtil.java index 390a8145..9f6d21a0 100644 --- a/src/com/massivecraft/massivecore/util/MUtil.java +++ b/src/com/massivecraft/massivecore/util/MUtil.java @@ -25,6 +25,7 @@ import java.util.TreeSet; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.DyeColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -477,7 +478,7 @@ public class MUtil // COLOR INT CODE // -------------------------------------------- // - public static int getCode(ChatColor chatColor) + public static int getChatColorCode(ChatColor chatColor) { switch (chatColor) { @@ -507,6 +508,63 @@ public class MUtil throw new IllegalArgumentException("The chat color " + chatColor.name() + " is not yet supported!"); } + public static ChatColor getChatColor(int chatColorCode) + { + switch (chatColorCode) + { + case 0x00: return ChatColor.BLACK; + case 0x1: return ChatColor.DARK_BLUE; + case 0x2: return ChatColor.DARK_GREEN; + case 0x3: return ChatColor.DARK_AQUA; + case 0x4: return ChatColor.DARK_RED; + case 0x5: return ChatColor.DARK_PURPLE; + case 0x6: return ChatColor.GOLD; + case 0x7: return ChatColor.GRAY; + case 0x8: return ChatColor.DARK_GRAY; + case 0x9: return ChatColor.BLUE; + case 0xA: return ChatColor.GREEN; + case 0xB: return ChatColor.AQUA; + case 0xC: return ChatColor.RED; + case 0xD: return ChatColor.LIGHT_PURPLE; + case 0xE: return ChatColor.YELLOW; + case 0xF: return ChatColor.WHITE; + case 0x10: return ChatColor.MAGIC; + case 0x11: return ChatColor.BOLD; + case 0x12: return ChatColor.STRIKETHROUGH; + case 0x13: return ChatColor.UNDERLINE; + case 0x14: return ChatColor.ITALIC; + case 0x15: return ChatColor.RESET; + } + throw new IllegalArgumentException("The chat color code " + chatColorCode + " is not yet supported!"); + } + + @SuppressWarnings("deprecation") + public static ChatColor getChatColor(DyeColor dyeColor) + { + int dyeColorCode = dyeColor.getData(); + + switch (dyeColorCode) + { + case 0x0: return ChatColor.WHITE; + case 0x1: return ChatColor.GOLD; + case 0x2: return ChatColor.LIGHT_PURPLE; + case 0x3: return ChatColor.AQUA; + case 0x4: return ChatColor.YELLOW; + case 0x5: return ChatColor.GREEN; + case 0x6: return ChatColor.LIGHT_PURPLE; + case 0x7: return ChatColor.DARK_GRAY; + case 0x8: return ChatColor.GRAY; + case 0x9: return ChatColor.DARK_AQUA; + case 0xA: return ChatColor.DARK_PURPLE; + case 0xB: return ChatColor.BLUE; + case 0xC: return ChatColor.GRAY; + case 0xD: return ChatColor.DARK_GREEN; + case 0xE: return ChatColor.RED; + case 0xF: return ChatColor.BLACK; + } + throw new IllegalArgumentException("The dye color " + dyeColor + " is not yet supported!"); + } + // -------------------------------------------- // // ENTITY DAMAGE EVENT // -------------------------------------------- //