Ver Fonte

preparations for getting rid of try / catch

Kajetan Johannes Hammerle há 3 anos atrás
pai
commit
32a673da99

+ 0 - 223
src/main/java/me/km/Client.java

@@ -2,7 +2,6 @@ package me.km;
 
 import com.mojang.blaze3d.matrix.MatrixStack;
 import java.awt.Color;
-import java.util.List;
 import me.km.blocks.ModBlocks;
 import me.km.blocks.cookingpot.TileEntityCookingPot;
 import me.km.items.ItemColoredSoup;
@@ -36,18 +35,12 @@ import net.minecraft.client.renderer.entity.EntityRenderer;
 import net.minecraft.client.renderer.entity.EntityRendererManager;
 import net.minecraft.client.world.ClientWorld;
 import net.minecraft.entity.Entity;
-import net.minecraft.item.Item;
-import net.minecraft.item.ArmorItem;
 import net.minecraft.item.BlockItem;
 import net.minecraft.item.ItemStack;
-import net.minecraft.nbt.CompoundNBT;
 import net.minecraft.particles.ParticleTypes;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.util.math.MathHelper;
 import net.minecraft.util.text.ITextComponent;
-import net.minecraft.util.text.StringTextComponent;
-import net.minecraft.util.text.TextFormatting;
-import net.minecraft.util.text.LanguageMap;
 import net.minecraft.world.GameType;
 import net.minecraft.world.GrassColors;
 import net.minecraft.world.biome.BiomeColors;
@@ -57,7 +50,6 @@ import net.minecraftforge.client.event.GuiScreenEvent;
 import net.minecraftforge.client.event.RenderGameOverlayEvent;
 import net.minecraftforge.client.event.RenderNameplateEvent;
 import net.minecraftforge.common.MinecraftForge;
-import net.minecraftforge.event.entity.player.ItemTooltipEvent;
 import net.minecraftforge.eventbus.api.SubscribeEvent;
 import net.minecraftforge.event.TickEvent;
 import net.minecraftforge.event.entity.player.PlayerInteractEvent;
@@ -261,219 +253,4 @@ public class Client {
             ItemStackDisplayGui.INSTANCE.setActive(false);
         }
     }
