Update json api (now version 1.1)
This commit is contained in:
@@ -27,7 +27,7 @@ import exopandora.worldhandler.builder.types.ArgumentType;
|
||||
import exopandora.worldhandler.gui.widget.button.EnumIcon;
|
||||
import exopandora.worldhandler.usercontent.model.Action;
|
||||
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.JsonUsercontent;
|
||||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
||||
@@ -63,7 +63,7 @@ public class UsercontentLoader
|
||||
.registerTypeAdapter(ArgumentType.class, new EnumTypeAdapter<ArgumentType>(ArgumentType.class))
|
||||
.registerTypeAdapter(EnumIcon.class, new EnumTypeAdapter<EnumIcon>(EnumIcon.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(JsonMenu.Type.class, new EnumTypeAdapter<JsonMenu.Type>(JsonMenu.Type.class))
|
||||
.create();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package exopandora.worldhandler.usercontent;
|
||||
|
||||
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.OnlyIn;
|
||||
|
||||
@@ -16,9 +16,9 @@ public class VisibleActiveObject<T> extends VisibleObject<T>
|
||||
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)
|
||||
|
||||
@@ -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.model.JsonItem;
|
||||
import exopandora.worldhandler.usercontent.model.JsonMenu;
|
||||
import exopandora.worldhandler.usercontent.model.JsonWidget;
|
||||
import exopandora.worldhandler.usercontent.model.AbstractJsonWidget;
|
||||
import exopandora.worldhandler.util.ActionHandler;
|
||||
import net.minecraft.util.text.IFormattableTextComponent;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class MenuFactory extends WidgetFactory
|
||||
public class MenuFactory extends AbstractWidgetFactory
|
||||
{
|
||||
public MenuFactory(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory)
|
||||
{
|
||||
@@ -35,11 +35,11 @@ public class MenuFactory extends WidgetFactory
|
||||
{
|
||||
return new MenuPageList<JsonItem>
|
||||
(
|
||||
menu.getDimensions().getX() + x,
|
||||
menu.getDimensions().getY() + y,
|
||||
menu.getLayout().getX() + x,
|
||||
menu.getLayout().getY() + y,
|
||||
menu.getAttributes().getItems(),
|
||||
menu.getDimensions().getWidth(),
|
||||
menu.getDimensions().getHeight(),
|
||||
menu.getLayout().getWidth(),
|
||||
menu.getLayout().getHeight(),
|
||||
menu.getAttributes().getLength(),
|
||||
container,
|
||||
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;
|
||||
|
||||
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);
|
||||
this.container = container;
|
||||
|
||||
@@ -1,106 +1,125 @@
|
||||
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.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.model.JsonItem;
|
||||
import exopandora.worldhandler.usercontent.model.JsonWidget;
|
||||
import exopandora.worldhandler.util.ActionHandler;
|
||||
import net.minecraft.util.text.IFormattableTextComponent;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
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 abstract class WidgetFactory
|
||||
public class WidgetFactory extends AbstractWidgetFactory
|
||||
{
|
||||
private final ActionHandlerFactory actionHandlerFactory;
|
||||
private final UsercontentAPI api;
|
||||
|
||||
public WidgetFactory(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory)
|
||||
{
|
||||
this.api = api;
|
||||
this.actionHandlerFactory = actionHandlerFactory;
|
||||
super(api, actionHandlerFactory);
|
||||
}
|
||||
|
||||
public ActionHandlerFactory getActionHandlerFactory()
|
||||
@Nullable
|
||||
public Widget createWidget(JsonWidget widget, Content content, Container container, int x, int y)
|
||||
{
|
||||
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 JsonWidget<T> widget;
|
||||
private final Supplier<String> player;
|
||||
|
||||
public UsercontentLogicMapped(UsercontentAPI api, ActionHandlerFactory actionHandlerFactory, Content content, JsonWidget<T> widget, Supplier<String> player)
|
||||
if(JsonWidget.Type.BUTTON.equals(widget.getType()))
|
||||
{
|
||||
this.api = api;
|
||||
this.actionHandlerFactory = actionHandlerFactory;
|
||||
this.content = content;
|
||||
this.widget = widget;
|
||||
this.player = player;
|
||||
return new GuiButtonTooltip
|
||||
(
|
||||
widget.getLayout().getX() + x,
|
||||
widget.getLayout().getY() + y,
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFormattableTextComponent translate(JsonItem item)
|
||||
else if(JsonWidget.Type.ITEM_BUTTON.equals(widget.getType()))
|
||||
{
|
||||
if(item.getTranslation() != null)
|
||||
{
|
||||
return new TranslationTextComponent(item.getTranslation());
|
||||
}
|
||||
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 new StringTextComponent(item.getId());
|
||||
return textfield;
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
private JsonTab tab;
|
||||
|
||||
@SerializedName("buttons")
|
||||
private List<JsonButton> buttons = null;
|
||||
@SerializedName("widgets")
|
||||
private List<JsonWidget> widgets = null;
|
||||
|
||||
@SerializedName("menus")
|
||||
private List<JsonMenu> menus = null;
|
||||
|
||||
@SerializedName("texts")
|
||||
private List<JsonText> texts = null;
|
||||
@SerializedName("labels")
|
||||
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.tab = tab;
|
||||
this.buttons = buttons;
|
||||
this.widgets = widgets;
|
||||
this.menus = menus;
|
||||
this.texts = texts;
|
||||
this.labels = labels;
|
||||
}
|
||||
|
||||
public String getTitle()
|
||||
@@ -54,14 +54,14 @@ public class JsonGui
|
||||
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()
|
||||
@@ -74,14 +74,14 @@ public class JsonGui
|
||||
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
|
||||
|
||||
@@ -6,7 +6,7 @@ import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class JsonText
|
||||
public class JsonLabel
|
||||
{
|
||||
@SerializedName("text")
|
||||
private String text;
|
||||
@@ -23,7 +23,7 @@ public class JsonText
|
||||
@SerializedName("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.x = x;
|
||||
@@ -6,7 +6,7 @@ import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class JsonDimensions
|
||||
public class JsonLayout
|
||||
{
|
||||
@SerializedName("x")
|
||||
private int x;
|
||||
@@ -20,7 +20,7 @@ public class JsonDimensions
|
||||
@SerializedName("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.y = y;
|
||||
@@ -7,14 +7,14 @@ import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class JsonMenu extends JsonWidget<Type>
|
||||
public class JsonMenu extends AbstractJsonWidget<Type>
|
||||
{
|
||||
@SerializedName("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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,75 +1,123 @@
|
||||
package exopandora.worldhandler.usercontent.model;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import exopandora.worldhandler.usercontent.model.JsonWidget.Type;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public abstract class JsonWidget<T extends Enum<T>>
|
||||
public class JsonWidget extends AbstractJsonWidget<Type>
|
||||
{
|
||||
@SerializedName("action")
|
||||
private Action action;
|
||||
@SerializedName("text")
|
||||
private String text;
|
||||
|
||||
@SerializedName("dimensions")
|
||||
private JsonDimensions dimensions;
|
||||
@SerializedName("type")
|
||||
private Type type;
|
||||
|
||||
@SerializedName("attributes")
|
||||
private Attributes attributes;
|
||||
|
||||
public JsonWidget(Action action, JsonDimensions dimensions, Attributes attributes)
|
||||
public JsonWidget(String text, Type type, Action action, JsonLayout layout, Attributes attributes)
|
||||
{
|
||||
this.action = action;
|
||||
this.dimensions = dimensions;
|
||||
this.attributes = attributes;
|
||||
super(action, layout, attributes);
|
||||
this.text = text;
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
if(this.type == null)
|
||||
{
|
||||
this.getAction().validate();
|
||||
|
||||
if(Arrays.stream(allowedTypes).noneMatch(type -> type.equals(this.getAction().getType())))
|
||||
throw new IllegalStateException("widget.type is null");
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
BUTTON,
|
||||
MENU
|
||||
TEXTFIELD,
|
||||
ITEM_BUTTON,
|
||||
ICON_BUTTON,
|
||||
LIST_BUTTON,
|
||||
SLIDER;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user