Skip to content

Commit 939c998

Browse files
authored
feat: Port various QoL changes from 1.21.1
1 parent c87228e commit 939c998

File tree

21 files changed

+212
-75
lines changed

21 files changed

+212
-75
lines changed

src/conduits/java/com/enderio/conduits/client/gui/RedstoneTimerFilterScreen.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.enderio.base.common.lang.EIOLang;
66
import com.enderio.conduits.common.menu.RedstoneTimerFilterMenu;
77
import com.enderio.core.client.gui.screen.EIOScreen;
8+
import com.enderio.core.client.gui.widgets.EIOImageButton;
9+
810
import net.minecraft.client.gui.components.Button;
911
import net.minecraft.client.gui.components.EditBox;
1012
import net.minecraft.network.chat.Component;
@@ -15,6 +17,7 @@ public class RedstoneTimerFilterScreen extends EIOScreen<RedstoneTimerFilterMenu
1517

1618
private static final Vector2i BG_SIZE = new Vector2i(183,201);
1719
private static final ResourceLocation BG_TEXTURE = EnderIO.loc("textures/gui/40/item_filter.png");
20+
private static final ResourceLocation BACK_TEXTURE = EnderIO.loc("textures/gui/icons/back.png");
1821

1922
public RedstoneTimerFilterScreen(RedstoneTimerFilterMenu pMenu, Inventory pPlayerInventory, Component pTitle) {
2023
super(pMenu, pPlayerInventory, pTitle);
@@ -23,7 +26,7 @@ public RedstoneTimerFilterScreen(RedstoneTimerFilterMenu pMenu, Inventory pPlaye
2326
@Override
2427
protected void init() {
2528
super.init();
26-
EditBox pWidget = new EditBox(this.font, this.leftPos + 60, this.topPos + 20, 60, 20, Component.literal("" + getMenu().getFilter().getMaxTicks())) {
29+
EditBox pWidget = new EditBox(this.font, this.leftPos + 65, this.topPos + 40, 60, 20, Component.literal("" + getMenu().getFilter().getMaxTicks())) {
2730
@Override
2831
public boolean charTyped(char pCodePoint, int pModifiers) {
2932
return Character.isDigit(pCodePoint) && super.charTyped(pCodePoint, pModifiers);
@@ -32,9 +35,10 @@ public boolean charTyped(char pCodePoint, int pModifiers) {
3235
pWidget.setValue("" + getMenu().getFilter().getMaxTicks());
3336
addRenderableWidget(pWidget);
3437
addRenderableWidget(Button.builder(EIOLang.CONFIRM, pButton -> getMenu().setTimer(pWidget.getValue()))
35-
.pos(this.leftPos + 60, this.topPos + 41)
38+
.pos(this.leftPos + 65, this.topPos + 62)
3639
.size(60, 20)
3740
.build());
41+
addRenderableWidget(new EIOImageButton(this, getGuiLeft() + 3, getGuiTop() + 3, 16, 16, 0, 0, 0, BACK_TEXTURE, button -> closeContainer()));
3842
}
3943

4044
@Override

src/conduits/java/com/enderio/conduits/common/init/ConduitItems.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public class ConduitItems {
108108
.end();
109109
})
110110
.tab(EIOCreativeTabs.GEAR)
111+
.properties(props -> props.stacksTo(1))
111112
.register();
112113

113114
private static ItemEntry<Item> createConduitItem(Supplier<? extends ConduitType<?>> type, String itemName) {

src/conduits/java/com/enderio/conduits/common/items/ConduitProbeItem.java

Lines changed: 98 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.enderio.conduits.common.conduit.connection.DynamicConnectionState;
1010
import com.enderio.conduits.common.conduit.type.item.ItemConduitData.ItemSidedData;
1111
import com.enderio.conduits.common.conduit.type.item.ItemConduitType;
12+
import com.enderio.conduits.common.init.ConduitLang;
1213
import com.enderio.conduits.common.network.C2SSyncProbeState;
1314
import com.enderio.conduits.common.util.InteractionUtil;
1415
import com.enderio.core.common.network.CoreNetwork;
@@ -17,18 +18,24 @@
1718
import net.minecraft.core.Direction;
1819
import net.minecraft.nbt.CompoundTag;
1920
import net.minecraft.network.chat.Component;
21+
import net.minecraft.network.chat.MutableComponent;
22+
import net.minecraft.resources.ResourceLocation;
23+
import net.minecraft.world.entity.player.Player;
2024
import net.minecraft.world.InteractionResult;
2125
import net.minecraft.world.item.Item;
2226
import net.minecraft.world.item.ItemStack;
2327
import net.minecraft.world.item.TooltipFlag;
2428
import net.minecraft.world.item.context.UseOnContext;
2529
import net.minecraft.world.level.Level;
2630
import net.minecraft.world.level.block.entity.BlockEntity;
31+
import com.enderio.base.common.lang.EIOLang;
2732
import org.apache.commons.lang3.StringUtils;
2833
import org.jetbrains.annotations.Nullable;
2934

35+
import java.util.ArrayList;
3036
import java.util.List;
3137
import java.util.Objects;
38+
import java.util.Set;
3239

3340
public class ConduitProbeItem extends Item {
3441
public static final String STATE_FIELD = "STATE";
@@ -42,7 +49,10 @@ public class ConduitProbeItem extends Item {
4249
public static final String ROUND_ROBIN = "ROUND_ROBIN";
4350
public static final String SELF_FEED = "SELF_FEED";
4451
public static final String PRIORITY = "PRIORITY";
45-
52+
53+
public static final Set<String> BOOL_TAG = Set.of(IS_INSERT, IS_EXTRACT, ROUND_ROBIN, SELF_FEED);
54+
public static final Set<String> COLOR_CONTROL_TAG = Set.of(INSERT_CHANNEL, EXTRACT_CHANNEL, REDSTONE_CHANNEL);
55+
4656
public ConduitProbeItem(Properties properties) {
4757
super(properties);
4858
}
@@ -68,39 +78,50 @@ public static void switchState(ItemStack stack, boolean syncToServer) {
6878
@Override
6979
public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) {
7080
BlockEntity block = context.getLevel().getBlockEntity(context.getClickedPos());
71-
if (block instanceof ConduitBlockEntity conduit) {
72-
if (context.getLevel().isClientSide()) return InteractionResult.SUCCESS;
73-
switch (getState(stack)) {
81+
Player player = context.getPlayer();
82+
if (player == null) {
83+
return InteractionResult.FAIL;
84+
}
85+
if (context.getLevel().isClientSide()) {
86+
return InteractionResult.SUCCESS;
87+
}
88+
if (!(block instanceof ConduitBlockEntity conduit)) {
89+
return super.onItemUseFirst(stack, context);
90+
}
91+
92+
switch (getState(stack)) {
7493
case COPY_PASTE -> {
7594
if (context.isSecondaryUseActive()) {
7695
handleCopy(conduit,
7796
InteractionUtil.fromClickLocation(context.getClickLocation(), context.getClickedPos().getCenter()),
78-
stack);
97+
stack,
98+
player);
7999
} else {
80100
handlePaste(conduit,
81101
InteractionUtil.fromClickLocation(context.getClickLocation(), context.getClickedPos().getCenter()),
82-
stack);
102+
stack,
103+
player);
83104
}
84105
}
85106
case PROBE -> {
86-
context.getPlayer().sendSystemMessage(Component.literal("This feature isn't implemented yet.").withStyle(ChatFormatting.RED));
87-
}
107+
player.sendSystemMessage(Component.literal("This feature isn't implemented yet.").withStyle(ChatFormatting.RED));
88108
}
89-
return InteractionResult.SUCCESS;
90109
}
91-
return super.onItemUseFirst(stack, context);
110+
return InteractionResult.SUCCESS;
92111
}
93112

94-
private void handleCopy(ConduitBlockEntity conduitBlock, Direction face, ItemStack itemStack) {
113+
private void handleCopy(ConduitBlockEntity conduitBlock, Direction face, ItemStack itemStack, Player player) {
95114
CompoundTag tag = itemStack.getOrCreateTag();
96115
CompoundTag conduitData = new CompoundTag();
97116
tag.put(CONDUIT_DATA, conduitData);
98117
ConduitBundle bundle = conduitBlock.getBundle();
99118
if (bundle.getConnectedTypes(face).isEmpty()) {
100119
return;
101120
}
102-
bundle.getTypes().forEach(conduitType -> {
103-
ConnectionState connectionState = bundle.getConnectionState(face, conduitType);
121+
MutableComponent message = Component.empty();
122+
bundle.getTypes().forEach(conduit -> {
123+
String conduitKey = ConduitType.getKey(conduit).toString();
124+
ConnectionState connectionState = bundle.getConnectionState(face, conduit);
104125
CompoundTag typeTag = new CompoundTag();
105126
if (connectionState instanceof DynamicConnectionState dynamic) {
106127
typeTag.putBoolean(IS_INSERT, dynamic.isInsert());
@@ -110,7 +131,7 @@ private void handleCopy(ConduitBlockEntity conduitBlock, Direction face, ItemSta
110131
typeTag.putInt(REDSTONE_CONTROL, dynamic.control().ordinal());
111132
typeTag.putInt(REDSTONE_CHANNEL, dynamic.redstoneChannel().ordinal());
112133

113-
if (conduitType instanceof ItemConduitType itemConduitType) {
134+
if (conduit instanceof ItemConduitType itemConduitType) {
114135
ItemSidedData sidedData = bundle.getNodeFor(itemConduitType).getConduitData().get(face);
115136
typeTag.putBoolean(ROUND_ROBIN, sidedData.isRoundRobin);
116137
typeTag.putBoolean(SELF_FEED, sidedData.isSelfFeed);
@@ -120,19 +141,44 @@ private void handleCopy(ConduitBlockEntity conduitBlock, Direction face, ItemSta
120141
typeTag.putBoolean(IS_INSERT, false);
121142
typeTag.putBoolean(IS_EXTRACT, false);
122143
}
123-
conduitData.put(Objects.requireNonNull(ConduitType.getKey(conduitType)).toString(), typeTag);
144+
conduitData.put(conduitKey, typeTag);
145+
146+
message.append(Component.literal("\n" + conduitKeyToDisplayName(conduitKey).getString() + ":\n").withStyle(ChatFormatting.UNDERLINE));
147+
typeTag.getAllKeys().forEach(key -> {
148+
StringBuilder sb = new StringBuilder();
149+
sb.append(" - " + key + ": ");
150+
if (BOOL_TAG.contains(key)) {
151+
sb.append(typeTag.getBoolean(key));
152+
} else if (COLOR_CONTROL_TAG.contains(key)) {
153+
sb.append(ColorControl.values()[typeTag.getInt(key)]);
154+
} else if (key.equals(REDSTONE_CONTROL)) {
155+
sb.append(RedstoneControl.values()[typeTag.getInt(key)]);
156+
} else {
157+
sb.append(typeTag.get(key));
158+
}
159+
sb.append("\n");
160+
message.append(Component.literal(sb.toString()).withStyle(ChatFormatting.GRAY));
161+
});
124162
});
163+
player.sendSystemMessage(TooltipUtil.withArgs(EIOLang.CONDUIT_PROBE_MESSAGE_COPIED, message));
125164
}
126-
127-
public void handlePaste(ConduitBlockEntity conduitBlock, Direction face, ItemStack itemStack) {
165+
166+
public void handlePaste(ConduitBlockEntity conduitBlock, Direction face, ItemStack itemStack, Player player) {
128167
CompoundTag tag = itemStack.getTag();
129168
if (tag == null || !(tag.get(CONDUIT_DATA) instanceof CompoundTag conduitData)) {
130169
return;
131170
}
171+
172+
List<String> pastedConduits = new ArrayList<String>();
132173
ConduitBundle bundle = conduitBlock.getBundle();
133-
bundle.getTypes().forEach(conduitType -> {
134-
CompoundTag typeTag = conduitData.getCompound(Objects.requireNonNull(ConduitType.getKey(conduitType)).toString());
135-
ConnectionState prevConnectionState = bundle.getConnectionState(face, conduitType);
174+
bundle.getTypes().forEach(conduit -> {
175+
String conduitKey = ConduitType.getKey(conduit).toString();
176+
CompoundTag typeTag = conduitData.getCompound(conduitKey);
177+
if (typeTag == null || typeTag.isEmpty()) {
178+
return;
179+
}
180+
181+
ConnectionState prevConnectionState = bundle.getConnectionState(face, conduit);
136182
DynamicConnectionState connectionState = null;
137183
if (prevConnectionState instanceof DynamicConnectionState) connectionState = (DynamicConnectionState) prevConnectionState;
138184
boolean wasConnected = connectionState != null;
@@ -144,15 +190,21 @@ public void handlePaste(ConduitBlockEntity conduitBlock, Direction face, ItemSta
144190
ColorControl.values()[typeTag.getInt(REDSTONE_CHANNEL)], wasConnected ? connectionState.filterInsert() : ItemStack.EMPTY,
145191
wasConnected ? connectionState.filterExtract() : ItemStack.EMPTY, wasConnected ? connectionState.upgradeExtract() : ItemStack.EMPTY
146192
);
147-
conduitBlock.handleConnectionStateUpdate(face, conduitType, newState);
193+
conduitBlock.handleConnectionStateUpdate(face, conduit, newState);
148194

149-
if (conduitType instanceof ItemConduitType itemConduitType && typeTag.contains(ROUND_ROBIN)) {
195+
if (conduit instanceof ItemConduitType itemConduitType && typeTag.contains(ROUND_ROBIN)) {
150196
ItemSidedData sidedData = bundle.getNodeFor(itemConduitType).getConduitData().compute(face);
151197
sidedData.isRoundRobin = typeTag.getBoolean(ROUND_ROBIN);
152198
sidedData.isSelfFeed = typeTag.getBoolean(SELF_FEED);
153199
sidedData.setPriority(typeTag.getInt(PRIORITY));
154200
}
201+
pastedConduits.add(conduitKeyToDisplayName(conduitKey).getString());
155202
});
203+
204+
if (!pastedConduits.isEmpty()) {
205+
String pastedConduitsString = String.join(", ", pastedConduits);
206+
player.sendSystemMessage(TooltipUtil.withArgs(EIOLang.CONDUIT_PROBE_MESSAGE_PASTED, pastedConduitsString));
207+
}
156208
conduitBlock.setChanged();
157209
conduitBlock.updateClient();
158210
}
@@ -162,19 +214,35 @@ public void appendHoverText(ItemStack stack, @Nullable Level level, List<Compone
162214
if (!(stack.getItem() instanceof ConduitProbeItem)) {
163215
return;
164216
}
165-
StringBuilder builder = new StringBuilder();
166-
for (String s : ConduitProbeItem.getState(stack).toString().toLowerCase().split("_")) {
167-
builder.append(StringUtils.capitalize(s));
168-
builder.append(" ");
217+
218+
tooltipComponents.add(TooltipUtil.styledWithArgs(EIOLang.CONDUIT_PROBE_MODE, ConduitProbeItem.getState(stack).getStateText()));
219+
220+
CompoundTag tag = stack.getTag();
221+
if (tag != null && (tag.get(CONDUIT_DATA) instanceof CompoundTag conduitData) && !conduitData.getAllKeys().isEmpty()) {
222+
tooltipComponents.add(EIOLang.CONDUIT_PROBE_CONTAINS_COPIED.withStyle(ChatFormatting.GRAY));
223+
conduitData.getAllKeys().forEach(conduitKey -> {
224+
tooltipComponents.add(Component.literal("- " + conduitKeyToDisplayName(conduitKey).getString()).withStyle(ChatFormatting.DARK_GRAY));
225+
});
169226
}
170-
builder.deleteCharAt(builder.length() - 1);
171-
tooltipComponents.add(TooltipUtil.style(Component.translatable("tooltip.enderio.conduit_probe.mode", builder.toString())));
172-
227+
173228
super.appendHoverText(stack, level, tooltipComponents, isAdvanced);
174229
}
175230

231+
// Can't use ResourceLocation because of backwards compatibility of conduitData
232+
private Component conduitKeyToDisplayName(String conduitKey) {
233+
String translationKey = "item." + conduitKey.replace(":", ".");
234+
return Component.translatable(translationKey);
235+
}
236+
176237
public enum State {
177238
PROBE,
178-
COPY_PASTE
239+
COPY_PASTE;
240+
241+
public Component getStateText() {
242+
return switch (this) {
243+
case PROBE -> EIOLang.CONDUIT_PROBE_PROBE;
244+
case COPY_PASTE -> EIOLang.CONDUIT_PROBE_COPY_PASTE;
245+
};
246+
}
179247
}
180248
}

src/conduits/java/com/enderio/conduits/common/network/C2SSyncProbeState.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import com.enderio.conduits.common.items.ConduitProbeItem;
44
import com.enderio.core.common.network.Packet;
5+
import com.enderio.core.common.util.TooltipUtil;
6+
57
import net.minecraft.network.FriendlyByteBuf;
6-
import net.minecraft.network.chat.Component;
78
import net.minecraft.world.item.ItemStack;
89
import net.minecraftforge.network.NetworkDirection;
910
import net.minecraftforge.network.NetworkEvent;
11+
import com.enderio.base.common.lang.EIOLang;
1012

1113
import java.util.Optional;
1214

@@ -36,7 +38,7 @@ public void handle(NetworkEvent.Context context) {
3638
probeStack = context.getSender().getOffhandItem();
3739
}
3840
ConduitProbeItem.setState(probeStack, state, false);
39-
context.getSender().sendSystemMessage(Component.literal("Changed probe to " + state));
41+
context.getSender().sendSystemMessage(TooltipUtil.withArgs(EIOLang.CONDUIT_PROBE_SWITCHED_MODE, state.getStateText()));
4042
});
4143
context.setPacketHandled(true);
4244
}

src/core/java/com/enderio/core/client/gui/screen/EIOScreen.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected void renderBg(GuiGraphics guiGraphics, float pPartialTick, int pMouseX
8787
@Override
8888
public boolean keyPressed(int pKeyCode, int pScanCode, int pModifiers) {
8989
if (pKeyCode == 256) { //ESC has priority
90-
Minecraft.getInstance().player.closeContainer();
90+
closeContainer();
9191
}
9292
for (EditBox editBox : editBoxList) {
9393
if (editBox.keyPressed(pKeyCode, pScanCode, pModifiers) || editBox.canConsumeInput()) {
@@ -160,4 +160,11 @@ protected void removeWidget(GuiEventListener guiEventListener) {
160160
public void addTooltip(LateTooltipData data) {
161161
tooltips.add(data);
162162
}
163+
164+
protected void closeContainer() {
165+
var player = Minecraft.getInstance().player;
166+
if (player != null) {
167+
player.closeContainer();
168+
}
169+
}
163170
}

src/core/java/com/enderio/core/client/gui/widgets/EIOImageButton.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.enderio.core.client.gui.widgets;
22

3-
import com.enderio.api.misc.Vector2i;
43
import com.enderio.core.client.gui.screen.EIOScreen;
4+
import com.enderio.core.EnderCore;
55
import net.minecraft.client.gui.GuiGraphics;
66
import net.minecraft.client.gui.components.ImageButton;
77
import net.minecraft.network.chat.Component;
@@ -12,6 +12,7 @@
1212
public class EIOImageButton extends ImageButton {
1313

1414
private final EIOScreen<?> addedOn;
15+
private static final ResourceLocation BUTTON_TEXTURE = EnderCore.loc("textures/gui/checkbox.png");
1516

1617
public EIOImageButton(EIOScreen<?> addedOn, int x, int y, int width, int height, int xTexStart, int yTexStart, ResourceLocation resourceLocation,
1718
OnPress onPress) {
@@ -39,7 +40,7 @@ public EIOImageButton(EIOScreen<?> addedOn, int x, int y, int width, int height,
3940

4041
@Override
4142
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
42-
addedOn.renderSimpleArea(guiGraphics, new Vector2i(getX(), getY()), new Vector2i(getX() + getWidth(), getY() + getHeight()));
43+
guiGraphics.blit(BUTTON_TEXTURE, getX(), getY(), this.width, this.height, this.isHovered ? 28 : 0, 0, 14, 14, 256, 256);
4344
super.renderWidget(guiGraphics, mouseX, mouseY, partialTick);
4445
}
4546
}

0 commit comments

Comments
 (0)