-
-    @SubscribeEvent
-    public void showingTooltip(ItemTooltipEvent e) {
-        ItemStack stack = e.getItemStack();
-        Item item = stack.getItem();
-        if(item instanceof ArmorItem) {
-            CompoundNBT com = stack.getTag();
-            if(com != null && com.contains("magic")) {
-                int magic = com.getInt("magic");
-                if(magic != 0) {
-                    List<ITextComponent> list = e.getToolTip();
-                    for(int i = 0; i < list.size(); i++) {
-                        if(list.get(i).getString().contains("+")) {
-                            list.add(i, new StringTextComponent(
-                                    String.format("%s+%d %s", TextFormatting.BLUE, magic,
-                                            LanguageMap.getInstance().translateKey("magicarmor"))));
-                            break;
-                        }
-                    }
-                }
-            }
-        }
-    }
-
-    /*@SubscribeEvent
-    public void test(net.minecraftforge.client.event.ClientChatEvent e)
-    {
-        net.minecraft.world.chunk.Chunk c = Minecraft.getInstance().player.world.getChunkAt(Minecraft.getInstance().player.getPosition());
-        
-        if(!e.getMessage().equals("run"))
-        {
-            return;
-        }
-
-        int cluster = 0;
-        int blocks = 0;
-        
-        net.minecraft.util.math.BlockPos.MutableBlockPos pos = new net.minecraft.util.math.BlockPos.MutableBlockPos();
-
-        for(int x = 0; x < 16; x += 4)
-        {
-            for(int z = 0; z < 16; z += 4)
-            {
-                for(int y = 0; y < 256; y += 16)
-                {
-                    cluster++;
-                    pos.setPos(x, y, z);
-                    blocks++;
-                    net.minecraft.block.Block current = c.getBlockState(pos).getBlock();
-                    for(int mx = 0; mx < 4; mx++)
-                    {
-                        for(int mz = 0; mz < 4; mz++)
-                        {
-                            for(int my = 0; my < 16; my++)
-                            {
-                                pos.setPos(x + mx, y + my, z + mz);
-                                net.minecraft.block.Block next = c.getBlockState(pos).getBlock();
-                                if(current != next)
-                                {
-                                    current = next;
-                                    blocks++;
-                                }
-                            }
-                        }
-                    }
-                }
-            }
-        }
-        
-        org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger();
-        logger.info("------------------------------------");
-        logger.info(String.format("cluster: %d", cluster));
-        logger.info(String.format("blocks: %d", blocks));
-        logger.info(String.format("compression rate: %f",  (16 * 16 * 256 * 16) / (cluster * 64.0 + blocks * 24.0)));
-        
-        cluster = 0;
-        blocks = 0;
-
-        for(int x = 0; x < 16; x++)
-        {
-            for(int z = 0; z < 16; z++)
-            {
-                cluster++;
-                pos.setPos(x, 0, z);
-                blocks++;
-                net.minecraft.block.Block current = c.getBlockState(pos).getBlock();
-                for(int y = 0; y < 256; y++)
-                {
-                    pos.setPos(x, y, z);
-                    net.minecraft.block.Block next = c.getBlockState(pos).getBlock();
-                    if(current != next)
-                    {
-                        current = next;
-                        blocks++;
-                    }
-                }
-            }
-        }
-        
-        logger.info("------");
-        logger.info(String.format("cluster %d blocks", cluster, blocks));
-        logger.info(String.format("cluster: %d", cluster));
-        logger.info(String.format("blocks: %d", blocks));
-        logger.info(String.format("compression rate: %f",  (16 * 16 * 256 * 16) / (cluster * 64.0 + blocks * 24.0)));
-        
-        cluster = 1;
-        blocks = 1;
-        pos.setPos(0, 0, 0);
-        net.minecraft.block.Block current = c.getBlockState(pos).getBlock();
-        for(int y = 0; y < 256; y++)
-        {
-            for(int x = 0; x < 16; x++)
-            {
-                for(int z = 0; z < 16; z++)
-                {
-                    pos.setPos(x, y, z);
-                    net.minecraft.block.Block next = c.getBlockState(pos).getBlock();
-                    if(current != next)
-                    {
-                        current = next;
-                        blocks++;
-                    }
-                }
-            }
-        }
-        
-        logger.info("------");
-        logger.info(String.format("cluster %d blocks", cluster, blocks));
-        logger.info(String.format("cluster: %d", cluster));
-        logger.info(String.format("blocks: %d", blocks));
-        logger.info(String.format("compression rate: %f",  (16 * 16 * 256 * 16) / (cluster * 64.0 + blocks * 24.0)));
-    }*/
- /*@SubscribeEvent
-    public void test(net.minecraftforge.client.event.ClientChatEvent e) {
-        int size;
-        try {
-            size = Short.parseShort(e.getMessage());
-        } catch(NumberFormatException ex) {
-            return;
-        }
-        World w = Minecraft.getInstance().player.world;
-        int idCounter = 0;
-        HashMap<BlockState, Integer> mapping = new HashMap<>();
-        int sizeY = Math.min(256, size);
-
-        int index = 0;
-        byte[] b = new byte[sizeY * size * size];
-
-        BlockPos.Mutable pos = new BlockPos.Mutable();
-        for(int x = 0; x < size; x++) {
-            for(int y = 0; y < sizeY; y++) {
-                for(int z = 0; z < size; z++) {
-                    pos.setPos(x, y, z);
-                    BlockState blockState = w.getBlockState(pos);
-                    Integer id = mapping.get(blockState);
-                    if(id == null) {
-                        id = idCounter++;
-                        mapping.put(blockState, id);
-                    }
-                    b[index++] = id.byteValue();
-                }
-            }
-        }
-
-        Thread th = new Thread(() -> {
-            try(FileOutputStream out = new FileOutputStream(new File(String.format("%d_%d_data", size, size)))) {
-                out.write(b);
-            } catch(IOException ex) {
-                ex.printStackTrace();
-            }
-        });
-        th.start();
-
-        org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger();
-        logger.info("------------------------------------");
-        logger.info(String.format("block types: %d", idCounter));
-    }*/
- /*@SubscribeEvent
-    public void test(net.minecraftforge.client.event.ClientChatEvent e) {
-        int size;
-        try {
-            size = Integer.parseInt(e.getMessage());
-        } catch(NumberFormatException ex) {
-            return;
-        }
-        World w = Minecraft.getInstance().player.world;
-
-        int[] counter = new int[256];
-
-        int sizeY = Math.min(256, size);
-        BlockPos.Mutable pos = new BlockPos.Mutable();
-        for(int cx = 0; cx < size; cx += 16) {
-            for(int cy = 0; cy < 16; cy += 16) {
-                for(int cz = 0; cz < size; cz += 16) {
-                    HashSet<BlockState> states = new HashSet<>();
-                    for(int x = 0; x < 16; x++) {
-                        for(int y = 0; y < 16; y++) {
-                            for(int z = 0; z < 16; z++) {
-                                pos.setPos(cx + x, cy + y, cz + z);
-                                states.add(w.getBlockState(pos));
-                            }
-                        }
-                    }
-                    counter[states.size()]++;
-                }
-            }
-        }
-
-        org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger();
-        logger.info("------------------------------------");
-        for(int i = 0; i < counter.length; i++) {
-            logger.info(String.format("%d: %d", i, counter[i]));
-        }
-
-    }*/
 }

