Skip to content

Commit

Permalink
Refactoring after lib update
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Emelyanov committed Apr 6, 2024
1 parent 15f941d commit 6bd6a55
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/main/java/mcjty/xnet/apiimpl/enums/ChannelMode.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package mcjty.xnet.apiimpl.enums;

import mcjty.lib.varia.ComponentFactory;
import mcjty.lib.gui.ITranslatableEnum;
import mcjty.xnet.utils.I18nUtils;
import mcjty.xnet.utils.ITranslatableEnum;

public enum ChannelMode implements ITranslatableEnum<ChannelMode> {
PRIORITY("xnet.enum.channelmode.priority"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import mcjty.xnet.apiimpl.Constants;
import mcjty.xnet.apiimpl.EnumStringTranslators;
import mcjty.xnet.apiimpl.enums.InsExtMode;
import mcjty.xnet.modules.controller.client.AbstractEditorPanel;
import mcjty.xnet.setup.Config;
import mcjty.xnet.utils.CastTools;
import mcjty.xnet.utils.TagUtils;
Expand All @@ -24,6 +23,7 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -120,14 +120,15 @@ private String getMinMaxTooltip() {
@Override
public void createGui(IEditorGui gui) {
advanced = gui.isAdvanced();
String[] speeds = advanced ? Constants.ADVANCED_SPEEDS : Constants.SPEEDS;
String[] speeds = Arrays.stream(advanced ? Constants.ADVANCED_SPEEDS : Constants.SPEEDS)
.map(s -> String.valueOf(Integer.parseInt(s) * 2)).toArray(String[]::new);
int maxrate = Config.getMaxFluidRate(advanced);

sideGui(gui);
colorsGui(gui);
redstoneGui(gui);
gui.nl();
((AbstractEditorPanel)gui).translatableChoices(TAG_MODE, fluidMode, InsExtMode.values())// TODO: 09.03.2024 remove AbstractEditorPanel cast after rftoolbase update
gui.translatableChoices(TAG_MODE, fluidMode, InsExtMode.values())
.choices(TAG_SPEED, SPEED_TOOLTIP.i18n(), Integer.toString(speed * 10), speeds)
.nl()

Expand Down Expand Up @@ -226,7 +227,7 @@ public void readFromNBT(CompoundTag tag) {
priority = TagUtils.getIntOrNull(tag, TAG_PRIORITY);
rate = TagUtils.getIntOrNull(tag, TAG_RATE);
minmax = TagUtils.getIntOrNull(tag, TAG_MINMAX);
setSpeed(TagUtils.getIntOrNull(tag, TAG_SPEED));
speed = TagUtils.getIntOrValue(tag, TAG_SPEED, 2);
if (tag.contains(TAG_FLT)) {
CompoundTag itemTag = tag.getCompound(TAG_FLT);
filter = ItemStack.of(itemTag);
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/mcjty/xnet/apiimpl/items/ItemConnectorSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import mcjty.xnet.apiimpl.enums.InsExtMode;
import mcjty.xnet.apiimpl.items.enums.ExtractMode;
import mcjty.xnet.apiimpl.items.enums.StackMode;
import mcjty.xnet.modules.controller.client.AbstractEditorPanel;
import mcjty.xnet.utils.CastTools;
import mcjty.xnet.utils.TagUtils;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -133,26 +132,21 @@ public void createGui(IEditorGui gui) {
colorsGui(gui);
redstoneGui(gui);
gui.nl();
((AbstractEditorPanel)gui).translatableChoices(TAG_MODE, itemMode, InsExtMode.values())// TODO: 09.03.2024 remove AbstractEditorPanel cast after rftoolbase update
.shift(5);
((AbstractEditorPanel)gui).translatableChoices(TAG_STACK, stackMode, StackMode.values());// TODO: 09.03.2024 remove AbstractEditorPanel cast after rftoolbase update
gui.translatableChoices(TAG_MODE, itemMode, InsExtMode.values()).shift(5);
gui.translatableChoices(TAG_STACK, stackMode, StackMode.values());

if (stackMode == StackMode.COUNT && itemMode == InsExtMode.EXT) {
gui.integer(TAG_EXTRACT_AMOUNT, ITEM_EXT_COUNT_TOOLTIP.i18n(), extractAmount, 30, 64);
}

gui
.shift(10)
.choices(TAG_SPEED, SPEED_TOOLTIP.i18n(), Integer.toString(speed * 5), speeds);
gui.shift(10).choices(TAG_SPEED, SPEED_TOOLTIP.i18n(), Integer.toString(speed * 5), speeds);
gui.nl();
gui
.label(PRIORITY_LABEL.i18n()).integer(TAG_PRIORITY, PRIORITY_TOOLTIP.i18n(), priority, 36).shift(5)
.label("#")
.integer(TAG_COUNT, getMinMaxTooltip(), count, 30);
.label("#").integer(TAG_COUNT, getMinMaxTooltip(), count, 30);

if (itemMode == InsExtMode.EXT) {
gui.shift(5);
((AbstractEditorPanel)gui).translatableChoices(TAG_EXTRACT, extractMode, ExtractMode.values());// TODO: 09.03.2024 remove AbstractEditorPanel cast after rftoolbase update
gui.shift(5).translatableChoices(TAG_EXTRACT, extractMode, ExtractMode.values());
}

gui.nl();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package mcjty.xnet.modules.controller.client;

import mcjty.lib.blockcommands.Command;
import mcjty.lib.gui.ITranslatableEnum;
import mcjty.lib.gui.events.BlockRenderEvent;
import mcjty.lib.gui.widgets.BlockRender;
import mcjty.lib.gui.widgets.ChoiceLabel;
import mcjty.lib.gui.widgets.ColorChoiceLabel;
import mcjty.lib.gui.widgets.EnumChoiceLabel;
import mcjty.lib.gui.widgets.ImageChoiceLabel;
import mcjty.lib.gui.widgets.Label;
import mcjty.lib.gui.widgets.Panel;
Expand All @@ -19,8 +21,6 @@
import mcjty.rftoolsbase.api.xnet.gui.IEditorGui;
import mcjty.xnet.XNet;
import mcjty.xnet.setup.XNetMessages;
import mcjty.xnet.utils.EnumChoiceLabel;
import mcjty.xnet.utils.ITranslatableEnum;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -162,15 +162,28 @@ private Integer validate(String i, Integer maximum, Integer minimum) {

@Override
public IEditorGui integer(String tag, String tooltip, Integer value, int width) {
return integer(tag, tooltip, value, width, Integer.MAX_VALUE, 0);
fitWidth(width);
TextField text = textfield(x, y, width, 14).text(value == null ? "" : value.toString())
.tooltips(parseTooltips(tooltip));
data.put(tag, value);
text.addTextEnterEvent((newText) -> update(tag, validate(newText, null, null)));
gui.getWindow().addFocusEvent((textFocus) -> {
if (textFocus == null) {
update(tag, validate(text.getText(), null, null));
}
});
panel.children(text);
components.put(tag, text);
x += width;
return this;
}

@Override
public IEditorGui integer(String tag, String tooltip, Integer value, int width, Integer maximum) {
return integer(tag, tooltip, value, width, maximum, 0);
}

// TODO: 06.03.2024 override IEditorGUI after rftoolbase update
@Override
public IEditorGui integer(String tag, String tooltip, Integer value, int width, int maximum, int minimum) {
fitWidth(width);
TextField text = textfield(x, y, width, 14).text(value == null ? String.valueOf(minimum) : value.toString())
Expand Down Expand Up @@ -288,15 +301,15 @@ public <T extends Enum<T>> IEditorGui choices(String tag, String tooltip, T curr
return choices(tag, tooltip, StringUtils.capitalize(current.toString().toLowerCase()), strings);
}

public IEditorGui translatableChoices(String tag, ITranslatableEnum<?> current, ITranslatableEnum<?>... values) {// TODO: 09.03.2024 move to rftoolsbase
@Override
public IEditorGui translatableChoices(String tag, ITranslatableEnum<?> current, ITranslatableEnum<?>... values) {
int w = 10;
for (ITranslatableEnum<?> s : values) {
w = Math.max(w, mc.font.width(s.getI18n()) + 14);
}

fitWidth(w);
EnumChoiceLabel choice = new EnumChoiceLabel().choices(values).choice(current)
.hint(x, y, w, 14);
EnumChoiceLabel choice = new EnumChoiceLabel().choices(values).choice(current).hint(x, y, w, 14);
data.put(tag, current.ordinal());
choice.event((newChoice) -> update(tag, newChoice.ordinal()));
panel.children(choice);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/mcjty/xnet/utils/ChannelChoiceLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import mcjty.lib.base.StyleConfig;
import mcjty.lib.client.RenderHelper;
import mcjty.lib.gui.GuiParser;
import mcjty.lib.gui.ITranslatableEnum;
import mcjty.lib.gui.Window;
import mcjty.lib.gui.events.ChoiceEvent;
import mcjty.lib.gui.widgets.AbstractLabel;
import mcjty.lib.gui.widgets.Widget;
import mcjty.lib.typed.Key;
Expand All @@ -26,7 +28,7 @@

public class ChannelChoiceLabel extends AbstractLabel<ChannelChoiceLabel> {

public static final String TYPE_ENUM_CHOICE_LABEL = "enumchoicelabel";
public static final String TYPE_CHANNEL_CHOICE_LABEL = "channelchoicelabel";
public static final Key<String> PARAM_CHOICE = new Key<>("choice", Type.STRING);
public static final Key<Integer> PARAM_CHOICE_IDX = new Key<>("choiceIdx", Type.INTEGER);

Expand Down Expand Up @@ -169,7 +171,6 @@ private void fireChoiceEvents(String choice) {

@Override
public void readFromGuiCommand(GuiParser.GuiCommand command) {
System.out.println("readFromGuiCommand");
super.readFromGuiCommand(command);
command.findCommand("choices").ifPresent(cmd -> {
cmd.commands().forEach(choiceCmd -> {
Expand All @@ -186,7 +187,6 @@ public void readFromGuiCommand(GuiParser.GuiCommand command) {

@Override
public void fillGuiCommand(GuiParser.GuiCommand command) {
System.out.println("fillGuiCommand");
super.fillGuiCommand(command);
command.removeParameter(1); // We don't need the name as set by the label
GuiParser.GuiCommand choicesCmd = new GuiParser.GuiCommand("choices");
Expand All @@ -207,6 +207,6 @@ public void fillGuiCommand(GuiParser.GuiCommand command) {

@Override
public GuiParser.GuiCommand createGuiCommand() {
return new GuiParser.GuiCommand(TYPE_ENUM_CHOICE_LABEL);
return new GuiParser.GuiCommand(TYPE_CHANNEL_CHOICE_LABEL);
}
}
4 changes: 0 additions & 4 deletions src/main/java/mcjty/xnet/utils/I18nConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ public enum I18nConstants {

I18nConstants(String langKey) {this.langKey = langKey;}

// public String i18n() {
//
// }

public String i18n(Object... formatArgs) {
if (formatArgs == null) {
return ComponentFactory.translatable(this.langKey).getString();
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/mcjty/xnet/utils/TagUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public static Integer getIntOrNull(CompoundTag tag, String key) {
}
}

public static Integer getIntOrValue(CompoundTag tag, String key, int value) {
if (key == null || key.isEmpty() || !tag.contains(key)) {
return value;
} else {
return tag.getInt(key);
}
}

public static String getStringOrNull(CompoundTag tag, String key) {
if (key == null || key.isEmpty() || !tag.contains(key)) {
return null;
Expand Down

0 comments on commit 6bd6a55

Please sign in to comment.