Browse Source

databank clean up

Kajetan Johannes Hammerle 5 years ago
parent
commit
1df1f95d9b

+ 12 - 30
src/main/java/me/km/blockprotections/BlockProtection.java

@@ -5,11 +5,8 @@ import me.km.module.Module;
 import me.km.utils.Location;
 import me.km.utils.Utils;
 import net.minecraft.block.Block;
-import net.minecraft.block.BlockButton;
 import net.minecraft.block.BlockContainer;
 import net.minecraft.block.BlockDoor;
-import net.minecraft.block.BlockPistonMoving;
-import net.minecraft.block.BlockTrapDoor;
 import net.minecraft.block.state.IBlockState;
 import net.minecraft.entity.player.EntityPlayer;
 import net.minecraft.init.Blocks;
@@ -40,15 +37,6 @@ public class BlockProtection extends Module
         return bank;
     }
     
-    private boolean shouldBeProtected(Block b)
-    {
-        return (b instanceof BlockDoor || 
-               b instanceof BlockContainer ||
-               b == Blocks.LEVER || 
-               b instanceof BlockButton || 
-               b instanceof BlockTrapDoor) && !(b instanceof BlockPistonMoving);   
-    }
-    
     private BlockPos getSameNearbyBlock(World w, BlockPos pos, Block b)
     {           
         Location l = new Location(w, pos);
@@ -104,21 +92,19 @@ public class BlockProtection extends Module
     {
         EntityPlayer p = e.getPlayer();
         IBlockState state = e.getState();
-        if(shouldBeProtected(state.getBlock()))
+
+        BlockPos pos = e.getPos();
+        if(Utils.getStateValue(state, BlockDoor.HALF) == BlockDoor.EnumDoorHalf.UPPER)
         {
-            BlockPos pos = e.getPos();
-            if(Utils.getStateValue(state, BlockDoor.HALF) == BlockDoor.EnumDoorHalf.UPPER)
-            {
-                pos = pos.add(0, -1, 0);
-            } 
-            World w = e.getWorld();
-            if(bank.hasAccess(pos, w, p) || KajetansMod.perms.hasPermission(p, BLOCK_BYPASS))
-            {
-                bank.remove(pos, w);
-                return;
-            }   
-            e.setCanceled(true);
-        }             
+            pos = pos.add(0, -1, 0);
+        } 
+        World w = e.getWorld();
+        if(bank.hasAccess(pos, w, p) || KajetansMod.perms.hasPermission(p, BLOCK_BYPASS))
+        {
+            // TODO remove protection
+            return;
+        }   
+        e.setCanceled(true);          
     }
     
     @SubscribeEvent(priority = EventPriority.HIGHEST)
@@ -129,10 +115,6 @@ public class BlockProtection extends Module
             return;
         }
         IBlockState state = e.getWorld().getBlockState(e.getPos());
-        if(!shouldBeProtected(state.getBlock()))
-        {
-            return;
-        }
         EntityPlayer p = e.getEntityPlayer();
         BlockPos pos = e.getPos();
         if(Utils.getStateValue(state, BlockDoor.HALF) == BlockDoor.EnumDoorHalf.UPPER)

+ 1 - 142
src/main/java/me/km/blockprotections/BlockProtectionBank.java

@@ -3,7 +3,6 @@ package me.km.blockprotections;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import me.km.KajetansMod;
-import java.util.ArrayList;
 import me.km.databank.IStatement;
 import me.km.world.ModDimensions;
 import net.minecraft.entity.player.EntityPlayer;
@@ -30,114 +29,7 @@ public class BlockProtectionBank
                     + "UNIQUE KEY (block_id, player_id), "
                     + "CONSTRAINT block_grant_ibfk_1 FOREIGN KEY (block_id) REFERENCES block (id) ON DELETE CASCADE);");
     }  
-    
-    private final IStatement addBlock = KajetansMod.databank.createStatement(
-            "INSERT IGNORE INTO block (x, y, z, world_name) VALUES (?, ?, ?, ?);");
-    
-    private final IStatement addBlockGrant = KajetansMod.databank.createStatement(
-            "INSERT IGNORE INTO block_grant (block_id, player_id) VALUES (LAST_INSERT_ID(), ?);");
 
