Add custom TunnelType registration API
This commit is contained in:
@@ -24,16 +24,65 @@
|
|||||||
package appeng.api.config;
|
package appeng.api.config;
|
||||||
|
|
||||||
|
|
||||||
public enum TunnelType
|
import appeng.api.AEApi;
|
||||||
{
|
import appeng.api.definitions.IItemDefinition;
|
||||||
ME, // Network Tunnel
|
import appeng.api.definitions.IParts;
|
||||||
IC2_POWER, // EU Tunnel
|
import appeng.api.parts.IPartItem;
|
||||||
FE_POWER, // Forge Energy tunnel
|
import com.google.common.base.Preconditions;
|
||||||
GTEU_POWER, // Forge Energy tunnel
|
import net.minecraft.item.ItemStack;
|
||||||
REDSTONE, // Redstone Tunnel
|
import net.minecraftforge.common.util.EnumHelper;
|
||||||
FLUID, // Fluid Tunnel
|
|
||||||
ITEM, // Item Tunnel
|
import javax.annotation.Nonnull;
|
||||||
LIGHT, // Light Tunnel
|
import java.util.function.Function;
|
||||||
BUNDLED_REDSTONE, // Bundled Redstone Tunnel
|
import java.util.function.Supplier;
|
||||||
COMPUTER_MESSAGE // Computer Message Tunnel
|
|
||||||
|
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;
|
package appeng.api.features;
|
||||||
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import appeng.api.config.TunnelType;
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraftforge.common.capabilities.Capability;
|
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
|
* @return null if no attunement can be found or attunement
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nullable
|
||||||
TunnelType getTunnelTypeByItem( ItemStack trigger );
|
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.common.capabilities.Capability;
|
||||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||||
import net.minecraftforge.oredict.OreDictionary;
|
import net.minecraftforge.oredict.OreDictionary;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@@ -277,4 +278,9 @@ public final class P2PTunnelRegistry implements IP2PTunnelRegistry {
|
|||||||
private void addNewAttunement(final IItemDefinition definition, final TunnelType type) {
|
private void addNewAttunement(final IItemDefinition definition, final TunnelType type) {
|
||||||
definition.maybeStack(1).ifPresent(definitionStack -> this.addNewAttunement(definitionStack, 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.AEApi;
|
||||||
import appeng.api.config.Actionable;
|
import appeng.api.config.*;
|
||||||
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.implementations.items.IMemoryCard;
|
import appeng.api.implementations.items.IMemoryCard;
|
||||||
import appeng.api.implementations.items.MemoryCardMessages;
|
import appeng.api.implementations.items.MemoryCardMessages;
|
||||||
import appeng.api.networking.events.MENetworkBootingStatusChange;
|
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);
|
final TunnelType tt = AEApi.instance().registries().p2pTunnel().getTunnelTypeByItem(is);
|
||||||
if (!is.isEmpty() && is.getItem() instanceof IMemoryCard) {
|
if (!is.isEmpty() && is.getItem() instanceof IMemoryCard mc) {
|
||||||
final IMemoryCard mc = (IMemoryCard) is.getItem();
|
|
||||||
final NBTTagCompound data = mc.getData(is);
|
final NBTTagCompound data = mc.getData(is);
|
||||||
|
|
||||||
final ItemStack newType = new ItemStack(data);
|
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 AEPartLocation dir = this.getHost().addPart(newType, this.getSide(), player, hand);
|
||||||
final IPart newBus = this.getHost().getPart(dir);
|
final IPart newBus = this.getHost().getPart(dir);
|
||||||
|
|
||||||
if (newBus instanceof PartP2PTunnel) {
|
if (newBus instanceof PartP2PTunnel newTunnel) {
|
||||||
final PartP2PTunnel newTunnel = (PartP2PTunnel) newBus;
|
|
||||||
|
|
||||||
if (pasteAsOutput) {
|
if (pasteAsOutput) {
|
||||||
newTunnel.setOutput(true);
|
newTunnel.setOutput(true);
|
||||||
@@ -233,56 +226,7 @@ public abstract class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicSt
|
|||||||
mc.notifyUser(player, MemoryCardMessages.INVALID_MACHINE);
|
mc.notifyUser(player, MemoryCardMessages.INVALID_MACHINE);
|
||||||
} else if (tt != null) // attunement
|
} else if (tt != null) // attunement
|
||||||
{
|
{
|
||||||
final ItemStack newType;
|
final ItemStack newType = tt.getPartItemStack();
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!newType.isEmpty() && !ItemStack.areItemsEqual(newType, this.getItemStack())) {
|
if (!newType.isEmpty() && !ItemStack.areItemsEqual(newType, this.getItemStack())) {
|
||||||
final boolean oldOutput = this.isOutput();
|
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 AEPartLocation dir = this.getHost().addPart(newType, this.getSide(), player, hand);
|
||||||
final IPart newBus = this.getHost().getPart(dir);
|
final IPart newBus = this.getHost().getPart(dir);
|
||||||
|
|
||||||
if (newBus instanceof PartP2PTunnel) {
|
if (newBus instanceof PartP2PTunnel newTunnel) {
|
||||||
final PartP2PTunnel newTunnel = (PartP2PTunnel) newBus;
|
|
||||||
newTunnel.setOutput(oldOutput);
|
newTunnel.setOutput(oldOutput);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -333,12 +276,11 @@ public abstract class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicSt
|
|||||||
}
|
}
|
||||||
|
|
||||||
final ItemStack is = player.inventory.getCurrentItem();
|
final ItemStack is = player.inventory.getCurrentItem();
|
||||||
if (!is.isEmpty() && is.getItem() instanceof IMemoryCard) {
|
if (!is.isEmpty() && is.getItem() instanceof IMemoryCard mc) {
|
||||||
if (Platform.isClient()) {
|
if (Platform.isClient()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
final IMemoryCard mc = (IMemoryCard) is.getItem();
|
|
||||||
final NBTTagCompound data = mc.getData(is);
|
final NBTTagCompound data = mc.getData(is);
|
||||||
final short storedFrequency = data.getShort("freq");
|
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 AEPartLocation dir = this.getHost().addPart(newType, this.getSide(), player, hand);
|
||||||
final IPart newBus = this.getHost().getPart(dir);
|
final IPart newBus = this.getHost().getPart(dir);
|
||||||
|
|
||||||
if (newBus instanceof PartP2PTunnel) {
|
if (newBus instanceof PartP2PTunnel newTunnel) {
|
||||||
final PartP2PTunnel newTunnel = (PartP2PTunnel) newBus;
|
|
||||||
newTunnel.setOutput(false);
|
newTunnel.setOutput(false);
|
||||||
newTunnel.getProxy().getP2P().updateFreq(newTunnel, newFreq);
|
newTunnel.getProxy().getP2P().updateFreq(newTunnel, newFreq);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user