Skip to content

Commit

Permalink
Quantum Tanks/Chests can now disallow input from their export face vi…
Browse files Browse the repository at this point in the history
…a screwdriver (#69)
  • Loading branch information
BraggestSage833 authored Aug 10, 2021
1 parent 95d1f58 commit 30cf93c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import codechicken.lib.vec.Matrix4;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IActiveOutputSide;
import gregtech.api.capability.impl.ItemHandlerProxy;
import gregtech.api.cover.ICoverable;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.ModularUI.Builder;
Expand Down Expand Up @@ -35,6 +37,7 @@
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;
Expand All @@ -58,6 +61,7 @@ public class MetaTileEntityQuantumChest extends MetaTileEntity implements ITiere
private static final String NBT_ITEMSTACK = "ItemStack";
private static final String NBT_PARTIALSTACK = "PartialStack";
private static final String NBT_ITEMCOUNT = "ItemAmount";
protected IItemHandler outputItemInventory;

public MetaTileEntityQuantumChest(ResourceLocation metaTileEntityId, int tier, long maxStoredItems) {
super(metaTileEntityId);
Expand Down Expand Up @@ -185,6 +189,7 @@ public void addInformation(ItemStack stack, @Nullable World player, List<String>
protected void initializeInventory() {
super.initializeInventory();
this.itemInventory = new QuantumChestItemHandler();
this.outputItemInventory = new ItemHandlerProxy(new ItemStackHandler(0), exportItems);
}

@Override
Expand Down Expand Up @@ -285,6 +290,9 @@ public void setOutputFacing(EnumFacing outputFacing) {
@Override
public boolean onWrenchClick(EntityPlayer playerIn, EnumHand hand, EnumFacing facing, CuboidRayTraceResult hitResult) {
if (!playerIn.isSneaking()) {
if (getOutputFacing() == facing || getFrontFacing() == facing) {
return false;
}
if (!getWorld().isRemote) {
setOutputFacing(facing);
}
Expand Down Expand Up @@ -342,17 +350,14 @@ public <T> T getCapability(Capability<T> capability, EnumFacing side) {
}
return null;
}
return super.getCapability(capability, side);

}

@Override
public boolean canPlaceCoverOnSide(EnumFacing side) {
//Done to prevent loops as output always acts as input
if (side == getOutputFacing()) {
return false;
else if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
IItemHandler itemHandler = (side == getOutputFacing() && !isAllowInputFromOutputSide()) ? outputItemInventory : itemInventory;
if (itemHandler.getSlots() > 0) {
return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(itemHandler);
}
return null;
}
return true;
return super.getCapability(capability, side);
}

@Override
Expand Down Expand Up @@ -383,6 +388,30 @@ public void clearMachineInventory(NonNullList<ItemStack> itemBuffer) {
clearInventory(itemBuffer, importItems);
}

@Override
public boolean onScrewdriverClick(EntityPlayer playerIn, EnumHand hand, EnumFacing facing, CuboidRayTraceResult hitResult) {
EnumFacing hitFacing = ICoverable.determineGridSideHit(hitResult);
if (facing == getOutputFacing() || (hitFacing == getOutputFacing() && playerIn.isSneaking())) {
if (!getWorld().isRemote) {
if (isAllowInputFromOutputSide()) {
setAllowInputFromOutputSide(false);
playerIn.sendMessage(new TextComponentTranslation("gregtech.machine.basic.input_from_output_side.disallow"));
} else {
setAllowInputFromOutputSide(true);
playerIn.sendMessage(new TextComponentTranslation("gregtech.machine.basic.input_from_output_side.allow"));
}
}
return true;
}
return super.onScrewdriverClick(playerIn, hand, facing, hitResult);
}
public void setAllowInputFromOutputSide(boolean allowInputFromOutputSide) {
this.allowInputFromOutputSide = allowInputFromOutputSide;
if (!getWorld().isRemote) {
markDirty();
}
}

private class QuantumChestItemHandler implements IItemHandler {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import codechicken.lib.vec.Matrix4;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IActiveOutputSide;
import gregtech.api.capability.impl.FluidHandlerProxy;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.cover.ICoverable;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.ModularUI.Builder;
Expand All @@ -28,11 +30,14 @@
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.items.ItemStackHandler;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;
Expand All @@ -49,7 +54,8 @@ public class MetaTileEntityQuantumTank extends MetaTileEntity implements ITiered
private final ItemStackHandler containerInventory;
private boolean autoOutputFluids;
private EnumFacing outputFacing;
private final boolean allowInputFromOutputSide = true;
private boolean allowInputFromOutputSide = true;
protected IFluidHandler outputFluidInventory;

public MetaTileEntityQuantumTank(ResourceLocation metaTileEntityId, int tier, int maxFluidCapacity) {
super(metaTileEntityId);
Expand All @@ -71,6 +77,7 @@ protected void initializeInventory() {
this.fluidInventory = fluidTank;
this.importFluids = new FluidTankList(false, fluidTank);
this.exportFluids = new FluidTankList(false, fluidTank);
this.outputFluidInventory = new FluidHandlerProxy(new FluidTankList(false), exportFluids);
}

@Override
Expand Down Expand Up @@ -153,11 +160,6 @@ protected FluidTankList createExportFluidHandler() {
return new FluidTankList(false, fluidTank);
}

@Override
public boolean hasFrontFacing() {
return false;
}

@Override
public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline) {
Textures.VOLTAGE_CASINGS[tier].render(renderState, translation, ArrayUtils.add(pipeline,
Expand Down Expand Up @@ -223,6 +225,14 @@ public EnumFacing getOutputFacing() {
return outputFacing == null ? EnumFacing.SOUTH : outputFacing;
}

@Override
public void setFrontFacing(EnumFacing frontFacing) {
super.setFrontFacing(EnumFacing.UP);
if (this.outputFacing == null) {
//set initial output facing as opposite to front
setOutputFacing(frontFacing.getOpposite());
}
}

@Override
public boolean isAutoOutputItems() {
Expand All @@ -235,7 +245,7 @@ public boolean isAutoOutputFluids() {

@Override
public boolean isAllowInputFromOutputSide() {
return true;
return allowInputFromOutputSide;
}

@Override
Expand Down Expand Up @@ -279,22 +289,24 @@ public <T> T getCapability(Capability<T> capability, EnumFacing side) {
}
return null;
}
return super.getCapability(capability, side);

}
else if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
IFluidHandler fluidHandler = (side == getOutputFacing() && !isAllowInputFromOutputSide()) ? outputFluidInventory : fluidInventory;
if (fluidHandler.getTankProperties().length > 0) {
return CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY.cast(fluidHandler);
}

@Override
public boolean canPlaceCoverOnSide(EnumFacing side) {
//Done to prevent loops as output always acts as input
if (side == getOutputFacing()) {
return false;
return null;
}
return true;
return super.getCapability(capability, side);

}

@Override
public boolean onWrenchClick(EntityPlayer playerIn, EnumHand hand, EnumFacing facing, CuboidRayTraceResult hitResult) {
if (!playerIn.isSneaking()) {
if (getOutputFacing() == facing || getFrontFacing() == facing) {
return false;
}
if (!getWorld().isRemote) {
setOutputFacing(facing);
}
Expand All @@ -303,14 +315,35 @@ public boolean onWrenchClick(EntityPlayer playerIn, EnumHand hand, EnumFacing fa
return super.onWrenchClick(playerIn, hand, facing, hitResult);
}

@Override
public boolean onScrewdriverClick(EntityPlayer playerIn, EnumHand hand, EnumFacing facing, CuboidRayTraceResult hitResult) {
EnumFacing hitFacing = ICoverable.determineGridSideHit(hitResult);
if (facing == getOutputFacing() || (hitFacing == getOutputFacing() && playerIn.isSneaking())) {
if (!getWorld().isRemote) {
if (isAllowInputFromOutputSide()) {
setAllowInputFromOutputSide(false);
playerIn.sendMessage(new TextComponentTranslation("gregtech.machine.basic.input_from_output_side.disallow"));
} else {
setAllowInputFromOutputSide(true);
playerIn.sendMessage(new TextComponentTranslation("gregtech.machine.basic.input_from_output_side.allow"));
}
}
return true;
}
return super.onScrewdriverClick(playerIn, hand, facing, hitResult);
}

public void setAllowInputFromOutputSide(boolean allowInputFromOutputSide) {
this.allowInputFromOutputSide = allowInputFromOutputSide;
if (!getWorld().isRemote) {
markDirty();
}
}
public void setAutoOutputFluids(boolean autoOutputFluids) {
this.autoOutputFluids = autoOutputFluids;
if (!getWorld().isRemote) {
writeCustomData(102, buf -> buf.writeBoolean(autoOutputFluids));
markDirty();
}
}


}

0 comments on commit 30cf93c

Please sign in to comment.