Skip to content

Commit

Permalink
adding comparator support for fluids
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothrazar committed Oct 20, 2024
1 parent 9501a0e commit b4cc542
Show file tree
Hide file tree
Showing 21 changed files with 139 additions and 24 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/lothrazar/cyclic/base/BlockBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
Expand Down Expand Up @@ -192,4 +193,16 @@ private static boolean hasCapabilityDir(Direction facing, IWorld world, BlockPos
}
return false;
}

//for comparators that dont use item inventories
protected int calcRedstoneFromFluid(TileEntity tileEntity) {
IFluidHandler fluid = tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY).orElse(null);
if (fluid.getFluidInTank(0).isEmpty()) {
return 0;
}
float cap = fluid.getTankCapacity(0);
float amt = fluid.getFluidInTank(0).getAmount();
float f = amt / cap;
return MathHelper.floor(f * 14.0F) + 1;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/lothrazar/cyclic/block/crafter/BlockCrafter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.minecraft.block.BlockState;
import net.minecraft.client.gui.ScreenManager;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.inventory.container.Container;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
Expand All @@ -42,6 +43,16 @@ public BlockCrafter(Properties properties) {
this.setHasGui();
}

@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}

@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
return Container.calcRedstone(worldIn.getTileEntity(pos));
}

@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
if (state.getBlock() != newState.getBlock()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ else if (!itemStacksInGrid.equals(lastRecipeGrid)) {
break;
}
}
this.updateComparatorOutputLevel();
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/lothrazar/cyclic/block/crate/BlockCrate.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.client.gui.ScreenManager;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
Expand All @@ -26,6 +27,16 @@ public BlockCrate(Properties properties) {
this.setHasGui();
}

@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}

@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
return Container.calcRedstone(worldIn.getTileEntity(pos));
}

@Override
public boolean hasTileEntity(BlockState state) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public ITextComponent getDisplayName() {

@Override
public Container createMenu(int i, PlayerInventory playerInventory, PlayerEntity playerEntity) {
this.updateComparatorOutputLevel();
return new ContainerCrate(i, world, pos, playerInventory, playerEntity);
}

Expand Down
21 changes: 0 additions & 21 deletions src/main/java/com/lothrazar/cyclic/block/dice/TileDice.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,8 @@ public void tick() {
BlockState stateold = world.getBlockState(pos);
BlockState newstate = stateold.with(BlockStateProperties.FACING, fac);
world.setBlockState(pos, newstate);
// world.notifyBlockUpdate(pos, stateold, newstate, 3);
}
}
//
//
// @Override
// public void update() {
// if (this.timer == 0) {
// this.spinningIfZero = 1;
// world.updateComparatorOutputLevel(pos, this.blockType);
// }
// else {
// this.timer--;
// //toggle block state
// if (this.timer % TICKS_PER_CHANGE == 0) {
// this.spinningIfZero = 0;
// EnumFacing fac = UtilDirection.getRandom(world.rand);
// IBlockState stateold = world.getBlockState(pos);
// IBlockState newstate = stateold.withProperty(BlockDice.PROPERTYFACING, fac);
// world.setBlockState(pos, newstate);
// // world.notifyBlockUpdate(pos, stateold, newstate, 3);
// }
// }
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;

public class BlockFluidHopper extends BlockBase {

Expand All @@ -24,6 +25,16 @@ public BlockFluidHopper(Properties properties) {
this.setHasFluidInteract();
}

@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}

@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
return calcRedstoneFromFluid(worldIn.getTileEntity(pos));
}

@Override
public void registerClient() {
// RenderTypeLookup.setRenderLayer(this, RenderType.getTranslucent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ private void tryExtract(Direction extractSide) {
if (stuff != null) {
success = UtilFluid.tryFillPositionFromTank(world, pos, extractSide, stuff, FLOW);
if (success) {
this.updateComparatorOutputLevel();
return;
}
}
if (!success && tank.getSpace() >= FluidAttributes.BUCKET_VOLUME) {
UtilFluid.extractSourceWaterloggedCauldron(world, target, tank);
this.updateComparatorOutputLevel();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@

import com.lothrazar.cyclic.block.hopper.BlockSimpleHopper;
import net.minecraft.block.BlockState;
import net.minecraft.inventory.container.Container;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;

public class BlockGoldHopper extends BlockSimpleHopper {

public BlockGoldHopper(Properties properties) {
super(properties.hardnessAndResistance(1.3F));
}

@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}

@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
return Container.calcRedstone(worldIn.getTileEntity(pos));
}

@Override
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
return new TileGoldHopper();
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/lothrazar/cyclic/block/melter/BlockMelter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup;
import net.minecraft.fluid.FluidState;
import net.minecraft.inventory.container.Container;
import net.minecraft.state.StateContainer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockDisplayReader;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.fml.client.registry.ClientRegistry;

Expand All @@ -25,6 +27,16 @@ public BlockMelter(Properties properties) {
this.setHasGui();
}

@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}

