Add DyeColor --> ChatColor method in MUtil. Add PotionEffectWrap. Add ARNullable.

This commit is contained in:
Olof Larsson 2015-03-30 11:02:18 +02:00
parent 17e053b0b1
commit a144e160e8
5 changed files with 246 additions and 3 deletions

View File

@ -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<PotionEffectWrap> getEffects(LivingEntity entity)
{
// Create Ret
List<PotionEffectWrap> ret = new MassiveList<PotionEffectWrap>();
// 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<? extends PotionEffectWrap> 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<? extends PotionEffectWrap> potionEffectWraps)
{
// Remove ...
removeEffects(entity);
// ... then add.
addEffects(entity, potionEffectWraps);
}
}

View File

@ -168,7 +168,7 @@ public class MassiveCommand
public void setAliases(List<String> aliases) { this.aliases = aliases; }
public void addAliases(String... aliases) { this.aliases.addAll(Arrays.asList(aliases)); }
public void addAliases(List<String> aliases) { this.aliases.addAll(aliases); }
public void addAliases(Collection<String> aliases) { this.aliases.addAll(aliases); }
// FIELD: requiredArgs
// These args must always be sent

View File

@ -28,7 +28,7 @@ public abstract class ARAbstractPrimitive<T> extends ArgReaderAbstract<T>
}
catch (Exception e)
{
throw new MassiveException().addMsg("<b>Invalid %s \"<h>%s\"<b>.", this.typename(), arg);
throw new MassiveException().addMsg("<b>Invalid %s \"<h>%s<b>\".", this.typename(), arg);
}
return result;

View File

@ -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<T> extends ArgReaderAbstract<T>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static <T> ARNullable<T> get(ArgReader<T> inner)
{
return new ARNullable<T>(inner);
}
public ARNullable(ArgReader<T> inner)
{
this.inner = inner;
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
protected ArgReader<T> inner;
public ArgReader<T> 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);
}
}

View File

@ -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
// -------------------------------------------- //