From eba3e2456a78db721e80065ccf072eb4e94801e6 Mon Sep 17 00:00:00 2001 From: NotMyWing Date: Mon, 10 Jun 2024 16:43:27 +1100 Subject: [PATCH] Add custom TunnelType registration API --- .../java/appeng/api/config/TunnelType.java | 73 ++++++++++++++++--- .../api/features/IP2PTunnelRegistry.java | 11 +-- .../registries/P2PTunnelRegistry.java | 6 ++ .../java/appeng/parts/p2p/PartP2PTunnel.java | 73 ++----------------- 4 files changed, 80 insertions(+), 83 deletions(-) diff --git a/src/api/java/appeng/api/config/TunnelType.java b/src/api/java/appeng/api/config/TunnelType.java index 71cfd77f972..a785816bafc 100644 --- a/src/api/java/appeng/api/config/TunnelType.java +++ b/src/api/java/appeng/api/config/TunnelType.java @@ -24,16 +24,65 @@ package appeng.api.config; -public enum TunnelType -{ - ME, // Network Tunnel - IC2_POWER, // EU Tunnel - FE_POWER, // Forge Energy tunnel - GTEU_POWER, // Forge Energy tunnel - REDSTONE, // Redstone Tunnel - FLUID, // Fluid Tunnel - ITEM, // Item Tunnel - LIGHT, // Light Tunnel - BUNDLED_REDSTONE, // Bundled Redstone Tunnel - COMPUTER_MESSAGE // Computer Message Tunnel +import appeng.api.AEApi; +import appeng.api.definitions.IItemDefinition; +import appeng.api.definitions.IParts; +import appeng.api.parts.IPartItem; +import com.google.common.base.Preconditions; +import net.minecraft.item.ItemStack; +import net.minecraftforge.common.util.EnumHelper; + +import javax.annotation.Nonnull; +import java.util.function.Function; +import java.util.function.Supplier; + +public enum TunnelType { + ME(tryPartStack(IParts::p2PTunnelME)), + IC2_POWER(tryPartStack(IParts::p2PTunnelEU)), + FE_POWER(tryPartStack(IParts::p2PTunnelFE)), + GTEU_POWER(tryPartStack(IParts::p2PTunnelGTEU)), + REDSTONE(tryPartStack(IParts::p2PTunnelRedstone)), + FLUID(tryPartStack(IParts::p2PTunnelFE)), + ITEM(tryPartStack(IParts::p2PTunnelItems)), + LIGHT(tryPartStack(IParts::p2PTunnelLight)); + + private ItemStack partItemStack; + private Supplier partItemStackSupplier; + + @Deprecated + TunnelType() { + this.partItemStack = ItemStack.EMPTY; + this.partItemStackSupplier = null; + } + + // Public facing. + TunnelType(ItemStack partItemStack) { + this.partItemStack = partItemStack; + this.partItemStackSupplier = null; + } + + // Work around things instantiating this class too early. + TunnelType(Supplier supplier) { + this.partItemStack = null; + this.partItemStackSupplier = supplier; + } + + private static Supplier tryPartStack(Function supplier) { + return () -> supplier.apply(AEApi.instance().definitions().parts()).maybeStack(1).orElse(ItemStack.EMPTY); + } + + public static TunnelType registerTunnelType(@Nonnull String name, @Nonnull ItemStack partItemStack) { + Preconditions.checkArgument(partItemStack.isEmpty() || partItemStack.getItem() instanceof IPartItem, + "Part item must be an instance of IPartItem"); + + return EnumHelper.addEnum(TunnelType.class, name, new Class[]{ItemStack.class}, partItemStack); + } + + public ItemStack getPartItemStack() { + if (this.partItemStackSupplier != null) { + this.partItemStack = this.partItemStackSupplier.get(); + this.partItemStackSupplier = null; + } + return partItemStack; + } } diff --git a/src/api/java/appeng/api/features/IP2PTunnelRegistry.java b/src/api/java/appeng/api/features/IP2PTunnelRegistry.java index 4f50b54d240..bafb264c823 100644 --- a/src/api/java/appeng/api/features/IP2PTunnelRegistry.java +++ b/src/api/java/appeng/api/features/IP2PTunnelRegistry.java @@ -24,13 +24,12 @@ package appeng.api.features; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - +import appeng.api.config.TunnelType; import net.minecraft.item.ItemStack; import net.minecraftforge.common.capabilities.Capability; -import appeng.api.config.TunnelType; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; /** @@ -59,6 +58,8 @@ public interface IP2PTunnelRegistry * * @return null if no attunement can be found or attunement */ - @Nonnull + @Nullable TunnelType getTunnelTypeByItem( ItemStack trigger ); + + TunnelType registerTunnelType( @Nonnull String enumName, @Nonnull ItemStack partStack ); } \ No newline at end of file diff --git a/src/main/java/appeng/core/features/registries/P2PTunnelRegistry.java b/src/main/java/appeng/core/features/registries/P2PTunnelRegistry.java index 4a3ee0c9109..dc886661e5c 100644 --- a/src/main/java/appeng/core/features/registries/P2PTunnelRegistry.java +++ b/src/main/java/appeng/core/features/registries/P2PTunnelRegistry.java @@ -37,6 +37,7 @@ import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.oredict.OreDictionary; +import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -277,4 +278,9 @@ private ItemStack getModItem(final String modID, final String name, final int me private void addNewAttunement(final IItemDefinition definition, final TunnelType type) { definition.maybeStack(1).ifPresent(definitionStack -> this.addNewAttunement(definitionStack, type)); } + + @Override + public TunnelType registerTunnelType(@NotNull String enumName, @NotNull ItemStack partStack) { + return TunnelType.registerTunnelType(enumName, partStack); + } } diff --git a/src/main/java/appeng/parts/p2p/PartP2PTunnel.java b/src/main/java/appeng/parts/p2p/PartP2PTunnel.java index 680b61e939f..797a60b416e 100644 --- a/src/main/java/appeng/parts/p2p/PartP2PTunnel.java +++ b/src/main/java/appeng/parts/p2p/PartP2PTunnel.java @@ -20,12 +20,7 @@ import appeng.api.AEApi; -import appeng.api.config.Actionable; -import appeng.api.config.PowerMultiplier; -import appeng.api.config.PowerUnits; -import appeng.api.config.SecurityPermissions; -import appeng.api.config.TunnelType; -import appeng.api.definitions.IParts; +import appeng.api.config.*; import appeng.api.implementations.items.IMemoryCard; import appeng.api.implementations.items.MemoryCardMessages; import appeng.api.networking.events.MENetworkBootingStatusChange; @@ -185,8 +180,7 @@ public boolean onPartActivate(final EntityPlayer player, final EnumHand hand, fi } final TunnelType tt = AEApi.instance().registries().p2pTunnel().getTunnelTypeByItem(is); - if (!is.isEmpty() && is.getItem() instanceof IMemoryCard) { - final IMemoryCard mc = (IMemoryCard) is.getItem(); + if (!is.isEmpty() && is.getItem() instanceof IMemoryCard mc) { final NBTTagCompound data = mc.getData(is); final ItemStack newType = new ItemStack(data); @@ -208,8 +202,7 @@ public boolean onPartActivate(final EntityPlayer player, final EnumHand hand, fi final AEPartLocation dir = this.getHost().addPart(newType, this.getSide(), player, hand); final IPart newBus = this.getHost().getPart(dir); - if (newBus instanceof PartP2PTunnel) { - final PartP2PTunnel newTunnel = (PartP2PTunnel) newBus; + if (newBus instanceof PartP2PTunnel newTunnel) { if (pasteAsOutput) { newTunnel.setOutput(true); @@ -233,56 +226,7 @@ public boolean onPartActivate(final EntityPlayer player, final EnumHand hand, fi mc.notifyUser(player, MemoryCardMessages.INVALID_MACHINE); } else if (tt != null) // attunement { - final ItemStack newType; - - final IParts parts = AEApi.instance().definitions().parts(); - - switch (tt) { - case LIGHT: - newType = parts.p2PTunnelLight().maybeStack(1).orElse(ItemStack.EMPTY); - break; - - case FE_POWER: - newType = parts.p2PTunnelFE().maybeStack(1).orElse(ItemStack.EMPTY); - break; - - case GTEU_POWER: - newType = parts.p2PTunnelGTEU().maybeStack(1).orElse(ItemStack.EMPTY); - break; - - case FLUID: - newType = parts.p2PTunnelFluids().maybeStack(1).orElse(ItemStack.EMPTY); - break; - - case IC2_POWER: - newType = parts.p2PTunnelEU().maybeStack(1).orElse(ItemStack.EMPTY); - break; - - case ITEM: - newType = parts.p2PTunnelItems().maybeStack(1).orElse(ItemStack.EMPTY); - break; - - case ME: - newType = parts.p2PTunnelME().maybeStack(1).orElse(ItemStack.EMPTY); - break; - - case REDSTONE: - newType = parts.p2PTunnelRedstone().maybeStack(1).orElse(ItemStack.EMPTY); - break; - - /* - * case COMPUTER_MESSAGE: - * for( ItemStack stack : parts.p2PTunnelOpenComputers().maybeStack( 1 ).asSet() ) - * { - * newType = stack; - * } - * break; - */ - - default: - newType = ItemStack.EMPTY; - break; - } + final ItemStack newType = tt.getPartItemStack(); if (!newType.isEmpty() && !ItemStack.areItemsEqual(newType, this.getItemStack())) { final boolean oldOutput = this.isOutput(); @@ -299,8 +243,7 @@ public boolean onPartActivate(final EntityPlayer player, final EnumHand hand, fi final AEPartLocation dir = this.getHost().addPart(newType, this.getSide(), player, hand); final IPart newBus = this.getHost().getPart(dir); - if (newBus instanceof PartP2PTunnel) { - final PartP2PTunnel newTunnel = (PartP2PTunnel) newBus; + if (newBus instanceof PartP2PTunnel newTunnel) { newTunnel.setOutput(oldOutput); try { @@ -333,12 +276,11 @@ public boolean onPartShiftActivate(final EntityPlayer player, final EnumHand han } final ItemStack is = player.inventory.getCurrentItem(); - if (!is.isEmpty() && is.getItem() instanceof IMemoryCard) { + if (!is.isEmpty() && is.getItem() instanceof IMemoryCard mc) { if (Platform.isClient()) { return true; } - final IMemoryCard mc = (IMemoryCard) is.getItem(); final NBTTagCompound data = mc.getData(is); final short storedFrequency = data.getShort("freq"); @@ -365,8 +307,7 @@ public boolean onPartShiftActivate(final EntityPlayer player, final EnumHand han final AEPartLocation dir = this.getHost().addPart(newType, this.getSide(), player, hand); final IPart newBus = this.getHost().getPart(dir); - if (newBus instanceof PartP2PTunnel) { - final PartP2PTunnel newTunnel = (PartP2PTunnel) newBus; + if (newBus instanceof PartP2PTunnel newTunnel) { newTunnel.setOutput(false); newTunnel.getProxy().getP2P().updateFreq(newTunnel, newFreq);