From 0121cc2a9332ea80a9e8b3859e3bc35b4160d1b1 Mon Sep 17 00:00:00 2001 From: thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> Date: Wed, 27 Nov 2024 21:03:47 -0800 Subject: [PATCH] WIP fix mail GUIs --- .../java/forestry/api/mail/IPostRegistry.java | 35 +++++++++---- .../forestry/mail/EventHandlerMailAlert.java | 5 +- src/main/java/forestry/mail/PostRegistry.java | 31 +++--------- .../forestry/mail/blocks/BlockTypeMail.java | 4 +- .../mail/client/MailClientHandler.java | 8 +-- .../forestry/mail/features/MailMenuTypes.java | 8 +-- .../forestry/mail/features/MailTiles.java | 4 +- .../forestry/mail/gui/ContainerLetter.java | 6 +-- .../java/forestry/mail/gui/GuiLetter.java | 49 +++++++++---------- ...ainerTrader.java => TradeStationMenu.java} | 12 ++--- ...eName.java => TradeStationNamingMenu.java} | 22 +++++---- ...ame.java => TradeStationNamingScreen.java} | 42 +++++++++------- ...GuiTrader.java => TradeStationScreen.java} | 22 ++++----- .../PacketLetterInfoResponsePlayer.java | 2 +- .../PacketLetterInfoResponseTrader.java | 2 +- .../packets/PacketTraderAddressRequest.java | 6 +-- .../packets/PacketTraderAddressResponse.java | 4 +- .../java/forestry/mail/tiles/TileMailbox.java | 2 +- ...ader.java => TradeStationBlockEntity.java} | 20 ++++---- 19 files changed, 142 insertions(+), 142 deletions(-) rename src/main/java/forestry/mail/gui/{ContainerTrader.java => TradeStationMenu.java} (83%) mode change 100755 => 100644 rename src/main/java/forestry/mail/gui/{ContainerTradeName.java => TradeStationNamingMenu.java} (61%) mode change 100755 => 100644 rename src/main/java/forestry/mail/gui/{GuiTradeName.java => TradeStationNamingScreen.java} (69%) mode change 100755 => 100644 rename src/main/java/forestry/mail/gui/{GuiTrader.java => TradeStationScreen.java} (77%) mode change 100755 => 100644 rename src/main/java/forestry/mail/tiles/{TileTrader.java => TradeStationBlockEntity.java} (94%) diff --git a/src/main/java/forestry/api/mail/IPostRegistry.java b/src/main/java/forestry/api/mail/IPostRegistry.java index 93f57884a..199cc5ff0 100644 --- a/src/main/java/forestry/api/mail/IPostRegistry.java +++ b/src/main/java/forestry/api/mail/IPostRegistry.java @@ -8,21 +8,19 @@ import javax.annotation.Nullable; import java.util.Map; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.item.ItemStack; import com.mojang.authlib.GameProfile; public interface IPostRegistry { - /* POST OFFICE */ - IPostOffice getPostOffice(ServerLevel world); + IPostOffice getPostOffice(ServerLevel level); /* MAIL ADDRESSES */ - IMailAddress getMailAddress(GameProfile gameProfile); + IMailAddress createMailAddress(GameProfile gameProfile); - IMailAddress getMailAddress(String traderName); + IMailAddress createMailAddress(String traderName); /* LETTERS */ boolean isLetter(ItemStack itemstack); @@ -56,11 +54,28 @@ public interface IPostRegistry { @Nullable ITradeStation getTradeStation(ServerLevel world, IMailAddress address); + /** + * Determines if a mailing address is not already in use. + * + * @param world the Minecraft world the Trader will be in + * @param address the potential address of the Trader + * @return {@code true} if the trade address has not yet been used before. + */ boolean isAvailableTradeAddress(ServerLevel world, IMailAddress address); - boolean isValidTradeAddress(Level world, IMailAddress address); - - /* PO BOXES */ - boolean isValidPOBox(Level world, IMailAddress address); + /** + * Determines whether a mailing address is valid for a Trade Station. + * + * @param address the potential address of the Trader + * @return {@code true} if the address is alphanumeric and is for a trade station + */ + boolean isValidTradeAddress(IMailAddress address); + /** + * Determines whether a mailing address is valid for a player PO box. + * + * @param address the potential address of the PO box + * @return {@code true} if the passed address is valid for PO Boxes. + */ + boolean isValidPOBox(IMailAddress address); } diff --git a/src/main/java/forestry/mail/EventHandlerMailAlert.java b/src/main/java/forestry/mail/EventHandlerMailAlert.java index 1f410daff..fd8d3ab8f 100644 --- a/src/main/java/forestry/mail/EventHandlerMailAlert.java +++ b/src/main/java/forestry/mail/EventHandlerMailAlert.java @@ -11,13 +11,10 @@ package forestry.mail; import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiGraphics; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; -import com.mojang.blaze3d.vertex.PoseStack; - import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.event.TickEvent; @@ -48,7 +45,7 @@ public void onRenderTick(TickEvent.RenderTickEvent event) { public void handlePlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { Player player = event.getEntity(); - IMailAddress address = PostManager.postRegistry.getMailAddress(player.getGameProfile()); + IMailAddress address = PostManager.postRegistry.createMailAddress(player.getGameProfile()); POBox pobox = PostRegistry.getOrCreatePOBox((ServerLevel) player.level(), address); PacketPOBoxInfoResponse packet = new PacketPOBoxInfoResponse(pobox.getPOBoxInfo()); NetworkUtil.sendToPlayer(packet, (ServerPlayer) player); diff --git a/src/main/java/forestry/mail/PostRegistry.java b/src/main/java/forestry/mail/PostRegistry.java index 502ad619e..106dd517b 100755 --- a/src/main/java/forestry/mail/PostRegistry.java +++ b/src/main/java/forestry/mail/PostRegistry.java @@ -20,7 +20,6 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; import com.mojang.authlib.GameProfile; @@ -46,13 +45,8 @@ public class PostRegistry implements IPostRegistry { private final Map carriers = new EnumMap<>(EnumAddressee.class); - /** - * @param world the Minecraft world the PO box will be in - * @param address the potential address of the PO box - * @return true if the passed address is valid for PO Boxes. - */ @Override - public boolean isValidPOBox(Level world, IMailAddress address) { + public boolean isValidPOBox(IMailAddress address) { return address.getType() == EnumAddressee.PLAYER && address.getName().matches("^[a-zA-Z0-9]+$"); } @@ -85,21 +79,11 @@ public static POBox getOrCreatePOBox(ServerLevel world, IMailAddress add) { return pobox; } - /** - * @param world the Minecraft world the Trader will be in - * @param address the potential address of the Trader - * @return true if the passed address can be an address for a trade station - */ @Override - public boolean isValidTradeAddress(Level world, IMailAddress address) { + public boolean isValidTradeAddress(IMailAddress address) { return address.getType() == EnumAddressee.TRADER && address.getName().matches("^[a-zA-Z0-9]+$"); } - /** - * @param world the Minecraft world the Trader will be in - * @param address the potential address of the Trader - * @return true if the trade address has not yet been used before. - */ @Override public boolean isAvailableTradeAddress(ServerLevel world, IMailAddress address) { return getTradeStation(world, address) == null; @@ -157,27 +141,26 @@ public void deleteTradeStation(ServerLevel world, IMailAddress address) { } @Override - public IPostOffice getPostOffice(ServerLevel world) { + public IPostOffice getPostOffice(ServerLevel level) { if (cachedPostOffice != null) { return cachedPostOffice; } - PostOffice office = world.getDataStorage().computeIfAbsent(PostOffice::new, PostOffice::new, PostOffice.SAVE_NAME); + PostOffice office = level.getDataStorage().computeIfAbsent(PostOffice::new, PostOffice::new, PostOffice.SAVE_NAME); - office.setWorld(world); + office.setWorld(level); cachedPostOffice = office; return office; } - @Override - public IMailAddress getMailAddress(GameProfile gameProfile) { + public IMailAddress createMailAddress(GameProfile gameProfile) { return new MailAddress(gameProfile); } @Override - public IMailAddress getMailAddress(String traderName) { + public IMailAddress createMailAddress(String traderName) { return new MailAddress(traderName); } diff --git a/src/main/java/forestry/mail/blocks/BlockTypeMail.java b/src/main/java/forestry/mail/blocks/BlockTypeMail.java index 3fa797fce..c894e5ecf 100644 --- a/src/main/java/forestry/mail/blocks/BlockTypeMail.java +++ b/src/main/java/forestry/mail/blocks/BlockTypeMail.java @@ -19,12 +19,12 @@ import forestry.core.tiles.TileForestry; import forestry.mail.features.MailTiles; import forestry.mail.tiles.TileStampCollector; -import forestry.mail.tiles.TileTrader; +import forestry.mail.tiles.TradeStationBlockEntity; import forestry.modules.features.FeatureTileType; public enum BlockTypeMail implements IBlockType { MAILBOX(MailTiles.MAILBOX, "mailbox", null), - TRADE_STATION(MailTiles.TRADER, "trade_station", TileTrader::serverTick), + TRADE_STATION(MailTiles.TRADER, "trade_station", TradeStationBlockEntity::serverTick), STAMP_COLLETOR(MailTiles.STAMP_COLLECTOR, "stamp_collector", TileStampCollector::serverTick); private final IMachineProperties machineProperties; diff --git a/src/main/java/forestry/mail/client/MailClientHandler.java b/src/main/java/forestry/mail/client/MailClientHandler.java index 95a6626be..20ccdbaab 100644 --- a/src/main/java/forestry/mail/client/MailClientHandler.java +++ b/src/main/java/forestry/mail/client/MailClientHandler.java @@ -12,8 +12,8 @@ import forestry.mail.gui.GuiLetter; import forestry.mail.gui.GuiMailbox; import forestry.mail.gui.GuiStampCollector; -import forestry.mail.gui.GuiTradeName; -import forestry.mail.gui.GuiTrader; +import forestry.mail.gui.TradeStationNamingScreen; +import forestry.mail.gui.TradeStationScreen; public class MailClientHandler implements IClientModuleHandler { @Override @@ -27,8 +27,8 @@ private static void onClientSetup(FMLClientSetupEvent event) { MenuScreens.register(MailMenuTypes.LETTER.menuType(), GuiLetter::new); MenuScreens.register(MailMenuTypes.MAILBOX.menuType(), GuiMailbox::new); MenuScreens.register(MailMenuTypes.STAMP_COLLECTOR.menuType(), GuiStampCollector::new); - MenuScreens.register(MailMenuTypes.TRADE_NAME.menuType(), GuiTradeName::new); - MenuScreens.register(MailMenuTypes.TRADER.menuType(), GuiTrader::new); + MenuScreens.register(MailMenuTypes.TRADE_NAME.menuType(), TradeStationNamingScreen::new); + MenuScreens.register(MailMenuTypes.TRADER.menuType(), TradeStationScreen::new); }); } } diff --git a/src/main/java/forestry/mail/features/MailMenuTypes.java b/src/main/java/forestry/mail/features/MailMenuTypes.java index 248485128..4f4f385d4 100644 --- a/src/main/java/forestry/mail/features/MailMenuTypes.java +++ b/src/main/java/forestry/mail/features/MailMenuTypes.java @@ -5,8 +5,8 @@ import forestry.mail.gui.ContainerLetter; import forestry.mail.gui.ContainerMailbox; import forestry.mail.gui.ContainerStampCollector; -import forestry.mail.gui.ContainerTradeName; -import forestry.mail.gui.ContainerTrader; +import forestry.mail.gui.TradeStationNamingMenu; +import forestry.mail.gui.TradeStationMenu; import forestry.modules.features.FeatureMenuType; import forestry.modules.features.FeatureProvider; import forestry.modules.features.IFeatureRegistry; @@ -20,6 +20,6 @@ public class MailMenuTypes { public static final FeatureMenuType LETTER = REGISTRY.menuType(ContainerLetter::fromNetwork, "letter"); public static final FeatureMenuType MAILBOX = REGISTRY.menuType(ContainerMailbox::fromNetwork, "mailbox"); public static final FeatureMenuType STAMP_COLLECTOR = REGISTRY.menuType(ContainerStampCollector::fromNetwork, "stamp_collector"); - public static final FeatureMenuType TRADE_NAME = REGISTRY.menuType(ContainerTradeName::fromNetwork, "trade_name"); - public static final FeatureMenuType TRADER = REGISTRY.menuType(ContainerTrader::fromNetwork, "trader"); + public static final FeatureMenuType TRADE_NAME = REGISTRY.menuType(TradeStationNamingMenu::fromNetwork, "trade_name"); + public static final FeatureMenuType TRADER = REGISTRY.menuType(TradeStationMenu::fromNetwork, "trader"); } diff --git a/src/main/java/forestry/mail/features/MailTiles.java b/src/main/java/forestry/mail/features/MailTiles.java index 2c27aac93..6cf519e58 100644 --- a/src/main/java/forestry/mail/features/MailTiles.java +++ b/src/main/java/forestry/mail/features/MailTiles.java @@ -4,7 +4,7 @@ import forestry.mail.blocks.BlockTypeMail; import forestry.mail.tiles.TileMailbox; import forestry.mail.tiles.TileStampCollector; -import forestry.mail.tiles.TileTrader; +import forestry.mail.tiles.TradeStationBlockEntity; import forestry.modules.features.FeatureProvider; import forestry.modules.features.FeatureTileType; import forestry.modules.features.IFeatureRegistry; @@ -16,5 +16,5 @@ public class MailTiles { public static final FeatureTileType MAILBOX = REGISTRY.tile(TileMailbox::new, "mailbox", () -> MailBlocks.BASE.get(BlockTypeMail.MAILBOX).collect()); public static final FeatureTileType STAMP_COLLECTOR = REGISTRY.tile(TileStampCollector::new, "stamp_collector", () -> MailBlocks.BASE.get(BlockTypeMail.STAMP_COLLETOR).collect()); - public static final FeatureTileType TRADER = REGISTRY.tile(TileTrader::new, "trader", () -> MailBlocks.BASE.get(BlockTypeMail.TRADE_STATION).collect()); + public static final FeatureTileType TRADER = REGISTRY.tile(TradeStationBlockEntity::new, "trader", () -> MailBlocks.BASE.get(BlockTypeMail.TRADE_STATION).collect()); } diff --git a/src/main/java/forestry/mail/gui/ContainerLetter.java b/src/main/java/forestry/mail/gui/ContainerLetter.java index 49b840837..e324bffcd 100755 --- a/src/main/java/forestry/mail/gui/ContainerLetter.java +++ b/src/main/java/forestry/mail/gui/ContainerLetter.java @@ -92,7 +92,7 @@ public void removed(Player PlayerEntity) { if (!PlayerEntity.level().isClientSide) { ILetter letter = inventory.getLetter(); if (!letter.isProcessed()) { - IMailAddress sender = PostManager.postRegistry.getMailAddress(PlayerEntity.getGameProfile()); + IMailAddress sender = PostManager.postRegistry.createMailAddress(PlayerEntity.getGameProfile()); letter.setSender(sender); } } @@ -160,8 +160,8 @@ public void handleRequestLetterInfo(Player player, String recipientName, EnumAdd @Nullable private static IMailAddress getRecipient(MinecraftServer minecraftServer, String recipientName, EnumAddressee type) { return switch (type) { - case PLAYER -> minecraftServer.getProfileCache().get(recipientName).map(PostManager.postRegistry::getMailAddress).orElse(null); - case TRADER -> PostManager.postRegistry.getMailAddress(recipientName); + case PLAYER -> minecraftServer.getProfileCache().get(recipientName).map(PostManager.postRegistry::createMailAddress).orElse(null); + case TRADER -> PostManager.postRegistry.createMailAddress(recipientName); }; } diff --git a/src/main/java/forestry/mail/gui/GuiLetter.java b/src/main/java/forestry/mail/gui/GuiLetter.java index af5028b57..81cb5ddde 100755 --- a/src/main/java/forestry/mail/gui/GuiLetter.java +++ b/src/main/java/forestry/mail/gui/GuiLetter.java @@ -86,32 +86,33 @@ public void init() { @Override public boolean keyPressed(int key, int scanCode, int modifiers) { - - // Set focus or enter text into address - if (this.address.isFocused()) { - if (key == GLFW.GLFW_KEY_ENTER) { - this.address.setFocused(false); - } else { - this.address.keyPressed(key, scanCode, modifiers); + if (key != GLFW.GLFW_KEY_ESCAPE && key != GLFW.GLFW_KEY_TAB) { + // Set focus or enter text into address + if (this.address.isFocused()) { + if (key == GLFW.GLFW_KEY_ENTER) { + this.address.setFocused(false); + }/* else { + this.address.keyPressed(key, scanCode, modifiers); + } + return true;*/ } - return true; - } - if (this.text.isFocused()) { - if (key == GLFW.GLFW_KEY_ENTER) { - if (hasShiftDown()) { - text.setValue(text.getValue() + "\n"); - } else { - this.text.setFocused(false); + if (this.text.isFocused()) { + if (key == GLFW.GLFW_KEY_ENTER) { + if (hasShiftDown()) { + this.text.setValue(text.getValue() + "\n"); + } else { + this.text.setFocused(false); + } + } else if (key == GLFW.GLFW_KEY_DOWN) { + text.advanceLine(); + } else if (key == GLFW.GLFW_KEY_UP) { + text.regressLine(); + }/* else if (text.moreLinesAllowed() || key == GLFW.GLFW_KEY_DELETE || key == GLFW.GLFW_KEY_BACKSLASH) { + this.text.keyPressed(key, scanCode, modifiers); } - } else if (key == GLFW.GLFW_KEY_DOWN) { - text.advanceLine(); - } else if (key == GLFW.GLFW_KEY_UP) { - text.regressLine(); - } else if (text.moreLinesAllowed() || key == GLFW.GLFW_KEY_DELETE || key == GLFW.GLFW_KEY_BACKSLASH) { - this.text.keyPressed(key, scanCode, modifiers); + return true;*/ } - return true; } return super.keyPressed(key, scanCode, modifiers); @@ -129,7 +130,6 @@ public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { @Override protected void renderBg(GuiGraphics graphics, float partialTicks, int mouseY, int mouseX) { - if (!isProcessedLetter && !checkedSessionVars) { checkedSessionVars = true; setFromSessionVars(); @@ -159,7 +159,7 @@ protected void renderBg(GuiGraphics graphics, float partialTicks, int mouseY, in graphics.drawWordWrap(this.font, Component.literal(text.getValue()), leftPos + 20, topPos + 34, 119, ColourProperties.INSTANCE.get("gui.mail.lettertext")); } else { clearTradeInfoWidgets(); - address.render(graphics, mouseX, mouseY, partialTicks); //TODO correct? + address.render(graphics, mouseX, mouseY, partialTicks); if (menu.getCarrierType() == EnumAddressee.TRADER) { drawTradePreview(graphics, 18, 32); } else { @@ -169,7 +169,6 @@ protected void renderBg(GuiGraphics graphics, float partialTicks, int mouseY, in } private void drawTradePreview(GuiGraphics graphics, int x, int y) { - Component infoString = null; if (menu.getTradeInfo() == null) { infoString = Component.translatable("for.gui.mail.no.trader"); diff --git a/src/main/java/forestry/mail/gui/ContainerTrader.java b/src/main/java/forestry/mail/gui/TradeStationMenu.java old mode 100755 new mode 100644 similarity index 83% rename from src/main/java/forestry/mail/gui/ContainerTrader.java rename to src/main/java/forestry/mail/gui/TradeStationMenu.java index 27ff67ad7..c096e53e7 --- a/src/main/java/forestry/mail/gui/ContainerTrader.java +++ b/src/main/java/forestry/mail/gui/TradeStationMenu.java @@ -21,15 +21,15 @@ import forestry.core.tiles.TileUtil; import forestry.mail.TradeStation; import forestry.mail.features.MailMenuTypes; -import forestry.mail.tiles.TileTrader; +import forestry.mail.tiles.TradeStationBlockEntity; -public class ContainerTrader extends ContainerTile { - public static ContainerTrader fromNetwork(int windowId, Inventory inv, FriendlyByteBuf data) { - TileTrader tile = TileUtil.getTile(inv.player.level(), data.readBlockPos(), TileTrader.class); - return new ContainerTrader(windowId, inv, tile); +public class TradeStationMenu extends ContainerTile { + public static TradeStationMenu fromNetwork(int windowId, Inventory inv, FriendlyByteBuf data) { + TradeStationBlockEntity tile = TileUtil.getTile(inv.player.level(), data.readBlockPos(), TradeStationBlockEntity.class); + return new TradeStationMenu(windowId, inv, tile); } - public ContainerTrader(int windowId, Inventory inv, TileTrader tile) { + public TradeStationMenu(int windowId, Inventory inv, TradeStationBlockEntity tile) { super(windowId, MailMenuTypes.TRADER.menuType(), inv, tile, 33, 138); // Trade good diff --git a/src/main/java/forestry/mail/gui/ContainerTradeName.java b/src/main/java/forestry/mail/gui/TradeStationNamingMenu.java old mode 100755 new mode 100644 similarity index 61% rename from src/main/java/forestry/mail/gui/ContainerTradeName.java rename to src/main/java/forestry/mail/gui/TradeStationNamingMenu.java index 29200b7d0..a36d3d5ea --- a/src/main/java/forestry/mail/gui/ContainerTradeName.java +++ b/src/main/java/forestry/mail/gui/TradeStationNamingMenu.java @@ -14,21 +14,25 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.entity.player.Player; import forestry.api.mail.IMailAddress; import forestry.core.gui.ContainerTile; import forestry.core.tiles.TileUtil; import forestry.mail.features.MailMenuTypes; -import forestry.mail.tiles.TileTrader; +import forestry.mail.tiles.TradeStationBlockEntity; -public class ContainerTradeName extends ContainerTile { - public static ContainerTradeName fromNetwork(int windowId, Inventory inv, FriendlyByteBuf data) { - TileTrader tile = TileUtil.getTile(inv.player.level(), data.readBlockPos(), TileTrader.class); - return new ContainerTradeName(windowId, inv, tile); +public class TradeStationNamingMenu extends ContainerTile { + private final Player player; + + public static TradeStationNamingMenu fromNetwork(int windowId, Inventory inv, FriendlyByteBuf data) { + TradeStationBlockEntity tile = TileUtil.getTile(inv.player.level(), data.readBlockPos(), TradeStationBlockEntity.class); + return new TradeStationNamingMenu(windowId, inv.player, tile); } - public ContainerTradeName(int windowId, Inventory inv, TileTrader tile) { + public TradeStationNamingMenu(int windowId, Player player, TradeStationBlockEntity tile) { super(windowId, MailMenuTypes.TRADE_NAME.menuType(), tile); + this.player = player; } public IMailAddress getAddress() { @@ -40,10 +44,8 @@ public void broadcastChanges() { super.broadcastChanges(); if (tile.isLinked()) { - for (Object crafter : containerListeners) { - if (crafter instanceof ServerPlayer player) { - tile.openGui(player, InteractionHand.MAIN_HAND, tile.getBlockPos()); - } + if (this.player instanceof ServerPlayer player) { + tile.openGui(player, InteractionHand.MAIN_HAND, tile.getBlockPos()); } } } diff --git a/src/main/java/forestry/mail/gui/GuiTradeName.java b/src/main/java/forestry/mail/gui/TradeStationNamingScreen.java old mode 100755 new mode 100644 similarity index 69% rename from src/main/java/forestry/mail/gui/GuiTradeName.java rename to src/main/java/forestry/mail/gui/TradeStationNamingScreen.java index dad7a2ad7..3291fff6f --- a/src/main/java/forestry/mail/gui/GuiTradeName.java +++ b/src/main/java/forestry/mail/gui/TradeStationNamingScreen.java @@ -14,51 +14,56 @@ import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.EditBox; -import net.minecraft.world.entity.player.Inventory; import net.minecraft.network.chat.Component; +import net.minecraft.world.entity.player.Inventory; import org.lwjgl.glfw.GLFW; +import forestry.api.mail.PostManager; import forestry.core.config.Constants; import forestry.core.gui.GuiForestry; import forestry.core.render.ColourProperties; import forestry.core.utils.NetworkUtil; import forestry.mail.network.packets.PacketTraderAddressRequest; -import forestry.mail.tiles.TileTrader; +import forestry.mail.tiles.TradeStationBlockEntity; -public class GuiTradeName extends GuiForestry { - private final TileTrader tile; +// Sets the name of a trade station when it's first placed down +public class TradeStationNamingScreen extends GuiForestry { + private final TradeStationBlockEntity tile; private EditBox addressNameField; - public GuiTradeName(ContainerTradeName container, Inventory inv, Component title) { + public TradeStationNamingScreen(TradeStationNamingMenu container, Inventory inv, Component title) { super(Constants.TEXTURE_PATH_GUI + "/tradername.png", container, inv, title); this.tile = container.getTile(); this.imageWidth = 176; this.imageHeight = 90; - addressNameField = new EditBox(this.font, leftPos + 44, topPos + 39, 90, 14, null); + this.addressNameField = new EditBox(this.font, leftPos + 44, topPos + 39, 90, 14, null); + this.addressNameField.setFilter(name -> PostManager.postRegistry.isValidTradeAddress(PostManager.postRegistry.createMailAddress(name))); + } @Override public void init() { super.init(); - addressNameField = new EditBox(this.font, leftPos + 44, topPos + 39, 90, 14, null); - addressNameField.setValue(menu.getAddress().getName()); - addressNameField.setFocused(true); + this.addressNameField = new EditBox(this.font, leftPos + 44, topPos + 39, 90, 14, null); + this.addressNameField.setValue(menu.getAddress().getName()); + addWidget(this.addressNameField); + + setFocused(this.addressNameField); } @Override public boolean keyPressed(int key, int scanCode, int modifiers) { - // Set focus or enter text into address - if (addressNameField.isFocused()) { - if (scanCode == GLFW.GLFW_KEY_ENTER) { + if (this.addressNameField.isFocused()) { + if (key == GLFW.GLFW_KEY_ENTER) { setAddress(); - } else { - addressNameField.keyPressed(key, scanCode, modifiers); + return true; + } else if (this.minecraft.options.keyInventory.matches(key, scanCode)) { + return true; } - return true; } return super.keyPressed(key, scanCode, modifiers); @@ -66,11 +71,10 @@ public boolean keyPressed(int key, int scanCode, int modifiers) { @Override public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { - if (super.mouseClicked(mouseX, mouseY, mouseButton)) { - return false; //TODO this return value + if (addressNameField.mouseClicked(mouseX, mouseY, mouseButton)) { + return true; } - addressNameField.mouseClicked(mouseX, mouseY, mouseButton); - return true; + return super.mouseClicked(mouseX, mouseY, mouseButton); } @Override diff --git a/src/main/java/forestry/mail/gui/GuiTrader.java b/src/main/java/forestry/mail/gui/TradeStationScreen.java old mode 100755 new mode 100644 similarity index 77% rename from src/main/java/forestry/mail/gui/GuiTrader.java rename to src/main/java/forestry/mail/gui/TradeStationScreen.java index b14c0b589..9608f3498 --- a/src/main/java/forestry/mail/gui/GuiTrader.java +++ b/src/main/java/forestry/mail/gui/TradeStationScreen.java @@ -17,21 +17,21 @@ import forestry.core.config.Constants; import forestry.core.gui.GuiForestry; import forestry.core.render.ColourProperties; -import forestry.mail.tiles.TileTrader; +import forestry.mail.tiles.TradeStationBlockEntity; -public class GuiTrader extends GuiForestry { - private final TileTrader tile; +public class TradeStationScreen extends GuiForestry { + private final TradeStationBlockEntity station; - public GuiTrader(ContainerTrader container, Inventory inv, Component title) { - super(Constants.TEXTURE_PATH_GUI + "/mailtrader2.png", container, inv, title); - this.tile = container.getTile(); + public TradeStationScreen(TradeStationMenu menu, Inventory inv, Component title) { + super(Constants.TEXTURE_PATH_GUI + "/mailtrader2.png", menu, inv, title); + this.station = menu.getTile(); this.imageWidth = 226; this.imageHeight = 220; } @Override protected void renderLabels(GuiGraphics graphics, int mouseX, int mouseY) { - Component name = tile.getTitle(); + Component name = station.getTitle(); graphics.drawString(this.font, name, textLayout.getCenteredOffset(name), 6, ColourProperties.INSTANCE.get("gui.mail.text")); Component receive = Component.translatable("for.gui.mail.receive"); @@ -44,16 +44,16 @@ protected void renderLabels(GuiGraphics graphics, int mouseX, int mouseY) { } @Override - protected void renderBg(GuiGraphics graphics, float partialTicks, int mouseY, int mouseX) { - super.renderBg(graphics, partialTicks, mouseY, mouseX); + protected void renderBg(GuiGraphics graphics, float partialTicks, int mouseX, int mouseY) { + super.renderBg(graphics, partialTicks, mouseX, mouseY); graphics.drawString(this.font, menu.getAddress().getName(), leftPos + 19, topPos + 22, ColourProperties.INSTANCE.get("gui.mail.text")); } @Override protected void addLedgers() { - addErrorLedger(tile); + addErrorLedger(station); addHintLedger("trade.station"); - addOwnerLedger(tile); + addOwnerLedger(station); } } diff --git a/src/main/java/forestry/mail/network/packets/PacketLetterInfoResponsePlayer.java b/src/main/java/forestry/mail/network/packets/PacketLetterInfoResponsePlayer.java index 02263bc73..5851c4c93 100644 --- a/src/main/java/forestry/mail/network/packets/PacketLetterInfoResponsePlayer.java +++ b/src/main/java/forestry/mail/network/packets/PacketLetterInfoResponsePlayer.java @@ -27,7 +27,7 @@ public void write(FriendlyByteBuf buffer) { } public static PacketLetterInfoResponsePlayer decode(FriendlyByteBuf buffer) { - return new PacketLetterInfoResponsePlayer(PostManager.postRegistry.getMailAddress(new GameProfile(buffer.readUUID(), buffer.readUtf()))); + return new PacketLetterInfoResponsePlayer(PostManager.postRegistry.createMailAddress(new GameProfile(buffer.readUUID(), buffer.readUtf()))); } public static void handle(PacketLetterInfoResponsePlayer msg, Player player) { diff --git a/src/main/java/forestry/mail/network/packets/PacketLetterInfoResponseTrader.java b/src/main/java/forestry/mail/network/packets/PacketLetterInfoResponseTrader.java index 05038983b..bce9e914f 100644 --- a/src/main/java/forestry/mail/network/packets/PacketLetterInfoResponseTrader.java +++ b/src/main/java/forestry/mail/network/packets/PacketLetterInfoResponseTrader.java @@ -48,7 +48,7 @@ public void write(FriendlyByteBuf buffer) { public static PacketLetterInfoResponseTrader decode(FriendlyByteBuf buffer) { if (buffer.readBoolean()) { - IMailAddress address = PostManager.postRegistry.getMailAddress(buffer.readUtf()); + IMailAddress address = PostManager.postRegistry.createMailAddress(buffer.readUtf()); GameProfile owner = new GameProfile(buffer.readUUID(), buffer.readUtf()); ItemStack tradegood = buffer.readItem(); NonNullList required = NetworkUtil.readItemStacks(buffer); diff --git a/src/main/java/forestry/mail/network/packets/PacketTraderAddressRequest.java b/src/main/java/forestry/mail/network/packets/PacketTraderAddressRequest.java index fc32f1898..26c52db3e 100644 --- a/src/main/java/forestry/mail/network/packets/PacketTraderAddressRequest.java +++ b/src/main/java/forestry/mail/network/packets/PacketTraderAddressRequest.java @@ -18,15 +18,15 @@ import forestry.api.modules.IForestryPacketServer; import forestry.core.network.PacketIdServer; import forestry.core.tiles.TileUtil; -import forestry.mail.tiles.TileTrader; +import forestry.mail.tiles.TradeStationBlockEntity; public record PacketTraderAddressRequest(BlockPos pos, String addressName) implements IForestryPacketServer { - public PacketTraderAddressRequest(TileTrader tile, String addressName) { + public PacketTraderAddressRequest(TradeStationBlockEntity tile, String addressName) { this(tile.getBlockPos(), addressName); } public static void handle(PacketTraderAddressRequest msg, ServerPlayer player) { - TileUtil.actOnTile(player.level(), msg.pos(), TileTrader.class, tile -> tile.handleSetAddressRequest(msg.addressName())); + TileUtil.actOnTile(player.level(), msg.pos(), TradeStationBlockEntity.class, tile -> tile.handleSetAddressRequest(msg.addressName())); } @Override diff --git a/src/main/java/forestry/mail/network/packets/PacketTraderAddressResponse.java b/src/main/java/forestry/mail/network/packets/PacketTraderAddressResponse.java index 64fd24fc7..0ebc64d3b 100644 --- a/src/main/java/forestry/mail/network/packets/PacketTraderAddressResponse.java +++ b/src/main/java/forestry/mail/network/packets/PacketTraderAddressResponse.java @@ -18,7 +18,7 @@ import forestry.api.modules.IForestryPacketClient; import forestry.core.network.PacketIdClient; import forestry.core.tiles.TileUtil; -import forestry.mail.tiles.TileTrader; +import forestry.mail.tiles.TradeStationBlockEntity; public record PacketTraderAddressResponse(BlockPos pos, String addressName) implements IForestryPacketClient { @Override @@ -37,6 +37,6 @@ public static PacketTraderAddressResponse decode(FriendlyByteBuf buffer) { } public static void handle(PacketTraderAddressResponse msg, Player player) { - TileUtil.actOnTile(player.level(), msg.pos, TileTrader.class, tile -> tile.handleSetAddressResponse(msg.addressName)); + TileUtil.actOnTile(player.level(), msg.pos, TradeStationBlockEntity.class, tile -> tile.handleSetAddressResponse(msg.addressName)); } } diff --git a/src/main/java/forestry/mail/tiles/TileMailbox.java b/src/main/java/forestry/mail/tiles/TileMailbox.java index 7ad990b49..cad2dabd7 100644 --- a/src/main/java/forestry/mail/tiles/TileMailbox.java +++ b/src/main/java/forestry/mail/tiles/TileMailbox.java @@ -70,7 +70,7 @@ public Container getOrCreateMailInventory(Level world, GameProfile playerProfile return getInternalInventory(); } - IMailAddress address = PostManager.postRegistry.getMailAddress(playerProfile); + IMailAddress address = PostManager.postRegistry.createMailAddress(playerProfile); return PostRegistry.getOrCreatePOBox((ServerLevel) world, address); } diff --git a/src/main/java/forestry/mail/tiles/TileTrader.java b/src/main/java/forestry/mail/tiles/TradeStationBlockEntity.java similarity index 94% rename from src/main/java/forestry/mail/tiles/TileTrader.java rename to src/main/java/forestry/mail/tiles/TradeStationBlockEntity.java index fbfd8f57d..50b338f9e 100644 --- a/src/main/java/forestry/mail/tiles/TileTrader.java +++ b/src/main/java/forestry/mail/tiles/TradeStationBlockEntity.java @@ -43,16 +43,16 @@ import forestry.mail.MailAddress; import forestry.mail.TradeStation; import forestry.mail.features.MailTiles; -import forestry.mail.gui.ContainerTradeName; -import forestry.mail.gui.ContainerTrader; +import forestry.mail.gui.TradeStationNamingMenu; +import forestry.mail.gui.TradeStationMenu; import forestry.mail.inventory.InventoryTradeStation; import forestry.mail.network.packets.PacketTraderAddressResponse; -public class TileTrader extends TileBase implements IOwnedTile { +public class TradeStationBlockEntity extends TileBase implements IOwnedTile { private final OwnerHandler ownerHandler = new OwnerHandler(); private IMailAddress address; - public TileTrader(BlockPos pos, BlockState state) { + public TradeStationBlockEntity(BlockPos pos, BlockState state) { super(MailTiles.TRADER.tileType(), pos, state); address = new MailAddress(); setInternalInventory(new InventoryTradeStation()); @@ -109,7 +109,7 @@ public void readData(FriendlyByteBuf data) { ownerHandler.readData(data); String addressName = data.readUtf(); if (!addressName.isEmpty()) { - address = PostManager.postRegistry.getMailAddress(addressName); + address = PostManager.postRegistry.createMailAddress(addressName); } } @@ -251,7 +251,7 @@ public IMailAddress getAddress() { } public void handleSetAddressRequest(String addressName) { - IMailAddress address = PostManager.postRegistry.getMailAddress(addressName); + IMailAddress address = PostManager.postRegistry.createMailAddress(addressName); setAddress(address); IMailAddress newAddress = getAddress(); @@ -264,7 +264,7 @@ public void handleSetAddressRequest(String addressName) { @OnlyIn(Dist.CLIENT) public void handleSetAddressResponse(String addressName) { - IMailAddress address = PostManager.postRegistry.getMailAddress(addressName); + IMailAddress address = PostManager.postRegistry.createMailAddress(addressName); setAddress(address); } @@ -279,7 +279,7 @@ private void setAddress(IMailAddress address) { ServerLevel world = (ServerLevel) this.level; IErrorLogic errorLogic = getErrorLogic(); - boolean hasValidTradeAddress = PostManager.postRegistry.isValidTradeAddress(world, address); + boolean hasValidTradeAddress = PostManager.postRegistry.isValidTradeAddress(address); errorLogic.setCondition(!hasValidTradeAddress, ForestryError.NOT_ALPHANUMERIC); boolean hasUniqueTradeAddress = PostManager.postRegistry.isAvailableTradeAddress(world, address); @@ -307,9 +307,9 @@ public IInventoryAdapter getInternalInventory() { @Override public AbstractContainerMenu createMenu(int windowId, Inventory inv, Player player) { if (isLinked()) { //TODO does this sync over? - return new ContainerTrader(windowId, inv, this); + return new TradeStationMenu(windowId, inv, this); } else { - return new ContainerTradeName(windowId, inv, this); + return new TradeStationNamingMenu(windowId, player, this); } } }