From 7e055d0a1aae240ce09823c7f9045fce05356d06 Mon Sep 17 00:00:00 2001 From: 90 Date: Wed, 31 May 2023 13:29:49 +0100 Subject: [PATCH] Refactor pattern provider menu Fixes a bug in which MEGA pattern providers could occasionally accept crafting patterns. --- .../block/MEGAPatternProviderBlock.java | 28 +++++++++++++++++++ .../MEGAPatternProviderBlockEntity.java | 8 +++--- .../menu/MEGAPatternProviderMenu.java | 25 ----------------- .../part/MEGAPatternProviderPart.java | 8 +++--- .../java/gripe/_90/megacells/MEGACells.java | 4 +-- .../gripe/_90/megacells/MEGACellsClient.java | 5 ++-- .../gripe/_90/megacells/forge/MEGACells.java | 4 +-- .../_90/megacells/forge/MEGACellsClient.java | 4 +-- 8 files changed, 45 insertions(+), 41 deletions(-) delete mode 100644 common/src/main/java/gripe/_90/megacells/menu/MEGAPatternProviderMenu.java diff --git a/common/src/main/java/gripe/_90/megacells/block/MEGAPatternProviderBlock.java b/common/src/main/java/gripe/_90/megacells/block/MEGAPatternProviderBlock.java index e3daa586..e9904108 100644 --- a/common/src/main/java/gripe/_90/megacells/block/MEGAPatternProviderBlock.java +++ b/common/src/main/java/gripe/_90/megacells/block/MEGAPatternProviderBlock.java @@ -9,7 +9,9 @@ import net.minecraft.network.chat.Component; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.MenuType; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.level.Level; @@ -19,23 +21,43 @@ import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.phys.BlockHitResult; +import appeng.api.config.SecurityPermissions; +import appeng.api.networking.IManagedGridNode; import appeng.api.util.IOrientable; import appeng.block.AEBaseBlockItem; import appeng.block.AEBaseEntityBlock; +import appeng.core.definitions.AEItems; +import appeng.helpers.iface.PatternProviderLogic; +import appeng.helpers.iface.PatternProviderLogicHost; +import appeng.menu.implementations.MenuTypeBuilder; +import appeng.menu.implementations.PatternProviderMenu; import appeng.menu.locator.MenuLocators; import appeng.util.InteractionUtil; +import appeng.util.inv.AppEngInternalInventory; +import appeng.util.inv.filter.AEItemDefinitionFilter; import gripe._90.megacells.block.entity.MEGAPatternProviderBlockEntity; import gripe._90.megacells.definition.MEGATranslations; public class MEGAPatternProviderBlock extends AEBaseEntityBlock { public static final BooleanProperty OMNIDIRECTIONAL = BooleanProperty.create("omnidirectional"); + public static final MenuType MENU = MenuTypeBuilder + .create(Menu::new, PatternProviderLogicHost.class) + .requirePermission(SecurityPermissions.BUILD) + .build("mega_pattern_provider"); public MEGAPatternProviderBlock(Properties props) { super(props); registerDefaultState(defaultBlockState().setValue(OMNIDIRECTIONAL, true)); } + public static PatternProviderLogic createLogic(IManagedGridNode mainNode, PatternProviderLogicHost host) { + var logic = new PatternProviderLogic(mainNode, host, 18); + ((AppEngInternalInventory) logic.getPatternInv()) + .setFilter(new AEItemDefinitionFilter(AEItems.PROCESSING_PATTERN)); + return logic; + } + @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { super.createBlockStateDefinition(builder); @@ -99,4 +121,10 @@ public void addCheckedInformation(ItemStack stack, Level level, List tooltip.add(MEGATranslations.ProcessingOnly.text()); } } + + public static class Menu extends PatternProviderMenu { + public Menu(int id, Inventory playerInventory, PatternProviderLogicHost host) { + super(MENU, id, playerInventory, host); + } + } } diff --git a/common/src/main/java/gripe/_90/megacells/block/entity/MEGAPatternProviderBlockEntity.java b/common/src/main/java/gripe/_90/megacells/block/entity/MEGAPatternProviderBlockEntity.java index 9c9c0ae4..56d61711 100644 --- a/common/src/main/java/gripe/_90/megacells/block/entity/MEGAPatternProviderBlockEntity.java +++ b/common/src/main/java/gripe/_90/megacells/block/entity/MEGAPatternProviderBlockEntity.java @@ -13,8 +13,8 @@ import appeng.menu.MenuOpener; import appeng.menu.locator.MenuLocator; +import gripe._90.megacells.block.MEGAPatternProviderBlock; import gripe._90.megacells.definition.MEGABlocks; -import gripe._90.megacells.menu.MEGAPatternProviderMenu; public class MEGAPatternProviderBlockEntity extends PatternProviderBlockEntity { @@ -24,17 +24,17 @@ public MEGAPatternProviderBlockEntity(BlockEntityType blockEntityType, BlockP @Override public PatternProviderLogic createLogic() { - return new PatternProviderLogic(this.getMainNode(), this, 18); + return MEGAPatternProviderBlock.createLogic(this.getMainNode(), this); } @Override public void openMenu(Player player, MenuLocator locator) { - MenuOpener.open(MEGAPatternProviderMenu.TYPE, player, locator); + MenuOpener.open(MEGAPatternProviderBlock.MENU, player, locator); } @Override public void returnToMainMenu(Player player, ISubMenu subMenu) { - MenuOpener.returnTo(MEGAPatternProviderMenu.TYPE, player, subMenu.getLocator()); + MenuOpener.returnTo(MEGAPatternProviderBlock.MENU, player, subMenu.getLocator()); } @Override diff --git a/common/src/main/java/gripe/_90/megacells/menu/MEGAPatternProviderMenu.java b/common/src/main/java/gripe/_90/megacells/menu/MEGAPatternProviderMenu.java deleted file mode 100644 index d6b9f90a..00000000 --- a/common/src/main/java/gripe/_90/megacells/menu/MEGAPatternProviderMenu.java +++ /dev/null @@ -1,25 +0,0 @@ -package gripe._90.megacells.menu; - -import net.minecraft.world.entity.player.Inventory; -import net.minecraft.world.inventory.MenuType; - -import appeng.api.config.SecurityPermissions; -import appeng.core.definitions.AEItems; -import appeng.helpers.iface.PatternProviderLogicHost; -import appeng.menu.implementations.MenuTypeBuilder; -import appeng.menu.implementations.PatternProviderMenu; -import appeng.util.inv.AppEngInternalInventory; -import appeng.util.inv.filter.AEItemDefinitionFilter; - -public class MEGAPatternProviderMenu extends PatternProviderMenu { - public static final MenuType TYPE = MenuTypeBuilder - .create(MEGAPatternProviderMenu::new, PatternProviderLogicHost.class) - .requirePermission(SecurityPermissions.BUILD) - .build("mega_pattern_provider"); - - public MEGAPatternProviderMenu(int id, Inventory playerInventory, PatternProviderLogicHost host) { - super(TYPE, id, playerInventory, host); - ((AppEngInternalInventory) logic.getPatternInv()) - .setFilter(new AEItemDefinitionFilter(AEItems.PROCESSING_PATTERN)); - } -} diff --git a/common/src/main/java/gripe/_90/megacells/part/MEGAPatternProviderPart.java b/common/src/main/java/gripe/_90/megacells/part/MEGAPatternProviderPart.java index bd7b7d2f..d423b318 100644 --- a/common/src/main/java/gripe/_90/megacells/part/MEGAPatternProviderPart.java +++ b/common/src/main/java/gripe/_90/megacells/part/MEGAPatternProviderPart.java @@ -22,8 +22,8 @@ import appeng.parts.PartModel; import appeng.parts.crafting.PatternProviderPart; +import gripe._90.megacells.block.MEGAPatternProviderBlock; import gripe._90.megacells.definition.MEGAParts; -import gripe._90.megacells.menu.MEGAPatternProviderMenu; import gripe._90.megacells.util.Utils; public class MEGAPatternProviderPart extends PatternProviderPart { @@ -46,17 +46,17 @@ public MEGAPatternProviderPart(IPartItem partItem) { @Override public PatternProviderLogic createLogic() { - return new PatternProviderLogic(this.getMainNode(), this, 18); + return MEGAPatternProviderBlock.createLogic(this.getMainNode(), this); } @Override public void openMenu(Player player, MenuLocator locator) { - MenuOpener.open(MEGAPatternProviderMenu.TYPE, player, locator); + MenuOpener.open(MEGAPatternProviderBlock.MENU, player, locator); } @Override public void returnToMainMenu(Player player, ISubMenu subMenu) { - MenuOpener.returnTo(MEGAPatternProviderMenu.TYPE, player, subMenu.getLocator()); + MenuOpener.returnTo(MEGAPatternProviderBlock.MENU, player, subMenu.getLocator()); } @Override diff --git a/fabric/src/main/java/gripe/_90/megacells/MEGACells.java b/fabric/src/main/java/gripe/_90/megacells/MEGACells.java index 811b1cdd..ad9e1649 100644 --- a/fabric/src/main/java/gripe/_90/megacells/MEGACells.java +++ b/fabric/src/main/java/gripe/_90/megacells/MEGACells.java @@ -6,6 +6,7 @@ import appeng.api.IAEAddonEntrypoint; import appeng.core.AppEng; +import gripe._90.megacells.block.MEGAPatternProviderBlock; import gripe._90.megacells.definition.MEGABlockEntities; import gripe._90.megacells.definition.MEGABlocks; import gripe._90.megacells.definition.MEGAItems; @@ -13,7 +14,6 @@ import gripe._90.megacells.init.InitStorageCells; import gripe._90.megacells.init.InitUpgrades; import gripe._90.megacells.integration.appbot.AppBotItems; -import gripe._90.megacells.menu.MEGAPatternProviderMenu; import gripe._90.megacells.service.CompressionService; import gripe._90.megacells.util.Utils; @@ -54,7 +54,7 @@ private void registerAll() { Registry.register(Registry.BLOCK_ENTITY_TYPE, blockEntity.getKey(), blockEntity.getValue()); } - Registry.register(Registry.MENU, AppEng.makeId("mega_pattern_provider"), MEGAPatternProviderMenu.TYPE); + Registry.register(Registry.MENU, AppEng.makeId("mega_pattern_provider"), MEGAPatternProviderBlock.MENU); } private void initCompression() { diff --git a/fabric/src/main/java/gripe/_90/megacells/MEGACellsClient.java b/fabric/src/main/java/gripe/_90/megacells/MEGACellsClient.java index 0b3cc2bc..b0a34228 100644 --- a/fabric/src/main/java/gripe/_90/megacells/MEGACellsClient.java +++ b/fabric/src/main/java/gripe/_90/megacells/MEGACellsClient.java @@ -32,12 +32,12 @@ import appeng.items.tools.powered.PortableCellItem; import gripe._90.megacells.block.MEGACraftingUnitType; +import gripe._90.megacells.block.MEGAPatternProviderBlock; import gripe._90.megacells.client.render.MEGACraftingUnitModelProvider; import gripe._90.megacells.definition.MEGABlockEntities; import gripe._90.megacells.definition.MEGABlocks; import gripe._90.megacells.definition.MEGAItems; import gripe._90.megacells.integration.appbot.AppBotItems; -import gripe._90.megacells.menu.MEGAPatternProviderMenu; import gripe._90.megacells.util.Utils; @Environment(EnvType.CLIENT) @@ -52,7 +52,8 @@ public void onAe2Initialized() { private void initScreens() { ClientLifecycleEvents.CLIENT_STARTED.register(client -> { - InitScreens.register(MEGAPatternProviderMenu.TYPE, PatternProviderScreen::new, + InitScreens.register(MEGAPatternProviderBlock.MENU, + PatternProviderScreen::new, "/screens/megacells/mega_pattern_provider.json"); }); } diff --git a/forge/src/main/java/gripe/_90/megacells/forge/MEGACells.java b/forge/src/main/java/gripe/_90/megacells/forge/MEGACells.java index 0c2452d7..5ee88d81 100644 --- a/forge/src/main/java/gripe/_90/megacells/forge/MEGACells.java +++ b/forge/src/main/java/gripe/_90/megacells/forge/MEGACells.java @@ -14,6 +14,7 @@ import appeng.core.AppEng; +import gripe._90.megacells.block.MEGAPatternProviderBlock; import gripe._90.megacells.definition.MEGABlockEntities; import gripe._90.megacells.definition.MEGABlocks; import gripe._90.megacells.definition.MEGAItems; @@ -23,7 +24,6 @@ import gripe._90.megacells.integration.appbot.AppBotItems; import gripe._90.megacells.integration.appmek.AppMekIntegration; import gripe._90.megacells.integration.appmek.AppMekItems; -import gripe._90.megacells.menu.MEGAPatternProviderMenu; import gripe._90.megacells.service.CompressionService; import gripe._90.megacells.util.Utils; @@ -74,7 +74,7 @@ private void registerAll(RegisterEvent event) { if (event.getRegistryKey().equals(Registry.MENU_REGISTRY)) { ForgeRegistries.MENU_TYPES.register(AppEng.makeId("mega_pattern_provider"), - MEGAPatternProviderMenu.TYPE); + MEGAPatternProviderBlock.MENU); } } diff --git a/forge/src/main/java/gripe/_90/megacells/forge/MEGACellsClient.java b/forge/src/main/java/gripe/_90/megacells/forge/MEGACellsClient.java index 47310646..17d4f3fe 100644 --- a/forge/src/main/java/gripe/_90/megacells/forge/MEGACellsClient.java +++ b/forge/src/main/java/gripe/_90/megacells/forge/MEGACellsClient.java @@ -29,13 +29,13 @@ import appeng.items.tools.powered.PortableCellItem; import gripe._90.megacells.block.MEGACraftingUnitType; +import gripe._90.megacells.block.MEGAPatternProviderBlock; import gripe._90.megacells.client.render.MEGACraftingUnitModelProvider; import gripe._90.megacells.definition.MEGABlockEntities; import gripe._90.megacells.definition.MEGABlocks; import gripe._90.megacells.definition.MEGAItems; import gripe._90.megacells.integration.appbot.AppBotItems; import gripe._90.megacells.integration.appmek.AppMekItems; -import gripe._90.megacells.menu.MEGAPatternProviderMenu; import gripe._90.megacells.util.Utils; public class MEGACellsClient { @@ -49,7 +49,7 @@ public MEGACellsClient() { } private void initScreens(FMLClientSetupEvent ignoredEvent) { - InitScreens.register(MEGAPatternProviderMenu.TYPE, PatternProviderScreen::new, + InitScreens.register(MEGAPatternProviderBlock.MENU, PatternProviderScreen::new, "/screens/megacells/mega_pattern_provider.json"); }