@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
return Container.calcRedstone(worldIn.getTileEntity(pos));
}

@Override
@Deprecated
public float getAmbientOcclusionLightValue(BlockState state, IBlockReader worldIn, BlockPos pos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ private boolean tryProcessRecipe() {
inventory.getStackInSlot(0).shrink(1);
inventory.getStackInSlot(1).shrink(1);
tank.fill(this.currentRecipe.getRecipeFluid(), FluidAction.EXECUTE);
updateComparatorOutputLevel();
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import net.minecraft.client.gui.ScreenManager;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup;
import net.minecraft.inventory.container.Container;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;

public class BlockPackager extends BlockBase {

Expand All @@ -19,6 +22,16 @@ public BlockPackager(Properties properties) {
this.setHasGui();
}

@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}

@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
return Container.calcRedstone(worldIn.getTileEntity(pos));
}

@Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
builder.add(BlockStateProperties.FACING).add(LIT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private void tryDoPackage() {
inputSlots.extractItem(0, total, false);
outputSlots.insertItem(0, output, false);
energy.extractEnergy(POWERCONF.get(), false);
this.updateComparatorOutputLevel();
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/lothrazar/cyclic/block/placer/BlockPlacer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.block.BlockState;
import net.minecraft.client.gui.ScreenManager;
import net.minecraft.entity.LivingEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
Expand All @@ -22,6 +23,16 @@ public BlockPlacer(Properties properties) {
this.setHasGui();
}

@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}

@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
return Container.calcRedstone(worldIn.getTileEntity(pos));
}

@Override
public void registerClient() {
ScreenManager.registerFactory(ContainerScreenRegistry.placer, ScreenPlacer::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public void tick() {
Direction dir = this.getBlockState().get(BlockStateProperties.FACING);
BlockPos offset = pos.offset(dir);
BlockState state = Block.getBlockFromItem(stack.getItem()).getDefaultState();
if (world.isAirBlock(offset) &&
world.setBlockState(offset, state)) {
if (world.isAirBlock(offset) && world.setBlockState(offset, state)) {
stack.shrink(1);
this.updateComparatorOutputLevel();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup;
import net.minecraft.fluid.FluidState;
import net.minecraft.inventory.container.Container;
import net.minecraft.state.StateContainer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockDisplayReader;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.fml.client.registry.ClientRegistry;

Expand All @@ -26,6 +28,16 @@ public BlockSolidifier(Properties properties) {
this.setHasFluidInteract();
}

@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}

@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
return Container.calcRedstone(worldIn.getTileEntity(pos));
}

@Override
@Deprecated
public float getAmbientOcclusionLightValue(BlockState state, IBlockReader worldIn, BlockPos pos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ private boolean tryProcessRecipe() {
inputSlots.getStackInSlot(2).shrink(1);
tank.drain(this.currentRecipe.fluidIngredient.getAmount(), FluidAction.EXECUTE);
outputSlots.insertItem(0, currentRecipe.getRecipeOutput(), false);
updateComparatorOutputLevel();
return true;
}
ModCyclic.LOGGER.info(pos + " recipe stop on fluid not enoughl");
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/lothrazar/cyclic/block/tank/BlockFluidTank.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public BlockFluidTank(Properties properties) {
this.setHasFluidInteract();
}

@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}

@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
return calcRedstoneFromFluid(worldIn.getTileEntity(pos));
}

@Override
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
if (!player.isCrouching() && player.getHeldItem(hand).getItem() == this.asItem()
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/lothrazar/cyclic/block/tank/TileTank.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
return super.getCapability(cap, side);
}

@Override
public void invalidateCaps() {
fluidCap.invalidate();
super.invalidateCaps();
Expand All @@ -65,6 +66,7 @@ public int getField(int field) {
@Override
public void setFluid(FluidStack fluid) {
tank.setFluid(fluid);
this.updateComparatorOutputLevel();
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/lothrazar/cyclic/block/tankcask/BlockCask.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public BlockCask(Properties properties) {
this.setHasFluidInteract();
}

@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}

@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
return calcRedstoneFromFluid(worldIn.getTileEntity(pos));
}

@Override
public boolean hasTileEntity(BlockState state) {
return true;
Expand Down
Loading

0 comments on commit b4cc542

Please sign in to comment.