+ 1 - 57
src/main/java/me/km/CommonEvents.java

@@ -7,6 +7,7 @@ import net.minecraftforge.event.furnace.FurnaceFuelBurnTimeEvent;
 import net.minecraftforge.eventbus.api.SubscribeEvent;
 
 public class CommonEvents {
+
     @SubscribeEvent
     public void getFuelBurnTime(FurnaceFuelBurnTimeEvent e) {
         Item item = e.getItemStack().getItem();
@@ -29,61 +30,4 @@ public class CommonEvents {
             e.setBurnTime(2700);
         }
     }
-
-    /*private static final java.util.HashSet<Class> test = new java.util.HashSet<>();
-    
-    static
-    {
-        test.add(net.minecraftforge.event.world.ChunkDataEvent.class);
-        test.add(net.minecraftforge.event.TickEvent.class);
-        test.add(net.minecraftforge.event.entity.living.LivingSpawnEvent.class);
-        test.add(net.minecraftforge.event.entity.living.LivingEvent.class);
-        test.add(net.minecraftforge.event.TickEvent.class);
-        test.add(net.minecraftforge.event.AttachCapabilitiesEvent.class);
-        test.add(net.minecraftforge.event.entity.EntityMobGriefingEvent.class);
-        test.add(net.minecraftforge.event.entity.living.LivingDestroyBlockEvent.class);
-        test.add(net.minecraftforge.event.entity.PlaySoundAtEntityEvent.class);
-        test.add(net.minecraftforge.event.entity.EntityEvent.class);
-        test.add(net.minecraftforge.event.LootTableLoadEvent.class);
-        test.add(net.minecraftforge.event.world.ChunkEvent.class);
-        test.add(net.minecraftforge.event.world.WorldEvent.class);
-        test.add(net.minecraftforge.event.entity.living.LivingFallEvent.class);
-        test.add(net.minecraftforge.event.entity.player.PlayerEvent.class);
-        test.add(net.minecraftforge.event.world.ChunkWatchEvent.class);
-        test.add(net.minecraftforge.event.entity.living.LivingEntityUseItemEvent.class);
-        test.add(net.minecraftforge.event.entity.living.LivingEquipmentChangeEvent.class);
-        test.add(net.minecraftforge.event.entity.EntityJoinWorldEvent.class);
-        test.add(net.minecraftforge.event.entity.living.LivingPackSizeEvent.class);
-    }
-    
-    @SubscribeEvent
-    public void onChat(net.minecraftforge.event.ServerChatEvent e)
-    {
-        try
-        {
-            Class c = Class.forName(e.getMessage());
-            if(!test.add(c))
-            {
-                test.remove(c);
-            }
-        }
-        catch(Exception ex)
-        {
-        }
-    }
-    
-    @SubscribeEvent
-    public void test(net.minecraftforge.eventbus.api.Event e)
-    {
-        Class c = e.getClass();
-        if(c.getEnclosingClass() != null)
-        {
-            c = c.getEnclosingClass();
-        }
-        if(test.contains(c))
-        {
-            return;
-        }
-        org.apache.logging.log4j.LogManager.getLogger().warn(c);
-    }*/
 }

+ 0 - 62
src/main/java/me/km/PackageHelper.java

@@ -1,62 +0,0 @@
-package me.km;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
-
-public class PackageHelper {
-    public static ArrayList<Class> getClasses(String packageName) {
-        URL jar = getPath();
-        if(jar != null) {
-            try {
-                ZipInputStream zip = new ZipInputStream(jar.openStream());
-                ArrayList<Class> classes = new ArrayList<>();
-                String path = packageName.replace(".", "/");
-                while(true) {
-                    ZipEntry e = zip.getNextEntry();
-                    if(e == null) {
-                        break;
-                    }
-                    String name = e.getName();
-                    if(name.startsWith(path)) {
-                        name = name.replace("/", ".").substring(0, e.getName().length() - 6);
-                        try {
-                            classes.add(Class.forName(name));
-                        } catch(ClassNotFoundException ex) {
-                        }
-                    }
-                }
-                return classes;
-            } catch(IOException ex) {
-            }
-        }
-        return new ArrayList<>();
-    }
-
-    private static URL getPath() {
-        try {
-            String s;
-            try {
-                s = KajetansMod.class.getProtectionDomain().getCodeSource().getLocation().getPath();
-                s = s.substring(5, s.length() - 25);
-            } catch(Exception ex) {
-                s = "../build/libs/km-1.0.jar";
-            }
-
-            File f = new File(s);
-            if(!f.exists()) {
-                for(int i = 0; i < 20; i++) {
-                    System.out.println(f + " does not exist");
-                }
-            }
-
-            return new URL("file:" + s);
-        } catch(Exception ex) {
-            ex.printStackTrace();
-        }
-        return null;
-    }
-}

