Skip to content

Commit

Permalink
Add custom TunnelType registration API
Browse files Browse the repository at this point in the history
  • Loading branch information
NotMyWing committed Jun 10, 2024
1 parent d40b983 commit eba3e24
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 83 deletions.
73 changes: 61 additions & 12 deletions src/api/java/appeng/api/config/TunnelType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 6 additions & 5 deletions src/api/java/appeng/api/features/IP2PTunnelRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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 );
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
73 changes: 7 additions & 66 deletions src/main/java/appeng/parts/p2p/PartP2PTunnel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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 {
Expand Down Expand Up @@ -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");

Expand All @@ -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);

Expand Down

0 comments on commit eba3e24

Please sign in to comment.