use of org.bukkit.FireworkEffect in project Denizen-For-Bukkit by DenizenScript.
the class ItemFirework method getFireworkDataMap.
public ListTag getFireworkDataMap() {
List<FireworkEffect> effects;
ListTag list = new ListTag();
if (item.getItemMeta() instanceof FireworkMeta) {
effects = ((FireworkMeta) item.getItemMeta()).getEffects();
} else {
effects = Collections.singletonList(((FireworkEffectMeta) item.getItemMeta()).getEffect());
}
if (effects != null) {
for (FireworkEffect effect : effects) {
if (effect == null) {
continue;
}
Color ColOne = effect.getColors() != null && effect.getColors().size() > 0 ? effect.getColors().get(0) : Color.BLUE;
Color ColTwo = effect.getFadeColors() != null && effect.getFadeColors().size() > 0 ? effect.getFadeColors().get(0) : ColOne;
MapTag effectMap = new MapTag();
effectMap.putObject("trail", new ElementTag(effect.hasTrail()));
effectMap.putObject("flicker", new ElementTag(effect.hasFlicker()));
effectMap.putObject("type", new ElementTag(effect.getType().name()));
effectMap.putObject("color", new ColorTag(ColOne));
effectMap.putObject("fade_color", new ColorTag(ColTwo));
list.addObject(effectMap);
}
}
return list;
}
use of org.bukkit.FireworkEffect in project Denizen-For-Bukkit by DenizenScript.
the class ItemFirework method getFireworkData.
public ListTag getFireworkData() {
List<FireworkEffect> effects;
ListTag list = new ListTag();
if (item.getItemMeta() instanceof FireworkMeta) {
effects = ((FireworkMeta) item.getItemMeta()).getEffects();
int power = ((FireworkMeta) item.getItemMeta()).getPower();
if (power != 0) {
list.add(String.valueOf(power));
}
} else {
effects = Collections.singletonList(((FireworkEffectMeta) item.getItemMeta()).getEffect());
}
if (effects != null) {
for (FireworkEffect effect : effects) {
if (effect == null) {
continue;
}
Color ColOne = effect.getColors() != null && effect.getColors().size() > 0 ? effect.getColors().get(0) : Color.BLUE;
Color ColTwo = effect.getFadeColors() != null && effect.getFadeColors().size() > 0 ? effect.getFadeColors().get(0) : ColOne;
list.add(effect.hasTrail() + "," + effect.hasFlicker() + "," + effect.getType().name() + "," + ColOne.getRed() + "," + ColOne.getGreen() + "," + ColOne.getBlue() + "," + ColTwo.getRed() + "," + ColTwo.getGreen() + "," + ColTwo.getBlue());
}
}
return list;
}
use of org.bukkit.FireworkEffect in project Denizen-For-Bukkit by DenizenScript.
the class ItemFirework method adjust.
@Override
public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("firework_power") && mechanism.requireInteger()) {
if (item.getItemMeta() instanceof FireworkMeta) {
ItemMeta meta = item.getItemMeta();
((FireworkMeta) meta).setPower(mechanism.getValue().asInt());
item.setItemMeta(meta);
} else {
mechanism.echoError("Cannot set the power of a firework effect!");
}
}
// -->
if (mechanism.matches("firework")) {
ItemMeta meta = item.getItemMeta();
if (!mechanism.hasValue()) {
if (meta instanceof FireworkMeta) {
((FireworkMeta) meta).clearEffects();
} else {
((FireworkEffectMeta) meta).setEffect(null);
}
} else {
Collection<ObjectTag> list = CoreUtilities.objectToList(mechanism.getValue(), mechanism.context);
for (ObjectTag object : list) {
if (object.canBeType(MapTag.class)) {
MapTag effectMap = object.asType(MapTag.class, mechanism.context);
FireworkEffect.Builder builder = FireworkEffect.builder();
ObjectTag type = effectMap.getObject("type");
ObjectTag color = effectMap.getObject("color");
ObjectTag fadeColor = effectMap.getObject("fade_color");
ObjectTag trail = effectMap.getObject("trail");
ObjectTag flicker = effectMap.getObject("flicker");
builder.trail(trail != null && trail.asElement().asBoolean());
builder.flicker(flicker != null && flicker.asElement().asBoolean());
if (type != null) {
ElementTag effectType = type.asElement();
if (effectType.matchesEnum(FireworkEffect.Type.class)) {
builder.with(FireworkEffect.Type.valueOf(effectType.asString().toUpperCase()));
} else {
mechanism.echoError("Invalid firework type '" + effectType.asString() + "'");
}
}
ColorTag co = new ColorTag(Color.BLACK);
if (color != null && ColorTag.matches(color.toString())) {
co = ColorTag.valueOf(color.toString(), mechanism.context);
} else if (color != null) {
mechanism.echoError("Invalid color '" + color + "'");
}
builder.withColor(co.getColor());
if (fadeColor != null) {
ColorTag fadeCo = ColorTag.valueOf(fadeColor.toString(), mechanism.context);
if (fadeCo != null) {
builder.withFade(fadeCo.getColor());
} else {
mechanism.echoError("Invalid fade color '" + fadeColor + "'");
}
}
FireworkEffect built = builder.build();
if (meta instanceof FireworkMeta) {
((FireworkMeta) meta).addEffect(built);
} else {
((FireworkEffectMeta) meta).setEffect(built);
}
} else {
String effect = object.toString();
String[] data = effect.split(",");
if (data.length == 9) {
FireworkEffect.Builder builder = FireworkEffect.builder();
builder.trail(new ElementTag(data[0]).asBoolean());
builder.flicker(new ElementTag(data[1]).asBoolean());
if (new ElementTag(data[2]).matchesEnum(FireworkEffect.Type.class)) {
builder.with(FireworkEffect.Type.valueOf(data[2].toUpperCase()));
} else {
mechanism.echoError("Invalid firework type '" + data[2] + "'");
}
builder.withColor(Color.fromRGB(new ElementTag(data[3]).asInt(), new ElementTag(data[4]).asInt(), new ElementTag(data[5]).asInt()));
builder.withFade(Color.fromRGB(new ElementTag(data[6]).asInt(), new ElementTag(data[7]).asInt(), new ElementTag(data[8]).asInt()));
FireworkEffect built = builder.build();
if (meta instanceof FireworkMeta) {
((FireworkMeta) meta).addEffect(built);
} else {
((FireworkEffectMeta) meta).setEffect(built);
}
} else if (data.length == 1) {
if (meta instanceof FireworkMeta) {
((FireworkMeta) meta).setPower(new ElementTag(data[0]).asInt());
} else {
mechanism.echoError("Cannot set the power of a firework effect!");
}
} else {
mechanism.echoError("Invalid firework data '" + effect + "'");
}
}
}
}
item.setItemMeta(meta);
}
}
use of org.bukkit.FireworkEffect in project Essentials by drtshock.
the class MetaItemStack method addFireworkMeta.
public void addFireworkMeta(final CommandSource sender, final boolean allowShortName, final String string, final IEssentials ess) throws Exception {
if (stack.getType() == Material.FIREWORK) {
final String[] split = splitPattern.split(string, 2);
if (split.length < 2) {
return;
}
if (split[0].equalsIgnoreCase("color") || split[0].equalsIgnoreCase("colour") || (allowShortName && split[0].equalsIgnoreCase("c"))) {
if (validFirework) {
if (!hasMetaPermission(sender, "firework", true, true, ess)) {
throw new Exception(tl("noMetaFirework"));
}
FireworkEffect effect = builder.build();
FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
fmeta.addEffect(effect);
if (fmeta.getEffects().size() > 1 && !hasMetaPermission(sender, "firework-multiple", true, true, ess)) {
throw new Exception(tl("multipleCharges"));
}
stack.setItemMeta(fmeta);
builder = FireworkEffect.builder();
}
List<Color> primaryColors = new ArrayList<Color>();
String[] colors = split[1].split(",");
for (String color : colors) {
if (colorMap.containsKey(color.toUpperCase())) {
validFirework = true;
primaryColors.add(colorMap.get(color.toUpperCase()).getFireworkColor());
} else {
throw new Exception(tl("invalidFireworkFormat", split[1], split[0]));
}
}
builder.withColor(primaryColors);
} else if (split[0].equalsIgnoreCase("shape") || split[0].equalsIgnoreCase("type") || (allowShortName && (split[0].equalsIgnoreCase("s") || split[0].equalsIgnoreCase("t")))) {
FireworkEffect.Type finalEffect = null;
split[1] = (split[1].equalsIgnoreCase("large") ? "BALL_LARGE" : split[1]);
if (fireworkShape.containsKey(split[1].toUpperCase())) {
finalEffect = fireworkShape.get(split[1].toUpperCase());
} else {
throw new Exception(tl("invalidFireworkFormat", split[1], split[0]));
}
if (finalEffect != null) {
builder.with(finalEffect);
}
} else if (split[0].equalsIgnoreCase("fade") || (allowShortName && split[0].equalsIgnoreCase("f"))) {
List<Color> fadeColors = new ArrayList<Color>();
String[] colors = split[1].split(",");
for (String color : colors) {
if (colorMap.containsKey(color.toUpperCase())) {
fadeColors.add(colorMap.get(color.toUpperCase()).getFireworkColor());
} else {
throw new Exception(tl("invalidFireworkFormat", split[1], split[0]));
}
}
if (!fadeColors.isEmpty()) {
builder.withFade(fadeColors);
}
} else if (split[0].equalsIgnoreCase("effect") || (allowShortName && split[0].equalsIgnoreCase("e"))) {
String[] effects = split[1].split(",");
for (String effect : effects) {
if (effect.equalsIgnoreCase("twinkle")) {
builder.flicker(true);
} else if (effect.equalsIgnoreCase("trail")) {
builder.trail(true);
} else {
throw new Exception(tl("invalidFireworkFormat", split[1], split[0]));
}
}
}
}
}
use of org.bukkit.FireworkEffect in project Essentials by drtshock.
the class ItemDb method serialize.
@Override
public String serialize(ItemStack is) {
String mat = is.getType().name();
if (is.getData().getData() != 0) {
mat = mat + ":" + is.getData().getData();
}
int quantity = is.getAmount();
// Add space AFTER you add something. We can trim at end.
StringBuilder sb = new StringBuilder();
sb.append(mat).append(" ").append(quantity).append(" ");
// ItemMeta applies to anything.
if (is.hasItemMeta()) {
ItemMeta meta = is.getItemMeta();
if (meta.hasDisplayName()) {
sb.append("name:").append(meta.getDisplayName().replaceAll(" ", "_")).append(" ");
}
if (meta.hasLore()) {
sb.append("lore:");
boolean first = true;
for (String s : meta.getLore()) {
// to do this since we need each line separated by |
if (!first) {
sb.append("|");
}
first = false;
sb.append(s.replaceAll(" ", "_"));
}
sb.append(" ");
}
if (meta.hasEnchants()) {
for (Enchantment e : meta.getEnchants().keySet()) {
sb.append(e.getName().toLowerCase()).append(":").append(meta.getEnchantLevel(e)).append(" ");
}
}
Set<ItemFlag> flags = meta.getItemFlags();
if (flags != null && !flags.isEmpty()) {
sb.append("itemflags:");
boolean first = true;
for (ItemFlag flag : flags) {
if (!first) {
sb.append(",");
}
sb.append(flag.name());
first = false;
}
}
}
switch(is.getType()) {
case WRITTEN_BOOK:
// Everything from http://wiki.ess3.net/wiki/Item_Meta#Books in that order.
// Interesting as I didn't see a way to do pages or chapters.
BookMeta bookMeta = (BookMeta) is.getItemMeta();
if (bookMeta.hasTitle()) {
sb.append("title:").append(bookMeta.getTitle()).append(" ");
}
if (bookMeta.hasAuthor()) {
sb.append("author:").append(bookMeta.getAuthor()).append(" ");
}
// Only other thing it could have is lore but that's done up there ^^^
break;
case ENCHANTED_BOOK:
EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) is.getItemMeta();
for (Enchantment e : enchantmentStorageMeta.getStoredEnchants().keySet()) {
sb.append(e.getName().toLowerCase()).append(":").append(enchantmentStorageMeta.getStoredEnchantLevel(e)).append(" ");
}
break;
case FIREWORK:
// Everything from http://wiki.ess3.net/wiki/Item_Meta#Fireworks in that order.
FireworkMeta fireworkMeta = (FireworkMeta) is.getItemMeta();
if (fireworkMeta.hasEffects()) {
for (FireworkEffect effect : fireworkMeta.getEffects()) {
if (effect.getColors() != null && !effect.getColors().isEmpty()) {
sb.append("color:");
boolean first = true;
for (Color c : effect.getColors()) {
if (!first) {
// same thing as above.
sb.append(",");
}
sb.append(c.toString());
first = false;
}
sb.append(" ");
}
sb.append("shape: ").append(effect.getType().name()).append(" ");
if (effect.getFadeColors() != null && !effect.getFadeColors().isEmpty()) {
sb.append("fade:");
boolean first = true;
for (Color c : effect.getFadeColors()) {
if (!first) {
// same thing as above.
sb.append(",");
}
sb.append(c.toString());
first = false;
}
sb.append(" ");
}
}
sb.append("power: ").append(fireworkMeta.getPower()).append(" ");
}
break;
case POTION:
Potion potion = Potion.fromItemStack(is);
for (PotionEffect e : potion.getEffects()) {
// long but needs to be effect:speed power:2 duration:120 in that order.
sb.append("splash:").append(potion.isSplash()).append(" ").append("effect:").append(e.getType().getName().toLowerCase()).append(" ").append("power:").append(e.getAmplifier()).append(" ").append("duration:").append(e.getDuration() / 20).append(" ");
}
break;
case SKULL_ITEM:
// item stack with meta
SkullMeta skullMeta = (SkullMeta) is.getItemMeta();
if (skullMeta != null && skullMeta.hasOwner()) {
sb.append("player:").append(skullMeta.getOwner()).append(" ");
}
break;
case LEATHER_HELMET:
case LEATHER_CHESTPLATE:
case LEATHER_LEGGINGS:
case LEATHER_BOOTS:
LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) is.getItemMeta();
int rgb = leatherArmorMeta.getColor().asRGB();
sb.append("color:").append(rgb).append(" ");
break;
case BANNER:
BannerMeta bannerMeta = (BannerMeta) is.getItemMeta();
if (bannerMeta != null) {
int basecolor = bannerMeta.getBaseColor().getColor().asRGB();
sb.append("basecolor:").append(basecolor).append(" ");
for (org.bukkit.block.banner.Pattern p : bannerMeta.getPatterns()) {
String type = p.getPattern().getIdentifier();
int color = p.getColor().getColor().asRGB();
sb.append(type).append(",").append(color).append(" ");
}
}
break;
case SHIELD:
// Hacky fix for accessing Shield meta - https://github.com/drtshock/Essentials/pull/745#issuecomment-234843795
BlockStateMeta shieldMeta = (BlockStateMeta) is.getItemMeta();
Banner shieldBannerMeta = (Banner) shieldMeta.getBlockState();
int basecolor = shieldBannerMeta.getBaseColor().getColor().asRGB();
sb.append("basecolor:").append(basecolor).append(" ");
for (org.bukkit.block.banner.Pattern p : shieldBannerMeta.getPatterns()) {
String type = p.getPattern().getIdentifier();
int color = p.getColor().getColor().asRGB();
sb.append(type).append(",").append(color).append(" ");
}
break;
}
return sb.toString().trim().replaceAll("ยง", "&");
}
Aggregations