From 4b8b125d05c0532c00fdd7bf4fcd1297407e67f3 Mon Sep 17 00:00:00 2001 From: Matthew Cech Date: Wed, 11 Sep 2019 23:10:36 -0700 Subject: [PATCH] + Added lua writing functionality --- src/commands/dew/CommandDewStart.java | 14 +++- src/commands/dew/core/api/IDewEntity.java | 6 ++ src/commands/dew/core/api/IDewMessage.java | 19 +++++ src/commands/dew/core/api/IDewPlayer.java | 3 +- src/commands/dew/core/data/DewMap.java | 2 +- src/commands/dew/core/data/DewMapTile.java | 7 +- .../dew/core/data/DewMovementInfo.java | 11 +++ src/commands/dew/core/impl/DewLuaCore.java | 75 ++++++++++++++++++- 8 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 src/commands/dew/core/api/IDewEntity.java create mode 100644 src/commands/dew/core/api/IDewMessage.java create mode 100644 src/commands/dew/core/data/DewMovementInfo.java diff --git a/src/commands/dew/CommandDewStart.java b/src/commands/dew/CommandDewStart.java index 340037e..f7d7f4a 100644 --- a/src/commands/dew/CommandDewStart.java +++ b/src/commands/dew/CommandDewStart.java @@ -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); } } diff --git a/src/commands/dew/core/api/IDewEntity.java b/src/commands/dew/core/api/IDewEntity.java new file mode 100644 index 0000000..24f89f0 --- /dev/null +++ b/src/commands/dew/core/api/IDewEntity.java @@ -0,0 +1,6 @@ +package commands.dew.core.api; + +public interface IDewEntity +{ + public String getUniqueID(); +} diff --git a/src/commands/dew/core/api/IDewMessage.java b/src/commands/dew/core/api/IDewMessage.java new file mode 100644 index 0000000..b6ac800 --- /dev/null +++ b/src/commands/dew/core/api/IDewMessage.java @@ -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 +{ + public String getMessageName(); + public T getData(); +} diff --git a/src/commands/dew/core/api/IDewPlayer.java b/src/commands/dew/core/api/IDewPlayer.java index 3fab60f..297cad1 100644 --- a/src/commands/dew/core/api/IDewPlayer.java +++ b/src/commands/dew/core/api/IDewPlayer.java @@ -1,7 +1,6 @@ package commands.dew.core.api; -public interface IDewPlayer +public interface IDewPlayer extends IDewEntity { public String getName(); - public String getUniqueID(); } diff --git a/src/commands/dew/core/data/DewMap.java b/src/commands/dew/core/data/DewMap.java index de139b0..57e43a9 100644 --- a/src/commands/dew/core/data/DewMap.java +++ b/src/commands/dew/core/data/DewMap.java @@ -6,5 +6,5 @@ public final class DewMap implements IDewLuaSerializable { public int mapWidth = 20; public int mapHeight = 20; - public DewMapTile[][] map; + public int[][] map; } diff --git a/src/commands/dew/core/data/DewMapTile.java b/src/commands/dew/core/data/DewMapTile.java index 1330bb4..ea302c1 100644 --- a/src/commands/dew/core/data/DewMapTile.java +++ b/src/commands/dew/core/data/DewMapTile.java @@ -1,8 +1,11 @@ package commands.dew.core.data; -public enum DewMapTile +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) diff --git a/src/commands/dew/core/data/DewMovementInfo.java b/src/commands/dew/core/data/DewMovementInfo.java new file mode 100644 index 0000000..e215199 --- /dev/null +++ b/src/commands/dew/core/data/DewMovementInfo.java @@ -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 +{ + public String entityID; + public int x; + public int y; +} diff --git a/src/commands/dew/core/impl/DewLuaCore.java b/src/commands/dew/core/impl/DewLuaCore.java index 26856c9..f5f3769 100644 --- a/src/commands/dew/core/impl/DewLuaCore.java +++ b/src/commands/dew/core/impl/DewLuaCore.java @@ -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 IDewLuaSerializable fromLua(String item) { for(Field field : item.getClass().getDeclaredFields()) { @@ -20,5 +91,7 @@ public class DewLuaCore implements IDewLuaCore e.printStackTrace(); } } + + return null; } }