Skip to content

Commit

Permalink
New connector gui features, translate controller gui, McJtyMods#551 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Emelyanov committed Apr 6, 2024
1 parent 6bd6a55 commit d778ba6
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 26 deletions.
72 changes: 59 additions & 13 deletions src/main/java/mcjty/xnet/modules/cables/client/GuiConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@
import mcjty.lib.container.GenericContainer;
import mcjty.lib.gui.GenericGuiContainer;
import mcjty.lib.gui.Window;
import mcjty.lib.gui.layout.HorizontalAlignment;
import mcjty.lib.gui.layout.VerticalAlignment;
import mcjty.lib.gui.widgets.BlockRender;
import mcjty.lib.gui.widgets.Label;
import mcjty.lib.gui.widgets.Panel;
import mcjty.lib.gui.widgets.TextField;
import mcjty.lib.gui.widgets.ToggleButton;
import mcjty.lib.network.PacketRequestDataFromServer;
import mcjty.lib.typed.TypedMap;
import mcjty.lib.varia.OrientationTools;
import mcjty.xnet.modules.cables.CableModule;
import mcjty.xnet.modules.cables.ConnectorType;
import mcjty.xnet.modules.cables.blocks.ConnectorTileEntity;
import mcjty.xnet.setup.XNetMessages;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.EnumProperty;

import javax.annotation.Nonnull;

Expand All @@ -22,6 +34,7 @@
import static mcjty.lib.gui.widgets.Widgets.vertical;
import static mcjty.xnet.apiimpl.Constants.TAG_NAME;
import static mcjty.xnet.modules.cables.blocks.ConnectorTileEntity.CMD_ENABLE;
import static mcjty.xnet.modules.cables.blocks.ConnectorTileEntity.CMD_GET_NAME;
import static mcjty.xnet.modules.cables.blocks.ConnectorTileEntity.PARAM_ENABLED;
import static mcjty.xnet.modules.cables.blocks.ConnectorTileEntity.PARAM_FACING;
import static mcjty.xnet.utils.I18nConstants.CONNECTOR_NAME_TOOLTIP;
Expand All @@ -30,18 +43,23 @@