-    public void add(int x, int y, int z, String worldName, int playerId)
-    {
-        try
-        {
-            addBlock.validate();
-            addBlock.setInt(1, x);
-            addBlock.setInt(2, y);
-            addBlock.setInt(3, z);
-            addBlock.setString(4, worldName);
-            addBlock.executeUpdate();
-            
-            addBlockGrant.validate();
-            addBlockGrant.setInt(1, playerId);
-            addBlockGrant.executeUpdate();
-        }
-        catch(SQLException ex)
-        {
-            ex.printStackTrace();
-        }
-    }
-    
-    private final IStatement deleteBlock = KajetansMod.databank.createStatement(
-            "DELETE FROM block WHERE x=? AND y=? AND z=? AND world_name=?;");
-    
-    public void remove(int x, int y, int z, String worldName)
-    {
-        try
-        {
-            deleteBlock.validate();
-            deleteBlock.setInt(1, x);
-            deleteBlock.setInt(2, y);
-            deleteBlock.setInt(3, z);
-            deleteBlock.setString(4, worldName);
-            deleteBlock.executeUpdate();
-        }
-        catch(SQLException ex)
-        {
-            ex.printStackTrace();
-        }
-    }
-    
-    public void remove(BlockPos pos, World w)
-    {
-        remove(pos.getX(), pos.getY(), pos.getZ(), ModDimensions.getWorldName(w));
-    }
-    
-    private final IStatement addPlayer = KajetansMod.databank.createStatement(
-            "INSERT IGNORE INTO block_grant (block_id, player_id) SELECT id, ? FROM block WHERE world_name=? AND x=? AND y=? AND z=?;");
-    
-    public boolean addPlayer(int x, int y, int z, String worldName, int playerId)
-    {
-        if(hasAccess(x, y, z, worldName, playerId))
-        {
-            return false;
-        }
-        try
-        {
-            addPlayer.validate();
-            addPlayer.setInt(1, playerId);
-            addPlayer.setString(2, worldName);
-            addPlayer.setInt(3, x);
-            addPlayer.setInt(4, y);
-            addPlayer.setInt(5, z);
-            addPlayer.executeUpdate();
-            return true;
-        }
-        catch(SQLException ex)
-        {
-            ex.printStackTrace();
-            return false;
-        }
-    }
-    
-    private final IStatement removePlayer = KajetansMod.databank.createStatement(
-            "DELETE block_grant FROM block_grant LEFT JOIN block ON block.id = block_grant.block_id " +
-            "WHERE block_grant.player_id = ? AND block.world_name=? AND block.x=? AND block.y=? AND block.z=?;");
-    
-    public boolean removePlayer(int x, int y, int z, String worldName, int playerId)
-    {
-        if(!hasAccess(x, y, z, worldName, playerId))
-        {
-            return false;
-        }
-        try
-        {
-            removePlayer.validate();
-            removePlayer.setInt(1, playerId);
-            removePlayer.setString(2, worldName);
-            removePlayer.setInt(3, x);
-            removePlayer.setInt(4, y);
-            removePlayer.setInt(5, z);
-            removePlayer.executeUpdate();
-            return true;
-        }
-        catch(SQLException ex)
-        {
-            ex.printStackTrace();
-            return false;
-        }
-    }
-    
     private final IStatement hasAccess = KajetansMod.databank.createStatement(
             "Select id from block_grant WHERE id = ? AND player_id = ?;");
     
@@ -177,7 +69,7 @@ public class BlockProtectionBank
     private final IStatement getId = KajetansMod.databank.createStatement(
             "SELECT id FROM block WHERE world_name=? AND x=? AND y=? AND z=?;");
     
-    public int getId(int x, int y, int z, String worldName)
+    private int getId(int x, int y, int z, String worldName)
     {
         try
         {
@@ -205,37 +97,4 @@ public class BlockProtectionBank
         }
         return -1;
     }
-    
-    private final IStatement getPlayerIds = KajetansMod.databank.createStatement(
-            "SELECT player_id FROM block LEFT JOIN block_grant ON block_grant.id = block.id WHERE world_name=? AND x=? AND y=? AND z=?;");
-    
-    public ArrayList<Integer> getAllIds(int x, int y, int z, String worldName)
-    {
-        ArrayList<Integer> list = new ArrayList<>();
-        try
-        {
-            getPlayerIds.validate();
-            getPlayerIds.setString(1, worldName);
-            getPlayerIds.setInt(2, x);
-            getPlayerIds.setInt(3, y);
-            getPlayerIds.setInt(4, z);
-            try(ResultSet rs = getPlayerIds.executeQuery())
-            {
-                while(rs.next())
-                {
-                    list.add(rs.getInt(1));
-                }
-                return list;
-            }
-            catch(SQLException ex)
-            {
-                ex.printStackTrace();
-            }
-        }
-        catch(SQLException ex)
-        {
-            ex.printStackTrace();
-        }
-        return list;
-    } 
 }

+ 4 - 2
src/main/java/me/km/databank/IStatement.java

