Bladeren bron

Moved SimpleConfig base to SnuviScript

Kajetan Johannes Hammerle 6 jaren geleden
bovenliggende
commit
84c0e6551b

+ 1 - 1
src/main/java/me/km/KajetansMod.java

@@ -119,7 +119,7 @@ public class KajetansMod
         event = e;
         // Konfiguration und Error-Dummy
         error = new Module("ERROR", "ERROR", TextFormatting.RED);
-        conf = new SimpleConfig(error, "config", true);
+        conf = new SimpleConfig(error, "", "config", true);
         debugMode = conf.getBoolean("debug", false);
         if(debugMode)
         {

+ 43 - 186
src/main/java/me/km/api/SimpleConfig.java

@@ -8,179 +8,61 @@ import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.TreeMap;
 import java.util.stream.Collectors;
+import me.hammerle.code.ISnuviLogger;
+import me.hammerle.code.Script;
+import me.hammerle.config.SnuviConfig;
+import me.hammerle.exceptions.SnuviException;
 import me.km.dimensions.ModDimensions;
 import me.km.exception.IllegalItemStackStringException;
 import me.km.utils.ItemStackUtils;
 import net.minecraft.item.ItemStack;
 import net.minecraft.util.math.Vec3d;
 
-public class SimpleConfig
-{            
-    private Module m;
-    
-    private final TreeMap<String, String> conf;
-    private File file;
-    
-    public SimpleConfig(Module m, String path, boolean load)
+public class SimpleConfig extends SnuviConfig
+{                   
+    public SimpleConfig(Module m, String path, String name, boolean load)
     {    
-        this.m = m;
-        file = new File("./" + path + ".snuvi");
-        conf = new TreeMap<>();
-        if(load && file.exists())
+        super(new ISnuviLogger() 
         {
-            try
+            @Override
+            public void printException(SnuviException ex)
             {
-                Files.readAllLines(file.toPath()).stream().forEach(s -> 
-                {
-                    int b = s.indexOf("=");
-                    if(b == -1)
-                    {
-                        m.sendWarningToConsole("Wrong syntax in '" + file.getPath() + "' - " + s);
-                        return;
-                    }
-                    conf.put(s.substring(0, b).trim(), s.substring(b + 1).trim());
-                });
-            } 
-            catch (MalformedInputException ex) 
-            {
-                m.sendWarningToConsole("'" + file.getPath() + "' contains an illegal character, change file encoding");
+                // should not happen
             }
-            catch (IOException ex) 
+
+            @Override
+            public void printException(SnuviException ex, Script s) 
             {
-                m.sendWarningToConsole("File '" + file.getPath() + "' cannot be read");
+                 // should not happen
             }
-        }
-        
-        //System.out.println("WUSI");
-        //conf.forEach((k, v) -> System.out.println(k + " " + v));
-    }
-    
-    public static File[] getFiles(String path)
-    {
-        return new File("./" + path).listFiles();
-    }
-    
-    public boolean exists()
-    {
-        return file.exists();
-    }
-    
-    public boolean delete()
-    {
-        return file.delete();
-    }
-    
-    public boolean save()
-    {
-        try
-        {
-            if(!file.getParentFile().exists())
+
+            @Override
+            public void printWarning(String s) 
             {
-                file.getParentFile().mkdirs();
+                m.sendWarningToConsole(s);
             }
-            if(!file.exists())
+
+            @Override
+            public void printInfo(String s) 
             {
-                file.createNewFile();
+                m.sendToConsole(s);
             }
-            Files.write(Paths.get(file.toURI()), conf.entrySet().stream()
-                            .map(e -> e.getKey() + "=" + e.getValue())
-                            .collect(Collectors.toList()), Charset.forName("UTF-8"));
-            return true;
-        }
-        catch(IOException ex)
+        }, path, name, "snuvi");
+       
+        if(load && exists())
         {
-            m.sendWarningToConsole("Can't write to '" + file.getPath() + "'");
-            ex.printStackTrace();
-            return false;
+            load();
         }
-    }
+    }   
     
-    // -----------------------------------------------------------------------------------
-    // Get Data
-    // -----------------------------------------------------------------------------------
-    
-    public String getString(String key)
-    {
-        return conf.get(key);
-    }
-    
-    public String getString(String key, String error)
-    {
-        String s =  conf.get(key);
-        if(s == null)
-        {
-            return error;
-        }
-        return s;
-    }
-    
-    public float getFloat(String key, float error)
-    {
-        String s = conf.get(key);
-        if(s == null)
-        {
-            return error;
-        }
-        try
-        {
-            return Float.parseFloat(s);
-        }
-        catch(NumberFormatException ex)
-        {
-            return error;
-        }
-    }
-    
-    public double getDouble(String key, double error)
-    {
-        String s = conf.get(key);
-        if(s == null)
-        {
-            return error;
-        }
-        try
-        {
-            return Double.parseDouble(s);
-        }
-        catch(NumberFormatException ex)
-        {
-            return error;
-        }
-    }
-    
-    public int getInt(String key, int error)
+    public static File[] getFiles(String path)
     {
-        String s = conf.get(key);
-        if(s == null)
-        {
-            return error;
-        }
-        try
-        {
-            return Integer.parseInt(s);
-        }
-        catch(NumberFormatException ex)
-        {
-            return error;
-        }
+        return new File("./" + path).listFiles();
     }
     
-    public boolean getBoolean(String key, boolean error)
-    {
-        String s = conf.get(key);
-        if(s == null)
-        {
-            return error;
-        }
-        try
-        {
-            return Boolean.valueOf(s);
-        }
-        catch(NumberFormatException ex)
-        {
-            return error;
-        }
-    }
+    // -----------------------------------------------------------------------------------
+    // get
+    // -----------------------------------------------------------------------------------
     
     public Location getLocation(String key)
     {
@@ -191,14 +73,14 @@ public class SimpleConfig
     
     public ItemStack getItemStack(String key)
     {
-        String s = conf.get(key);
+        Object s = conf.get(key);
         if(s == null)
         {
             return ItemStack.EMPTY;
         }
         try
         {
-            return ItemStackUtils.getStackFromNbtString(s);
+            return ItemStackUtils.getStackFromNbtString(s.toString());
         }
         catch(IllegalItemStackStringException ex)
         {
@@ -209,44 +91,19 @@ public class SimpleConfig
     // -----------------------------------------------------------------------------------
     // Add Data
     // -----------------------------------------------------------------------------------
-    
-    public void setString(String key, String value)
-    {
-        conf.put(key, value);
-    }
-    
-    public void setDouble(String key, double d)
-    {
-        setString(key, String.valueOf(d));
-    }
-    
-    public void setFloat(String key, float d)
-    {
-        setString(key, String.valueOf(d));
-    }
-    
-    public void setInt(String key, int i)
-    {
-        setString(key, String.valueOf(i));
-    }
-    
-    public void setBoolean(String key, boolean b)
-    {
-        setString(key, String.valueOf(b));
-    }
-    
+      
     public void setLocation(String key, Location l)
     {
-        setString(key + ".world", l.getWorld().getWorldInfo().getWorldName());
-        setDouble(key + ".x", l.getX());
-        setDouble(key + ".y", l.getY());
-        setDouble(key + ".z", l.getZ());
-        setDouble(key + ".yaw", l.getYaw());
-        setDouble(key + ".pitch", l.getPitch());
+        set(key + ".world", l.getWorld().getWorldInfo().getWorldName());
+        set(key + ".x", l.getX());
+        set(key + ".y", l.getY());
+        set(key + ".z", l.getZ());
+        set(key + ".yaw", l.getYaw());
+        set(key + ".pitch", l.getPitch());
     }
     
     public void setItemStack(String key, ItemStack stack)
     {
-        setString(key, ItemStackUtils.getNbtString(stack));
+        set(key, ItemStackUtils.getNbtString(stack));
     }
 }

+ 1 - 1
src/main/java/me/km/api/Utils.java

@@ -632,7 +632,7 @@ public class Utils
     
     public static Location getWarp(Module m, String name)
     {
-        SimpleConfig conf = new SimpleConfig(m, "warp/" + name, true);
+        SimpleConfig conf = new SimpleConfig(m, "warp", name, true);
         if(conf.exists())
         {
             return conf.getLocation("warp");

+ 1 - 1
src/main/java/me/km/commands/CommandDelHome.java

@@ -30,7 +30,7 @@ public class CommandDelHome extends ModuleCommand
         {
             return false;
         }
-        if(new SimpleConfig(this.getModule(), "home/" + ((EntityPlayer) cs).getUniqueID() + "/" + arg[0], false).delete())
+        if(new SimpleConfig(this.getModule(), "home/" + ((EntityPlayer) cs).getUniqueID(), arg[0], false).delete())
         {
             this.getModule().send(cs, "Der Home " + arg[0] + " wurde gelöscht.");
             return true;

+ 1 - 1
src/main/java/me/km/commands/CommandDelWarp.java

@@ -23,7 +23,7 @@ public class CommandDelWarp extends ModuleCommand
         {
             return false;
         }
-        if(new SimpleConfig(this.getModule(), "warp/" + arg[0], false).delete())
+        if(new SimpleConfig(this.getModule(), "warp", arg[0], false).delete())
         {
             this.getModule().send(cs, "Der Warp " + arg[0] + " wurde gelöscht.");
             return true;

+ 1 - 1
src/main/java/me/km/commands/CommandHome.java

@@ -76,7 +76,7 @@ public class CommandHome extends ModuleCommand
             return true;
         }
                   
-        SimpleConfig sc = new SimpleConfig(this.getModule(), "home/" + playeruuid + "/" + homename, true);
+        SimpleConfig sc = new SimpleConfig(this.getModule(), "home/" + playeruuid, homename, true);
         Location l = sc.getLocation("home");
         if(l == null)
         {

+ 1 - 1
src/main/java/me/km/commands/CommandSetHome.java

@@ -22,7 +22,7 @@ public class CommandSetHome extends ModuleCommand
 
     public boolean addHome(String name, String uuid, Location l)
     {
-        SimpleConfig sc = new SimpleConfig(this.getModule(), "home/" + uuid + "/" + name, false);
+        SimpleConfig sc = new SimpleConfig(this.getModule(), "home/" + uuid, name, false);
         if(sc.exists())
         {
             return false;

+ 1 - 1
src/main/java/me/km/commands/CommandSetWarp.java

@@ -22,7 +22,7 @@ public class CommandSetWarp extends ModuleCommand
 
     public boolean addWarp(String name, Location l)
     {
-        SimpleConfig sc = new SimpleConfig(this.getModule(), "warp/" + name, false);
+        SimpleConfig sc = new SimpleConfig(this.getModule(), "warp", name, false);
         if(sc.exists())
         {
             return false;

+ 9 - 9
src/main/java/me/km/dimensions/ChangeWorldEvent.java

@@ -60,17 +60,17 @@ public class ChangeWorldEvent extends ModuleListener
         {
             s = ModDimensions.getWorldName(DimensionManager.getWorld(dimension));
         }
-        SimpleConfig sp = new SimpleConfig(KajetansMod.worldManager, "worldinfo/" + p.getUniqueID() + "/" + s, false);  
+        SimpleConfig sp = new SimpleConfig(KajetansMod.worldManager, "worldinfo/" + p.getUniqueID(), s, false);  
         
         // general data
-        sp.setInt("exp", p.experienceTotal);     
+        sp.set("exp", p.experienceTotal);     
         FoodStats stats = p.getFoodStats();
-        sp.setFloat("exhaustion", ReflectionUtils.getExhaustion(stats));
-        sp.setFloat("saturation", ReflectionUtils.getSaturation(stats));
-        sp.setInt("foodlevel", stats.getFoodLevel());
-        sp.setFloat("currenthealth", p.getHealth());
-        sp.setFloat("maxhealth", 20);
-        sp.setInt("maxair", p.getAir());
+        sp.set("exhaustion", ReflectionUtils.getExhaustion(stats));
+        sp.set("saturation", ReflectionUtils.getSaturation(stats));
+        sp.set("foodlevel", stats.getFoodLevel());
+        sp.set("currenthealth", p.getHealth());
+        sp.set("maxhealth", 20);
+        sp.set("maxair", p.getAir());
         
         
         // position in world
@@ -126,7 +126,7 @@ public class ChangeWorldEvent extends ModuleListener
         {
             s = ModDimensions.getWorldName(DimensionManager.getWorld(dimension));
         }
-        SimpleConfig sp = new SimpleConfig(KajetansMod.worldManager, "worldinfo/" + p.getUniqueID() + "/" + s, true); 
+        SimpleConfig sp = new SimpleConfig(KajetansMod.worldManager, "worldinfo/" + p.getUniqueID(), s, true); 
         if(!sp.exists())
         {
             this.getModule().sendToConsole("Der Spieler '" + p.getName() + "' hat keine Daten in der Welt '" + s + "'.");

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

@@ -681,6 +681,18 @@ public class MinecraftFunctions implements ISnuviLogger
         printGeneralException(ex);
     }
     
+    @Override
+    public void printInfo(String s)
+    {
+        sendToDevsWithSuffix(s);
+    }
+    
+    @Override
+    public void printWarning(String s)
+    {
+        sendWarningToAllDevs(s);
+    }
+    
     private void printGeneralException(SnuviException ex)
     {
         if(ex.getCode() != null)