Skip to content

Commit

Permalink
WIP fix mail GUIs
Browse files Browse the repository at this point in the history
  • Loading branch information
thedarkcolour committed Nov 28, 2024
1 parent 88ab19a commit 0121cc2
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 142 deletions.
35 changes: 25 additions & 10 deletions src/main/java/forestry/api/mail/IPostRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
5 changes: 1 addition & 4 deletions src/main/java/forestry/mail/EventHandlerMailAlert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 7 additions & 24 deletions src/main/java/forestry/mail/PostRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -46,13 +45,8 @@ public class PostRegistry implements IPostRegistry {

private final Map<EnumAddressee, IPostalCarrier> 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]+$");
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/forestry/mail/blocks/BlockTypeMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/forestry/mail/client/MailClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
});
}
}
8 changes: 4 additions & 4 deletions src/main/java/forestry/mail/features/MailMenuTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +20,6 @@ public class MailMenuTypes {
public static final FeatureMenuType<ContainerLetter> LETTER = REGISTRY.menuType(ContainerLetter::fromNetwork, "letter");
public static final FeatureMenuType<ContainerMailbox> MAILBOX = REGISTRY.menuType(ContainerMailbox::fromNetwork, "mailbox");
public static final FeatureMenuType<ContainerStampCollector> STAMP_COLLECTOR = REGISTRY.menuType(ContainerStampCollector::fromNetwork, "stamp_collector");
public static final FeatureMenuType<ContainerTradeName> TRADE_NAME = REGISTRY.menuType(ContainerTradeName::fromNetwork, "trade_name");
public static final FeatureMenuType<ContainerTrader> TRADER = REGISTRY.menuType(ContainerTrader::fromNetwork, "trader");
public static final FeatureMenuType<TradeStationNamingMenu> TRADE_NAME = REGISTRY.menuType(TradeStationNamingMenu::fromNetwork, "trade_name");
public static final FeatureMenuType<TradeStationMenu> TRADER = REGISTRY.menuType(TradeStationMenu::fromNetwork, "trader");
}
4 changes: 2 additions & 2 deletions src/main/java/forestry/mail/features/MailTiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,5 +16,5 @@ public class MailTiles {

public static final FeatureTileType<TileMailbox> MAILBOX = REGISTRY.tile(TileMailbox::new, "mailbox", () -> MailBlocks.BASE.get(BlockTypeMail.MAILBOX).collect());
public static final FeatureTileType<TileStampCollector> STAMP_COLLECTOR = REGISTRY.tile(TileStampCollector::new, "stamp_collector", () -> MailBlocks.BASE.get(BlockTypeMail.STAMP_COLLETOR).collect());
public static final FeatureTileType<TileTrader> TRADER = REGISTRY.tile(TileTrader::new, "trader", () -> MailBlocks.BASE.get(BlockTypeMail.TRADE_STATION).collect());
public static final FeatureTileType<TradeStationBlockEntity> TRADER = REGISTRY.tile(TradeStationBlockEntity::new, "trader", () -> MailBlocks.BASE.get(BlockTypeMail.TRADE_STATION).collect());
}
6 changes: 3 additions & 3 deletions src/main/java/forestry/mail/gui/ContainerLetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
};
}

Expand Down
49 changes: 24 additions & 25 deletions src/main/java/forestry/mail/gui/GuiLetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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 {
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TileTrader> {
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<TradeStationBlockEntity> {
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
Expand Down
Loading

0 comments on commit 0121cc2

Please sign in to comment.