use of org.bukkit.attribute.Attribute in project RoseLoot by Rosewood-Development.
the class ItemLootMeta method apply.
/**
* Applies stored ItemMeta information to the given ItemStack
*
* @param itemStack The ItemStack to apply ItemMeta to
* @param context The LootContext
* @return The same ItemStack
*/
public ItemStack apply(ItemStack itemStack, LootContext context) {
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null)
return itemStack;
if (this.displayName != null)
itemMeta.setDisplayName(context.formatText(this.displayName));
if (this.lore != null)
itemMeta.setLore(this.lore.stream().map(context::formatText).collect(Collectors.toList()));
if (this.customModelData != null && NMSUtil.getVersionNumber() > 13)
itemMeta.setCustomModelData(this.customModelData);
if (this.unbreakable != null)
itemMeta.setUnbreakable(this.unbreakable);
if (this.hideFlags != null)
itemMeta.addItemFlags(this.hideFlags.toArray(new ItemFlag[0]));
if (itemStack.getType() != Material.ENCHANTED_BOOK) {
if (this.randomEnchantments != null) {
List<Enchantment> possibleEnchantments = new ArrayList<>();
if (!this.randomEnchantments.isEmpty()) {
// Not empty, use the suggested
possibleEnchantments.addAll(this.randomEnchantments);
} else {
// Empty, pick from every applicable enchantment for the item
for (Enchantment enchantment : Enchantment.values()) if (enchantment.canEnchantItem(itemStack))
possibleEnchantments.add(enchantment);
}
Enchantment enchantment = possibleEnchantments.get(LootUtils.RANDOM.nextInt(possibleEnchantments.size()));
int level = LootUtils.RANDOM.nextInt(enchantment.getMaxLevel()) + 1;
itemMeta.addEnchant(enchantment, level, true);
}
if (this.enchantments != null) {
for (EnchantmentData enchantmentData : this.enchantments) {
int level = enchantmentData.getLevel().getInteger();
if (level > 0)
itemMeta.addEnchant(enchantmentData.getEnchantment(), level, true);
}
}
}
if (this.attributes != null) {
Multimap<Attribute, AttributeModifier> attributes = ArrayListMultimap.create();
this.attributes.forEach(x -> attributes.put(x.getAttribute(), x.toAttributeModifier()));
itemMeta.setAttributeModifiers(attributes);
}
if (itemMeta instanceof Damageable && this.minDurability != null) {
Damageable damageable = (Damageable) itemMeta;
int max = itemStack.getType().getMaxDurability();
if (this.maxDurability == null) {
// Set fixed durability value
int durability = this.minDurability.getAsInt(max);
damageable.setDamage(itemStack.getType().getMaxDurability() - durability);
} else {
// Set random durability in range
int minDurability = this.minDurability.getAsInt(max);
int maxDurability = this.maxDurability.getAsInt(max);
damageable.setDamage(itemStack.getType().getMaxDurability() - LootUtils.randomInRange(minDurability, maxDurability));
}
}
if (this.repairCost != null && itemMeta instanceof Repairable)
((Repairable) itemMeta).setRepairCost(this.repairCost);
Optional<Block> lootedBlock = context.get(LootContextParams.LOOTED_BLOCK);
if (lootedBlock.isPresent() && lootedBlock.get().getType() == itemStack.getType()) {
Block block = lootedBlock.get();
if (this.copyBlockState != null && this.copyBlockState && itemMeta instanceof BlockStateMeta)
((BlockStateMeta) itemMeta).setBlockState(block.getState());
if (this.copyBlockData != null && this.copyBlockData && itemMeta instanceof BlockDataMeta)
((BlockDataMeta) itemMeta).setBlockData(block.getBlockData());
if (this.copyBlockName != null && this.copyBlockName && block.getState() instanceof Nameable)
itemMeta.setDisplayName(((Nameable) block.getState()).getCustomName());
}
itemStack.setItemMeta(itemMeta);
if (this.enchantmentLevel != null)
EnchantingUtils.randomlyEnchant(itemStack, this.enchantmentLevel.getInteger(), this.includeTreasureEnchantments, this.uncappedRandomEnchants);
return itemStack;
}
use of org.bukkit.attribute.Attribute in project HoloItemsAPI by StrangeOne101.
the class CustomItem method buildStack.
/**
* Create a new ItemStack for use. NOT for updating existing ones; see {@link #updateStack(ItemStack, Player)}
* @param player The player
* @return The ItemStack
*/
public ItemStack buildStack(Player player) {
ItemStack stack = new ItemStack(getMaterial());
this.random = new Random(name.hashCode());
ItemMeta meta = stack.getItemMeta();
// It's important to use the functions `getDisplayName()` and `getLore()` bellow
// instead of the field names in case an object overrides them
meta.setDisplayName(replaceVariables(getDisplayName(), meta.getPersistentDataContainer()));
if (meta instanceof LeatherArmorMeta) {
((LeatherArmorMeta) meta).setColor(Color.fromRGB(hex));
} else if (meta instanceof PotionMeta) {
((PotionMeta) meta).setColor(Color.fromRGB(hex));
}
List<String> lore = new ArrayList<>();
for (String line : getLore()) {
lore.add(replaceVariables(line, meta.getPersistentDataContainer()));
}
if (!this.jsonLore) {
meta.setLore(lore);
}
// Used for resource packs
if (internalIntID != 0)
meta.setCustomModelData(internalIntID);
if (meta instanceof SkullMeta) {
if (extraData != null) {
ItemUtils.setSkin((SkullMeta) meta, extraData);
}
}
if (player != null) {
if (properties.contains(Properties.OWNER)) {
Properties.OWNER.set(meta.getPersistentDataContainer(), player.getUniqueId());
Properties.OWNER_NAME.set(meta.getPersistentDataContainer(), player.getName());
}
}
if (properties.contains(Properties.COOLDOWN)) {
Properties.COOLDOWN.set(meta.getPersistentDataContainer(), 0L);
}
Properties.ITEM_ID.set(meta.getPersistentDataContainer(), getInternalName());
// meta.getPersistentDataContainer().set(HoloItemsPlugin.getKeys().CUSTOM_ITEM_ID, PersistentDataType.STRING, getInternalName());
if (getMaxDurability() > 0) {
meta.getPersistentDataContainer().set(HoloItemsAPI.getKeys().CUSTOM_ITEM_DURABILITY, PersistentDataType.INTEGER, 0);
}
// If the item shouldn't be stackable, add a random INTEGER to the NBT
Properties.UNSTACKABLE.set(meta.getPersistentDataContainer(), !isStackable());
if (glow && enchantments.isEmpty()) {
stack.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 1);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
for (Enchantment ench : enchantments.keySet()) {
meta.addEnchant(ench, enchantments.get(ench), true);
}
if (flags != null && flags.length > 0)
meta.addItemFlags(flags);
stack.setItemMeta(meta);
// Add all attributes to the item
for (Attribute attr : getAttributes().keySet()) {
for (AttributeModifier.Operation operation : getAttributes().get(attr).keySet()) {
ItemUtils.setAttribute(getAttributes().get(attr).get(operation), attr, operation, stack, this);
}
}
if (this.jsonLore) {
ReflectionUtils.setTrueLore(stack, lore);
}
if (onBuild != null) {
meta = stack.getItemMeta();
onBuild.accept(stack, meta);
}
for (String key : nbt.keySet()) {
stack = HoloItemsAPI.getNMS().writeNBT(nbt.get(key), key, stack);
}
return stack;
}
use of org.bukkit.attribute.Attribute in project HoloItemsAPI by StrangeOne101.
the class CustomItem method updateStack.
/**
* Updates an existing itemstack with updated lore, name and variables
* @param stack The itemstack
* @param player The player holding it
* @return The updated stack
*/
public ItemStack updateStack(ItemStack stack, Player player) {
ItemMeta originalMeta = stack.getItemMeta();
ItemMeta meta = originalMeta;
if (getMaterial() != stack.getType()) {
if (originalMeta instanceof Damageable) {
int damage = ((Damageable) originalMeta).getDamage();
// Rebuild from scratch
stack = buildStack(player);
meta = stack.getItemMeta();
if (meta instanceof Damageable) {
((Damageable) meta).setDamage(damage);
}
}
}
if (properties.contains(Properties.OWNER)) {
UUID uuid = Properties.OWNER.get(meta.getPersistentDataContainer());
String ownerName;
if (uuid != null) {
// The owner can still be none if this is built using no player
if (Bukkit.getPlayer(uuid) != null) {
// If the player is online, use the new name
ownerName = Bukkit.getPlayer(uuid).getName();
} else if (Properties.OWNER_NAME.has(meta.getPersistentDataContainer())) {
ownerName = Properties.OWNER_NAME.get(meta.getPersistentDataContainer());
} else
// Failsafe is the new player's name
ownerName = player.getName();
Properties.OWNER_NAME.set(meta.getPersistentDataContainer(), ownerName);
} else {
// Owner is not defined but it should be
if (player != null) {
// Be sure we aren't gonna get an NPE
Properties.OWNER.set(meta.getPersistentDataContainer(), player.getUniqueId());
Properties.OWNER_NAME.set(meta.getPersistentDataContainer(), player.getName());
}
}
}
if (!properties.contains(Properties.RENAMABLE) || Properties.RENAMABLE.get(meta.getPersistentDataContainer()) == 0) {
// It's important to use the functions `getDisplayName()` and `getLore()` bellow
// instead of the field names in case an object overrides them
meta.setDisplayName(replaceVariables(getDisplayName(), meta.getPersistentDataContainer()));
}
List<String> lore = new ArrayList<>();
for (String line : getLore()) {
lore.add(replaceVariables(line, meta.getPersistentDataContainer()));
}
if (!this.jsonLore) {
meta.setLore(lore);
}
if (meta instanceof LeatherArmorMeta) {
((LeatherArmorMeta) meta).setColor(Color.fromRGB(hex));
} else if (meta instanceof PotionMeta) {
((PotionMeta) meta).setColor(Color.fromRGB(hex));
}
// Used for resource packs
if (internalIntID != 0)
meta.setCustomModelData(internalIntID);
if (meta instanceof SkullMeta) {
if (extraData != null) {
ItemUtils.setSkin((SkullMeta) meta, extraData);
}
}
if (glow && enchantments.isEmpty()) {
stack.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 1);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
for (Enchantment ench : enchantments.keySet()) {
meta.addEnchant(ench, enchantments.get(ench), true);
}
if (flags != null && flags.length > 0)
meta.addItemFlags(flags);
stack.setItemMeta(meta);
// Add all attributes to the item
for (Attribute attr : getAttributes().keySet()) {
for (AttributeModifier.Operation operation : getAttributes().get(attr).keySet()) {
ItemUtils.setAttribute(getAttributes().get(attr).get(operation), attr, operation, stack, this);
}
}
if (this.jsonLore) {
ReflectionUtils.setTrueLore(stack, lore);
}
if (onUpdate != null) {
meta = stack.getItemMeta();
onUpdate.accept(stack, meta);
}
for (String key : nbt.keySet()) {
stack = HoloItemsAPI.getNMS().writeNBT(nbt.get(key), key, stack);
}
return stack;
}
use of org.bukkit.attribute.Attribute in project HoloItemsAPI by StrangeOne101.
the class EntityUtils method decode.
/**
* Gets all the properties in the map and applies them to the entity
* @param entity The entity to apply the properties to
* @param properties The properties
*/
public static void decode(Entity entity, Map<String, Object> properties) {
if (properties.containsKey("motion"))
entity.setVelocity((Vector) properties.get("motion"));
if (properties.containsKey("name"))
entity.setCustomName((String) properties.get("name"));
if (properties.containsKey("nameVisible"))
entity.setCustomNameVisible((boolean) properties.get("nameVisible"));
if (properties.containsKey("ticksLived"))
entity.setTicksLived((int) properties.get("ticksLived"));
if (entity instanceof Ageable) {
((Ageable) entity).setAge((int) properties.get("age"));
}
if (entity instanceof LivingEntity) {
if (properties.containsKey("attributes")) {
List<AttributeModifier> modifiers = (List<AttributeModifier>) properties.get("attributes");
for (AttributeModifier mod : modifiers) {
Attribute attribute = Attribute.valueOf(mod.getName());
if (((LivingEntity) entity).getAttribute(attribute) != null)
((LivingEntity) entity).getAttribute(attribute).addModifier(mod);
}
}
if (properties.containsKey("health"))
((LivingEntity) entity).setHealth((Double) properties.get("health"));
if (properties.containsKey("fire"))
entity.setFireTicks((Integer) properties.get("fire"));
if (properties.containsKey("invincible"))
((LivingEntity) entity).setNoDamageTicks((Integer) properties.get("invincible"));
if (properties.containsKey("itempickup"))
((LivingEntity) entity).setCanPickupItems((boolean) properties.get("itempickup"));
if (properties.containsKey("potions")) {
Collection<PotionEffect> potions = (Collection<PotionEffect>) properties.get("potions");
((LivingEntity) entity).addPotionEffects(potions);
}
}
if (entity instanceof Tameable) {
if (properties.containsKey("tamed")) {
((Tameable) entity).setTamed((boolean) properties.get("tamed"));
}
if (properties.containsKey("owner")) {
OfflinePlayer owner = Bukkit.getOfflinePlayer((UUID) properties.get("owner"));
((Tameable) entity).setOwner(owner);
}
}
if (entity instanceof Cat) {
if (properties.containsKey("collar"))
((Cat) entity).setCollarColor(DyeColor.values()[(int) properties.get("collar")]);
if (properties.containsKey("variant"))
((Cat) entity).setCatType(Cat.Type.values()[(int) properties.get("variant")]);
} else if (entity instanceof Wolf) {
if (properties.containsKey("collar"))
((Wolf) entity).setCollarColor(DyeColor.values()[(int) properties.get("collar")]);
} else if (entity instanceof Horse) {
if (properties.containsKey("variant")) {
int variant = (int) properties.get("variant");
int color = variant & 0xFF;
int style = variant >> 8;
((Horse) entity).setStyle(Horse.Style.values()[style]);
((Horse) entity).setColor(Horse.Color.values()[color]);
}
if (properties.containsKey("temper"))
((Horse) entity).setDomestication((int) properties.get("temper"));
}
}
use of org.bukkit.attribute.Attribute in project HoloItemsAPI by StrangeOne101.
the class EntityUtils method encode.
/**
* Gets all the properties of an entity and puts them all in a map. Should only be used for LivingEntities
* @param entity The entity to encode
* @param flags The flags to skip on the entity
* @return The properties
*/
public static Map<String, Object> encode(Entity entity, int flags) {
Map<String, Object> properties = new HashMap<>();
properties.put("type", entity.getType().toString());
if (!test(flags, SKIP_NAME)) {
String name = entity.getCustomName();
if (name != null)
properties.put("name", name);
properties.put("nameVisible", entity.isCustomNameVisible());
}
// Age
if (!test(flags, SKIP_AGE)) {
properties.put("tickedLived", entity.getTicksLived());
if (entity instanceof Ageable) {
properties.put("age", ((Ageable) entity).getAge());
}
}
// Location
if (!test(flags, SKIP_LOCATION)) {
properties.put("pos", entity.getLocation());
}
// Velocity
if (!test(flags, SKIP_VELOCITY)) {
properties.put("motion", entity.getVelocity());
}
// Attributes, Memories, Health, Potions and on fire, etc
if (entity instanceof LivingEntity) {
if (!test(flags, SKIP_ATTRIBUTES)) {
List<AttributeModifier> modifiers = new ArrayList<>();
for (Attribute attr : Attribute.values()) {
AttributeInstance a = ((LivingEntity) entity).getAttribute(attr);
if (a != null)
modifiers.addAll(a.getModifiers());
}
properties.put("attributes", modifiers);
}
if (!test(flags, SKIP_HEALTH))
properties.put("health", ((LivingEntity) entity).getHealth());
if (!test(flags, SKIP_POTIONS))
properties.put("potions", ((LivingEntity) entity).getActivePotionEffects());
if (!test(flags, SKIP_LIVING_EFFECTS)) {
properties.put("fire", entity.getFireTicks());
properties.put("invincible", ((LivingEntity) entity).getNoDamageTicks());
}
properties.put("itempickup", ((LivingEntity) entity).getCanPickupItems());
if (!test(flags, SKIP_MEMORIES)) {
Map<String, Object> memories = new HashMap<>();
for (MemoryKey key : MemoryKey.values()) {
Object o = ((LivingEntity) entity).getMemory(key);
if (o != null)
memories.put(key.toString(), o);
}
properties.put("memories", memories);
}
}
if (entity instanceof Tameable && !test(flags, SKIP_OWNER)) {
properties.put("owner", ((Tameable) entity).getOwner().getUniqueId());
properties.put("tamed", ((Tameable) entity).isTamed());
}
if (entity instanceof Cat && !test(flags, SKIP_VARIANT)) {
// Convert Color to number
properties.put("collar", ((Cat) entity).getCollarColor().ordinal());
// Convert type to number
properties.put("variant", ((Cat) entity).getCatType().ordinal());
} else if (entity instanceof Wolf) {
properties.put("collar", ((Wolf) entity).getCollarColor().ordinal());
} else if (entity instanceof Horse && !test(flags, SKIP_VARIANT)) {
int variant = ((Horse) entity).getColor().ordinal();
variant += ((Horse) entity).getStyle().ordinal() << 8;
properties.put("variant", variant);
properties.put("temper", ((Horse) entity).getDomestication());
} else if (entity instanceof TropicalFish && !test(flags, SKIP_VARIANT)) {
int variant = ((TropicalFish) entity).getBodyColor().ordinal();
variant += ((TropicalFish) entity).getPatternColor().ordinal() << 8;
variant += ((TropicalFish) entity).getPattern().ordinal() << 16;
properties.put("variant", variant);
} else if (entity instanceof Parrot && !test(flags, SKIP_VARIANT)) {
properties.put("variant", ((Parrot) entity).getVariant().ordinal());
} else if (entity instanceof Colorable && !test(flags, SKIP_VARIANT)) {
// Sheep and Shulker
if (((Colorable) entity).getColor() != null)
properties.put("variant", ((Colorable) entity).getColor().ordinal());
} else if (entity instanceof Llama && !test(flags, SKIP_VARIANT)) {
properties.put("variant", ((Llama) entity).getColor().ordinal());
properties.put("strength", ((Llama) entity).getStrength());
} else if (entity instanceof Rabbit && !test(flags, SKIP_VARIANT)) {
properties.put("variant", ((Rabbit) entity).getRabbitType().ordinal());
} else if (entity instanceof Creeper && !test(flags, SKIP_VARIANT)) {
properties.put("variant", ((Creeper) entity).isPowered());
} else if (entity instanceof Snowman) {
properties.put("derp", ((Snowman) entity).isDerp());
} else if (entity instanceof IronGolem) {
properties.put("playermade", ((IronGolem) entity).isPlayerCreated());
} else if (entity instanceof Villager) {
if (!test(flags, SKIP_VARIANT)) {
properties.put("variant", ((Villager) entity).getVillagerType().ordinal());
}
if (!test(flags, SKIP_TRADES)) {
properties.put("trades", ((Villager) entity).getRecipes());
properties.put("profession", ((Villager) entity).getProfession().ordinal());
properties.put("exp", ((Villager) entity).getVillagerExperience());
properties.put("lvl", ((Villager) entity).getVillagerLevel());
}
}
if (entity instanceof InventoryHolder && !test(flags, SKIP_INVENTORY)) {
properties.put("inventory", ((InventoryHolder) entity).getInventory());
}
return properties;
}
Aggregations