Skip to content

Commit

Permalink
adding comparator support to more blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothrazar committed Oct 20, 2024
1 parent 88570c9 commit 9501a0e
Show file tree
Hide file tree
Showing 22 changed files with 154 additions and 6 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ dependencies {

// optional dependencies & mods for testing compat

implementation fg.deobf("curse.maven:jade-324717:3910873")

implementation fg.deobf("curse.maven:mantle-74924:3482897")
implementation fg.deobf("curse.maven:tinkers-construct-74072:3482903")
implementation fg.deobf("curse.maven:cucumber-272335:3507886")
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/lothrazar/cyclic/base/TileEntityBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,10 @@ public void setFluid(FluidStack fluid) {}
@Deprecated
@Override
public int getSizeInventory() {
IItemHandler invo = this.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(null);
if (invo != null) {
return invo.getSlots();
}
return 0;
}

Expand All @@ -535,6 +539,13 @@ public boolean isEmpty() {
@Deprecated
@Override
public ItemStack getStackInSlot(int index) {
IItemHandler invo = this.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(null);
try {
if (invo != null && index < invo.getSlots()) {
return invo.getStackInSlot(index);
}
}
catch (Exception e) {}
return ItemStack.EMPTY;
}

Expand Down Expand Up @@ -609,4 +620,8 @@ public boolean getBlockStateVertical() {
return this.getBlockState().get(BlockStateProperties.FACING).getAxis().isVertical();
return false;
}

public void updateComparatorOutputLevel() {
world.updateComparatorOutputLevel(pos, this.getBlockState().getBlock());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.block.SoundType;
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 @@ -25,6 +26,16 @@ public BlockItemCollector(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 onBlockPlacedBy(World world, BlockPos pos, BlockState state, LivingEntity entity, ItemStack stack) {
if (entity != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void tick() {
break;
}
remainder = inventory.insertItem(i, remainder, false);
updateComparatorOutputLevel();
}
stackEntity.setItem(remainder);
if (remainder.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void startSpinning() {
public void tick() {
if (this.timer == 0) {
this.spinningIfZero = 1;
world.updateComparatorOutputLevel(pos, this.getBlockState().getBlock());
updateComparatorOutputLevel();
}
else {
this.timer--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.block.BlockState;
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.state.StateContainer;
Expand All @@ -35,6 +36,16 @@ public BlockItemShelf(Properties properties) {
super(properties.hardnessAndResistance(0.8F).notSolid());
}

@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() {
ClientRegistry.bindTileEntityRenderer(TileRegistry.ENDER_ITEM_SHELF.get(), ItemShelfRenderer::new);
Expand Down Expand Up @@ -89,6 +100,7 @@ public ActionResultType onBlockActivated(BlockState state, World world, BlockPos
//try to insert
boolean oldEmpty = shelfStack.isEmpty();
ItemStack remaining = shelf.inventory.insertItem(slot, heldItem, false);
world.updateComparatorOutputLevel(pos, shelf.getBlockState().getBlock());
if (remaining.isEmpty() || remaining.getCount() != shelfStack.getCount()) {
player.setHeldItem(hand, remaining);
player.swingArm(hand);
Expand All @@ -102,6 +114,7 @@ public ActionResultType onBlockActivated(BlockState state, World world, BlockPos
//withdraw direct to players empty hand
int q = player.isCrouching() ? 1 : 64;
ItemStack retrieved = shelf.inventory.extractItem(slot, q, false);
world.updateComparatorOutputLevel(pos, shelf.getBlockState().getBlock());
player.setHeldItem(hand, retrieved);
player.swingArm(hand);
}
Expand All @@ -114,6 +127,7 @@ public ActionResultType onBlockActivated(BlockState state, World world, BlockPos
player.setHeldItem(hand, forPlayer);
player.swingArm(hand);
shelf.inventory.insertItem(slot, forShelf, false);
world.updateComparatorOutputLevel(pos, shelf.getBlockState().getBlock());
}
return ActionResultType.SUCCESS;
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/lothrazar/cyclic/block/fishing/BlockFisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
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.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockDisplayReader;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.fml.client.registry.ClientRegistry;

public class BlockFisher extends BlockBase {
Expand All @@ -21,6 +23,16 @@ public BlockFisher(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 shouldDisplayFluidOverlay(BlockState state, IBlockDisplayReader world, BlockPos pos, FluidState fluidState) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public void tick() {
catch (Exception e) {
ModCyclic.LOGGER.error("Fishing Block: Loot table failed", e);
}
updateComparatorOutputLevel();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 @@ -24,6 +25,16 @@ public BlockForester(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() {
ClientRegistry.bindTileEntityRenderer(TileRegistry.FORESTER, RenderForester::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public void tick() {
else if (this.isSapling(dropMe)) {
ActionResultType result = TileEntityBase.rightClickBlock(fakePlayer, world, targetPos, Hand.OFF_HAND, Direction.DOWN);
if (result == ActionResultType.CONSUME) {
updateComparatorOutputLevel();
//ok then DRAIN POWER
energy.extractEnergy(cost, 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 BlockGeneratorFluid extends BlockBase {

Expand All @@ -21,6 +24,16 @@ public BlockGeneratorFluid(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
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 @@ -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 BlockGeneratorFood extends BlockBase {

Expand All @@ -20,6 +23,16 @@ public BlockGeneratorFood(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 @@ -92,6 +92,7 @@ private void tryConsumeFuel() {
this.burnTimeMax = burnTimeTicks;
this.burnTime = this.burnTimeMax;
stack.shrink(1);
updateComparatorOutputLevel();
//nether items, mob drops
// lava fluid
//exp fluid
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 BlockGeneratorFuel extends BlockBase {

Expand All @@ -20,6 +23,16 @@ public BlockGeneratorFuel(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 tryConsumeFuel() {
else {
stack.shrink(1);
}
updateComparatorOutputLevel();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
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.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;

public class BlockGeneratorDrops extends BlockBase {

Expand All @@ -19,6 +22,16 @@ public BlockGeneratorDrops(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(LIT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lothrazar.cyclic.block.generatoritem;

import com.lothrazar.cyclic.ModCyclic;
import com.lothrazar.cyclic.base.TileEntityBase;
import com.lothrazar.cyclic.block.battery.TileBattery;
import com.lothrazar.cyclic.capability.CustomEnergyStorage;
Expand Down Expand Up @@ -106,7 +105,7 @@ private void findMatchingRecipe() {
this.burnTime = this.burnTimeMax;
this.burnPerTick = this.currentRecipe.getRfpertick();
this.inputSlots.extractItem(0, 1, false);
ModCyclic.LOGGER.info("found genrecipe" + currentRecipe.getId());
updateComparatorOutputLevel();
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.client.gui.ScreenManager;
import net.minecraft.inventory.container.Container;
import net.minecraft.state.StateContainer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;

public class BlockGeneratorPeat extends BlockBase {

Expand All @@ -16,6 +19,16 @@ public BlockGeneratorPeat(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 @@ -109,6 +109,7 @@ public void tick() {
fuelRate = peat.getPeatFuelValue();
inventory.extractItem(0, 1, false);
this.timer = BURNTIME;
updateComparatorOutputLevel();
}
}
}
Expand Down
Loading

0 comments on commit 9501a0e

Please sign in to comment.