+ 1 - 0
src/main/java/me/km/snuviscript/MinecraftFunctions.java

@@ -21,6 +21,7 @@ public class MinecraftFunctions {
         TitleCommands.registerFunctions(sm);
         PlayerCommands.registerFunctions(sm, scripts, perms, scheduler, server, playerBank);
         WorldCommands.registerFunctions(sm, server);
+        GameRuleCommands.registerFunctions(sm, server);
         ItemCommands.registerFunctions(sm);
         LocationCommands.registerFunctions(sm);
         BlockCommands.registerFunctions(sm);

+ 15 - 10
src/main/java/me/km/snuviscript/commands/BlockCommands.java

@@ -27,7 +27,9 @@ import net.minecraft.world.IWorld;
 import net.minecraft.world.World;
 
 public class BlockCommands {
+
     private static class Offset {
+
         private final int x;
         private final int y;
         private final int z;
@@ -160,22 +162,25 @@ public class BlockCommands {
             }
             l.getWorld().setBlockState(l.getBlockPos(), (BlockState) in[1].get(sc), flag);
         });
-        sm.registerConsumer("block.setsign", (sc, in) -> {
+        sm.registerFunction("block.setsign", (sc, in) -> {
             Location l = (Location) in[0].get(sc);
-            SignTileEntity sign = (SignTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
+            TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
+            if(te == null || !(te instanceof SignTileEntity)) {
+                return false;
+            }
+            SignTileEntity sign = (SignTileEntity) te;
             sign.signText[in[1].getInt(sc)] = new StringTextComponent(SnuviUtils.connect(sc, in, 2));
-
             SUpdateTileEntityPacket packet = sign.getUpdatePacket();
-            World w = sign.getWorld();
-            if(w != null) {
-                w.getPlayers().stream().filter(p -> p instanceof ServerPlayerEntity)
-                        .forEach(p -> ((ServerPlayerEntity) p).connection.sendPacket(packet));
-            }
+            l.getWorld().getPlayers().forEach(p -> ((ServerPlayerEntity) p).connection.sendPacket(packet));
+            return true;
         });
         sm.registerFunction("block.getsign", (sc, in) -> {
             Location l = (Location) in[0].get(sc);
-            SignTileEntity sign = (SignTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
-            return sign.signText[in[1].getInt(sc)].getString();
+            TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
+            if(te == null || !(te instanceof SignTileEntity)) {
+                return null;
+            }
+            return ((SignTileEntity) te).signText[in[1].getInt(sc)].getString();
         });
         sm.registerConsumer("block.setdoorstatus", (sc, in) -> {
             Location l = (Location) in[0].get(sc);

+ 0 - 30
src/main/java/me/km/snuviscript/commands/EntityCommands.java

@@ -42,16 +42,6 @@ public class EntityCommands {
         sm.registerFunction("entity.isblocking", (sc, in) -> ((LivingEntity) in[0].get(sc)).isActiveItemStackBlocking());
         sm.registerFunction("entity.getarmorthoughness", (sc, in) -> ((LivingEntity) in[0].get(sc)).getAttribute(SharedMonsterAttributes.ARMOR_TOUGHNESS).getValue());
         sm.registerFunction("entity.getarmor", (sc, in) -> (double) ((LivingEntity) in[0].get(sc)).getTotalArmorValue());
-        sm.registerFunction("entity.getmagicarmor", (sc, in) -> {
-            int level = 0;
-            for(ItemStack stack : ((LivingEntity) in[0].get(sc)).getArmorInventoryList()) {
-                CompoundNBT com = stack.getTag();
-                if(com != null && com.contains("magic")) {
-                    level += com.getInt("magic");
-                }
-            }
-            return level;
-        });
         sm.registerFunction("entity.getenchantmentmodifier", (sc, in) -> EnchantmentHelper.getEnchantmentModifierDamage(((LivingEntity) in[0].get(sc)).getArmorInventoryList(), (DamageSource) in[1].get(sc)));
         sm.registerConsumer("entity.setburning", (sc, in) -> ((Entity) in[0].get(sc)).setFire(in[1].getInt(sc)));
         sm.registerFunction("entity.isburning", (sc, in) -> ((Entity) in[0].get(sc)).isBurning());
@@ -75,26 +65,6 @@ public class EntityCommands {
             DamageSource damageSource = (in.length >= 3) ? (DamageSource) in[2].get(sc) : DamageSource.GENERIC;
             scheduler.scheduleTask(() -> liv.attackEntityFrom(damageSource, damage));
         });
-        sm.registerConsumer("entity.damagedirect", (sc, in) -> {
-            LivingEntity liv = (LivingEntity) in[0].get(sc);
-            float damageAmount = in[1].getFloat(sc);
-            DamageSource ds = (DamageSource) in[2].get(sc);
-            if(!liv.isInvulnerableTo(ds)) {
-                float f2 = Math.max(damageAmount - liv.getAbsorptionAmount(), 0.0F);
-                liv.setAbsorptionAmount(liv.getAbsorptionAmount() - (damageAmount - f2));
-                float absorbedDamage = damageAmount - f2;
-                if(absorbedDamage > 0.0f && absorbedDamage < 3.4028235E37f && ds.getTrueSource() instanceof ServerPlayerEntity) {
-                    ((ServerPlayerEntity) ds.getTrueSource()).addStat(Stats.DAMAGE_DEALT_ABSORBED, Math.round(absorbedDamage * 10.0f));
-                }
-
-                if(f2 > 0.0f) {
-                    float f1 = liv.getHealth();
-                    liv.getCombatTracker().trackDamage(ds, f1, f2);
-                    liv.setHealth(f1 - f2);
-                    liv.setAbsorptionAmount(liv.getAbsorptionAmount() - f2);
-                }
-            }
-        });
         sm.registerFunction("entity.fromsource", (sc, in) -> {
             DamageSource ds = (DamageSource) in[0].get(sc);
             Entity ent = ds.getTrueSource();

+ 50 - 0
src/main/java/me/km/snuviscript/commands/GameRuleCommands.java

@@ -0,0 +1,50 @@
+package me.km.snuviscript.commands;
+
+import me.hammerle.snuviscript.code.ScriptManager;
+import me.km.utils.ReflectionUtils;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.world.GameRules;
+import net.minecraft.world.GameRules.RuleKey;
+import net.minecraft.world.World;
+
+public class GameRuleCommands {
+    private static GameRules.RuleKey ruleKey = null;
+    
+    @SuppressWarnings("")
+    public static void registerFunctions(ScriptManager sm, MinecraftServer server) {
+        sm.registerFunction("gamerule.getkey", (sc, in) -> {
+            String name = in[0].getString(sc);
+            ruleKey = null;
+            GameRules.visitAll(new GameRules.IRuleEntryVisitor() {
+                @Override
+                public <T extends GameRules.RuleValue<T>> void visit(GameRules.RuleKey<T> key, GameRules.RuleType<T> type) {
+                    if(key.getName().equals(name)) {
+                        ruleKey = key;
+                    }
+                }
+            });
+            return ruleKey;
+        });
+        sm.registerFunction("gamerule.getvalue", (sc, in) -> {
+            return ((World) in[0].get(sc)).getGameRules().get((RuleKey) in[1].get(sc));
+        });
+        sm.registerFunction("gamerule.isbool", (sc, in) -> {
+            return in[0].get(sc) instanceof GameRules.BooleanValue;
+        });
+        sm.registerFunction("gamerule.isint", (sc, in) -> {
+            return in[0].get(sc) instanceof GameRules.IntegerValue;
+        });
+        sm.registerFunction("gamerule.getbool", (sc, in) -> {
+            return ((GameRules.BooleanValue) in[0].get(sc)).get();
+        });
+        sm.registerFunction("gamerule.getint", (sc, in) -> {
+            return (double) ((GameRules.IntegerValue) in[0].get(sc)).get();
+        });
+        sm.registerConsumer("gamerule.setbool", (sc, in) -> {
+            ((GameRules.BooleanValue) in[0].get(sc)).set(in[1].getBoolean(sc), server);
+        });
+        sm.registerConsumer("gamerule.setint", (sc, in) -> {
+            ReflectionUtils.setIntegerValue((GameRules.IntegerValue) in[0].get(sc), in[1].getInt(sc));
+        });
+    }
+}

+ 35 - 22
src/main/java/me/km/snuviscript/commands/ItemCommands.java

@@ -10,7 +10,6 @@ import net.minecraft.block.Block;
 import net.minecraft.entity.ai.attributes.AttributeModifier;
 import net.minecraft.entity.player.PlayerEntity;
 import net.minecraft.inventory.EquipmentSlotType;
-import net.minecraft.item.ArmorItem;
 import net.minecraft.item.Item;
 import net.minecraft.item.ItemStack;
 import net.minecraft.nbt.CompoundNBT;
@@ -24,6 +23,7 @@ import net.minecraft.util.text.StringTextComponent;
 import net.minecraft.world.World;
 
 public class ItemCommands {
+
     public static void registerFunctions(ScriptManager sm) {
         sm.registerConsumer("item.drop", (sc, in) -> {
             Location l = (Location) in[0].get(sc);
@@ -70,29 +70,42 @@ public class ItemCommands {
             list.add(new EffectInstance(Mapper.getPotion(in[1].getString(sc)), in[2].getInt(sc), in[3].getInt(sc)));
             PotionUtils.appendEffects(stack, list);
         });
-        sm.registerConsumer("item.addflag", (sc, in) -> {
-            ItemStackUtils.addItemFlag((ItemStack) in[0].get(sc), ItemStackUtils.ItemFlag.valueOf(in[1].getString(sc).toUpperCase()));
+        sm.registerFunction("item.hide", (sc, in) -> {
+            int flag = 0;
+            switch(in[1].getString(sc)) {
+                case "enchantments":
+                    flag = 1;
+                    break;
+                case "attributes":
+                    flag = 2;
+                    break;
+                case "unbreakable":
+                    flag = 4;
+                    break;
+                case "destroys":
+                    flag = 8;
+                    break;
+                case "placed_on":
+                    flag = 16;
+                    break;
+                case "potion_effects":
+                    flag = 32;
+                    break;
+            }
+            if(flag == 0) {
+                return false;
+            }
+            ItemStack stack = (ItemStack) in[0].get(sc);
+            CompoundNBT com = stack.getTag() != null ? stack.getTag() : new CompoundNBT();
+            com.putInt("HideFlags", com.getInt("HideFlags") | flag);
+            stack.setTag(com);
+            return true;
         });
         sm.registerConsumer("item.addattribute", (sc, in) -> {
-            ItemStackUtils.addAttribute((ItemStack) in[0].get(sc),
-                    ItemStackUtils.Attribute.valueOf(in[1].getString(sc).toUpperCase()),
-                    EquipmentSlotType.valueOf(in[2].getString(sc).toUpperCase()),
-                    in[3].getDouble(sc),
-                    AttributeModifier.Operation.valueOf(in[4].getString(sc).toUpperCase()));
-        });
-        sm.registerConsumer("item.setmagic", (sc, in) -> {
-            ItemStack stack = (ItemStack) in[0].get(sc);
-            int value = in[1].getInt(sc);
-            if(stack.getItem() instanceof ArmorItem) {
-                CompoundNBT com = stack.getTag();
-                if(com == null) {
-                    com = new CompoundNBT();
-                    stack.setTag(com);
-                }
-                com.putInt("magic", value);
-            } else {
-                throw new IllegalArgumentException("magic resistance is only for armor");
-            }
+            ((ItemStack) in[0].get(sc)).addAttributeModifier(in[1].getString(sc),
+                    new AttributeModifier("modifier", in[3].getDouble(sc),
+                            AttributeModifier.Operation.values()[in[4].getInt(sc)]),
+                    (EquipmentSlotType) in[2].get(sc));
         });
         sm.registerFunction("item.clone", (sc, in) -> ((ItemStack) in[0].get(sc)).copy());
         sm.registerFunction("item.getmaxdamage", (sc, in) -> (double) ((ItemStack) in[0].get(sc)).getMaxDamage());

+ 13 - 6
src/main/java/me/km/snuviscript/commands/LivingCommands.java

@@ -11,8 +11,10 @@ import net.minecraft.entity.MobEntity;
 import net.minecraft.entity.SharedMonsterAttributes;
 import net.minecraft.entity.ai.attributes.AttributeModifier;
 import net.minecraft.entity.ai.attributes.IAttribute;
+import net.minecraft.entity.ai.attributes.IAttributeInstance;
 
 public class LivingCommands {
+
     public static void registerFunctions(ScriptManager sm) {
         IAttribute[] attributes = new IAttribute[]{
             SharedMonsterAttributes.ARMOR, SharedMonsterAttributes.ARMOR_TOUGHNESS,
@@ -25,17 +27,22 @@ public class LivingCommands {
         for(IAttribute attribute : attributes) {
             String name = attribute.getName().substring(8).toLowerCase();
             sm.registerConsumer("living.set" + name, (sc, in) -> {
-                LivingEntity liv = (LivingEntity) in[0].get(sc);
-                double amount = in[1].getDouble(sc) - liv.getAttribute(attribute).getBaseValue();
+                IAttributeInstance a = ((LivingEntity) in[0].get(sc)).getAttribute(attribute);
+                if(a == null) {
+                    return;
+                }
+                double amount = in[1].getDouble(sc) - a.getBaseValue();
                 UUID uuid = new UUID(name.length(), name.hashCode());
-                liv.getAttribute(attribute).removeModifier(uuid);
-                liv.getAttribute(attribute).applyModifier(new AttributeModifier(uuid, name, amount, AttributeModifier.Operation.ADDITION));
+                a.removeModifier(uuid);
+                a.applyModifier(new AttributeModifier(uuid, name, amount, AttributeModifier.Operation.ADDITION));
             });
             sm.registerFunction("living.get" + name, (sc, in) -> {
-                return ((LivingEntity) in[0].get(sc)).getAttribute(attribute).getValue();
+                IAttributeInstance a = ((LivingEntity) in[0].get(sc)).getAttribute(attribute);
+                return a == null ? null : a.getValue();
             });
             sm.registerFunction("living.getbase" + name, (sc, in) -> {
-                return ((LivingEntity) in[0].get(sc)).getAttribute(attribute).getBaseValue();
+                IAttributeInstance a = ((LivingEntity) in[0].get(sc)).getAttribute(attribute);
+                return a == null ? null : a.getBaseValue();
             });
         }
         sm.registerConsumer("living.removeai", (sc, in) -> {

+ 31 - 15
src/main/java/me/km/snuviscript/commands/ReadCommands.java

@@ -9,6 +9,7 @@ import me.km.utils.Mapper;
 import me.km.utils.Utils;
 import net.minecraft.entity.Entity;
 import net.minecraft.entity.EntityType;
+import net.minecraft.inventory.EquipmentSlotType;
 import net.minecraft.item.Item;
 import net.minecraft.item.ItemStack;
 import net.minecraft.nbt.CompoundNBT;
@@ -18,27 +19,33 @@ import net.minecraft.util.text.StringTextComponent;
 import net.minecraft.world.server.ServerWorld;
 
 public class ReadCommands {
+
     public static void registerFunctions(ScriptManager sm, MinecraftServer server) {
         sm.registerFunction("read.player", (sc, in) -> Utils.getPlayerByName(server, in[0].getString(sc)));
         sm.registerFunction("read.location", (sc, in) -> new Location(server, in[0].getString(sc)));
         sm.registerFunction("read.item", (sc, in) -> {
-            String s = in[0].getString(sc);
-            if(s.startsWith("{")) {
-                String left = SnuviUtils.connect(sc, in, 1);
-                return ItemStackUtils.getStackFromNbtString(s + left);
-            }
-            Item item = Mapper.getItem(s);
-            int amount = in.length >= 2 ? in[1].getInt(sc) : 1;
-            ItemStack stack = new ItemStack(item, amount);
-            if(in.length >= 3) {
-                stack.setDisplayName(new StringTextComponent(in[2].getString(sc)));
-            }
-            if(in.length >= 4) {
-                for(int i = 3; i < in.length; i++) {
-                    ItemStackUtils.addLore(stack, in[i].getString(sc));
+            try {
+                String s = in[0].getString(sc);
+                if(s.startsWith("{")) {
+                    String left = SnuviUtils.connect(sc, in, 1);
+                    CompoundNBT c = JsonToNBT.getTagFromJson(s + left);
+                    return ItemStack.read(c);
+                }
+                Item item = Mapper.getItem(s);
+                int amount = in.length >= 2 ? in[1].getInt(sc) : 1;
+                ItemStack stack = new ItemStack(item, amount);
+                if(in.length >= 3) {
+                    stack.setDisplayName(new StringTextComponent(in[2].getString(sc)));
                 }
+                if(in.length >= 4) {
+                    for(int i = 3; i < in.length; i++) {
+                        ItemStackUtils.addLore(stack, in[i].getString(sc));
+                    }
+                }
+                return stack;
+            } catch(Exception ex) {
+                return null;
             }
-            return stack;
         });
         sm.registerFunction("read.spawnmob", (sc, in) -> {
             Location l = (Location) in[0].get(sc);
@@ -51,5 +58,14 @@ public class ReadCommands {
             return entity;
         });
         sm.registerFunction("read.uuid", (sc, in) -> UUID.fromString(in[0].getString(sc)));
+        sm.registerFunction("read.slot", (sc, in) -> {
+            String name = in[0].getString(sc);
+            for(EquipmentSlotType slot : EquipmentSlotType.values()) {
+                if(slot.getName().equals(name)) {
+                    return slot;
+                }
+            }
+            return null;
+        });
     }
 }

+ 1 - 3
src/main/java/me/km/snuviscript/commands/TextCommands.java

@@ -1,8 +1,6 @@
 package me.km.snuviscript.commands;
 
-import java.util.UUID;
 import me.hammerle.snuviscript.code.ScriptManager;
-import me.km.utils.ItemStackUtils;
 import me.km.utils.Location;
 import net.minecraft.entity.Entity;
 import net.minecraft.item.ItemStack;
@@ -15,7 +13,7 @@ public class TextCommands {
     public static void registerFunctions(ScriptManager sm) {
         sm.registerFunction("text.location", (sc, in) -> ((Location) in[0].get(sc)).toString());
         sm.registerFunction("text.locationblock", (sc, in) -> ((Location) in[0].get(sc)).toBlockString());
-        sm.registerFunction("text.item", (sc, in) -> ItemStackUtils.getNbtString((ItemStack) in[0].get(sc)));
+        sm.registerFunction("text.item", (sc, in) -> ((ItemStack) in[0].get(sc)).write(new CompoundNBT()).toString());
         sm.registerFunction("text.click", (sc, in) -> {
             Object message = in[0].get(sc);
             ITextComponent text;

+ 0 - 142
src/main/java/me/km/utils/ItemStackUtils.java

@@ -1,13 +1,8 @@
 package me.km.utils;
 
-import com.mojang.brigadier.exceptions.CommandSyntaxException;
 import java.util.ArrayList;
 import java.util.List;
-import net.minecraft.entity.ai.attributes.AttributeModifier;
-import net.minecraft.entity.ai.attributes.AttributeModifier.Operation;
-import net.minecraft.inventory.EquipmentSlotType;
 import net.minecraft.item.ItemStack;
-import net.minecraft.nbt.JsonToNBT;
 import net.minecraft.nbt.CompoundNBT;
 import net.minecraft.nbt.ListNBT;
 import net.minecraft.nbt.StringNBT;
@@ -15,143 +10,6 @@ import net.minecraft.util.text.ITextComponent;
 import net.minecraft.util.text.StringTextComponent;
 
 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 static void addAttribute(ItemStack stack, Attribute a, EquipmentSlotType slot, double amount, Operation op) {
-        stack.addAttributeModifier(a.getName(), new AttributeModifier("modifier", amount, op), 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) {
-        CompoundNBT com = stack.hasTag() ? stack.getTag() : new CompoundNBT();
-        if(com == null) // this will never happen, but the compiler wants it
-        {
-            return;
-        }
-        int i = com.getInt("HideFlags");
-        for(ItemFlag f : flags) {
-            i |= f.getBit(); // bitwise 'or'
-        }
-        com.putInt("HideFlags", i);
-        stack.setTag(com);
-    }
-
-    public static void removeItemFlags(ItemStack stack, ItemFlag... flags) {
-        CompoundNBT com = stack.hasTag() ? stack.getTag() : new CompoundNBT();
-        if(com == null) // this will never happen, but the compiler wants it
-        {
-            return;
-        }
-        int i = com.getInt("HideFlags");
-        for(ItemFlag f : flags) {
-            i &= ~f.getBit(); // bitwise 'and' with inversed bits
-        }
-        com.putInt("HideFlags", i);
-        stack.setTag(com);
-    }
-
-    public static boolean hasItemFlag(ItemStack stack, ItemFlag flag) {
-        if(!stack.hasTag()) {
-            return false;
-        }
-        CompoundNBT com = stack.getTag();
-        if(com == null) // this will never happen, but the compiler wants it
-        {
-            return false;
-        }
-        byte b = flag.getBit();
-        return (com.getInt("HideFlags") & b) == b;
-    }
-
-    public static String getNbtString(ItemStack stack) {
-        return stack.write(new CompoundNBT()).toString();
-    }
-
-    public static ItemStack getStackFromNbtString(String s) throws CommandSyntaxException {
-        CompoundNBT c = JsonToNBT.getTagFromJson(s);
-        return ItemStack.read(c);
-    }
-
     public static void setLore(ItemStack stack, List<Object> list) {
         CompoundNBT com = stack.getOrCreateChildTag("display");
         ListNBT nbtList = new ListNBT();

+ 1 - 3
src/main/resources/assets/km/lang/en_us.json

@@ -274,7 +274,5 @@
     "key.function.6": "Function Key 6", 
     "key.function.7": "Function Key 7", 
     "key.function.8": "Function Key 8", 
-    "key.function.9": "Function Key 9",
-    
-    "magicarmor": "Magic Armor"
+    "key.function.9": "Function Key 9"
 }