Add custom TunnelType registration API

This commit is contained in:
NotMyWing
2024-06-10 16:43:27 +11:00
parent d40b9831c1
commit eba3e2456a
4 changed files with 80 additions and 83 deletions
+61 -12
View File
@@ -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<ItemStack> 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<ItemStack> supplier) {
this.partItemStack = null;
this.partItemStackSupplier = supplier;
}
private static Supplier<ItemStack> tryPartStack(Function<IParts, IItemDefinition> 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;
}
}
@@ -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 );
}
@@ -37,6 +37,7 @@ import net.minecraft.util.EnumFacing;
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 @@ public final class P2PTunnelRegistry implements IP2PTunnelRegistry {
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);
}
}
@@ -20,12 +20,7 @@ package appeng.parts.p2p;
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 abstract class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicSt
}
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 abstract class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicSt
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 abstract class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicSt
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 abstract class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicSt
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 abstract class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicSt
}
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 abstract class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicSt
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);