@@ -3,10 +3,12 @@ package me.km.databank;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 
-public interface IStatement
+public interface IStatement extends AutoCloseable
 {
     public void validate() throws SQLException;
-    public void close() throws SQLException; 
+    
+    @Override
+    public void close() throws SQLException;
     
     public void setInt(int index, int i) throws SQLException;
     public void setLong(int index, long l) throws SQLException;

+ 1 - 150
src/main/java/me/km/plots/ProtectionBank.java

@@ -47,110 +47,7 @@ public class ProtectionBank extends Module
     //--------------------------------------------------------------------------
     // Plot-Methoden
     //--------------------------------------------------------------------------
-    
-    private final IStatement add = KajetansMod.databank.createStatement(
-            "INSERT INTO plots (x1,y1,z1,x2,y2,z2,world_name,name) VALUES (?,?,?,?,?,?,?,?);");
-    public void add(int x1, int y1, int z1, int x2, int y2, int z2, String worldName, String plotName)
-    {
-        int minX = Math.min(x1, x2);
-        int minY = Math.min(y1, y2);
-        int minZ = Math.min(z1, z2);
-        int maxX = Math.max(x1, x2);
-        int maxY = Math.max(y1, y2);
-        int maxZ = Math.max(z1, z2);
-        
-        try
-        {
-            add.validate();
-            add.setInt(1, minX);
-            add.setInt(2, minY);
-            add.setInt(3, minZ);
-            add.setInt(4, maxX);
-            add.setInt(5, maxY);
-            add.setInt(6, maxZ);
-            add.setString(7, worldName);
-            add.setString(8, plotName);
-            add.executeUpdate();
-        }
-        catch(SQLException ex)
-        {
-            ex.printStackTrace();
-        }
-    }
 
-    private final IStatement removePlayer = KajetansMod.databank.createStatement(
-            "DELETE FROM plot_grant WHERE plot_id = ? AND player_id = ?;");
-    
-    public void removePlayer(int plotId, int playerId)
-    {
-        try
-        {
-            removePlayer.validate();
-            removePlayer.setInt(1, plotId);
-            removePlayer.setInt(2, playerId);
-            removePlayer.executeUpdate();
-        }
-        catch(SQLException ex)
-        {
-            ex.printStackTrace();
-        }
-    }
-    
-    private final IStatement addPlayer = KajetansMod.databank.createStatement(
-            "INSERT IGNORE INTO plot_grant (plot_id, player_id) VALUES(?,?);");
-    
-    public void addPlayer(int plotId, int playerId)
-    {
-        try
-        {
-            addPlayer.validate();
-            addPlayer.setInt(1, plotId);
-            addPlayer.setInt(2, playerId);
-            addPlayer.executeUpdate();
-        }
-        catch(SQLException ex)
-        {
-            ex.printStackTrace();
-        }
-    }
-    
-    private final IStatement isOverlapping = KajetansMod.databank.createStatement(
-            "SELECT id FROM plots WHERE x2 >= ? AND ? >= x1 AND y2 >= ? AND ? >= y1 AND z2 >= ? AND ? >= z1 AND world_name=?;");
-    
-    public boolean isOverlapping(int x1, int y1, int z1, int x2, int y2, int z2, World w)
-    {
-        int minX = Math.min(x1, x2);
-        int minY = Math.min(y1, y2);
-        int minZ = Math.min(z1, z2);
-        int maxX = Math.max(x1, x2);
-        int maxY = Math.max(y1, y2);
-        int maxZ = Math.max(z1, z2);
-        
-        try
-        {
-            isOverlapping.validate();
-            isOverlapping.setInt(1, minX);
-            isOverlapping.setInt(2, maxX);
-            isOverlapping.setInt(3, minY);
-            isOverlapping.setInt(4, maxY);
-            isOverlapping.setInt(5, minZ);
-            isOverlapping.setInt(6, maxZ);
-            try(ResultSet rs = isOverlapping.executeQuery())
-            {
-                return rs.next();
-            }
-            catch(SQLException ex)
-            {
-                ex.printStackTrace();
-            }
-        }
-        catch(SQLException ex)
-        {
-            ex.printStackTrace();
-        }
-        return false;
-    }
-    
     private final IStatement getIds = KajetansMod.databank.createStatement(
             "SELECT id FROM plots WHERE x1<=? AND x2>=? AND y1<=? AND y2>=? AND z1<=? AND z2>=? AND world_name=?;");
     
@@ -227,50 +124,4 @@ public class ProtectionBank extends Module
     {
         return canBuild(pos.getX(), pos.getY(), pos.getZ(), ModDimensions.getWorldName(w), KajetansMod.playerbank.getPlayerId(p.getUniqueID()));
     }
-    
-    private final IStatement remove = KajetansMod.databank.createStatement(
-            "DELETE FROM plots WHERE id=?;");
-    
-    public void remove(int plotId)
-    {
-        try
-        {
-            remove.validate();
-            remove.setInt(1, plotId);
-            remove.executeUpdate();
-        }
-        catch(SQLException ex)
-        {
-            ex.printStackTrace();
-        }
-    }
-    
-    private final IStatement getName = KajetansMod.databank.createStatement(
-            "SELECT name FROM plots WHERE id=?;");
-    
-    public String getName(int plotId)
-    {
-        try
-        {
-            getName.validate();
-            getName.setInt(1, plotId);
-            try(ResultSet rs = getName.executeQuery())
-            {
-                if(rs.next())
-                {
-                    return rs.getString(1);
-                }
-                return "-error-";
-            }
-            catch(SQLException ex)
-            {
-                ex.printStackTrace();
-            }
-        }
-        catch(SQLException ex)
-        {
-            ex.printStackTrace();
-        }
-        return "-error-";
-    }
-}
+}

+ 81 - 294
src/main/java/me/km/snuviscript/MinecraftFunctions.java

@@ -364,7 +364,7 @@ public class MinecraftFunctions
                 return new Location(((EntityPlayer) in[0].get(sc)).world, 
                         Utils.getTargetBlock((EntityPlayer) in[0].get(sc), in[1].getInt(sc), in[2].getBoolean(sc)));
             }
-            return new Location(((EntityPlayer) in[0].get(sc)).world, Utils.getTargetBlock((EntityPlayer) in[0].get(sc), in[1].getInt(sc)));
+            return new Location(((EntityPlayer) in[0].get(sc)).world, Utils.getTargetBlock((EntityPlayer) in[0].get(sc), in[1].getInt(sc), true));
         });
                 
         parser.registerFunction("player.gettargetentity", (sc, in) -> Utils.getTargetedEntity((EntityPlayer) in[0].get(sc), in[1].getDouble(sc), getClass(in[2].getString(sc))));
@@ -455,25 +455,14 @@ public class MinecraftFunctions
             p.sendPlayerAbilities();
             return true;
         });
-        parser.registerFunction("player.near", (sc, in) -> 
-        {     
-            EntityPlayer p = (EntityPlayer) in[1].get(sc);
-            in[0].set(sc, Utils.getPlayers(p, in[2].getDouble(sc))); 
-            return Void.TYPE;
-        });
-        parser.registerFunction("player.getinvslot", (sc, in) -> 
-        {     
-            return ((EntityPlayer) in[0].get(sc)).inventory.mainInventory.get(in[1].getInt(sc));
-        });
+        parser.registerFunction("player.near", (sc, in) -> Utils.getPlayers((Entity) in[0].get(sc), in[1].getDouble(sc)));
+        parser.registerFunction("player.getinvslot", (sc, in) -> ((EntityPlayer) in[0].get(sc)).inventory.mainInventory.get(in[1].getInt(sc)));
         parser.registerFunction("player.setinvslot", (sc, in) -> 
         {     
             ((EntityPlayer) in[0].get(sc)).inventory.mainInventory.set(in[1].getInt(sc), ((ItemStack) in[2].get(sc)).copy()); 
             return Void.TYPE;
         });
