123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- package me.km.snuviscript;
- import me.km.exception.IllegalStringLocationException;
- import me.km.exception.IllegalItemStackStringException;
- import me.km.api.Location;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.stream.Collectors;
- import me.km.dimensions.ModDimensions;
- import me.km.utils.ItemStackUtils;
- import net.minecraft.block.Block;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.Vec3d;
- public class ScriptUtils
- {
- public static Number getNumber(String s)
- {
- Double d = Double.parseDouble(s);
- if(d.intValue() == d)
- {
- return d.intValue();
- }
- else if(d.longValue() == d)
- {
- return d.longValue();
- }
- return d;
- }
-
- public static int getInt(Object o)
- {
- return ((Number) o).intValue();
- }
-
- public static byte getByte(Object o)
- {
- return ((Number) o).byteValue();
- }
-
- public static double getDouble(Object o)
- {
- return ((Number) o).doubleValue();
- }
-
- public static float getFloat(Object o)
- {
- return ((Number) o).floatValue();
- }
-
- public static String connect(Collection<Object> c, String s, int skip)
- {
- return c.stream().skip(skip).map(o -> o.toString()).collect(Collectors.joining(s));
- }
-
- public static String connect(Object[] c, String s, int skip)
- {
- return Arrays.stream(c, skip, c.length).map(o -> o.toString()).collect(Collectors.joining(s));
- }
-
- public static String connect(Collection<Object> c, int skip)
- {
- return c.stream().skip(skip).map(o -> String.valueOf(o)).collect(Collectors.joining());
- }
-
- public static String connect(Object[] c, int skip)
- {
- return Arrays.stream(c, skip, c.length).map(o -> String.valueOf(o)).collect(Collectors.joining());
- }
-
- /** Generiert einen String aus dem gegebenen Double, ein etwaiges <code>.0</code> wird entfernt.
- * @param d ein Double
- * @return der generierte String
- */
- public static String doubleToString(double d)
- {
- if((int) d == d)
- {
- return String.valueOf((int) d);
- }
- return String.valueOf(d);
- }
-
- /** Generiert eine Location aus dem gegebenen String.
- * @param location String-Location
- * @throws IllegalStringLocationException wenn der String nicht das Format "world-name:x:y:z" oder "..:yaw:pitch" besitzt.
- * @return die generierte Location
- */
- public static Location getLocation(String location) throws IllegalStringLocationException
- {
- String[] parts = location.split(":");
- try
- {
- if(parts.length > 4)
- {
- return new Location(ModDimensions.getWorldFromName(parts[0]), new Vec3d(Double.parseDouble(parts[1]),
- Double.parseDouble(parts[2]), Double.parseDouble(parts[3])),
- Float.parseFloat(parts[4]), Float.parseFloat(parts[5]));
- }
- return new Location(ModDimensions.getWorldFromName(parts[0]), new Vec3d(Double.parseDouble(parts[1]), Double.parseDouble(parts[2]), Double.parseDouble(parts[3])));
- }
- catch(Exception ex)
- {
- throw new IllegalStringLocationException(location);
- }
- }
-
- /** Rundet eine Location zu einer Block-Location
- * @param l Location
- * @return die generierte Location
- */
- public static Location roundLocation(Location l)
- {
- return new Location(l.getWorld(), new BlockPos(l.getPos()));
- }
-
- /** Generiert einen Location-String aus der gegebenen Location.
- * @param l eine Location
- * @return Location-String im Format "world-name:x:y:z"
- */
- public static String getLocationString(Location l)
- {
- Vec3d v = l.getPos();
- return ModDimensions.getWorldName(l.getWorld()) + ":" + v.xCoord + ":" + v.yCoord + ":" + v.zCoord + ":" + l.getYaw()+ ":" + l.getPitch();
- }
-
- /** Generiert einen Block-Location-String aus der gegebenen Location.
- * @param l eine Location
- * @return Location-String im Format "world-name:x:y:z"
- */
- public static String getBlockLocationString(Location l)
- {
- Vec3d v = l.getPos();
- return ModDimensions.getWorldName(l.getWorld()) + ":" + ((int) v.xCoord) + ":" + ((int) v.yCoord) + ":" + ((int) v.zCoord);
- }
-
- 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].startsWith("km:") && !parts[0].startsWith("minecraft:"))
- {
- parts[0] = "minecraft:" + parts[0];
- }
- parts[0] = 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(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);
- }
-
- /** Konvertiert einen String in die gewünschten Formate (true, false, null, Zahl, ...)
- * @param s ein String
- * @return ein konvertiertes Object
- */
- public static Object convertInput(String s)
- {
- if(s == null)
- {
- return null;
- }
- else if(s.startsWith("\"") && s.endsWith("\""))
- {
- if(s.length() <= 1)
- {
- return "\"";
- }
- return s.substring(1, s.length() - 1);
- }
- else if(s.startsWith("$"))
- {
- return new Variable(s);
- }
- else if(s.equals("true"))
- {
- return true;
- }
- else if(s.equals("false"))
- {
- return false;
- }
- else if(s.equals("null"))
- {
- return null;
- }
- try
- {
- return getNumber(s);
- }
- catch(NumberFormatException ex)
- {
- return s;
- }
- }
- }
|