浏览代码

fixed bugs when a var does not exist

Kajetan Johannes Hammerle 6 年之前
父节点
当前提交
5050efba94
共有 1 个文件被更改,包括 27 次插入15 次删除
  1. 27 15
      src/main/java/me/km/snuviscript/ScriptEvents.java

+ 27 - 15
src/main/java/me/km/snuviscript/ScriptEvents.java

@@ -7,6 +7,7 @@ import java.util.function.Consumer;
 import java.util.stream.Collectors;
 import me.hammerle.snuviscript.code.Script;
 import me.hammerle.snuviscript.code.SnuviUtils;
+import me.hammerle.snuviscript.inputprovider.Variable;
 import me.kcm.events.PlayerPreRespawnEvent;
 import me.kcm.events.PlayerTabListNameEvent;
 import me.km.KajetansMod;
@@ -82,11 +83,20 @@ public class ScriptEvents
         handleEvent(p, event, before, null);
     }
     
+    private void ifVarNotNull(Script sc, String name, Consumer<Variable> c)
+    {
+        Variable v = sc.getVar(name);
+        if(v != null)
+        {
+            c.accept(v);
+        }
+    }
+    
     private void simpleCancel(Script sc, Event e, String name)
     {
         try
         {
-            e.setCanceled(sc.getVar("cancel").getBoolean(sc)); 
+            ifVarNotNull(sc, "cancel", v -> e.setCanceled(v.getBoolean(sc)));
         }
         catch(Exception ex)
         {
@@ -117,7 +127,8 @@ public class ScriptEvents
             ScriptVars.setItemVars(sc, inv.getStackInSlot(slot));
             sc.setVar("cancel", false); 
         }, null);
-        return script.getVar("cancel").getBoolean(script);
+        Variable v = script.getVar("cancel");
+        return v != null && v.getBoolean(script);
     }
 
     public static void onInventoryClose(Script script, SnuviInventory inv, EntityPlayer p)
@@ -171,8 +182,8 @@ public class ScriptEvents
         {
             try
             {
-                e.setAmount(sc.getVar("player_damage").getFloat(sc));
-                e.setCanceled(sc.getVar("cancel").getBoolean(sc)); 
+                ifVarNotNull(sc, "player_damage", v -> e.setAmount(v.getFloat(sc)));
+                ifVarNotNull(sc, "cancel", v -> e.setCanceled(v.getBoolean(sc)));
             }
             catch(Exception ex)
             {
@@ -238,8 +249,8 @@ public class ScriptEvents
             {
                 try
                 {
-                    e.setAmount(sc.getVar("heal").getFloat(sc));
-                    e.setCanceled(sc.getVar("cancel").getBoolean(sc)); 
+                    ifVarNotNull(sc, "heal", v -> e.setAmount(v.getFloat(sc)));
+                    ifVarNotNull(sc, "cancel", v -> e.setCanceled(v.getBoolean(sc)));
                 }
                 catch(Exception ex)
                 {
@@ -289,11 +300,12 @@ public class ScriptEvents
         {
             try
             {
-                if(sc.getVar("clear").getBoolean(sc))
+                Variable clear = sc.getVar("clear");
+                if(clear != null && clear.getBoolean(sc))
                 {
                     p.inventory.clear();
                 }
-                e.setCanceled(sc.getVar("cancel").getBoolean(sc)); 
+                ifVarNotNull(sc, "cancel", v -> e.setCanceled(v.getBoolean(sc)));
             }
             catch(Exception ex)
             {
@@ -321,8 +333,8 @@ public class ScriptEvents
         {
             try
             {
-                e.setAmount(sc.getVar("entity_damage").getFloat(sc));
-                e.setCanceled(sc.getVar("cancel").getBoolean(sc)); 
+                ifVarNotNull(sc, "entity_damage", v -> e.setAmount(v.getFloat(sc)));
+                ifVarNotNull(sc, "cancel", v -> e.setCanceled(v.getBoolean(sc)));
             }
             catch(Exception ex)
             {
@@ -634,8 +646,8 @@ public class ScriptEvents
         {
             try
             {
-                e.setDuration(sc.getVar("duration").getInt(sc));
-                e.setCanceled(sc.getVar("cancel").getBoolean(sc)); 
+                ifVarNotNull(sc, "duration", v -> e.setDuration(v.getInt(sc)));
+                ifVarNotNull(sc, "cancel", v -> e.setCanceled(v.getBoolean(sc)));
             }
             catch(Exception ex)
             {
@@ -774,8 +786,8 @@ public class ScriptEvents
         {
             try
             {
-                e.setComponent(new TextComponentString(sc.getVar("message").getString(sc)));
-                e.setCanceled(sc.getVar("cancel").getBoolean(sc)); 
+                ifVarNotNull(sc, "message", v -> e.setComponent(new TextComponentString(v.getString(sc))));
+                ifVarNotNull(sc, "cancel", v -> e.setCanceled(v.getBoolean(sc)));
             }
             catch(Exception ex)
             {
@@ -794,7 +806,7 @@ public class ScriptEvents
         {
             try
             {
-                e.setName(new TextComponentString(sc.getVar("tab_name").getString(sc)));
+                ifVarNotNull(sc, "tab_name", v -> e.setName(new TextComponentString(v.getString(sc))));
             }
             catch(Exception ex)
             {