-        parser.registerFunction("player.getenderslot", (sc, in) -> 
-        {     
-            return ((EntityPlayer) in[0].get(sc)).getInventoryEnderChest().getStackInSlot(in[1].getInt(sc));
-        });
+        parser.registerFunction("player.getenderslot", (sc, in) -> ((EntityPlayer) in[0].get(sc)).getInventoryEnderChest().getStackInSlot(in[1].getInt(sc)));
         parser.registerFunction("player.setenderslot", (sc, in) -> 
         {     
             ((EntityPlayer) in[0].get(sc)).getInventoryEnderChest().setInventorySlotContents(in[1].getInt(sc), ((ItemStack) in[2].get(sc)).copy()); 
@@ -610,9 +599,11 @@ public class MinecraftFunctions
         });
         parser.registerFunction("world.settime", (sc, in) -> 
         {    
-            ((World) in[0].get(sc)).setWorldTime(in[0].getLong(sc));
+            ((World) in[0].get(sc)).setWorldTime(in[1].getLong(sc));
             return Void.TYPE;
         });
+        parser.registerFunction("world.gettime", (sc, in) -> (double) ((World) in[0].get(sc)).getWorldTime());
+        parser.registerFunction("world.hasstorm", (sc, in) -> ((World) in[0].get(sc)).isRaining());
         parser.registerFunction("world.clearweather", (sc, in) -> 
         {    
             WorldInfo wi = ((World) in[0].get(sc)).getWorldInfo();
@@ -749,7 +740,7 @@ public class MinecraftFunctions
         });
         parser.registerFunction("item.addflag", (sc, in) -> 
         { 
-            ItemStackUtils.addItemFlag((ItemStack) in[0].get(sc), ItemStackUtils.ItemFlag.valueOf(in[0].getString(sc).toUpperCase()));
+            ItemStackUtils.addItemFlag((ItemStack) in[0].get(sc), ItemStackUtils.ItemFlag.valueOf(in[1].getString(sc).toUpperCase()));
             return Void.TYPE;
         });
         parser.registerFunction("item.addattribute", (sc, in) -> 
@@ -866,8 +857,6 @@ public class MinecraftFunctions
                     return null;
             }
         });
-        parser.registerFunction("loc.gettime", (sc, in) -> (double) ((Location) in[0].get(sc)).getWorld().getWorldTime());
-        parser.registerFunction("loc.hasstorm", (sc, in) -> ((Location) in[0].get(sc)).getWorld().isRaining());
         parser.registerFunction("loc.isbetween", (sc, in) -> 
         { 
             Location l1 = (Location) in[0].get(sc);
@@ -877,6 +866,34 @@ public class MinecraftFunctions
                     l1.getY() >= Math.min(l2.getY(), l3.getY()) && l1.getY() <= Math.max(l2.getY(), l3.getY()) &&
                     l1.getZ() >= Math.min(l2.getZ(), l3.getZ()) && l1.getZ() <= Math.max(l2.getZ(), l3.getZ());
         });
+        parser.registerFunction("loc.sort", (sc, in) -> 
+        { 
+            Location l1 = (Location) in[0].get(sc);
+            Location l2 = (Location) in[1].get(sc);
+            
+            if(l1.getX() > l2.getX())
+            {
+                double tmp = l1.getX();
+                l1.setX(l2.getX());
+                l2.setX(tmp);
+            }
+            
+            if(l1.getY() > l2.getY())
+            {
+                double tmp = l1.getY();
+                l1.setY(l2.getY());
+                l2.setY(tmp);
+            }
+            
+            if(l1.getZ() > l2.getZ())
+            {
+                double tmp = l1.getZ();
+                l1.setZ(l2.getZ());
+                l2.setZ(tmp);
+            }
+            
+            return Void.TYPE;
+        });
 
         // ---------------------------------------------------------------------    
         // Block-library
@@ -1429,7 +1446,7 @@ public class MinecraftFunctions
             BlockPos pos = l.getBlockPos();
             if(!w.isBlockLoaded(pos))
             {
-                return false;
+                return null;
             }
            
             String type = in[0].getString(sc);
@@ -1437,9 +1454,11 @@ public class MinecraftFunctions
             double y = l.getY();
             double z = l.getZ();
 
-            if (EntityList.LIGHTNING_BOLT.equals(new ResourceLocation(type)))
+            if(EntityList.LIGHTNING_BOLT.equals(new ResourceLocation(type)))
             {
-                w.addWeatherEffect(new EntityLightningBolt(w, x, y, z, false));
+                Entity ent = new EntityLightningBolt(w, x, y, z, false);
+                w.addWeatherEffect(ent);
+                return ent;
             }
             else
             {
@@ -1455,7 +1474,7 @@ public class MinecraftFunctions
                     }
                     catch(NBTException ex) 
                     {
-                        return false;
+                        return null;
                     }
                 }
 
@@ -1463,7 +1482,7 @@ public class MinecraftFunctions
                 Entity entity = AnvilChunkLoader.readWorldEntityPos(nbttagcompound, w, x, y, z, true);
                 if(entity == null)
                 {
-                    return false;
+                    return null;
                 }
                 else
                 {
@@ -1473,15 +1492,10 @@ public class MinecraftFunctions
                         ((EntityLiving) entity).onInitialSpawn(w.getDifficultyForLocation(new BlockPos(entity)), null);
                     }
                 }
+                return entity;
             }
-            return true;
-        });
-        parser.registerFunction("entity.near", (sc, in) -> 
-        {  
-            Entity ent = (Entity) in[1].get(sc);
-            in[0].set(sc, Utils.getLiving(ent, in[2].getDouble(sc))); 
-            return Void.TYPE;
         });
