Update json api (now version 1.1)
This commit is contained in:
@@ -77,11 +77,11 @@
|
|||||||
"category_index": 3,
|
"category_index": 3,
|
||||||
"active_content": "example"
|
"active_content": "example"
|
||||||
},
|
},
|
||||||
"buttons": [
|
"widgets": [
|
||||||
{
|
{
|
||||||
"text": "Button",
|
"text": "Button",
|
||||||
"type": "button",
|
"type": "button",
|
||||||
"dimensions": {
|
"layout": {
|
||||||
"x": 0,
|
"x": 0,
|
||||||
"y": 0,
|
"y": 0,
|
||||||
"width": 114,
|
"width": 114,
|
||||||
@@ -114,7 +114,7 @@
|
|||||||
"function": "updateText"
|
"function": "updateText"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dimensions": {
|
"layout": {
|
||||||
"x": 0,
|
"x": 0,
|
||||||
"y": 48,
|
"y": 48,
|
||||||
"width": 114,
|
"width": 114,
|
||||||
@@ -140,7 +140,7 @@
|
|||||||
"value": "/give @p minecraft:diamond_sword 1"
|
"value": "/give @p minecraft:diamond_sword 1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dimensions": {
|
"layout": {
|
||||||
"x": 0,
|
"x": 0,
|
||||||
"y": 24,
|
"y": 24,
|
||||||
"width": 20,
|
"width": 20,
|
||||||
@@ -158,7 +158,7 @@
|
|||||||
"value": "main"
|
"value": "main"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dimensions": {
|
"layout": {
|
||||||
"x": 24,
|
"x": 24,
|
||||||
"y": 24,
|
"y": 24,
|
||||||
"width": 20,
|
"width": 20,
|
||||||
@@ -178,7 +178,7 @@
|
|||||||
"value": "settings"
|
"value": "settings"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dimensions": {
|
"layout": {
|
||||||
"x": 48,
|
"x": 48,
|
||||||
"y": 24,
|
"y": 24,
|
||||||
"width": 66,
|
"width": 66,
|
||||||
@@ -197,7 +197,7 @@
|
|||||||
"index": 0
|
"index": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dimensions": {
|
"layout": {
|
||||||
"x": 118,
|
"x": 118,
|
||||||
"y": 72,
|
"y": 72,
|
||||||
"width": 114,
|
"width": 114,
|
||||||
@@ -234,7 +234,7 @@
|
|||||||
"index": 2
|
"index": 2
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dimensions": {
|
"layout": {
|
||||||
"x": 0,
|
"x": 0,
|
||||||
"y": 96,
|
"y": 96,
|
||||||
"width": 114,
|
"width": 114,
|
||||||
@@ -256,7 +256,7 @@
|
|||||||
"function": "toggleVisibility"
|
"function": "toggleVisibility"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dimensions": {
|
"layout": {
|
||||||
"x": 0,
|
"x": 0,
|
||||||
"y": 72,
|
"y": 72,
|
||||||
"width": 56,
|
"width": 56,
|
||||||
@@ -275,7 +275,7 @@
|
|||||||
"function": "toggleEnabled"
|
"function": "toggleEnabled"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dimensions": {
|
"layout": {
|
||||||
"x": 58,
|
"x": 58,
|
||||||
"y": 72,
|
"y": 72,
|
||||||
"width": 56,
|
"width": 56,
|
||||||
@@ -291,7 +291,7 @@
|
|||||||
"action": {
|
"action": {
|
||||||
"type": "back_to_game"
|
"type": "back_to_game"
|
||||||
},
|
},
|
||||||
"dimensions": {
|
"layout": {
|
||||||
"x": 118,
|
"x": 118,
|
||||||
"y": 96,
|
"y": 96,
|
||||||
"width": 114,
|
"width": 114,
|
||||||
@@ -302,7 +302,7 @@
|
|||||||
"menus": [
|
"menus": [
|
||||||
{
|
{
|
||||||
"type": "page_list",
|
"type": "page_list",
|
||||||
"dimensions": {
|
"layout": {
|
||||||
"x": 118,
|
"x": 118,
|
||||||
"y": 0,
|
"y": 0,
|
||||||
"width": 114,
|
"width": 114,
|
||||||
@@ -339,7 +339,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"texts": [
|
"labels": [
|
||||||
{
|
{
|
||||||
"text": "Text",
|
"text": "Text",
|
||||||
"x": 0,
|
"x": 0,
|
||||||
|
|||||||
@@ -25,15 +25,15 @@ import exopandora.worldhandler.usercontent.UsercontentConfig;
|
|||||||
import exopandora.worldhandler.usercontent.VisibleActiveObject;
|
import exopandora.worldhandler.usercontent.VisibleActiveObject;
|
||||||
import exopandora.worldhandler.usercontent.VisibleObject;
|
import exopandora.worldhandler.usercontent.VisibleObject;
|
||||||
import exopandora.worldhandler.usercontent.factory.ActionHandlerFactory;
|
import exopandora.worldhandler.usercontent.factory.ActionHandlerFactory;
|
||||||
import exopandora.worldhandler.usercontent.factory.ButtonFactory;
|
import exopandora.worldhandler.usercontent.factory.WidgetFactory;
|
||||||
import exopandora.worldhandler.usercontent.factory.MenuFactory;
|
import exopandora.worldhandler.usercontent.factory.MenuFactory;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonButton;
|
import exopandora.worldhandler.usercontent.model.JsonWidget;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonCommand;
|
import exopandora.worldhandler.usercontent.model.JsonCommand;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonMenu;
|
import exopandora.worldhandler.usercontent.model.JsonMenu;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonModel;
|
import exopandora.worldhandler.usercontent.model.JsonModel;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonText;
|
import exopandora.worldhandler.usercontent.model.JsonLabel;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonUsercontent;
|
import exopandora.worldhandler.usercontent.model.JsonUsercontent;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonWidget;
|
import exopandora.worldhandler.usercontent.model.AbstractJsonWidget;
|
||||||
import exopandora.worldhandler.util.TextUtils;
|
import exopandora.worldhandler.util.TextUtils;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.gui.widget.TextFieldWidget;
|
import net.minecraft.client.gui.widget.TextFieldWidget;
|
||||||
@@ -57,7 +57,7 @@ public class ContentUsercontent extends Content
|
|||||||
private final Map<String, VisibleActiveObject<TextFieldWidget>> textfields = new HashMap<String, VisibleActiveObject<TextFieldWidget>>();
|
private final Map<String, VisibleActiveObject<TextFieldWidget>> textfields = new HashMap<String, VisibleActiveObject<TextFieldWidget>>();
|
||||||
private final List<VisibleActiveObject<Widget>> buttons = new ArrayList<VisibleActiveObject<Widget>>();
|
private final List<VisibleActiveObject<Widget>> buttons = new ArrayList<VisibleActiveObject<Widget>>();
|
||||||
private final UsercontentAPI api;
|
private final UsercontentAPI api;
|
||||||
private final ButtonFactory buttonFactory;
|
private final WidgetFactory buttonFactory;
|
||||||
private final MenuFactory menuFactory;
|
private final MenuFactory menuFactory;
|
||||||
|
|
||||||
public ContentUsercontent(UsercontentConfig config) throws Exception
|
public ContentUsercontent(UsercontentConfig config) throws Exception
|
||||||
@@ -68,7 +68,7 @@ public class ContentUsercontent extends Content
|
|||||||
this.builders = this.createBuilders(this.content.getModel());
|
this.builders = this.createBuilders(this.content.getModel());
|
||||||
this.api = new UsercontentAPI(this.builders.stream().map(VisibleObject::getObject).collect(Collectors.toList()));
|
this.api = new UsercontentAPI(this.builders.stream().map(VisibleObject::getObject).collect(Collectors.toList()));
|
||||||
ActionHandlerFactory actionHandlerFactory = new ActionHandlerFactory(this.api, this.builders, this.engineAdapter);
|
ActionHandlerFactory actionHandlerFactory = new ActionHandlerFactory(this.api, this.builders, this.engineAdapter);
|
||||||
this.buttonFactory = new ButtonFactory(this.api, actionHandlerFactory);
|
this.buttonFactory = new WidgetFactory(this.api, actionHandlerFactory);
|
||||||
this.menuFactory = new MenuFactory(this.api, actionHandlerFactory);
|
this.menuFactory = new MenuFactory(this.api, actionHandlerFactory);
|
||||||
this.engineAdapter.addObject("api", this.api);
|
this.engineAdapter.addObject("api", this.api);
|
||||||
this.engineAdapter.eval(config.getJs());
|
this.engineAdapter.eval(config.getJs());
|
||||||
@@ -91,22 +91,22 @@ public class ContentUsercontent extends Content
|
|||||||
this.textfields.clear();
|
this.textfields.clear();
|
||||||
this.buttons.clear();
|
this.buttons.clear();
|
||||||
|
|
||||||
for(JsonButton button : this.getWidgets(this.content.getGui().getButtons(), JsonWidget.Type.BUTTON))
|
for(JsonWidget json : this.getWidgets(this.content.getGui().getWidgets(), AbstractJsonWidget.Type.BUTTON))
|
||||||
{
|
{
|
||||||
Widget widget = this.buttonFactory.createButton(button, this, container, x, y);
|
Widget widget = this.buttonFactory.createWidget(json, this, container, x, y);
|
||||||
|
|
||||||
if(JsonButton.Type.TEXTFIELD.equals(button.getType()))
|
if(JsonWidget.Type.TEXTFIELD.equals(json.getType()))
|
||||||
{
|
{
|
||||||
VisibleActiveObject<TextFieldWidget> visObj = new VisibleActiveObject<TextFieldWidget>(button, (TextFieldWidget) widget);
|
VisibleActiveObject<TextFieldWidget> visObj = new VisibleActiveObject<TextFieldWidget>(json, (TextFieldWidget) widget);
|
||||||
this.textfields.put(button.getAttributes().getId(), visObj);
|
this.textfields.put(json.getAttributes().getId(), visObj);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.buttons.add(new VisibleActiveObject<Widget>(button, widget));
|
this.buttons.add(new VisibleActiveObject<Widget>(json, widget));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(JsonMenu menu : this.getWidgets(this.content.getGui().getMenus(), JsonWidget.Type.MENU))
|
for(JsonMenu menu : this.getWidgets(this.content.getGui().getMenus(), AbstractJsonWidget.Type.MENU))
|
||||||
{
|
{
|
||||||
container.add(this.menuFactory.createMenu(menu, this, container, x, y));
|
container.add(this.menuFactory.createMenu(menu, this, container, x, y));
|
||||||
}
|
}
|
||||||
@@ -147,13 +147,13 @@ public class ContentUsercontent extends Content
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this.content.getGui() != null && this.content.getGui().getTexts() != null)
|
if(this.content.getGui() != null && this.content.getGui().getLabels() != null)
|
||||||
{
|
{
|
||||||
for(JsonText text : this.content.getGui().getTexts())
|
for(JsonLabel label : this.content.getGui().getLabels())
|
||||||
{
|
{
|
||||||
if(text.getVisible() == null || text.getVisible().eval(this.engineAdapter))
|
if(label.getVisible() == null || label.getVisible().eval(this.engineAdapter))
|
||||||
{
|
{
|
||||||
container.getMinecraft().fontRenderer.func_243248_b(matrix, TextUtils.formatNonnull(text.getText()), text.getX() + x, text.getY() + y, text.getColor());
|
container.getMinecraft().fontRenderer.func_243248_b(matrix, TextUtils.formatNonnull(label.getText()), label.getX() + x, label.getY() + y, label.getColor());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -227,7 +227,7 @@ public class ContentUsercontent extends Content
|
|||||||
return builders;
|
return builders;
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends JsonWidget<?>> List<T> getWidgets(List<T> list, JsonWidget.Type type)
|
private <T extends AbstractJsonWidget<?>> List<T> getWidgets(List<T> list, AbstractJsonWidget.Type type)
|
||||||
{
|
{
|
||||||
List<T> result = new ArrayList<T>();
|
List<T> result = new ArrayList<T>();
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import exopandora.worldhandler.builder.types.ArgumentType;
|
|||||||
import exopandora.worldhandler.gui.widget.button.EnumIcon;
|
import exopandora.worldhandler.gui.widget.button.EnumIcon;
|
||||||
import exopandora.worldhandler.usercontent.model.Action;
|
import exopandora.worldhandler.usercontent.model.Action;
|
||||||
import exopandora.worldhandler.usercontent.model.BooleanExpression;
|
import exopandora.worldhandler.usercontent.model.BooleanExpression;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonButton;
|
import exopandora.worldhandler.usercontent.model.JsonWidget;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonMenu;
|
import exopandora.worldhandler.usercontent.model.JsonMenu;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonUsercontent;
|
import exopandora.worldhandler.usercontent.model.JsonUsercontent;
|
||||||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
||||||
@@ -63,7 +63,7 @@ public class UsercontentLoader
|
|||||||
.registerTypeAdapter(ArgumentType.class, new EnumTypeAdapter<ArgumentType>(ArgumentType.class))
|
.registerTypeAdapter(ArgumentType.class, new EnumTypeAdapter<ArgumentType>(ArgumentType.class))
|
||||||
.registerTypeAdapter(EnumIcon.class, new EnumTypeAdapter<EnumIcon>(EnumIcon.class))
|
.registerTypeAdapter(EnumIcon.class, new EnumTypeAdapter<EnumIcon>(EnumIcon.class))
|
||||||
.registerTypeAdapter(BooleanExpression.Type.class, new EnumTypeAdapter<BooleanExpression.Type>(BooleanExpression.Type.class))
|
.registerTypeAdapter(BooleanExpression.Type.class, new EnumTypeAdapter<BooleanExpression.Type>(BooleanExpression.Type.class))
|
||||||
.registerTypeAdapter(JsonButton.Type.class, new EnumTypeAdapter<JsonButton.Type>(JsonButton.Type.class))
|
.registerTypeAdapter(JsonWidget.Type.class, new EnumTypeAdapter<JsonWidget.Type>(JsonWidget.Type.class))
|
||||||
.registerTypeAdapter(Action.Type.class, new EnumTypeAdapter<Action.Type>(Action.Type.class))
|
.registerTypeAdapter(Action.Type.class, new EnumTypeAdapter<Action.Type>(Action.Type.class))
|
||||||
.registerTypeAdapter(JsonMenu.Type.class, new EnumTypeAdapter<JsonMenu.Type>(JsonMenu.Type.class))
|
.registerTypeAdapter(JsonMenu.Type.class, new EnumTypeAdapter<JsonMenu.Type>(JsonMenu.Type.class))
|
||||||
.create();
|
.create();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package exopandora.worldhandler.usercontent;
|
package exopandora.worldhandler.usercontent;
|
||||||
|
|
||||||
import exopandora.worldhandler.usercontent.model.BooleanExpression;
|
import exopandora.worldhandler.usercontent.model.BooleanExpression;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonButton;
|
import exopandora.worldhandler.usercontent.model.JsonWidget;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
|
|
||||||
@@ -16,9 +16,9 @@ public class VisibleActiveObject<T> extends VisibleObject<T>
|
|||||||
this.active = active;
|
this.active = active;
|
||||||
}
|
}
|
||||||
|
|
||||||
public VisibleActiveObject(JsonButton button, T object)
|
public VisibleActiveObject(JsonWidget widget, T object)
|
||||||
{
|
{
|
||||||
this(button.getAttributes() != null ? button.getAttributes().getVisible() : null, button.getAttributes() != null ? button.getAttributes().getEnabled() : null, object);
|
this(widget.getAttributes() != null ? widget.getAttributes().getVisible() : null, widget.getAttributes() != null ? widget.getAttributes().getEnabled() : null, object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEnabled(ScriptEngineAdapter engine)
|
public boolean isEnabled(ScriptEngineAdapter engine)
|
||||||
|
|||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package exopandora.worldhandler.usercontent.factory;
|
||||||
|
|
||||||
|
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.usercontent.UsercontentAPI;
|
||||||
|
import exopandora.worldhandler.usercontent.model.JsonItem;
|
||||||
|
import exopandora.worldhandler.usercontent.model.AbstractJsonWidget;
|
||||||
|
import exopandora.worldhandler.util.ActionHandler;
|
||||||
|
import net.minecraft.util.text.IFormattableTextComponent;
|
||||||
|
import net.minecraft.util.text.StringTextComponent;
|
||||||
|
import net.minecraft.util.text.TranslationTextComponent;
|
||||||
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
|
|
||||||
|
@OnlyIn(Dist.CLIENT)
|
||||||
|
public abstract class AbstractWidgetFactory
|
||||||
|
{
|
||||||
|
private final ActionHandlerFactory actionHandlerFactory;
|
||||||
|
private final UsercontentAPI api;
|
||||||
|
|
||||||
|
public AbstractWidgetFactory(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory)
|
||||||
|
{
|
||||||
|
this.api = api;
|
||||||
|
this.actionHandlerFactory = actionHandlerFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ActionHandlerFactory getActionHandlerFactory()
|
||||||
|
{
|
||||||
|
return this.actionHandlerFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UsercontentAPI getApi()
|
||||||
|
{
|
||||||
|
return this.api;
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnlyIn(Dist.CLIENT)
|
||||||
|
public static class UsercontentLogicMapped<T extends Enum<T>> implements ILogicMapped<JsonItem>
|
||||||
|
{
|
||||||
|
private final ActionHandlerFactory actionHandlerFactory;
|
||||||
|
private final UsercontentAPI api;
|
||||||
|
private final Content content;
|
||||||
|
private final AbstractJsonWidget<T> widget;
|
||||||
|
private final Supplier<String> player;
|
||||||
|
|
||||||
|
public UsercontentLogicMapped(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory, Content content, AbstractJsonWidget<T> widget, Supplier<String> player)
|
||||||
|
{
|
||||||
|
this.api = api;
|
||||||
|
this.actionHandlerFactory = actionHandlerFactory;
|
||||||
|
this.content = content;
|
||||||
|
this.widget = widget;
|
||||||
|
this.player = player;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IFormattableTextComponent translate(JsonItem item)
|
||||||
|
{
|
||||||
|
if(item.getTranslation() != null)
|
||||||
|
{
|
||||||
|
return new TranslationTextComponent(item.getTranslation());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new StringTextComponent(item.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IFormattableTextComponent toTooltip(JsonItem item)
|
||||||
|
{
|
||||||
|
return new StringTextComponent(item.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick(JsonItem item)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.api.updateValue(this.widget.getAttributes().getId(), item.getId());
|
||||||
|
ActionHandler action = this.actionHandlerFactory.createActionHandler(this.content, this.widget.getAction(), this.player, item.getId());
|
||||||
|
|
||||||
|
if(action != null)
|
||||||
|
{
|
||||||
|
action.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
WorldHandler.LOGGER.error("Error executing action for widget");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getId()
|
||||||
|
{
|
||||||
|
return this.widget.getAttributes().getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onInit(JsonItem item)
|
||||||
|
{
|
||||||
|
this.onClick(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,125 +0,0 @@
|
|||||||
package exopandora.worldhandler.usercontent.factory;
|
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
import com.google.common.base.Predicates;
|
|
||||||
|
|
||||||
import exopandora.worldhandler.gui.container.Container;
|
|
||||||
import exopandora.worldhandler.gui.content.Content;
|
|
||||||
import exopandora.worldhandler.gui.widget.button.GuiButtonIcon;
|
|
||||||
import exopandora.worldhandler.gui.widget.button.GuiButtonItem;
|
|
||||||
import exopandora.worldhandler.gui.widget.button.GuiButtonList;
|
|
||||||
import exopandora.worldhandler.gui.widget.button.GuiButtonTooltip;
|
|
||||||
import exopandora.worldhandler.gui.widget.button.GuiSlider;
|
|
||||||
import exopandora.worldhandler.gui.widget.button.GuiTextFieldTooltip;
|
|
||||||
import exopandora.worldhandler.gui.widget.button.LogicSliderSimple;
|
|
||||||
import exopandora.worldhandler.usercontent.UsercontentAPI;
|
|
||||||
import exopandora.worldhandler.usercontent.model.JsonButton;
|
|
||||||
import exopandora.worldhandler.usercontent.model.JsonItem;
|
|
||||||
import exopandora.worldhandler.util.TextUtils;
|
|
||||||
import net.minecraft.client.gui.widget.Widget;
|
|
||||||
import net.minecraft.util.ResourceLocation;
|
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
||||||
import net.minecraftforge.registries.ForgeRegistries;
|
|
||||||
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
|
||||||
public class ButtonFactory extends WidgetFactory
|
|
||||||
{
|
|
||||||
public ButtonFactory(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory)
|
|
||||||
{
|
|
||||||
super(api, actionHandlerFactory);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public Widget createButton(JsonButton button, Content content, Container container, int x, int y)
|
|
||||||
{
|
|
||||||
if(JsonButton.Type.BUTTON.equals(button.getType()))
|
|
||||||
{
|
|
||||||
return new GuiButtonTooltip
|
|
||||||
(
|
|
||||||
button.getDimensions().getX() + x,
|
|
||||||
button.getDimensions().getY() + y,
|
|
||||||
button.getDimensions().getWidth(),
|
|
||||||
button.getDimensions().getHeight(),
|
|
||||||
TextUtils.formatNonnull(button.getText()),
|
|
||||||
TextUtils.formatNonnull(button.getAttributes() != null ? button.getAttributes().getTooltip() : null),
|
|
||||||
this.getActionHandlerFactory().createActionHandler(content, button.getAction(), container::getPlayer)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else if(JsonButton.Type.ITEM_BUTTON.equals(button.getType()))
|
|
||||||
{
|
|
||||||
return new GuiButtonItem
|
|
||||||
(
|
|
||||||
button.getDimensions().getX() + x,
|
|
||||||
button.getDimensions().getY() + y,
|
|
||||||
button.getDimensions().getWidth(),
|
|
||||||
button.getDimensions().getHeight(),
|
|
||||||
ForgeRegistries.ITEMS.getValue(new ResourceLocation(button.getAttributes().getItem())),
|
|
||||||
this.getActionHandlerFactory().createActionHandler(content, button.getAction(), container::getPlayer)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else if(JsonButton.Type.ICON_BUTTON.equals(button.getType()))
|
|
||||||
{
|
|
||||||
return new GuiButtonIcon
|
|
||||||
(
|
|
||||||
button.getDimensions().getX() + x,
|
|
||||||
button.getDimensions().getY() + y,
|
|
||||||
button.getDimensions().getWidth(),
|
|
||||||
button.getDimensions().getHeight(),
|
|
||||||
button.getAttributes().getIcon(),
|
|
||||||
TextUtils.formatNonnull(button.getAttributes().getTooltip()),
|
|
||||||
this.getActionHandlerFactory().createActionHandler(content, button.getAction(), container::getPlayer)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else if(JsonButton.Type.LIST_BUTTON.equals(button.getType()))
|
|
||||||
{
|
|
||||||
return new GuiButtonList<JsonItem>
|
|
||||||
(
|
|
||||||
button.getDimensions().getX() + x,
|
|
||||||
button.getDimensions().getY() + y,
|
|
||||||
button.getAttributes().getItems(),
|
|
||||||
button.getDimensions().getWidth(),
|
|
||||||
button.getDimensions().getHeight(),
|
|
||||||
container,
|
|
||||||
new UsercontentLogicMapped<JsonButton.Type>(this.getApi(), this.getActionHandlerFactory(), content, button, container::getPlayer)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else if(JsonButton.Type.SLIDER.equals(button.getType()))
|
|
||||||
{
|
|
||||||
Consumer<Integer> responder = this.getActionHandlerFactory().createResponder(integer -> integer.toString(), button.getAttributes().getId(), button.getAction());
|
|
||||||
return new GuiSlider
|
|
||||||
(
|
|
||||||
button.getDimensions().getX() + x,
|
|
||||||
button.getDimensions().getY() + y,
|
|
||||||
button.getDimensions().getWidth(),
|
|
||||||
button.getDimensions().getHeight(),
|
|
||||||
button.getAttributes().getMin(),
|
|
||||||
button.getAttributes().getMax(),
|
|
||||||
button.getAttributes().getStart(),
|
|
||||||
container,
|
|
||||||
new LogicSliderSimple(button.getAttributes().getId(), TextUtils.formatNonnull(button.getText()), responder)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else if(JsonButton.Type.TEXTFIELD.equals(button.getType()))
|
|
||||||
{
|
|
||||||
GuiTextFieldTooltip textfield = new GuiTextFieldTooltip
|
|
||||||
(
|
|
||||||
button.getDimensions().getX() + x,
|
|
||||||
button.getDimensions().getY() + y,
|
|
||||||
button.getDimensions().getWidth(),
|
|
||||||
button.getDimensions().getHeight(),
|
|
||||||
TextUtils.formatNonnull(button.getText())
|
|
||||||
);
|
|
||||||
textfield.setValidator(Predicates.notNull());
|
|
||||||
textfield.setText(this.getApi().getValue(button.getAttributes().getId()));
|
|
||||||
textfield.setResponder(this.getActionHandlerFactory().createResponder(string -> textfield.getText(), button.getAttributes().getId(), button.getAction()));
|
|
||||||
|
|
||||||
return textfield;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -14,14 +14,14 @@ import exopandora.worldhandler.gui.widget.button.GuiButtonTooltip;
|
|||||||
import exopandora.worldhandler.usercontent.UsercontentAPI;
|
import exopandora.worldhandler.usercontent.UsercontentAPI;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonItem;
|
import exopandora.worldhandler.usercontent.model.JsonItem;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonMenu;
|
import exopandora.worldhandler.usercontent.model.JsonMenu;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonWidget;
|
import exopandora.worldhandler.usercontent.model.AbstractJsonWidget;
|
||||||
import exopandora.worldhandler.util.ActionHandler;
|
import exopandora.worldhandler.util.ActionHandler;
|
||||||
import net.minecraft.util.text.IFormattableTextComponent;
|
import net.minecraft.util.text.IFormattableTextComponent;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
public class MenuFactory extends WidgetFactory
|
public class MenuFactory extends AbstractWidgetFactory
|
||||||
{
|
{
|
||||||
public MenuFactory(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory)
|
public MenuFactory(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory)
|
||||||
{
|
{
|
||||||
@@ -35,11 +35,11 @@ public class MenuFactory extends WidgetFactory
|
|||||||
{
|
{
|
||||||
return new MenuPageList<JsonItem>
|
return new MenuPageList<JsonItem>
|
||||||
(
|
(
|
||||||
menu.getDimensions().getX() + x,
|
menu.getLayout().getX() + x,
|
||||||
menu.getDimensions().getY() + y,
|
menu.getLayout().getY() + y,
|
||||||
menu.getAttributes().getItems(),
|
menu.getAttributes().getItems(),
|
||||||
menu.getDimensions().getWidth(),
|
menu.getLayout().getWidth(),
|
||||||
menu.getDimensions().getHeight(),
|
menu.getLayout().getHeight(),
|
||||||
menu.getAttributes().getLength(),
|
menu.getAttributes().getLength(),
|
||||||
container,
|
container,
|
||||||
new UsercontentLogicPageList<JsonMenu.Type>(this.getApi(), this.getActionHandlerFactory(), content, container, menu, container::getPlayer)
|
new UsercontentLogicPageList<JsonMenu.Type>(this.getApi(), this.getActionHandlerFactory(), content, container, menu, container::getPlayer)
|
||||||
@@ -54,7 +54,7 @@ public class MenuFactory extends WidgetFactory
|
|||||||
{
|
{
|
||||||
private final Container container;
|
private final Container container;
|
||||||
|
|
||||||
public UsercontentLogicPageList(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory, Content content, Container container, JsonWidget<T> widget, Supplier<String> player)
|
public UsercontentLogicPageList(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory, Content content, Container container, AbstractJsonWidget<T> widget, Supplier<String> player)
|
||||||
{
|
{
|
||||||
super(api, actionHandlerFactory, content, widget, player);
|
super(api, actionHandlerFactory, content, widget, player);
|
||||||
this.container = container;
|
this.container = container;
|
||||||
|
|||||||
@@ -1,106 +1,125 @@
|
|||||||
package exopandora.worldhandler.usercontent.factory;
|
package exopandora.worldhandler.usercontent.factory;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import exopandora.worldhandler.WorldHandler;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import com.google.common.base.Predicates;
|
||||||
|
|
||||||
|
import exopandora.worldhandler.gui.container.Container;
|
||||||
import exopandora.worldhandler.gui.content.Content;
|
import exopandora.worldhandler.gui.content.Content;
|
||||||
import exopandora.worldhandler.gui.menu.impl.ILogicMapped;
|
import exopandora.worldhandler.gui.widget.button.GuiButtonIcon;
|
||||||
|
import exopandora.worldhandler.gui.widget.button.GuiButtonItem;
|
||||||
|
import exopandora.worldhandler.gui.widget.button.GuiButtonList;
|
||||||
|
import exopandora.worldhandler.gui.widget.button.GuiButtonTooltip;
|
||||||
|
import exopandora.worldhandler.gui.widget.button.GuiSlider;
|
||||||
|
import exopandora.worldhandler.gui.widget.button.GuiTextFieldTooltip;
|
||||||
|
import exopandora.worldhandler.gui.widget.button.LogicSliderSimple;
|
||||||
import exopandora.worldhandler.usercontent.UsercontentAPI;
|
import exopandora.worldhandler.usercontent.UsercontentAPI;
|
||||||
import exopandora.worldhandler.usercontent.model.JsonItem;
|
|
||||||
import exopandora.worldhandler.usercontent.model.JsonWidget;
|
import exopandora.worldhandler.usercontent.model.JsonWidget;
|
||||||
import exopandora.worldhandler.util.ActionHandler;
|
import exopandora.worldhandler.usercontent.model.JsonItem;
|
||||||
import net.minecraft.util.text.IFormattableTextComponent;
|
import exopandora.worldhandler.util.TextUtils;
|
||||||
import net.minecraft.util.text.StringTextComponent;
|
import net.minecraft.client.gui.widget.Widget;
|
||||||
import net.minecraft.util.text.TranslationTextComponent;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
|
import net.minecraftforge.registries.ForgeRegistries;
|
||||||
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
public abstract class WidgetFactory
|
public class WidgetFactory extends AbstractWidgetFactory
|
||||||
{
|
{
|
||||||
private final ActionHandlerFactory actionHandlerFactory;
|
|
||||||
private final UsercontentAPI api;
|
|
||||||
|
|
||||||
public WidgetFactory(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory)
|
public WidgetFactory(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory)
|
||||||
{
|
{
|
||||||
this.api = api;
|
super(api, actionHandlerFactory);
|
||||||
this.actionHandlerFactory = actionHandlerFactory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionHandlerFactory getActionHandlerFactory()
|
@Nullable
|
||||||
|
public Widget createWidget(JsonWidget widget, Content content, Container container, int x, int y)
|
||||||
{
|
{
|
||||||
return this.actionHandlerFactory;
|
if(JsonWidget.Type.BUTTON.equals(widget.getType()))
|
||||||
}
|
|
||||||
|
|
||||||
public UsercontentAPI getApi()
|
|
||||||
{
|
|
||||||
return this.api;
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
|
||||||
public static class UsercontentLogicMapped<T extends Enum<T>> implements ILogicMapped<JsonItem>
|
|
||||||
{
|
|
||||||
private final ActionHandlerFactory actionHandlerFactory;
|
|
||||||
private final UsercontentAPI api;
|
|
||||||
private final Content content;
|
|
||||||
private final JsonWidget<T> widget;
|
|
||||||
private final Supplier<String> player;
|
|
||||||
|
|
||||||
public UsercontentLogicMapped(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory, Content content, JsonWidget<T> widget, Supplier<String> player)
|
|
||||||
{
|
{
|
||||||
this.api = api;
|
return new GuiButtonTooltip
|
||||||
this.actionHandlerFactory = actionHandlerFactory;
|
(
|
||||||
this.content = content;
|
widget.getLayout().getX() + x,
|
||||||
this.widget = widget;
|
widget.getLayout().getY() + y,
|
||||||
this.player = player;
|
widget.getLayout().getWidth(),
|
||||||
|
widget.getLayout().getHeight(),
|
||||||
|
TextUtils.formatNonnull(widget.getText()),
|
||||||
|
TextUtils.formatNonnull(widget.getAttributes() != null ? widget.getAttributes().getTooltip() : null),
|
||||||
|
this.getActionHandlerFactory().createActionHandler(content, widget.getAction(), container::getPlayer)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if(JsonWidget.Type.ITEM_BUTTON.equals(widget.getType()))
|
||||||
|
{
|
||||||
|
return new GuiButtonItem
|
||||||
|
(
|
||||||
|
widget.getLayout().getX() + x,
|
||||||
|
widget.getLayout().getY() + y,
|
||||||
|
widget.getLayout().getWidth(),
|
||||||
|
widget.getLayout().getHeight(),
|
||||||
|
ForgeRegistries.ITEMS.getValue(new ResourceLocation(widget.getAttributes().getItem())),
|
||||||
|
this.getActionHandlerFactory().createActionHandler(content, widget.getAction(), container::getPlayer)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if(JsonWidget.Type.ICON_BUTTON.equals(widget.getType()))
|
||||||
|
{
|
||||||
|
return new GuiButtonIcon
|
||||||
|
(
|
||||||
|
widget.getLayout().getX() + x,
|
||||||
|
widget.getLayout().getY() + y,
|
||||||
|
widget.getLayout().getWidth(),
|
||||||
|
widget.getLayout().getHeight(),
|
||||||
|
widget.getAttributes().getIcon(),
|
||||||
|
TextUtils.formatNonnull(widget.getAttributes().getTooltip()),
|
||||||
|
this.getActionHandlerFactory().createActionHandler(content, widget.getAction(), container::getPlayer)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if(JsonWidget.Type.LIST_BUTTON.equals(widget.getType()))
|
||||||
|
{
|
||||||
|
return new GuiButtonList<JsonItem>
|
||||||
|
(
|
||||||
|
widget.getLayout().getX() + x,
|
||||||
|
widget.getLayout().getY() + y,
|
||||||
|
widget.getAttributes().getItems(),
|
||||||
|
widget.getLayout().getWidth(),
|
||||||
|
widget.getLayout().getHeight(),
|
||||||
|
container,
|
||||||
|
new UsercontentLogicMapped<JsonWidget.Type>(this.getApi(), this.getActionHandlerFactory(), content, widget, container::getPlayer)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if(JsonWidget.Type.SLIDER.equals(widget.getType()))
|
||||||
|
{
|
||||||
|
Consumer<Integer> responder = this.getActionHandlerFactory().createResponder(integer -> integer.toString(), widget.getAttributes().getId(), widget.getAction());
|
||||||
|
return new GuiSlider
|
||||||
|
(
|
||||||
|
widget.getLayout().getX() + x,
|
||||||
|
widget.getLayout().getY() + y,
|
||||||
|
widget.getLayout().getWidth(),
|
||||||
|
widget.getLayout().getHeight(),
|
||||||
|
widget.getAttributes().getMin(),
|
||||||
|
widget.getAttributes().getMax(),
|
||||||
|
widget.getAttributes().getStart(),
|
||||||
|
container,
|
||||||
|
new LogicSliderSimple(widget.getAttributes().getId(), TextUtils.formatNonnull(widget.getText()), responder)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if(JsonWidget.Type.TEXTFIELD.equals(widget.getType()))
|
||||||
|
{
|
||||||
|
GuiTextFieldTooltip textfield = new GuiTextFieldTooltip
|
||||||
|
(
|
||||||
|
widget.getLayout().getX() + x,
|
||||||
|
widget.getLayout().getY() + y,
|
||||||
|
widget.getLayout().getWidth(),
|
||||||
|
widget.getLayout().getHeight(),
|
||||||
|
TextUtils.formatNonnull(widget.getText())
|
||||||
|
);
|
||||||
|
textfield.setValidator(Predicates.notNull());
|
||||||
|
textfield.setText(this.getApi().getValue(widget.getAttributes().getId()));
|
||||||
|
textfield.setResponder(this.getActionHandlerFactory().createResponder(string -> textfield.getText(), widget.getAttributes().getId(), widget.getAction()));
|
||||||
|
|
||||||
|
return textfield;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
return null;
|
||||||
public IFormattableTextComponent translate(JsonItem item)
|
|
||||||
{
|
|
||||||
if(item.getTranslation() != null)
|
|
||||||
{
|
|
||||||
return new TranslationTextComponent(item.getTranslation());
|
|
||||||
}
|
|
||||||
|
|
||||||
return new StringTextComponent(item.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public IFormattableTextComponent toTooltip(JsonItem item)
|
|
||||||
{
|
|
||||||
return new StringTextComponent(item.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onClick(JsonItem item)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
this.api.updateValue(this.widget.getAttributes().getId(), item.getId());
|
|
||||||
ActionHandler action = this.actionHandlerFactory.createActionHandler(this.content, this.widget.getAction(), this.player, item.getId());
|
|
||||||
|
|
||||||
if(action != null)
|
|
||||||
{
|
|
||||||
action.run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(Exception e)
|
|
||||||
{
|
|
||||||
WorldHandler.LOGGER.error("Error executing action for widget");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getId()
|
|
||||||
{
|
|
||||||
return this.widget.getAttributes().getId();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onInit(JsonItem item)
|
|
||||||
{
|
|
||||||
this.onClick(item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
package exopandora.worldhandler.usercontent.model;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
|
||||||
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
|
|
||||||
|
@OnlyIn(Dist.CLIENT)
|
||||||
|
public abstract class AbstractJsonWidget<T extends Enum<T>>
|
||||||
|
{
|
||||||
|
@SerializedName("action")
|
||||||
|
private Action action;
|
||||||
|
|
||||||
|
@SerializedName("layout")
|
||||||
|
private JsonLayout layout;
|
||||||
|
|
||||||
|
@SerializedName("attributes")
|
||||||
|
private Attributes attributes;
|
||||||
|
|
||||||
|
public AbstractJsonWidget(Action action, JsonLayout layout, Attributes attributes)
|
||||||
|
{
|
||||||
|
this.action = action;
|
||||||
|
this.layout = layout;
|
||||||
|
this.attributes = attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Action getAction()
|
||||||
|
{
|
||||||
|
return this.action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAction(Action action)
|
||||||
|
{
|
||||||
|
this.action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonLayout getLayout()
|
||||||
|
{
|
||||||
|
return this.layout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDimensions(JsonLayout layout)
|
||||||
|
{
|
||||||
|
this.layout = layout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Attributes getAttributes()
|
||||||
|
{
|
||||||
|
return this.attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttributes(Attributes attributes)
|
||||||
|
{
|
||||||
|
this.attributes = attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract T getType();
|
||||||
|
public abstract void setType(T type);
|
||||||
|
public abstract void validate() throws IllegalStateException;
|
||||||
|
|
||||||
|
protected void validateAction(Action.Type... allowedTypes) throws IllegalStateException
|
||||||
|
{
|
||||||
|
if(this.getAction() != null)
|
||||||
|
{
|
||||||
|
this.getAction().validate();
|
||||||
|
|
||||||
|
if(Arrays.stream(allowedTypes).noneMatch(type -> type.equals(this.getAction().getType())))
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("Illegal action for type " + this.getType().toString().toLowerCase());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnlyIn(Dist.CLIENT)
|
||||||
|
public static enum Type
|
||||||
|
{
|
||||||
|
BUTTON,
|
||||||
|
MENU
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
package exopandora.worldhandler.usercontent.model;
|
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
|
||||||
|
|
||||||
import exopandora.worldhandler.usercontent.model.JsonButton.Type;
|
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
||||||
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
|
||||||
public class JsonButton extends JsonWidget<Type>
|
|
||||||
{
|
|
||||||
@SerializedName("text")
|
|
||||||
private String text;
|
|
||||||
|
|
||||||
@SerializedName("type")
|
|
||||||
private Type type;
|
|
||||||
|
|
||||||
public JsonButton(String text, Type type, Action action, JsonDimensions dimensions, Attributes attributes)
|
|
||||||
{
|
|
||||||
super(action, dimensions, attributes);
|
|
||||||
this.text = text;
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getText()
|
|
||||||
{
|
|
||||||
return this.text;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setText(String text)
|
|
||||||
{
|
|
||||||
this.text = text;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Type getType()
|
|
||||||
{
|
|
||||||
return this.type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setType(Type type)
|
|
||||||
{
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void validate() throws IllegalStateException
|
|
||||||
{
|
|
||||||
if(this.type == null)
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.type is null");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(this.type == Type.TEXTFIELD)
|
|
||||||
{
|
|
||||||
if(this.getAttributes() == null)
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.attributes is null");
|
|
||||||
}
|
|
||||||
else if(this.getAttributes().getId() == null)
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.attributes.id is null");
|
|
||||||
}
|
|
||||||
else if(this.getAttributes().getId().isEmpty())
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.attributes.id is empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.validateAction(Action.Type.SET, Action.Type.JS);
|
|
||||||
}
|
|
||||||
else if(this.type == Type.ITEM_BUTTON)
|
|
||||||
{
|
|
||||||
if(this.getAttributes() == null)
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.attributes is null");
|
|
||||||
}
|
|
||||||
else if(this.getAttributes().getItem() == null)
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.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);
|
|
||||||
}
|
|
||||||
else if(this.type == Type.ICON_BUTTON)
|
|
||||||
{
|
|
||||||
if(this.getAttributes() == null)
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.attributes is null");
|
|
||||||
}
|
|
||||||
else if(this.getAttributes().getIcon() == null)
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.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);
|
|
||||||
}
|
|
||||||
else if(this.type == Type.LIST_BUTTON)
|
|
||||||
{
|
|
||||||
if(this.getAttributes() == null)
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.attributes is null");
|
|
||||||
}
|
|
||||||
else if(this.getAttributes().getItems() == null)
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.attributes.items is null");
|
|
||||||
}
|
|
||||||
else if(this.getAttributes().getItems().isEmpty())
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.attributes.items is empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.validateAction(Action.Type.SET, Action.Type.JS);
|
|
||||||
}
|
|
||||||
else if(this.type == Type.SLIDER)
|
|
||||||
{
|
|
||||||
if(this.getAttributes() == null)
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("button.attributes is null");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.validateAction(Action.Type.SET, Action.Type.JS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
|
||||||
public static enum Type
|
|
||||||
{
|
|
||||||
BUTTON,
|
|
||||||
TEXTFIELD,
|
|
||||||
ITEM_BUTTON,
|
|
||||||
ICON_BUTTON,
|
|
||||||
LIST_BUTTON,
|
|
||||||
SLIDER;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -16,22 +16,22 @@ public class JsonGui
|
|||||||
@SerializedName("tab")
|
@SerializedName("tab")
|
||||||
private JsonTab tab;
|
private JsonTab tab;
|
||||||
|
|
||||||
@SerializedName("buttons")
|
@SerializedName("widgets")
|
||||||
private List<JsonButton> buttons = null;
|
private List<JsonWidget> widgets = null;
|
||||||
|
|
||||||
@SerializedName("menus")
|
@SerializedName("menus")
|
||||||
private List<JsonMenu> menus = null;
|
private List<JsonMenu> menus = null;
|
||||||
|
|
||||||
@SerializedName("texts")
|
@SerializedName("labels")
|
||||||
private List<JsonText> texts = null;
|
private List<JsonLabel> labels = null;
|
||||||
|
|
||||||
public JsonGui(String title, JsonTab tab, List<JsonButton> buttons, List<JsonMenu> menus, List<JsonText> texts)
|
public JsonGui(String title, JsonTab tab, List<JsonWidget> widgets, List<JsonMenu> menus, List<JsonLabel> labels)
|
||||||
{
|
{
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.tab = tab;
|
this.tab = tab;
|
||||||
this.buttons = buttons;
|
this.widgets = widgets;
|
||||||
this.menus = menus;
|
this.menus = menus;
|
||||||
this.texts = texts;
|
this.labels = labels;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTitle()
|
public String getTitle()
|
||||||
@@ -54,14 +54,14 @@ public class JsonGui
|
|||||||
this.tab = tab;
|
this.tab = tab;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<JsonButton> getButtons()
|
public List<JsonWidget> getWidgets()
|
||||||
{
|
{
|
||||||
return this.buttons;
|
return this.widgets;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setButtons(List<JsonButton> buttons)
|
public void setButtons(List<JsonWidget> widgets)
|
||||||
{
|
{
|
||||||
this.buttons = buttons;
|
this.widgets = widgets;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<JsonMenu> getMenus()
|
public List<JsonMenu> getMenus()
|
||||||
@@ -74,14 +74,14 @@ public class JsonGui
|
|||||||
this.menus = menus;
|
this.menus = menus;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<JsonText> getTexts()
|
public List<JsonLabel> getLabels()
|
||||||
{
|
{
|
||||||
return this.texts;
|
return this.labels;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTexts(List<JsonText> texts)
|
public void setTexts(List<JsonLabel> labels)
|
||||||
{
|
{
|
||||||
this.texts = texts;
|
this.labels = labels;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void validate() throws IllegalStateException
|
public void validate() throws IllegalStateException
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import net.minecraftforge.api.distmarker.Dist;
|
|||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
public class JsonText
|
public class JsonLabel
|
||||||
{
|
{
|
||||||
@SerializedName("text")
|
@SerializedName("text")
|
||||||
private String text;
|
private String text;
|
||||||
@@ -23,7 +23,7 @@ public class JsonText
|
|||||||
@SerializedName("visible")
|
@SerializedName("visible")
|
||||||
private BooleanExpression visible;
|
private BooleanExpression visible;
|
||||||
|
|
||||||
public JsonText(String text, int x, int y, int color, BooleanExpression visible)
|
public JsonLabel(String text, int x, int y, int color, BooleanExpression visible)
|
||||||
{
|
{
|
||||||
this.text = text;
|
this.text = text;
|
||||||
this.x = x;
|
this.x = x;
|
||||||
@@ -6,7 +6,7 @@ import net.minecraftforge.api.distmarker.Dist;
|
|||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
public class JsonDimensions
|
public class JsonLayout
|
||||||
{
|
{
|
||||||
@SerializedName("x")
|
@SerializedName("x")
|
||||||
private int x;
|
private int x;
|
||||||
@@ -20,7 +20,7 @@ public class JsonDimensions
|
|||||||
@SerializedName("height")
|
@SerializedName("height")
|
||||||
private int height;
|
private int height;
|
||||||
|
|
||||||
public JsonDimensions(int x, int y, int width, int height)
|
public JsonLayout(int x, int y, int width, int height)
|
||||||
{
|
{
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
@@ -7,14 +7,14 @@ import net.minecraftforge.api.distmarker.Dist;
|
|||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
public class JsonMenu extends JsonWidget<Type>
|
public class JsonMenu extends AbstractJsonWidget<Type>
|
||||||
{
|
{
|
||||||
@SerializedName("type")
|
@SerializedName("type")
|
||||||
private Type type;
|
private Type type;
|
||||||
|
|
||||||
public JsonMenu(Type type, Action action, JsonDimensions dimensions, Attributes attributes)
|
public JsonMenu(Type type, Action action, JsonLayout layout, Attributes attributes)
|
||||||
{
|
{
|
||||||
super(action, dimensions, attributes);
|
super(action, layout, attributes);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,75 +1,123 @@
|
|||||||
package exopandora.worldhandler.usercontent.model;
|
package exopandora.worldhandler.usercontent.model;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
|
||||||
|
import exopandora.worldhandler.usercontent.model.JsonWidget.Type;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
public abstract class JsonWidget<T extends Enum<T>>
|
public class JsonWidget extends AbstractJsonWidget<Type>
|
||||||
{
|
{
|
||||||
@SerializedName("action")
|
@SerializedName("text")
|
||||||
private Action action;
|
private String text;
|
||||||
|
|
||||||
@SerializedName("dimensions")
|
@SerializedName("type")
|
||||||
private JsonDimensions dimensions;
|
private Type type;
|
||||||
|
|
||||||
@SerializedName("attributes")
|
public JsonWidget(String text, Type type, Action action, JsonLayout layout, Attributes attributes)
|
||||||
private Attributes attributes;
|
|
||||||
|
|
||||||
public JsonWidget(Action action, JsonDimensions dimensions, Attributes attributes)
|
|
||||||
{
|
{
|
||||||
this.action = action;
|
super(action, layout, attributes);
|
||||||
this.dimensions = dimensions;
|
this.text = text;
|
||||||
this.attributes = attributes;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Action getAction()
|
public String getText()
|
||||||
{
|
{
|
||||||
return this.action;
|
return this.text;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAction(Action action)
|
public void setText(String text)
|
||||||
{
|
{
|
||||||
this.action = action;
|
this.text = text;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonDimensions getDimensions()
|
public Type getType()
|
||||||
{
|
{
|
||||||
return this.dimensions;
|
return this.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDimensions(JsonDimensions dimensions)
|
public void setType(Type type)
|
||||||
{
|
{
|
||||||
this.dimensions = dimensions;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Attributes getAttributes()
|
@Override
|
||||||
|
public void validate() throws IllegalStateException
|
||||||
{
|
{
|
||||||
return this.attributes;
|
if(this.type == null)
|
||||||
}
|
|
||||||
|
|
||||||
public void setAttributes(Attributes attributes)
|
|
||||||
{
|
|
||||||
this.attributes = attributes;
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract T getType();
|
|
||||||
public abstract void setType(T type);
|
|
||||||
public abstract void validate() throws IllegalStateException;
|
|
||||||
|
|
||||||
protected void validateAction(Action.Type... allowedTypes) throws IllegalStateException
|
|
||||||
{
|
|
||||||
if(this.getAction() != null)
|
|
||||||
{
|
{
|
||||||
this.getAction().validate();
|
throw new IllegalStateException("widget.type is null");
|
||||||
|
}
|
||||||
|
|
||||||
if(Arrays.stream(allowedTypes).noneMatch(type -> type.equals(this.getAction().getType())))
|
if(this.type == Type.TEXTFIELD)
|
||||||
|
{
|
||||||
|
if(this.getAttributes() == null)
|
||||||
{
|
{
|
||||||
throw new IllegalStateException("Illegal action for type " + this.getType().toString().toLowerCase());
|
throw new IllegalStateException("widget.attributes is null");
|
||||||
}
|
}
|
||||||
|
else if(this.getAttributes().getId() == null)
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("widget.attributes.id is null");
|
||||||
|
}
|
||||||
|
else if(this.getAttributes().getId().isEmpty())
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("widget.attributes.id is empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.validateAction(Action.Type.SET, Action.Type.JS);
|
||||||
|
}
|
||||||
|
else if(this.type == Type.ITEM_BUTTON)
|
||||||
|
{
|
||||||
|
if(this.getAttributes() == null)
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("widget.attributes is null");
|
||||||
|
}
|
||||||
|
else if(this.getAttributes().getItem() == null)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
else if(this.type == Type.ICON_BUTTON)
|
||||||
|
{
|
||||||
|
if(this.getAttributes() == null)
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("widget.attributes is null");
|
||||||
|
}
|
||||||
|
else if(this.getAttributes().getIcon() == null)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
else if(this.type == Type.LIST_BUTTON)
|
||||||
|
{
|
||||||
|
if(this.getAttributes() == null)
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("widget.attributes is null");
|
||||||
|
}
|
||||||
|
else if(this.getAttributes().getItems() == null)
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("widget.attributes.items is null");
|
||||||
|
}
|
||||||
|
else if(this.getAttributes().getItems().isEmpty())
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("widget.attributes.items is empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.validateAction(Action.Type.SET, Action.Type.JS);
|
||||||
|
}
|
||||||
|
else if(this.type == Type.SLIDER)
|
||||||
|
{
|
||||||
|
if(this.getAttributes() == null)
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("widget.attributes is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.validateAction(Action.Type.SET, Action.Type.JS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,6 +125,10 @@ public abstract class JsonWidget<T extends Enum<T>>
|
|||||||
public static enum Type
|
public static enum Type
|
||||||
{
|
{
|
||||||
BUTTON,
|
BUTTON,
|
||||||
MENU
|
TEXTFIELD,
|
||||||
|
ITEM_BUTTON,
|
||||||
|
ICON_BUTTON,
|
||||||
|
LIST_BUTTON,
|
||||||
|
SLIDER;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user