123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- package me.km.utils;
- import java.util.ArrayList;
- import java.util.List;
- import me.km.exception.IllegalItemStackStringException;
- import net.minecraft.block.Block;
- import net.minecraft.entity.ai.attributes.AttributeModifier;
- import net.minecraft.inventory.EntityEquipmentSlot;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.JsonToNBT;
- import net.minecraft.nbt.NBTException;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.nbt.NBTTagString;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.World;
- public class ItemStackUtils
- {
- // -----------------------------------------------------------------------------------
- // Attribute
- // -----------------------------------------------------------------------------------
-
- public enum Attribute
- {
- /**
- * value from 0 to 30;
- */
- ARMOR("generic.armor"),
- /**
- * value from 0 to 20;
- */
- ARMOR_TOUGHNESS("generic.armorToughness"),
- /**
- * value from 0 to 1.7E308;
- */
- ATTACK_DAMAGE("generic.attackDamage"),
- /**
- * value from 0 to 1;
- */
- KNOCKBACK_RESISTANCE("generic.knockbackResistance"),
- /**
- * value from 0 to 1.7E308;
- */
- MAX_HEALTH("generic.maxHealth"),
- /**
- * value from 0 to 1.7E308;
- */
- MOVEMENT_SPEED("generic.movementSpeed"),
- /**
- * value from 0 to 1024;
- */
- ATTACK_SPEED("generic.attackSpeed"),
- /**
- * value from -1024 to 1024;
- */
- LUCK("generic.luck");
-
- private final String name;
-
- Attribute(String name)
- {
- this.name = name;
- }
-
- public String getName()
- {
- return name;
- }
- }
-
- public enum Operation
- {
- ADD, MUL, MUL_CHANGED
- }
-
- public static void addAttribute(ItemStack stack, Attribute a, EntityEquipmentSlot slot, double amount, Operation op)
- {
- stack.addAttributeModifier(a.getName(), new AttributeModifier("modifier", amount, op.ordinal()), slot);
- }
-
- // -----------------------------------------------------------------------------------
- // Item-Flags
- // -----------------------------------------------------------------------------------
-
- public enum ItemFlag
- {
- /**
- * Setting to show/hide enchants
- */
- HIDE_ENCHANTS,
- /**
- * Setting to show/hide Attributes like Damage
- */
- HIDE_ATTRIBUTES,
- /**
- * Setting to show/hide the unbreakable State
- */
- HIDE_UNBREAKABLE,
- /**
- * Setting to show/hide what the ItemStack can break/destroy
- */
- HIDE_DESTROYS,
- /**
- * Setting to show/hide where this ItemStack can be build/placed on
- */
- HIDE_PLACED_ON,
- /**
- * Setting to show/hide potion effects on this ItemStack
- */
- HIDE_POTION_EFFECTS;
-
- public byte getBit()
- {
- return (byte) (1 << this.ordinal());
- }
- }
- public static void addItemFlag(ItemStack stack, ItemFlag... flags)
- {
- NBTTagCompound com = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound();
- if(com == null) // this will never happen, but the compiler wants it
- {
- return;
- }
- int i = com.getInteger("HideFlags");
- for (ItemFlag f : flags)
- {
- i |= f.getBit(); // bitwise 'or'
- }
- com.setInteger("HideFlags", i);
- stack.setTagCompound(com);
- }
-
- public static void removeItemFlags(ItemStack stack, ItemFlag... flags)
- {
- NBTTagCompound com = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound();
- if(com == null) // this will never happen, but the compiler wants it
- {
- return;
- }
- int i = com.getInteger("HideFlags");
- for (ItemFlag f : flags)
- {
- i &= ~f.getBit(); // bitwise 'and' with inversed bits
- }
- com.setInteger("HideFlags", i);
- stack.setTagCompound(com);
- }
-
- public static boolean hasItemFlag(ItemStack stack, ItemFlag flag)
- {
- if(!stack.hasTagCompound())
- {
- return false;
- }
- NBTTagCompound com = stack.getTagCompound();
- if(com == null) // this will never happen, but the compiler wants it
- {
- return false;
- }
- byte b = flag.getBit();
- return (com.getInteger("HideFlags") & b) == b;
- }
-
- // -----------------------------------------------------------------------------------
- // Converter
- // -----------------------------------------------------------------------------------
-
- public static String getNbtString(ItemStack stack)
- {
- return stack.writeToNBT(new NBTTagCompound()).toString();
- }
-
- public static ItemStack getStackFromNbtString(String s) throws IllegalItemStackStringException
- {
- try
- {
- NBTTagCompound c = JsonToNBT.getTagFromJson(s);
- return new ItemStack(c);
- }
- catch(NBTException ex)
- {
- throw new IllegalItemStackStringException(ex.getMessage());
- }
- }
-
- // -----------------------------------------------------------------------------------
- // Misc
- // -----------------------------------------------------------------------------------
- public static void setLore(ItemStack stack, List<String> list)
- {
- NBTTagCompound com = stack.getOrCreateSubCompound("display");
- NBTTagList nbtList = new NBTTagList();
- list.forEach(s -> nbtList.appendTag(new NBTTagString(s)));
- com.setTag("Lore", nbtList);
- }
-
- public static void addLore(ItemStack stack, String s)
- {
- NBTTagCompound com = stack.getOrCreateSubCompound("display");
- NBTTagList nbtList = com.getTagList("Lore", 8);
- nbtList.appendTag(new NBTTagString(s));
- com.setTag("Lore", nbtList);
- }
-
- public static List<String> getLore(ItemStack stack)
- {
- NBTTagCompound com = stack.getOrCreateSubCompound("display");
- NBTTagList nbtList = com.getTagList("Lore", 8);
- ArrayList<String> list = new ArrayList<>();
- for(int i = 0; i < nbtList.tagCount(); i++)
- {
- list.add(nbtList.getStringTagAt(i));
- }
- return list;
- }
-
- public static void addLore(ItemStack stack, String s, int i)
- {
- NBTTagCompound com = stack.getOrCreateSubCompound("display");
- NBTTagList list = com.getTagList("Lore", 8);
- if(i >= list.tagCount())
- {
- list.appendTag(new NBTTagString(s));
- }
- else
- {
- list.set(i, new NBTTagString(s));
- }
- com.setTag("Lore", list);
- }
-
- public static void drop(World w, BlockPos pos, ItemStack stack)
- {
- Block.spawnAsEntity(w, pos, stack);
- }
- }
|