+        parser.registerFunction("entity.near", (sc, in) -> Utils.getLiving((Entity) in[0].get(sc), in[1].getDouble(sc)));
         parser.registerFunction("entity.setspeed", (sc, in) -> 
         {  
             ((EntityLivingBase) in[0].get(sc)).getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(in[1].getDouble(sc));
@@ -1526,7 +1540,7 @@ public class MinecraftFunctions
         // ---------------------------------------------------------------------
         parser.registerFunction("villager.showtrades", (sc, in) -> 
         { 
-            ((EntityPlayer) in[0].get(sc)).displayVillagerTradeGui(((EntityVillager) in[1].get(sc))); 
+            ((EntityPlayerMP) in[0].get(sc)).displayVillagerTradeGui(((EntityVillager) in[1].get(sc))); 
             return Void.TYPE; 
         });
         parser.registerFunction("villager.cleartrades", (sc, in) -> 
@@ -1698,143 +1712,41 @@ public class MinecraftFunctions
 
         parser.registerFunction("databank.prepare", (sc, in) -> 
         {    
-            try
-            {
-                PreparedStatement p = KajetansMod.databank.prepareUnsafeStatement(in[0].getString(sc));
-                sc.addCloseable(p);
-                return p; 
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
+            PreparedStatement p = KajetansMod.databank.prepareUnsafeStatement(in[0].getString(sc));
+            sc.addCloseable(p);
+            return p; 
         });
         parser.registerFunction("databank.setint", (sc, in) -> 
         {    
-            try
-            {
-                ((PreparedStatement) in[0].get(sc)).setInt(in[1].getInt(sc), in[2].getInt(sc));
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
+            ((PreparedStatement) in[0].get(sc)).setInt(in[1].getInt(sc), in[2].getInt(sc));
             return Void.TYPE;
         });
         parser.registerFunction("databank.setlong", (sc, in) -> 
         {    
-            try
-            {
-                ((PreparedStatement) in[0].get(sc)).setLong(in[1].getInt(sc), in[2].getLong(sc));
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
+            ((PreparedStatement) in[0].get(sc)).setLong(in[1].getInt(sc), in[2].getLong(sc));
             return Void.TYPE;
         });
         parser.registerFunction("databank.setdouble", (sc, in) -> 
         {    
-            try
-            {
-                ((PreparedStatement) in[0].get(sc)).setDouble(in[1].getInt(sc), in[2].getDouble(sc));
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
+            ((PreparedStatement) in[0].get(sc)).setDouble(in[1].getInt(sc), in[2].getDouble(sc));
             return Void.TYPE;
         });
         parser.registerFunction("databank.setstring", (sc, in) -> 
         {    
-            try
-            {
-                ((PreparedStatement) in[0].get(sc)).setString(in[1].getInt(sc), in[2].getString(sc));
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
+            ((PreparedStatement) in[0].get(sc)).setString(in[1].getInt(sc), in[2].getString(sc));
             return Void.TYPE;
         });
         parser.registerFunction("databank.setbool", (sc, in) -> 
         {    
-            try
-            {
-                ((PreparedStatement) in[0].get(sc)).setBoolean(in[1].getInt(sc), in[2].getBoolean(sc));
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
+            ((PreparedStatement) in[0].get(sc)).setBoolean(in[1].getInt(sc), in[2].getBoolean(sc));
             return Void.TYPE;
         });
-        parser.registerFunction("databank.getint", (sc, in) -> 
-        {    
-            try
-            {
-                return (double) ((ResultSet) in[0].get(sc)).getInt(in[1].getInt(sc));
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
-        });
-        parser.registerFunction("databank.getlong", (sc, in) -> 
-        {    
-            try
-            {
-                return (double) ((ResultSet) in[0].get(sc)).getLong(in[1].getInt(sc));
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
-        });
-        parser.registerFunction("databank.getdouble", (sc, in) -> 
-        {    
-            try
-            {
-                return ((ResultSet) in[0].get(sc)).getDouble(in[1].getInt(sc));
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
-        });
-        parser.registerFunction("databank.getstring", (sc, in) -> 
-        {    
-            try
-            {
-                return ((ResultSet) in[0].get(sc)).getString(in[1].getInt(sc));
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
-        });
-        parser.registerFunction("databank.getbool", (sc, in) -> 
-        {    
-            try
-            {
-                return ((ResultSet) in[0].get(sc)).getBoolean(in[1].getInt(sc));
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
-        });
-        parser.registerFunction("databank.execute", (sc, in) -> 
-        {    
-            try
-            {
-                return ((PreparedStatement) in[0].get(sc)).executeQuery();
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
-        });
+        parser.registerFunction("databank.getint", (sc, in) -> (double) ((ResultSet) in[0].get(sc)).getInt(in[1].getInt(sc)));
+        parser.registerFunction("databank.getlong", (sc, in) -> (double) ((ResultSet) in[0].get(sc)).getLong(in[1].getInt(sc)));
+        parser.registerFunction("databank.getdouble", (sc, in) -> ((ResultSet) in[0].get(sc)).getDouble(in[1].getInt(sc)));
+        parser.registerFunction("databank.getstring", (sc, in) -> ((ResultSet) in[0].get(sc)).getString(in[1].getInt(sc)));
+        parser.registerFunction("databank.getbool", (sc, in) -> ((ResultSet) in[0].get(sc)).getBoolean(in[1].getInt(sc)));
+        parser.registerFunction("databank.execute", (sc, in) -> ((PreparedStatement) in[0].get(sc)).executeQuery());
         parser.registerFunction("databank.workerexecute", (sc, in) -> 
         {    
             final PreparedStatement p = (PreparedStatement) in[0].get(sc);
@@ -1843,7 +1755,20 @@ public class MinecraftFunctions
             {
                 try
                 {
-                    p.execute();
+                    p.execute();                
+                }
+                catch(SQLException ex)
+                {
+                    KajetansMod.scheduler.scheduleTask(() -> 
+                    {
+                        ChatChannel.getDevChannel().sendWarning("Worker error in script '" + name + "'");
+                        ChatChannel.getDevChannel().sendWarning(ex.getLocalizedMessage());
+                    });
+                }
+                sc.removeCloseable(p);
+                try
+                {
+                    p.close();
                 }
                 catch(SQLException ex)
                 {
@@ -1856,82 +1781,18 @@ public class MinecraftFunctions
             }); 
             return Void.TYPE;
         });
-        parser.registerFunction("databank.next", (sc, in) -> 
-        {    
-            try
-            {
-                return ((ResultSet) in[0].get(sc)).next();
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
-        });
+        parser.registerFunction("databank.next", (sc, in) -> ((ResultSet) in[0].get(sc)).next());
         parser.registerFunction("databank.close", (sc, in) -> 
         {    
-            try
-            {
-                ((ResultSet) in[0].get(sc)).close();
-                return Void.TYPE;
-            }
-            catch(SQLException ex)
-            {
-                throw new IllegalArgumentException(ex.getMessage());
-            }
+            AutoCloseable auto = (AutoCloseable) in[0].get(sc);
+            auto.close();
+            sc.removeCloseable(auto);
+            return Void.TYPE;
         });
         
         // ---------------------------------------------------------------------  
         // Plot-library   
         // ---------------------------------------------------------------------  
-        parser.registerFunction("plot.add", (sc, in) -> 
-        {    
-            final Location l1 = (Location) in[0].get(sc);
-            final Location l2 = (Location) in[1].get(sc);
-            final BlockPos pos1 = l1.getBlockPos();
-            final BlockPos pos2 = l2.getBlockPos();
-            final int x1 = pos1.getX();
-            final int y1 = pos1.getY();
-            final int z1 = pos1.getZ();
-            final int x2 = pos2.getX();
-            final int y2 = pos2.getY();
-            final int z2 = pos2.getZ();
-            final String world = ModDimensions.getWorldName(l1.getWorld());
-            final String name = in[2].getString(sc);
-            KajetansMod.scheduler.getWorker().add(() -> 
-            {
-                KajetansMod.plots.add(x1, y1, z1, x2, y2, z2, world, name);
-            });
-            return Void.TYPE;
-        });
-        parser.registerFunction("plot.remove", (sc, in) -> 
-        {    
-            final int plotId = in[0].getInt(sc);
-            KajetansMod.scheduler.getWorker().add(() -> 
-            {
-                KajetansMod.plots.remove(plotId);
-            });
-            return Void.TYPE;
-        });
-        parser.registerFunction("plot.addplayer", (sc, in) -> 
-        {    
-            final int plotId = in[0].getInt(sc);
-            final int playerId = in[1].getInt(sc);
-            KajetansMod.scheduler.getWorker().add(() -> 
-            {
-                KajetansMod.plots.addPlayer(plotId, playerId);
-            });
-            return Void.TYPE;
-        });
-        parser.registerFunction("plot.removeplayer", (sc, in) -> 
-        {    
-            final int plotId = in[0].getInt(sc);
-            final int playerId = in[1].getInt(sc);
-            KajetansMod.scheduler.getWorker().add(() -> 
-            {
-                KajetansMod.plots.removePlayer(plotId, playerId);
-            });
-            return Void.TYPE;
-        });
         parser.registerFunction("plot.getids", (sc, in) -> 
         {  
             Location l = (Location) in[0].get(sc);
@@ -1943,89 +1804,16 @@ public class MinecraftFunctions
             Location l = (Location) in[0].get(sc);
             return KajetansMod.plots.canBuild(l.getWorld(), l.getBlockPos(), (EntityPlayer) in[1].get(sc));
         });
-        parser.registerFunction("plot.getname", (sc, in) -> KajetansMod.plots.getName(in[0].getInt(sc)));
-        parser.registerFunction("plot.doesintersect", (sc, in) -> 
-        {    
-            Location l = (Location) in[0].get(sc);
-            BlockPos l1 = l.getBlockPos();
-            BlockPos l2 = ((Location) in[1].get(sc)).getBlockPos();
-            return KajetansMod.plots.isOverlapping(l1.getX(), l1.getY(), l1.getZ(), l2.getX(), l2.getY(), l2.getZ(), l.getWorld());
-        });
         
         // ---------------------------------------------------------------------  
         // block protection library   
         // ---------------------------------------------------------------------  
-        parser.registerFunction("protect.add", (sc, in) -> 
-        {    
-            final Location l1 = (Location) in[0].get(sc);
-            final BlockPos pos1 = l1.getBlockPos();
-            final int x1 = pos1.getX();
-            final int y1 = pos1.getY();
-            final int z1 = pos1.getZ();
-            final int playerId = in[1].getInt(sc);
-            final String world = ModDimensions.getWorldName(l1.getWorld());
-            KajetansMod.scheduler.getWorker().add(() -> 
-            {
-                KajetansMod.blocks.getDatabank().add(x1, y1, z1, world, playerId);
-            });
-            return Void.TYPE;
-        });
-        parser.registerFunction("protect.remove", (sc, in) -> 
-        {    
-            final Location l1 = (Location) in[0].get(sc);
-            final BlockPos pos1 = l1.getBlockPos();
-            final int x1 = pos1.getX();
-            final int y1 = pos1.getY();
-            final int z1 = pos1.getZ();
-            final String world = ModDimensions.getWorldName(l1.getWorld());
-            KajetansMod.scheduler.getWorker().add(() -> 
-            {
-                KajetansMod.blocks.getDatabank().remove(x1, y1, z1, world);
-            });
-            return Void.TYPE;
-        });
-        parser.registerFunction("protect.addplayer", (sc, in) -> 
-        {    
-            final Location l1 = (Location) in[0].get(sc);
-            final BlockPos pos1 = l1.getBlockPos();
-            final int x1 = pos1.getX();
-            final int y1 = pos1.getY();
-            final int z1 = pos1.getZ();
-            final String world = ModDimensions.getWorldName(l1.getWorld());
-            final int playerId = in[1].getInt(sc);
-            KajetansMod.scheduler.getWorker().add(() -> 
-            {
-                KajetansMod.blocks.getDatabank().addPlayer(x1, y1, z1, world, playerId);
-            });
-            return Void.TYPE;
-        });
-        parser.registerFunction("protect.removeplayer", (sc, in) -> 
-        {    
-            final Location l1 = (Location) in[0].get(sc);
-            final BlockPos pos1 = l1.getBlockPos();
-            final int x1 = pos1.getX();
-            final int y1 = pos1.getY();
-            final int z1 = pos1.getZ();
-            final String world = ModDimensions.getWorldName(l1.getWorld());
-            final int playerId = in[1].getInt(sc);
-            KajetansMod.scheduler.getWorker().add(() -> 
-            {
-                KajetansMod.blocks.getDatabank().removePlayer(x1, y1, z1, world, playerId);
-            });
-            return Void.TYPE;
-        });
         parser.registerFunction("protect.hasaccess", (sc, in) -> 
         {  
             Location l = (Location) in[0].get(sc);
             BlockPos pos = l.getBlockPos();
             return KajetansMod.blocks.getDatabank().hasAccess(pos, l.getWorld(), (EntityPlayer) in[1].get(sc));
         });
-        parser.registerFunction("protect.getids", (sc, in) -> 
-        {    
-            Location l = (Location) in[0].get(sc);
-            BlockPos pos = l.getBlockPos();
-            return KajetansMod.blocks.getDatabank().getAllIds(pos.getX(), pos.getY(), pos.getZ(), ModDimensions.getWorldName(l.getWorld()));
-        });
         
         // ---------------------------------------------------------------------  
         // Script-library   
@@ -2315,7 +2103,6 @@ public class MinecraftFunctions
         {
             Location l = (Location) in[0].get(sc);
             String data = SnuviUtils.connect(sc, in, 1).replace('\'', '"');
-            
             try 
             {
                 NBTTagCompound nbt = JsonToNBT.getTagFromJson(data);
@@ -2588,7 +2375,7 @@ public class MinecraftFunctions
         doForGroup(group, sc, p -> p.sendMessage(text));
     }
     
-    private static ITextComponent concat(Script sc, int start, String pre, InputProvider... ob)
+    private static ITextComponent concat(Script sc, int start, String pre, InputProvider... ob) throws Exception
     {
         TextComponentString text = new TextComponentString(pre);
         Object o;
@@ -2606,4 +2393,4 @@ public class MinecraftFunctions
         }
         return text;
     }
-}
+}

+ 18 - 36
src/main/java/me/km/snuviscript/ScriptBank.java

@@ -37,12 +37,10 @@ public class ScriptBank
                 + "UNIQUE INDEX (map,keyname,seckeyname));");
     }
 
-    private final IStatement setVar = KajetansMod.databank.createStatement(
-            "INSERT INTO scriptdata (player_id,var,value) SELECT ?, ?, ? ON DUPLICATE KEY UPDATE scriptdata.value=?;");
-    
     public void setVar(String value, String var, UUID uuid)
     {
-        try
+        try(IStatement setVar = KajetansMod.databank.createStatement(
+                "INSERT INTO scriptdata (player_id,var,value) SELECT ?, ?, ? ON DUPLICATE KEY UPDATE scriptdata.value=?;"))
         {
             setVar.validate();
             setVar.setInt(1, KajetansMod.playerbank.getPlayerId(uuid));
@@ -57,12 +55,10 @@ public class ScriptBank
         }
     }
 
-    private final IStatement deleteVar = KajetansMod.databank.createStatement(
-            "DELETE FROM scriptdata WHERE player_id = ? AND var = ?;");
-    
     public void deleteVar(String var, UUID uuid)
     {
-        try
+        try(IStatement deleteVar = KajetansMod.databank.createStatement(
+                "DELETE FROM scriptdata WHERE player_id = ? AND var = ?;"))
         {
             deleteVar.validate();
             deleteVar.setInt(1, KajetansMod.playerbank.getPlayerId(uuid));
@@ -110,12 +106,10 @@ public class ScriptBank
         return getVar(var, uuid, null);
     }    
     
-    private final IStatement addMapElement = KajetansMod.databank.createStatement(
-            "INSERT INTO scriptmaps (map,keyname,value) SELECT ?, ?, ? ON DUPLICATE KEY UPDATE value=?;");
-    
     public void addMapElement(String map, String key, String value)
     {
-        try
+        try(IStatement addMapElement = KajetansMod.databank.createStatement(
+                "INSERT INTO scriptmaps (map,keyname,value) SELECT ?, ?, ? ON DUPLICATE KEY UPDATE value=?;"))
         {
             addMapElement.validate();
             addMapElement.setString(1, map);
@@ -130,12 +124,10 @@ public class ScriptBank
         }
     }
     
-    private final IStatement removeMapElement = KajetansMod.databank.createStatement(
-            "DELETE FROM scriptmaps WHERE map = ? AND keyname = ?;");
-    
     public void removeMapElement(String map, String key)
     {
-        try
+        try(IStatement removeMapElement = KajetansMod.databank.createStatement(
+                "DELETE FROM scriptmaps WHERE map = ? AND keyname = ?;"))
         {
             removeMapElement.validate();
             removeMapElement.setString(1, map);
@@ -148,12 +140,10 @@ public class ScriptBank
         }
     }
     
-    private final IStatement removeMap = KajetansMod.databank.createStatement(
-            "DELETE FROM scriptmaps WHERE map = ?;");
-    
     public void removeMap(String map)
     {
-        try
+        try(IStatement removeMap = KajetansMod.databank.createStatement(
+                "DELETE FROM scriptmaps WHERE map = ?;"))
         {
             removeMap.validate();
             removeMap.setString(1, map);
@@ -195,12 +185,10 @@ public class ScriptBank
         return null;
     } 
     
-    private final IStatement addDualMapElement = KajetansMod.databank.createStatement(
-            "INSERT INTO scriptdualmaps (map,keyname,seckeyname,value) SELECT ?, ?, ?, ? ON DUPLICATE KEY UPDATE value=?;");
-    
     public void addDualMapElement(String map, String key, String key2, String value)
     {
-        try
+        try(IStatement addDualMapElement = KajetansMod.databank.createStatement(
+                "INSERT INTO scriptdualmaps (map,keyname,seckeyname,value) SELECT ?, ?, ?, ? ON DUPLICATE KEY UPDATE value=?;"))
         {
             addDualMapElement.validate();
             addDualMapElement.setString(1, map);
@@ -216,12 +204,10 @@ public class ScriptBank
         }
     }
     
-    private final IStatement removeDualMapElement = KajetansMod.databank.createStatement(
-            "DELETE FROM scriptdualmaps WHERE map = ? AND keyname = ?;");
-    
     public void removeDualMapElement(String map, String key)
     {
-        try
+        try(IStatement removeDualMapElement = KajetansMod.databank.createStatement(
+                "DELETE FROM scriptdualmaps WHERE map = ? AND keyname = ?;"))
         {
             removeDualMapElement.validate();
             removeDualMapElement.setString(1, map);
@@ -234,12 +220,10 @@ public class ScriptBank
         }
     }
     
-    private final IStatement removeDualMap = KajetansMod.databank.createStatement(
-            "DELETE FROM scriptdualmaps WHERE map = ?;");
-    
     public void removeDualMap(String map)
     {
-        try
+        try(IStatement removeDualMap = KajetansMod.databank.createStatement(
+                "DELETE FROM scriptdualmaps WHERE map = ?;"))
         {
             removeDualMap.validate();
             removeDualMap.setString(1, map);
@@ -251,12 +235,10 @@ public class ScriptBank
         }
     }
     
-    private final IStatement removeDualMapElement2 = KajetansMod.databank.createStatement(
-            "DELETE FROM scriptdualmaps WHERE map = ? AND keyname = ? AND seckeyname = ?;");
-    
     public void removeDualMapElement(String map, String key, String key2)
     {
-        try
+        try(IStatement removeDualMapElement2 = KajetansMod.databank.createStatement(
+                "DELETE FROM scriptdualmaps WHERE map = ? AND keyname = ? AND seckeyname = ?;"))
         {
             removeDualMapElement2.validate();
             removeDualMapElement2.setString(1, map);

+ 1 - 20
src/main/java/me/km/snuviscript/ScriptEvents.java

@@ -784,25 +784,6 @@ public class ScriptEvents
         });
     }
     
-    @SubscribeEvent
-    public void onGetPlayerName(net.minecraftforge.event.entity.player.PlayerEvent.NameFormat e)
-    {
-        handleEvent(e.getEntityPlayer(), "display_name", (sc) -> 
-        {
-            sc.setVar("display", e.getDisplayname());
-        }, (sc) -> 
-        {
-            try
-            {
-                e.setDisplayname(sc.getVar("display").getString(sc));
-            }
-            catch(Exception ex)
-            {
-                KajetansMod.scripts.logger.print("invalid var in 'display_name' event", ex, null, sc.getName(), sc, sc.getActiveRealLine());
-            }
-        });
-    }
-    
     @SubscribeEvent
     public void onGetPlayerName(PlayerTabListNameEvent e)
     {
@@ -813,7 +794,7 @@ public class ScriptEvents
         {
             try
             {
-                e.setName(new TextComponentString("tab_name"));
+                e.setName(new TextComponentString(sc.getVar("tab_name").getString(sc)));
             }
             catch(Exception ex)
             {

+ 18 - 2
src/main/java/me/km/snuviscript/SnuviLogger.java

@@ -16,7 +16,14 @@ public class SnuviLogger implements ISnuviLogger
             Module m = KajetansMod.scripts;
             if(scriptname != null)
             {
-                m.sendWarningToConsole("error in '" + scriptname + "'");
+                if(sc != null)
+                {
+                    m.sendWarningToConsole("error in '" + scriptname + "' " + sc.getId());
+                }
+                else
+                {
+                    m.sendWarningToConsole("error in '" + scriptname + "'");
+                }
             }
             if(message != null)
             {
@@ -25,6 +32,7 @@ public class SnuviLogger implements ISnuviLogger
             if(ex != null)
             {
                 m.sendHelpListElementToConsole("exception", ex.getClass().getSimpleName());
+                m.sendHelpListElementToConsole("exception", ex.getMessage());
             }
             if(function != null)
             {
@@ -39,7 +47,14 @@ public class SnuviLogger implements ISnuviLogger
         ChatChannel dev = ChatChannel.getDevChannel();
         if(scriptname != null)
         {
-            dev.sendWarning("error in '" + scriptname + "'");
+            if(sc != null)
+            {
+                dev.sendWarning("error in '" + scriptname + "' " + sc.getId());
+            }
+            else
+            {
+                dev.sendWarning("error in '" + scriptname + "'");
+            }
         }
         if(message != null)
         {
@@ -48,6 +63,7 @@ public class SnuviLogger implements ISnuviLogger
         if(ex != null)
         {
             dev.sendList("exception", ex.getClass().getSimpleName());
+            dev.sendList("exception", ex.getMessage());
         }
         if(function != null)
         {

+ 0 - 10
src/main/java/me/km/utils/Utils.java

@@ -367,16 +367,6 @@ public class Utils
         return ray.getBlockPos();   
     }
     
-    public static BlockPos getTargetBlock(Entity ent, double range)
-    {
-        return getTargetBlock(ent, range, false);  
-    }
-    
-    public static BlockPos getTargetBlock(Entity ent)
-    {
-        return getTargetBlock(ent, 20);
-    }  
-    
     public static EntityPlayerMP getPlayerByName(String name) throws PlayerNotFoundException
     {
         String nameLower = name.toLowerCase();