public class GuiConnector extends GenericGuiContainer<ConnectorTileEntity, GenericContainer> {

public static final int WIDTH = 220;
public static final int HEIGHT = 50;
public static final int WIDTH = 230;
public static final int HEIGHT = 60;

private final ToggleButton[] toggleButtons = new ToggleButton[6];
private final BlockRender[] connectedBlockRenders = new BlockRender[6];

// public GuiConnector(AdvancedConnectorTileEntity te, EmptyContainer container, PlayerInventory inventory) {
// this((ConnectorTileEntity) te, container, inventory);
// }

public GuiConnector(ConnectorTileEntity tileEntity, GenericContainer container, Inventory inventory) {
super(tileEntity, container, inventory, CableModule.CONNECTOR.get().getManualEntry());

Level level = tileEntity.getLevel();
if (level == null) {
return;
}
XNetMessages.INSTANCE.sendToServer(new PacketRequestDataFromServer(level.dimension(), tileEntity.getBlockPos(), CMD_GET_NAME.name(), TypedMap.EMPTY, false));
imageWidth = WIDTH;
imageHeight = HEIGHT;
}
Expand All @@ -55,31 +73,59 @@ public void init() {
super.init();

Panel toplevel = vertical().filledRectThickness(2);

Level level = tileEntity.getLevel();
if (level == null) {
return;
}
BlockEntity te = level.getBlockEntity(tileEntity.getBlockPos());
if (te instanceof ConnectorTileEntity) {
tileEntity.setConnectorName(((ConnectorTileEntity) te).getConnectorName());
}
String connectorName = tileEntity.getConnectorName();
TextField nameField = new TextField().name(TAG_NAME).tooltips(CONNECTOR_NAME_TOOLTIP.i18n());

Panel namePanel = horizontal().children(label(NAME_LABEL.i18n()), nameField);
if (connectorName != null && ! connectorName.isEmpty()) {
nameField.text(tileEntity.getConnectorName());
}
Label nameLabel = label(NAME_LABEL.i18n())
.verticalAlignment(VerticalAlignment.ALIGN_CENTER)
.horizontalAlignment(HorizontalAlignment.ALIGN_LEFT);
Panel namePanel = horizontal().children(nameLabel, nameField).desiredHeight(20);
toplevel.children(namePanel);

Panel togglePanel = horizontal().
children(label(DIRECTIONS_LABEL.i18n()));
Label directionsLabel = label(DIRECTIONS_LABEL.i18n())
.desiredWidth(70)
.horizontalAlignment(HorizontalAlignment.ALIGN_LEFT);
Panel directionsPanel = horizontal().children(directionsLabel);
Panel directionValuesPanel = vertical();
Panel directionNamesPanel = horizontal(6, 5);
Panel directionBlocksPanel = horizontal();
for (Direction facing : OrientationTools.DIRECTION_VALUES) {
BlockPos consumerPos = tileEntity.getBlockPos().relative(facing);

BlockState state = level.getBlockState(consumerPos);
ItemStack item = state.getBlock().getCloneItemStack(level, consumerPos, state);
connectedBlockRenders[facing.ordinal()] = new BlockRender().renderItem(item).userObject("block");
toggleButtons[facing.ordinal()] = new ToggleButton().text(facing.getSerializedName().substring(0, 1).toUpperCase())
.desiredWidth(16)
.verticalAlignment(VerticalAlignment.ALIGN_CENTER)
.horizontalAlignment(HorizontalAlignment.ALIGN_CENTER)
.event(() -> {
sendServerCommandTyped(XNetMessages.INSTANCE, CMD_ENABLE,
TypedMap.builder()
.put(PARAM_FACING, facing.ordinal())
.put(PARAM_ENABLED, toggleButtons[facing.ordinal()].isPressed())
.build());
});
toggleButtons[facing.ordinal()].pressed(tileEntity.isEnabled(facing));
togglePanel.children(toggleButtons[facing.ordinal()]);
boolean isEnabled = !tileEntity.getBlockState().getValue(EnumProperty.create(facing.getName(), ConnectorType.class)).equals(ConnectorType.NONE);
toggleButtons[facing.ordinal()].pressed(isEnabled);
directionNamesPanel.children(toggleButtons[facing.ordinal()]);
}
toplevel.children(togglePanel);

directionBlocksPanel.children(connectedBlockRenders);
directionValuesPanel.children(directionBlocksPanel, directionNamesPanel);
directionsPanel.children(directionValuesPanel);
toplevel.children(directionsPanel);
toplevel.bounds(leftPos, topPos, WIDTH, HEIGHT);
window = new Window(this, toplevel);

window.bind(XNetMessages.INSTANCE, TAG_NAME, tileEntity, TAG_NAME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import mcjty.xnet.client.ChannelClientInfo;
import mcjty.xnet.client.ConnectedBlockClientInfo;
import mcjty.xnet.client.ConnectorClientInfo;
import mcjty.xnet.modules.controller.ChannelInfo;
import mcjty.xnet.modules.controller.ControllerModule;
import mcjty.xnet.modules.controller.blocks.TileEntityController;
import mcjty.xnet.setup.Config;
Expand Down Expand Up @@ -101,6 +102,8 @@
import static mcjty.xnet.utils.I18nConstants.CONNECTOR_REMOVE_TOOLTIP;
import static mcjty.xnet.utils.I18nConstants.CREATE_LABEL;
import static mcjty.xnet.utils.I18nConstants.DOUBLE_CLICK_HIGHLIGHT;
import static mcjty.xnet.utils.I18nConstants.EDIT_CHANNEL_TOOLTIP_EXIST_FORMATTED;
import static mcjty.xnet.utils.I18nConstants.EDIT_CHANNEL_TOOLTIP_NOT_EXIST_FORMATTED;
import static mcjty.xnet.utils.I18nConstants.ERROR_COPY;
import static mcjty.xnet.utils.I18nConstants.ERROR_LARGE_COPY;
import static mcjty.xnet.utils.I18nConstants.ERROR_NOTHING_SELECTED;
Expand Down Expand Up @@ -195,9 +198,18 @@ public void doubleClick(int index) {
}
});

ChannelInfo[] channels = tileEntity.getChannels();
for (int i = 0 ; i < MAX_CHANNELS ; i++) {
String name = TAG_CHANNEL + (i+1);
channelButtons[i] = window.findChild(name);
if (channels[i] == null) {
channelButtons[i].tooltips(EDIT_CHANNEL_TOOLTIP_NOT_EXIST_FORMATTED.i18n(i + 1));
} else {
channelButtons[i]
.tooltips(I18nUtils.getSplitedTooltip(
EDIT_CHANNEL_TOOLTIP_EXIST_FORMATTED.i18n(i + 1, channels[i].getType().getName()))
);
}
}

energyBar = window.findChild(WIDGET_ENERGY_BAR);
Expand Down Expand Up @@ -321,23 +333,40 @@ private void createConnector(SidedPos sidedPos) {
}

private void removeChannel() {
int selectedChannel = getSelectedChannel();
if (selectedChannel == -1) {
return;
}
showMessage(minecraft, this, getWindowManager(), 50, 50,
ChatFormatting.RED + String.format(MESSAGE_CONFIRM_REMOVE_CHANNEL_FORMATTED.i18n(), (getSelectedChannel() + 1)),
ChatFormatting.RED + MESSAGE_CONFIRM_REMOVE_CHANNEL_FORMATTED.i18n((selectedChannel + 1)),
() -> {
sendServerCommandTyped(XNetMessages.INSTANCE, TileEntityController.CMD_REMOVECHANNEL,
TypedMap.builder()
.put(PARAM_INDEX, getSelectedChannel())
.put(PARAM_INDEX, selectedChannel)
.build());

channelButtons[selectedChannel].tooltips(EDIT_CHANNEL_TOOLTIP_NOT_EXIST_FORMATTED.i18n(selectedChannel + 1));
refresh();
});
}

private void createChannel(String typeId) {
int selectedChannel = getSelectedChannel();
if (selectedChannel == -1) {
return;
}
sendServerCommandTyped(XNetMessages.INSTANCE, TileEntityController.CMD_CREATECHANNEL,
TypedMap.builder()
.put(PARAM_INDEX, getSelectedChannel())
.put(PARAM_INDEX, selectedChannel)
.put(PARAM_TYPE, typeId)
.build());
IChannelType type = XNet.xNetApi.findType(typeId);
if (type != null) {
channelButtons[selectedChannel].tooltips(I18nUtils.getSplitedTooltip(
EDIT_CHANNEL_TOOLTIP_EXIST_FORMATTED.i18n((selectedChannel + 1), type.getName()))
);
}

refresh();
}

Expand Down Expand Up @@ -370,7 +399,7 @@ private void refreshChannelEditor() {
ChannelClientInfo info = tileEntity.clientChannels.get(editingChannel);
if (info != null) {
ChannelEditorPanel editor = new ChannelEditorPanel(channelEditPanel, minecraft, this, editingChannel);
editor.label(String.format(CHANNEL_LABEL_FORMATTED.i18n(), (editingChannel + 1)))
editor.label(CHANNEL_LABEL_FORMATTED.i18n(editingChannel + 1))
.shift(5)
.toggle(TAG_ENABLED, CHANNEL_ENABLE_TOOLTIP.i18n(), info.isEnabled())
.shift(5)
Expand Down Expand Up @@ -492,7 +521,7 @@ private void pasteConnector() {
String type = root.get(TAG_TYPE).getAsString();
IChannelType channelType = XNet.xNetApi.findType(type);
if (channelType == null) {
showMessage(minecraft, this, getWindowManager(), 50, 50, ChatFormatting.RED + String.format(ERROR_UNS_CH_FORMATTED.i18n(), type));
showMessage(minecraft, this, getWindowManager(), 50, 50, ChatFormatting.RED + ERROR_UNS_CH_FORMATTED.i18n(type));
return;
}

Expand Down Expand Up @@ -527,7 +556,7 @@ private void pasteChannel() {
String type = root.get(TAG_TYPE).getAsString();
IChannelType channelType = XNet.xNetApi.findType(type);
if (channelType == null) {
showMessage(minecraft, this, getWindowManager(), 50, 50, ChatFormatting.RED + String.format(ERROR_UNS_CH_FORMATTED.i18n(), type));
showMessage(minecraft, this, getWindowManager(), 50, 50, ChatFormatting.RED + ERROR_UNS_CH_FORMATTED.i18n(type));
return;
}
PacketServerCommandTyped packet = new PacketServerCommandTyped(tileEntity.getBlockPos(), tileEntity.getDimension(), CMD_PASTECHANNEL.name(), TypedMap.builder()
Expand Down Expand Up @@ -736,6 +765,10 @@ protected void renderBg(@Nonnull PoseStack matrixStack, float v, int x1, int x2)
String channel = String.valueOf(i + 1);
ChannelClientInfo info = tileEntity.clientChannels.get(i);
if (info != null) {
channelButtons[i]
.tooltips(I18nUtils.getSplitedTooltip(
EDIT_CHANNEL_TOOLTIP_EXIST_FORMATTED.i18n((i + 1), info.getType().getName()))
);
IndicatorIcon icon = info.getChannelSettings().getIndicatorIcon();
if (icon != null) {
channelButtons[i].image(icon.getImage(), icon.getU(), icon.getV(), icon.getIw(), icon.getIh());
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/mcjty/xnet/utils/I18nConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ public enum I18nConstants {
ERROR_COPY("xnet.error.copy"),
ERROR_READ_COPY("xnet.error.read.copy"),
ERROR_LARGE_COPY("xnet.error.large.copy"),
ERROR_UNS_CH_FORMATTED("xnet.uns.ch.formatted");
ERROR_UNS_CH_FORMATTED("xnet.uns.ch.formatted"),

EDIT_CHANNEL_TOOLTIP_EXIST_FORMATTED("xnet.channel.edit.tooltip.exist"),
EDIT_CHANNEL_TOOLTIP_NOT_EXIST_FORMATTED("xnet.channel.edit.tooltip.notexist");


private final String langKey;
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/assets/xnet/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,8 @@
"xnet.connector.upgrade": "Sneak right click this on a|normal connector to upgrade it|to an advanced connector",
"xnet.connector.was.upgraded": "Connector was upgraded",
"xnet.connector.already.advanced": "This connector is already advanced!",
"xnet.connector.use.it.to.upgrade": "Use this item on a connector to upgrade it!"
"xnet.connector.use.it.to.upgrade": "Use this item on a connector to upgrade it!",

"xnet.channel.edit.tooltip.exist": "Edit channel %d|Type: %s",
"xnet.channel.edit.tooltip.notexist": "Edit channel %d"
}
12 changes: 7 additions & 5 deletions src/main/resources/assets/xnet/lang/ru_ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"message.xnet.redstone_proxy_upd.gold": "Эта версия обновляет блок!",

"xnet.channel.energy": "Энергия",
"xnet.channel.fluid": "Жидкость",
"xnet.channel.fluid": "Жидкости",
"xnet.channel.item": "Предметы",
"xnet.channel.logic": "Редстоун",
"xnet.min": "Мин",
Expand Down Expand Up @@ -144,9 +144,9 @@
"xnet.index.label": "Индекс",
"xnet.double.click.highlight": "(двойной клик для выделения)",
"xnet.enum.channelmode.priority": "Приоритет",
"xnet.enum.channelmode.priority.tooltip": "Режим распределения предметов|По приоритету(выше-раньше)",
"xnet.enum.channelmode.roundrobin": "Круговой",
"xnet.enum.channelmode.roundrobin.tooltip": "Режим распределения предметов|Круговое распределение",
"xnet.enum.channelmode.priority.tooltip": "Режим распределения предметов|По приоритету (выше-раньше)",
"xnet.enum.channelmode.roundrobin": "Равномерно",
"xnet.enum.channelmode.roundrobin.tooltip": "Режим распределения предметов|Равномерное распределение",
"xnet.enum.insextmode.ins": "Помещать",
"xnet.enum.insextmode.ins.tooltip": "Помещать в подключенный блок",
"xnet.enum.insextmode.ext": "Извлекать",
Expand Down Expand Up @@ -199,5 +199,7 @@
"xnet.connector.upgrade": "ПКМ сидя по коннектору|чтобы улучшить его|до продвинутого",
"xnet.connector.was.upgraded": "Коннектор улучшен",
"xnet.connector.already.advanced": "Коннектор уже улучшен!",
"xnet.connector.use.it.to.upgrade": "Используйте этот предмет, чтобы улучшить коннектор!"
"xnet.connector.use.it.to.upgrade": "Используйте этот предмет, чтобы улучшить коннектор!",
"xnet.channel.edit.tooltip.exist": "Настроить канал %d|Тип: %s",
"xnet.channel.edit.tooltip.notexist": "Настроить канал %d"
}

0 comments on commit d778ba6

Please sign in to comment.