use of org.bukkit.inventory.meta.FireworkMeta in project CitizensAPI by CitizensDev.
the class ItemStorage method deserialiseMeta.
private static void deserialiseMeta(DataKey root, ItemStack res) {
if (root.keyExists("encoded-meta")) {
byte[] raw = BaseEncoding.base64().decode(root.getString("encoded-meta"));
try {
BukkitObjectInputStream inp = new BukkitObjectInputStream(new ByteArrayInputStream(raw));
ItemMeta meta = (ItemMeta) inp.readObject();
res.setItemMeta(meta);
Bukkit.getPluginManager().callEvent(new CitizensDeserialiseMetaEvent(root, res));
return;
} catch (IOException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
if (SUPPORTS_CUSTOM_MODEL_DATA) {
try {
if (root.keyExists("custommodel")) {
ItemMeta meta = ensureMeta(res);
meta.setCustomModelData(root.getInt("custommodel"));
res.setItemMeta(meta);
}
} catch (Throwable t) {
SUPPORTS_CUSTOM_MODEL_DATA = false;
}
}
if (root.keyExists("flags")) {
ItemMeta meta = ensureMeta(res);
for (DataKey key : root.getRelative("flags").getIntegerSubKeys()) {
meta.addItemFlags(ItemFlag.valueOf(key.getString("")));
}
res.setItemMeta(meta);
}
if (root.keyExists("lore")) {
ItemMeta meta = ensureMeta(res);
List<String> lore = Lists.newArrayList();
for (DataKey key : root.getRelative("lore").getIntegerSubKeys()) lore.add(key.getString(""));
meta.setLore(lore);
res.setItemMeta(meta);
}
if (root.keyExists("displayname")) {
ItemMeta meta = ensureMeta(res);
meta.setDisplayName(root.getString("displayname"));
res.setItemMeta(meta);
}
if (root.keyExists("firework")) {
FireworkMeta meta = ensureMeta(res);
for (DataKey sub : root.getRelative("firework.effects").getIntegerSubKeys()) {
meta.addEffect(deserialiseFireworkEffect(sub));
}
meta.setPower(root.getInt("firework.power"));
res.setItemMeta(meta);
}
if (root.keyExists("book")) {
BookMeta meta = ensureMeta(res);
for (DataKey sub : root.getRelative("book.pages").getIntegerSubKeys()) {
meta.addPage(sub.getString(""));
}
meta.setTitle(root.getString("book.title"));
meta.setAuthor(root.getString("book.author"));
res.setItemMeta(meta);
}
if (root.keyExists("armor")) {
LeatherArmorMeta meta = ensureMeta(res);
meta.setColor(Color.fromRGB(root.getInt("armor.color")));
res.setItemMeta(meta);
}
if (root.keyExists("map")) {
MapMeta meta = ensureMeta(res);
meta.setScaling(root.getBoolean("map.scaling"));
res.setItemMeta(meta);
}
if (root.keyExists("blockstate")) {
BlockStateMeta meta = ensureMeta(res);
if (root.keyExists("blockstate.banner")) {
Banner banner = (Banner) meta.getBlockState();
deserialiseBanner(root.getRelative("blockstate"), banner);
banner.update(true);
meta.setBlockState(banner);
}
res.setItemMeta(meta);
}
if (root.keyExists("enchantmentstorage")) {
EnchantmentStorageMeta meta = ensureMeta(res);
for (DataKey key : root.getRelative("enchantmentstorage").getSubKeys()) {
meta.addStoredEnchant(deserialiseEnchantment(key.name()), key.getInt(""), true);
}
res.setItemMeta(meta);
}
if (root.keyExists("skull")) {
SkullMeta meta = ensureMeta(res);
if (root.keyExists("skull.owner") && !root.getString("skull.owner").isEmpty()) {
meta.setOwner(root.getString("skull.owner", ""));
}
if (root.keyExists("skull.texture") && !root.getString("skull.texture").isEmpty()) {
CitizensAPI.getSkullMetaProvider().setTexture(root.getString("skull.texture", ""), meta);
}
res.setItemMeta(meta);
}
if (root.keyExists("banner")) {
BannerMeta meta = ensureMeta(res);
if (root.keyExists("banner.basecolor")) {
meta.setBaseColor(DyeColor.valueOf(root.getString("banner.basecolor")));
}
if (root.keyExists("banner.patterns")) {
for (DataKey sub : root.getRelative("banner.patterns").getIntegerSubKeys()) {
Pattern pattern = new Pattern(DyeColor.valueOf(sub.getString("color")), PatternType.getByIdentifier(sub.getString("type")));
meta.addPattern(pattern);
}
}
res.setItemMeta(meta);
}
if (root.keyExists("potion")) {
PotionMeta meta = ensureMeta(res);
try {
PotionData data = new PotionData(PotionType.valueOf(root.getString("potion.data.type")), root.getBoolean("potion.data.extended"), root.getBoolean("potion.data.upgraded"));
meta.setBasePotionData(data);
} catch (Throwable t) {
}
for (DataKey sub : root.getRelative("potion.effects").getIntegerSubKeys()) {
int duration = sub.getInt("duration");
int amplifier = sub.getInt("amplifier");
PotionEffectType type = PotionEffectType.getByName(sub.getString("type"));
boolean ambient = sub.getBoolean("ambient");
meta.addCustomEffect(new PotionEffect(type, duration, amplifier, ambient), true);
}
res.setItemMeta(meta);
}
if (root.keyExists("crossbow") && SUPPORTS_1_14_API) {
CrossbowMeta meta = null;
try {
meta = ensureMeta(res);
} catch (Throwable t) {
SUPPORTS_1_14_API = false;
// old MC version
}
if (meta != null) {
for (DataKey key : root.getRelative("crossbow.projectiles").getSubKeys()) {
meta.addChargedProjectile(ItemStorage.loadItemStack(key));
}
res.setItemMeta(meta);
}
}
if (root.keyExists("repaircost") && res.getItemMeta() instanceof Repairable) {
ItemMeta meta = ensureMeta(res);
((Repairable) meta).setRepairCost(root.getInt("repaircost"));
res.setItemMeta(meta);
}
if (root.keyExists("attributes") && SUPPORTS_ATTRIBUTES) {
ItemMeta meta = ensureMeta(res);
try {
for (DataKey attr : root.getRelative("attributes").getSubKeys()) {
Attribute attribute = Attribute.valueOf(attr.name());
for (DataKey modifier : attr.getIntegerSubKeys()) {
UUID uuid = UUID.fromString(modifier.getString("uuid"));
String name = modifier.getString("name");
double amount = modifier.getDouble("amount");
Operation operation = Operation.valueOf(modifier.getString("operation"));
EquipmentSlot slot = modifier.keyExists("slot") ? EquipmentSlot.valueOf(modifier.getString("slot")) : null;
meta.addAttributeModifier(attribute, new AttributeModifier(uuid, name, amount, operation, slot));
}
}
} catch (Throwable e) {
SUPPORTS_ATTRIBUTES = false;
}
res.setItemMeta(meta);
}
ItemMeta meta = res.getItemMeta();
if (meta != null) {
try {
meta.setUnbreakable(root.getBoolean("unbreakable", false));
} catch (Throwable t) {
// probably backwards-compat issue, don't log
}
res.setItemMeta(meta);
}
Bukkit.getPluginManager().callEvent(new CitizensDeserialiseMetaEvent(root, res));
}
use of org.bukkit.inventory.meta.FireworkMeta in project EliteMobs by MagmaGuy.
the class FireworksBarrage method doFireworksBarrage.
public void doFireworksBarrage(EliteEntity eliteEntity) {
eliteEntity.getLivingEntity().setAI(false);
if (eliteEntity.getLivingEntity().getLocation().clone().add(new Vector(0, 10, 0)).getBlock().getType().equals(Material.AIR))
if (!eliteEntity.getLivingEntity().getType().equals(EntityType.GHAST))
eliteEntity.getLivingEntity().teleport(eliteEntity.getLivingEntity().getLocation().clone().add(new Vector(0, 10, 0)));
FireworksBarrage fireworksBarrage = this;
new BukkitRunnable() {
final Location initialLocation = eliteEntity.getLivingEntity().getLocation().clone();
int counter = 0;
@Override
public void run() {
if (!eliteEntity.isValid()) {
cancel();
return;
}
for (int i = 0; i < 2; i++) {
Firework firework = (Firework) eliteEntity.getLivingEntity().getWorld().spawnEntity(eliteEntity.getLivingEntity().getLocation(), EntityType.FIREWORK);
FireworkMeta fireworkMeta = firework.getFireworkMeta();
fireworkMeta.setPower(10);
fireworkMeta.addEffect(FireworkEffect.builder().withColor(Color.RED, Color.WHITE, Color.BLUE).flicker(true).build());
firework.setFireworkMeta(fireworkMeta);
firework.setVelocity(new Vector(ThreadLocalRandom.current().nextDouble(-0.5, 0.5), ThreadLocalRandom.current().nextDouble(), ThreadLocalRandom.current().nextDouble(-0.5, 0.5)));
}
for (Entity nearbyEntity : eliteEntity.getLivingEntity().getNearbyEntities(20, 20, 20)) if (nearbyEntity instanceof Player)
if (((Player) nearbyEntity).getGameMode().equals(GameMode.ADVENTURE) || ((Player) nearbyEntity).getGameMode().equals(GameMode.SURVIVAL)) {
Firework firework = (Firework) eliteEntity.getLivingEntity().getWorld().spawnEntity(eliteEntity.getLivingEntity().getLocation(), EntityType.FIREWORK);
FireworkMeta fireworkMeta = firework.getFireworkMeta();
fireworkMeta.setPower(10);
fireworkMeta.addEffect(FireworkEffect.builder().withColor(Color.RED, Color.WHITE, Color.BLUE).flicker(true).build());
firework.setFireworkMeta(fireworkMeta);
firework.setVelocity(((Player) nearbyEntity).getEyeLocation().clone().subtract(firework.getLocation()).toVector().normalize().multiply(0.5));
firework.setShotAtAngle(true);
new FireworkTask(firework, nearbyEntity.getLocation().clone(), eliteEntity, fireworksBarrage);
}
counter++;
if (counter > 10) {
cancel();
eliteEntity.getLivingEntity().setAI(true);
eliteEntity.getLivingEntity().teleport(initialLocation);
}
}
}.runTaskTimer(MetadataHandler.PLUGIN, 0, 10);
}
use of org.bukkit.inventory.meta.FireworkMeta in project Village_Defense by Plajer.
the class Util method spawnRandomFirework.
public static void spawnRandomFirework(Location location) {
Firework fw = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
FireworkMeta fwm = fw.getFireworkMeta();
// Our random generator
Random r = new Random();
// Get the type
int rt = r.nextInt(4) + 1;
FireworkEffect.Type type = FireworkEffect.Type.BALL;
if (rt == 1)
type = FireworkEffect.Type.BALL;
if (rt == 2)
type = FireworkEffect.Type.BALL_LARGE;
if (rt == 3)
type = FireworkEffect.Type.BURST;
if (rt == 4)
type = FireworkEffect.Type.CREEPER;
if (rt == 5)
type = FireworkEffect.Type.STAR;
// Get our random colours
int r1i = r.nextInt(250) + 1;
int r2i = r.nextInt(250) + 1;
Color c1 = Color.fromBGR(r1i);
Color c2 = Color.fromBGR(r2i);
// Create our effect with this
FireworkEffect effect = FireworkEffect.builder().flicker(r.nextBoolean()).withColor(c1).withFade(c2).with(type).trail(r.nextBoolean()).build();
// Then apply the effect to the meta
fwm.addEffect(effect);
// Generate some random power and set it
int rp = r.nextInt(2) + 1;
fwm.setPower(rp);
fw.setFireworkMeta(fwm);
}
use of org.bukkit.inventory.meta.FireworkMeta in project Essentials by EssentialsX.
the class Commandfirework method run.
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
final ItemStack stack = user.getBase().getItemInHand();
if (stack.getType() == Material.FIREWORK) {
if (args.length > 0) {
if (args[0].equalsIgnoreCase("clear")) {
FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
fmeta.clearEffects();
stack.setItemMeta(fmeta);
user.sendMessage(tl("fireworkEffectsCleared"));
} else if (args.length > 1 && (args[0].equalsIgnoreCase("power") || (args[0].equalsIgnoreCase("p")))) {
FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
try {
int power = Integer.parseInt(args[1]);
fmeta.setPower(power > 3 ? 4 : power);
} catch (NumberFormatException e) {
throw new Exception(tl("invalidFireworkFormat", args[1], args[0]));
}
stack.setItemMeta(fmeta);
} else if ((args[0].equalsIgnoreCase("fire") || (args[0].equalsIgnoreCase("f"))) && user.isAuthorized("essentials.firework.fire")) {
int amount = 1;
boolean direction = false;
if (args.length > 1) {
if (NumberUtil.isInt(args[1])) {
final int serverLimit = ess.getSettings().getSpawnMobLimit();
amount = Integer.parseInt(args[1]);
if (amount > serverLimit) {
amount = serverLimit;
user.sendMessage(tl("mobSpawnLimit"));
}
} else {
direction = true;
}
}
for (int i = 0; i < amount; i++) {
Firework firework = (Firework) user.getWorld().spawnEntity(user.getLocation(), EntityType.FIREWORK);
FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
if (direction) {
final Vector vector = user.getBase().getEyeLocation().getDirection().multiply(0.070);
if (fmeta.getPower() > 1) {
fmeta.setPower(1);
}
firework.setVelocity(vector);
}
firework.setFireworkMeta(fmeta);
}
} else {
final MetaItemStack mStack = new MetaItemStack(stack);
for (String arg : args) {
try {
mStack.addFireworkMeta(user.getSource(), true, arg, ess);
} catch (Exception e) {
user.sendMessage(tl("fireworkSyntax"));
throw e;
}
}
if (mStack.isValidFirework()) {
FireworkMeta fmeta = (FireworkMeta) mStack.getItemStack().getItemMeta();
FireworkEffect effect = mStack.getFireworkBuilder().build();
if (fmeta.getEffects().size() > 0 && !user.isAuthorized("essentials.firework.multiple")) {
throw new Exception(tl("multipleCharges"));
}
fmeta.addEffect(effect);
stack.setItemMeta(fmeta);
} else {
user.sendMessage(tl("fireworkSyntax"));
throw new Exception(tl("fireworkColor"));
}
}
} else {
throw new NotEnoughArgumentsException();
}
} else {
throw new Exception(tl("holdFirework"));
}
}
use of org.bukkit.inventory.meta.FireworkMeta in project Denizen-For-Bukkit by DenizenScript.
the class FireworkCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
final LocationTag location = (LocationTag) scriptEntry.getObject("location");
ElementTag type = scriptEntry.getElement("type");
List<ColorTag> primary = scriptEntry.argForPrefixList("primary", ColorTag.class, true);
if (primary == null) {
primary = Collections.singletonList(new ColorTag(Color.YELLOW));
}
List<ColorTag> fade = scriptEntry.argForPrefixList("fade", ColorTag.class, true);
boolean flicker = scriptEntry.argAsBoolean("flicker");
boolean trail = scriptEntry.argAsBoolean("trail");
ElementTag power = scriptEntry.argForPrefixAsElement("power", "1");
DurationTag life = scriptEntry.argForPrefix("life", DurationTag.class, true);
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), location, type, power, life, db("flicker", flicker), db("trail", trail), db("primary colors", primary), db("fade colors", fade));
}
Firework firework = location.getWorld().spawn(location, Firework.class);
FireworkMeta fireworkMeta = firework.getFireworkMeta();
fireworkMeta.setPower(power.asInt());
Builder fireworkBuilder = FireworkEffect.builder();
fireworkBuilder.with(FireworkEffect.Type.valueOf(type.asString().toUpperCase()));
fireworkBuilder.withColor(Conversion.convertColors(primary));
if (fade != null) {
fireworkBuilder.withFade(Conversion.convertColors(fade));
}
if (flicker) {
fireworkBuilder.withFlicker();
}
if (trail) {
fireworkBuilder.withTrail();
}
fireworkMeta.addEffects(fireworkBuilder.build());
firework.setFireworkMeta(fireworkMeta);
if (life != null) {
NMSHandler.getEntityHelper().setFireworkLifetime(firework, life.getTicksAsInt());
}
scriptEntry.addObject("launched_firework", new EntityTag(firework));
}
Aggregations