Update to 1.18
Rewrite command builders Usercontent api v2
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
package exopandora.worldhandler.usercontent;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import exopandora.worldhandler.builder.impl.BuilderUsercontent;
|
||||
import exopandora.worldhandler.builder.argument.IDeserializableArgument;
|
||||
import exopandora.worldhandler.builder.impl.UsercontentCommandBuilder;
|
||||
import net.minecraft.Util;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.chat.ChatType;
|
||||
@@ -15,9 +15,9 @@ import net.minecraft.network.chat.TextComponent;
|
||||
public class UsercontentAPI
|
||||
{
|
||||
private final Map<String, String> values = new HashMap<String, String>();
|
||||
private final List<BuilderUsercontent> builders;
|
||||
private final Map<String, UsercontentCommandBuilder> builders;
|
||||
|
||||
public UsercontentAPI(List<BuilderUsercontent> builders)
|
||||
public UsercontentAPI(Map<String, UsercontentCommandBuilder> builders)
|
||||
{
|
||||
this.builders = builders;
|
||||
}
|
||||
@@ -28,33 +28,47 @@ public class UsercontentAPI
|
||||
return this.values.get(id);
|
||||
}
|
||||
|
||||
public void updateValue(String id, String value)
|
||||
public void setValue(String id, String value)
|
||||
{
|
||||
this.values.put(id, value);
|
||||
}
|
||||
|
||||
public void addChatMessage(Object object)
|
||||
{
|
||||
if(object != null)
|
||||
{
|
||||
Minecraft.getInstance().gui.handleChat(ChatType.CHAT, new TextComponent(object.toString()), Util.NIL_UUID);
|
||||
}
|
||||
Minecraft.getInstance().gui.handleChat(ChatType.CHAT, new TextComponent(object != null ? object.toString() : "null"), Util.NIL_UUID);
|
||||
}
|
||||
|
||||
public void setCommandArgument(int command, int index, String object)
|
||||
public void setArgument(String command, String argument, String value)
|
||||
{
|
||||
if(command < this.builders.size() && command >= 0)
|
||||
UsercontentCommandBuilder builder = this.builders.get(command);
|
||||
|
||||
if(builder != null)
|
||||
{
|
||||
this.builders.get(command).set(index, object);
|
||||
builder.setArgument(argument, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getCommandArgument(int command, int index)
|
||||
private IDeserializableArgument getArgumentInternal(String command, String argument)
|
||||
{
|
||||
if(command < this.builders.size() && command >= 0)
|
||||
UsercontentCommandBuilder builder = this.builders.get(command);
|
||||
|
||||
if(builder != null)
|
||||
{
|
||||
return this.builders.get(command).get(index);
|
||||
return builder.getArgument(argument);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getArgument(String command, String argument)
|
||||
{
|
||||
IDeserializableArgument result = this.getArgumentInternal(command, argument);
|
||||
|
||||
if(result != null)
|
||||
{
|
||||
return result.serialize();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -24,9 +24,9 @@ import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import exopandora.worldhandler.WorldHandler;
|
||||
import exopandora.worldhandler.builder.types.ArgumentType;
|
||||
import exopandora.worldhandler.gui.widget.button.EnumIcon;
|
||||
import exopandora.worldhandler.usercontent.model.Action;
|
||||
import exopandora.worldhandler.usercontent.model.ArgumentType;
|
||||
import exopandora.worldhandler.usercontent.model.BooleanExpression;
|
||||
import exopandora.worldhandler.usercontent.model.JsonMenu;
|
||||
import exopandora.worldhandler.usercontent.model.JsonUsercontent;
|
||||
@@ -173,7 +173,7 @@ public class UsercontentLoader
|
||||
@Override
|
||||
public void write(JsonWriter writer, T value) throws IOException
|
||||
{
|
||||
writer.value(value.name().toLowerCase());
|
||||
writer.value(value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,7 +23,7 @@ public class VisibleObject<T>
|
||||
return true;
|
||||
}
|
||||
|
||||
public T getObject()
|
||||
public T get()
|
||||
{
|
||||
return this.object;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.util.function.Supplier;
|
||||
|
||||
import exopandora.worldhandler.WorldHandler;
|
||||
import exopandora.worldhandler.gui.content.Content;
|
||||
import exopandora.worldhandler.gui.menu.impl.ILogicMapped;
|
||||
import exopandora.worldhandler.gui.widget.menu.impl.ILogicMapped;
|
||||
import exopandora.worldhandler.usercontent.UsercontentAPI;
|
||||
import exopandora.worldhandler.usercontent.model.AbstractJsonWidget;
|
||||
import exopandora.worldhandler.usercontent.model.JsonItem;
|
||||
@@ -73,7 +73,7 @@ public abstract class AbstractWidgetFactory
|
||||
{
|
||||
try
|
||||
{
|
||||
this.api.updateValue(this.widget.getAttributes().getId(), item.getId());
|
||||
this.api.setValue(this.widget.getAttributes().getId(), item.getId());
|
||||
ActionHandler action = this.actionHandlerFactory.createActionHandler(this.content, this.widget.getAction(), this.player, item.getId());
|
||||
|
||||
if(action != null)
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package exopandora.worldhandler.usercontent.factory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import exopandora.worldhandler.builder.impl.BuilderGeneric;
|
||||
import exopandora.worldhandler.builder.impl.BuilderUsercontent;
|
||||
import exopandora.worldhandler.builder.impl.LiteralCommandBuilder;
|
||||
import exopandora.worldhandler.builder.impl.UsercontentCommandBuilder;
|
||||
import exopandora.worldhandler.gui.content.Content;
|
||||
import exopandora.worldhandler.usercontent.ScriptEngineAdapter;
|
||||
import exopandora.worldhandler.usercontent.UsercontentAPI;
|
||||
@@ -21,10 +21,10 @@ import exopandora.worldhandler.util.CommandHelper;
|
||||
public class ActionHandlerFactory
|
||||
{
|
||||
private final UsercontentAPI api;
|
||||
private final List<VisibleObject<BuilderUsercontent>> builders;
|
||||
private final Map<String, VisibleObject<UsercontentCommandBuilder>> builders;
|
||||
private final ScriptEngineAdapter engine;
|
||||
|
||||
public ActionHandlerFactory(UsercontentAPI api, List<VisibleObject<BuilderUsercontent>> builders, ScriptEngineAdapter engine)
|
||||
public ActionHandlerFactory(UsercontentAPI api, Map<String, VisibleObject<UsercontentCommandBuilder>> builders, ScriptEngineAdapter engine)
|
||||
{
|
||||
this.api = api;
|
||||
this.builders = builders;
|
||||
@@ -57,11 +57,11 @@ public class ActionHandlerFactory
|
||||
{
|
||||
return () ->
|
||||
{
|
||||
VisibleObject<BuilderUsercontent> visObj = this.builders.get(action.getAttributes().getCommand());
|
||||
VisibleObject<UsercontentCommandBuilder> visObj = this.builders.get(action.getAttributes().getCommand());
|
||||
|
||||
if(visObj != null && visObj.getObject() != null)
|
||||
if(visObj != null && visObj.get() != null)
|
||||
{
|
||||
visObj.getObject().set(action.getAttributes().getIndex(), value != null ? value : action.getAttributes().getValue());
|
||||
visObj.get().setArgument(action.getAttributes().getArgument(), value != null ? value : action.getAttributes().getValue());
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -72,11 +72,20 @@ public class ActionHandlerFactory
|
||||
{
|
||||
return () ->
|
||||
{
|
||||
if(action.getAttributes().getValue() == null)
|
||||
if(action.getAttributes().getValue() != null && !action.getAttributes().getValue().isEmpty() && action.getAttributes().getCommand() != null)
|
||||
{
|
||||
CommandHelper.sendCommand(player.get(), this.builders.get(action.getAttributes().getCommand()).getObject());
|
||||
CommandHelper.sendCommand(player.get(), this.builders.get(action.getAttributes().getCommand()).get(), action.getAttributes().getValue());
|
||||
}
|
||||
else if(!action.getAttributes().getValue().isEmpty())
|
||||
};
|
||||
}
|
||||
}
|
||||
else if(Action.Type.RUN_STRING.equals(action.getType()))
|
||||
{
|
||||
if(action.getAttributes() != null)
|
||||
{
|
||||
return () ->
|
||||
{
|
||||
if(action.getAttributes().getValue() != null && !action.getAttributes().getValue().isEmpty())
|
||||
{
|
||||
String command = action.getAttributes().getValue();
|
||||
|
||||
@@ -85,7 +94,7 @@ public class ActionHandlerFactory
|
||||
command = command.substring(1);
|
||||
}
|
||||
|
||||
CommandHelper.sendCommand(player.get(), new BuilderGeneric(command));
|
||||
CommandHelper.sendCommand(player.get(), new LiteralCommandBuilder(command), LiteralCommandBuilder.Label.ROOT);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -118,8 +127,8 @@ public class ActionHandlerFactory
|
||||
return string ->
|
||||
{
|
||||
String value = toStringMapper.apply(string);
|
||||
this.api.updateValue(id, value);
|
||||
this.builders.get(action.getAttributes().getCommand()).getObject().set(action.getAttributes().getIndex(), value);
|
||||
this.api.setValue(id, value);
|
||||
this.builders.get(action.getAttributes().getCommand()).get().setArgument(action.getAttributes().getArgument(), value);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -130,12 +139,12 @@ public class ActionHandlerFactory
|
||||
return string ->
|
||||
{
|
||||
String value = toStringMapper.apply(string);
|
||||
this.api.updateValue(id, value);
|
||||
this.api.setValue(id, value);
|
||||
this.engine.invokeFunction(action.getAttributes().getFunction(), value);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return string -> this.api.updateValue(id, toStringMapper.apply(string));
|
||||
return string -> this.api.setValue(id, toStringMapper.apply(string));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import javax.annotation.Nullable;
|
||||
|
||||
import exopandora.worldhandler.gui.container.Container;
|
||||
import exopandora.worldhandler.gui.content.Content;
|
||||
import exopandora.worldhandler.gui.menu.Menu;
|
||||
import exopandora.worldhandler.gui.menu.impl.ILogicPageList;
|
||||
import exopandora.worldhandler.gui.menu.impl.MenuPageList;
|
||||
import exopandora.worldhandler.gui.widget.button.GuiButtonBase;
|
||||
import exopandora.worldhandler.gui.widget.button.GuiButtonTooltip;
|
||||
import exopandora.worldhandler.gui.widget.menu.Menu;
|
||||
import exopandora.worldhandler.gui.widget.menu.impl.ILogicPageList;
|
||||
import exopandora.worldhandler.gui.widget.menu.impl.MenuPageList;
|
||||
import exopandora.worldhandler.usercontent.UsercontentAPI;
|
||||
import exopandora.worldhandler.usercontent.model.AbstractJsonWidget;
|
||||
import exopandora.worldhandler.usercontent.model.JsonItem;
|
||||
|
||||
@@ -68,6 +68,13 @@ public class Action
|
||||
throw new IllegalStateException("action.attributes is null");
|
||||
}
|
||||
}
|
||||
else if(this.type == Type.RUN_STRING)
|
||||
{
|
||||
if(this.getAttributes() == null)
|
||||
{
|
||||
throw new IllegalStateException("action.attributes is null");
|
||||
}
|
||||
}
|
||||
else if(this.type == Type.JS)
|
||||
{
|
||||
if(this.getAttributes() == null)
|
||||
@@ -86,6 +93,7 @@ public class Action
|
||||
OPEN,
|
||||
SET,
|
||||
RUN,
|
||||
RUN_STRING,
|
||||
BACK,
|
||||
BACK_TO_GAME,
|
||||
JS;
|
||||
|
||||
@@ -8,19 +8,19 @@ public class ActionAttributes
|
||||
private String function;
|
||||
|
||||
@SerializedName("command")
|
||||
private int command;
|
||||
private String command;
|
||||
|
||||
@SerializedName("index")
|
||||
private int index;
|
||||
@SerializedName("argument")
|
||||
private String argument;
|
||||
|
||||
@SerializedName("value")
|
||||
private String value;
|
||||
|
||||
public ActionAttributes(String function, int command, int index, String value)
|
||||
public ActionAttributes(String function, String command, String argument, String value)
|
||||
{
|
||||
this.function = function;
|
||||
this.command = command;
|
||||
this.index = index;
|
||||
this.argument = argument;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@@ -34,24 +34,24 @@ public class ActionAttributes
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
public int getCommand()
|
||||
public String getCommand()
|
||||
{
|
||||
return this.command;
|
||||
}
|
||||
|
||||
public void setCommand(int command)
|
||||
public void setCommand(String command)
|
||||
{
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
public int getIndex()
|
||||
public String getArgument()
|
||||
{
|
||||
return this.index;
|
||||
return this.argument;
|
||||
}
|
||||
|
||||
public void setIndex(int index)
|
||||
public void setArgument(String argument)
|
||||
{
|
||||
this.index = index;
|
||||
this.argument = argument;
|
||||
}
|
||||
|
||||
public String getValue()
|
||||
|
||||
@@ -1,45 +1,41 @@
|
||||
package exopandora.worldhandler.usercontent.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import exopandora.worldhandler.builder.CommandSyntax.Argument;
|
||||
|
||||
public class JsonCommand
|
||||
{
|
||||
@SerializedName("name")
|
||||
private String name;
|
||||
@SerializedName("label")
|
||||
private String label;
|
||||
|
||||
@SerializedName("syntax")
|
||||
private List<Argument> syntax = null;
|
||||
private JsonArgument syntax = null;
|
||||
|
||||
@SerializedName("visible")
|
||||
private BooleanExpression visible;
|
||||
|
||||
public JsonCommand(String name, List<Argument> syntax, BooleanExpression visible)
|
||||
public JsonCommand(String label, JsonArgument syntax, BooleanExpression visible)
|
||||
{
|
||||
this.name = name;
|
||||
this.label = label;
|
||||
this.syntax = syntax;
|
||||
this.visible = visible;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
public String getLabel()
|
||||
{
|
||||
return this.name;
|
||||
return this.label;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
public void setLabel(String label)
|
||||
{
|
||||
this.name = name;
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public List<Argument> getSyntax()
|
||||
public JsonArgument getSyntax()
|
||||
{
|
||||
return this.syntax;
|
||||
}
|
||||
|
||||
public void setSyntax(List<Argument> syntax)
|
||||
public void setSyntax(JsonArgument syntax)
|
||||
{
|
||||
this.syntax = syntax;
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package exopandora.worldhandler.usercontent.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public class JsonModel
|
||||
{
|
||||
@SerializedName("commands")
|
||||
private List<JsonCommand> commands = null;
|
||||
|
||||
public JsonModel(List<JsonCommand> commands)
|
||||
{
|
||||
this.commands = commands;
|
||||
}
|
||||
|
||||
public List<JsonCommand> getCommands()
|
||||
{
|
||||
return this.commands;
|
||||
}
|
||||
|
||||
public void setCommands(List<JsonCommand> commands)
|
||||
{
|
||||
this.commands = commands;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,31 @@
|
||||
package exopandora.worldhandler.usercontent.model;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public class JsonUsercontent
|
||||
{
|
||||
@SerializedName("model")
|
||||
private JsonModel model;
|
||||
@SerializedName("commands")
|
||||
private Map<String, JsonCommand> commands = null;
|
||||
|
||||
@SerializedName("gui")
|
||||
private JsonGui gui;
|
||||
|
||||
public JsonUsercontent(JsonModel model, JsonGui gui)
|
||||
public JsonUsercontent(Map<String, JsonCommand> commands, JsonGui gui)
|
||||
{
|
||||
this.model = model;
|
||||
this.commands = commands;
|
||||
this.gui = gui;
|
||||
}
|
||||
|
||||
public JsonModel getModel()
|
||||
public Map<String, JsonCommand> getCommands()
|
||||
{
|
||||
return this.model;
|
||||
return this.commands;
|
||||
}
|
||||
|
||||
public void setModel(JsonModel model)
|
||||
public void setCommands(Map<String, JsonCommand> commands)
|
||||
{
|
||||
this.model = model;
|
||||
this.commands = commands;
|
||||
}
|
||||
|
||||
public JsonGui getGui()
|
||||
|
||||
@@ -75,7 +75,7 @@ public class JsonWidget extends AbstractJsonWidget<Type>
|
||||
throw new IllegalStateException("widget.attributes.item is null");
|
||||
}
|
||||
|
||||
this.validateAction(Action.Type.OPEN, Action.Type.SET, Action.Type.RUN, Action.Type.BACK, Action.Type.BACK_TO_GAME, Action.Type.JS);
|
||||
this.validateAction(Action.Type.OPEN, Action.Type.SET, Action.Type.RUN, Action.Type.RUN_STRING, Action.Type.BACK, Action.Type.BACK_TO_GAME, Action.Type.JS);
|
||||
}
|
||||
else if(this.type == Type.ICON_BUTTON)
|
||||
{
|
||||
@@ -88,7 +88,7 @@ public class JsonWidget extends AbstractJsonWidget<Type>
|
||||
throw new IllegalStateException("widget.attributes.icon is null");
|
||||
}
|
||||
|
||||
this.validateAction(Action.Type.OPEN, Action.Type.SET, Action.Type.RUN, Action.Type.BACK, Action.Type.BACK_TO_GAME, Action.Type.JS);
|
||||
this.validateAction(Action.Type.OPEN, Action.Type.SET, Action.Type.RUN, Action.Type.RUN_STRING, Action.Type.BACK, Action.Type.BACK_TO_GAME, Action.Type.JS);
|
||||
}
|
||||
else if(this.type == Type.LIST_BUTTON)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user