123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- package me.km.snuviscript;
- import me.km.exception.IllegalItemStackStringException;
- import me.hammerle.code.ScriptUtils;
- import me.km.utils.ItemStackUtils;
- import net.minecraft.block.Block;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- public class ScriptHelper
- {
- public static Item getItem(String stack) throws IllegalItemStackStringException
- {
- Item item = Item.getByNameOrId(stack);
- if(item == null)
- {
- Block b = Block.getBlockFromName(stack);
- if(b == null)
- {
- throw new IllegalItemStackStringException(stack);
- }
- return Item.getItemFromBlock(b);
- }
- return item;
- }
-
- /** Gibt einen ItemStack zurück
- * @param stack ein ItemStack-String im Format "Material", "Material:DV" oder "Material:DV:Amount"
- * @throws IllegalItemStackStringException wenn das Format nicht erfüllt ist
- * @return den ItemStack
- */
- private static ItemStack getSimpleItemStack(String stack) throws IllegalItemStackStringException
- {
- String[] parts = stack.split(":");
- if(parts[0].equals("km") || parts[0].equals("minecraft"))
- {
- if(parts.length <= 1)
- {
- throw new IllegalItemStackStringException(stack);
- }
- String[] newParts = new String[parts.length - 1];
- newParts[0] = parts[0].toLowerCase() + ":" + parts[1].toLowerCase();
- for(int i = 1; i < newParts.length; i++)
- {
- newParts[i] = parts[i + 1].toLowerCase();
- }
- parts = newParts;
- }
- else
- {
- parts[0] = "minecraft:" + parts[0].toLowerCase();
- }
- int amount = 1;
- int data = 0;
- if(parts.length >= 2)
- {
- try
- {
- data = Integer.parseInt(parts[1]);
- if(data < 0)
- {
- throw new IllegalItemStackStringException(stack);
- }
- }
- catch(NumberFormatException ex)
- {
- throw new IllegalItemStackStringException(stack);
- }
- if(parts.length >= 3)
- {
- try
- {
- amount = Integer.parseInt(parts[2]);
- if(amount < 1)
- {
- throw new IllegalItemStackStringException(stack);
- }
- }
- catch(NumberFormatException ex)
- {
- throw new IllegalItemStackStringException(stack);
- }
- }
- }
- return new ItemStack(getItem(parts[0]), amount, data);
- }
-
- /** Gibt einen ItemStack zurück
- * @param args ein Array der mindestens einen gültigen ItemStack-String enthalten muss.
- * Weiters kann auch ein Name und eine Lore mitgegeben werden, andernfalls muss der
- * Array vorher an seiner Grenze sein oder null verwendet werden.
- * @param start die Stelle, an der der ItemStack beginnt
- * @throws IllegalItemStackStringException wenn das Format des ItemStack-Strings nicht erfüllt ist
- * @return den ItemStack
- */
- public static ItemStack getItemStack(Object[] args, int start) throws IllegalItemStackStringException
- {
- if(args[start].toString().startsWith("{"))
- {
- return ItemStackUtils.getStackFromNbtString(ScriptUtils.connect(args, " ", start).replace('\'', '"'));
- }
- ItemStack stack = getSimpleItemStack(args[start].toString());
- if(args.length >= start + 2)
- {
- if(!(args[start + 1] == null))
- {
- stack.setStackDisplayName(args[start + 1].toString());
- }
- }
- if(args.length >= start + 3)
- {
- if(!(args[start + 2] == null))
- {
- for(int i = start + 2; i < args.length; i++)
- {
- ItemStackUtils.addLore(stack, args[i].toString());
- }
- }
- }
- return stack;
- }
-
- /** Gibt einen ItemStack-String zurück
- * @param stack ein ItemStack
- * @return den ItemStack-String im Format "Material:DV:Amount"
- */
- public static String getItemStackString(ItemStack stack)
- {
- return ItemStackUtils.getNbtString(stack);
- }
- }
|