+ Added lua writing functionality
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package commands.dew;
|
||||
|
||||
import commands.dew.core.data.*;
|
||||
import commands.dew.core.impl.DewLuaCore;
|
||||
import core.SubCommand;
|
||||
import core.SubCommandFormattable;
|
||||
import dataStructures.KittyChannel;
|
||||
@@ -16,9 +18,19 @@ public class CommandDewStart extends SubCommand
|
||||
super(roleLevel, contentRating);
|
||||
}
|
||||
|
||||
private String asCodeBlock(String string)
|
||||
{
|
||||
return "```lua\n" + string + "```";
|
||||
}
|
||||
|
||||
@Override
|
||||
public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input)
|
||||
{
|
||||
return new SubCommandFormattable ("weh");
|
||||
String out = "";
|
||||
|
||||
out += asCodeBlock(DewLuaCore.toLua(new DewMap()));
|
||||
out += asCodeBlock(DewLuaCore.toLua(new DewMovementInfo()));
|
||||
|
||||
return new SubCommandFormattable(out);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package commands.dew.core.api;
|
||||
|
||||
public interface IDewEntity
|
||||
{
|
||||
public String getUniqueID();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package commands.dew.core.api;
|
||||
|
||||
// Commands are standalone pieces of data that contain only structures.
|
||||
// Each command is simplistic and represents an action in the game world.
|
||||
// Format:
|
||||
/*
|
||||
return
|
||||
{
|
||||
name="SomeCommand",
|
||||
data={
|
||||
-- some object here --
|
||||
}
|
||||
}
|
||||
*/
|
||||
public interface IDewMessage<T>
|
||||
{
|
||||
public String getMessageName();
|
||||
public T getData();
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package commands.dew.core.api;
|
||||
|
||||
public interface IDewPlayer
|
||||
public interface IDewPlayer extends IDewEntity
|
||||
{
|
||||
public String getName();
|
||||
public String getUniqueID();
|
||||
}
|
||||
|
||||
@@ -6,5 +6,5 @@ public final class DewMap implements IDewLuaSerializable<DewMap>
|
||||
{
|
||||
public int mapWidth = 20;
|
||||
public int mapHeight = 20;
|
||||
public DewMapTile[][] map;
|
||||
public int[][] map;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package commands.dew.core.data;
|
||||
|
||||
import commands.dew.core.api.IDewLuaSerializable;
|
||||
|
||||
public enum DewMapTile
|
||||
{
|
||||
Default(0), Tree(1);
|
||||
// Assign multiples of 2 for flag purposes
|
||||
Default(0), Grass(1), Tree(2);
|
||||
|
||||
private final int value;
|
||||
private DewMapTile(int value)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package commands.dew.core.data;
|
||||
|
||||
import commands.dew.core.api.IDewLuaSerializable;
|
||||
|
||||
// Treat all data objects as structs for the purposes of this.
|
||||
public class DewMovementInfo implements IDewLuaSerializable<DewMovementInfo>
|
||||
{
|
||||
public String entityID;
|
||||
public int x;
|
||||
public int y;
|
||||
}
|
||||
@@ -7,7 +7,78 @@ import commands.dew.core.api.IDewLuaSerializable;
|
||||
|
||||
public class DewLuaCore implements IDewLuaCore
|
||||
{
|
||||
public static void toLua(IDewLuaSerializable item)
|
||||
public static String toLua(IDewLuaSerializable<?> item)
|
||||
{
|
||||
return toLua(item, 1);
|
||||
}
|
||||
|
||||
public static String toLua(IDewLuaSerializable<?> item, int tabs)
|
||||
{
|
||||
String out = "{";
|
||||
|
||||
Field[] fields = item.getClass().getDeclaredFields();
|
||||
for(int i = 0; i < fields.length; ++i)
|
||||
{
|
||||
Field current = fields[i];
|
||||
try
|
||||
{
|
||||
String line = "\n";
|
||||
|
||||
String fieldTypeRaw = current.getType().toString().toLowerCase();
|
||||
int fieldLastDotIndex = fieldTypeRaw.lastIndexOf('.');
|
||||
String fieldType = fieldLastDotIndex <= 0 ? fieldTypeRaw : fieldTypeRaw.substring(fieldLastDotIndex + 1);
|
||||
|
||||
for(int j = 0; j < tabs; ++j)
|
||||
line += "\t";
|
||||
|
||||
line += current.getName() + "=";
|
||||
|
||||
switch(fieldType)
|
||||
{
|
||||
case "int":
|
||||
line += "" + current.get(item);
|
||||
break;
|
||||
|
||||
case "string":
|
||||
String content = (String)current.get(item);
|
||||
|
||||
if(content == null)
|
||||
line += "nil";
|
||||
else if(content.length() == 0)
|
||||
line += "\"\"";
|
||||
else
|
||||
line += "\"" + content + "\"";
|
||||
|
||||
break;
|
||||
|
||||
case "dewmaptile ":
|
||||
line += "\"" + current.get(item).toString() + "\"";
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.print("Found a type we couldn't parse: Raw - " + fieldTypeRaw + " | Parsed - " + fieldType + "\n");
|
||||
break;
|
||||
}
|
||||
|
||||
// If we're not the last line, add a comma.
|
||||
if(i != fields.length - 1)
|
||||
{
|
||||
line += ",";
|
||||
}
|
||||
|
||||
out += line;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
}
|
||||
|
||||
out += "\n}\n";
|
||||
return out;
|
||||
}
|
||||
|
||||
public static <T> IDewLuaSerializable<T> fromLua(String item)
|
||||
{
|
||||
for(Field field : item.getClass().getDeclaredFields())
|
||||
{
|
||||
@@ -20,5 +91,7 @@ public class DewLuaCore implements IDewLuaCore
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user