Skip to content

Commit 61aefc8

Browse files
committed
Add comparator support
1 parent bee91d8 commit 61aefc8

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/main/java/commoble/jumbofurnace/jumbo_furnace/JumboFurnaceBlock.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.Random;
66

7+
import javax.annotation.Nonnull;
78
import javax.annotation.Nullable;
89

910
import com.mojang.datafixers.util.Pair;
@@ -33,13 +34,15 @@
3334
import net.minecraft.util.SoundEvents;
3435
import net.minecraft.util.math.BlockPos;
3536
import net.minecraft.util.math.BlockRayTraceResult;
37+
import net.minecraft.util.math.MathHelper;
3638
import net.minecraft.world.IBlockReader;
3739
import net.minecraft.world.IWorld;
3840
import net.minecraft.world.World;
3941
import net.minecraftforge.api.distmarker.Dist;
4042
import net.minecraftforge.api.distmarker.OnlyIn;
4143
import net.minecraftforge.common.Tags;
4244
import net.minecraftforge.fml.network.NetworkHooks;
45+
import net.minecraftforge.items.IItemHandler;
4346

4447
public class JumboFurnaceBlock extends Block
4548
{
@@ -303,6 +306,64 @@ else if (x == 2 && z == 1)
303306
return null;
304307
}
305308
}
309+
310+
@Override
311+
@Deprecated
312+
public boolean hasComparatorInputOverride(BlockState state)
313+
{
314+
return true;
315+
}
316+
317+
@Override
318+
@Deprecated
319+
public int getComparatorInputOverride(BlockState state, World world, BlockPos pos)
320+
{
321+
BlockPos corePos = getCorePos(state, pos);
322+
TileEntity te = world.getTileEntity(corePos);
323+
if (!(te instanceof JumboFurnaceCoreTileEntity) || !state.hasProperty(Y))
324+
{
325+
// if we are in an invalid state, return 0
326+
return 0;
327+
}
328+
329+
JumboFurnaceCoreTileEntity core = (JumboFurnaceCoreTileEntity)te;
330+
int y = state.get(Y);
331+
332+
// top layer of blocks: comparator output is input inventory
333+
// middle layer of blocks: comparator output is fuel inventory
334+
// bottom layer of blocks: comparator output is output inventory
335+
336+
switch(y)
337+
{
338+
case 0: return calcRedstoneFromItemHandler(core.output);
339+
case 1: return calcRedstoneFromItemHandler(core.fuel);
340+
case 2: return calcRedstoneFromItemHandler(core.input);
341+
default: return 0;
342+
}
343+
}
344+
345+
// same math as Container.calcRedstone
346+
public static int calcRedstoneFromItemHandler(@Nonnull IItemHandler handler)
347+
{
348+
int nonEmptySlots = 0;
349+
float totalItemValue = 0.0F;
350+
int slots = handler.getSlots();
351+
352+
for (int slot = 0; slot < slots; ++slot)
353+
{
354+
ItemStack itemstack = handler.getStackInSlot(slot);
355+
if (!itemstack.isEmpty())
356+
{
357+
totalItemValue += itemstack.getCount() / (float) Math.min(handler.getSlotLimit(slot), itemstack.getMaxStackSize());
358+
++nonEmptySlots;
359+
}
360+
}
361+
362+
float averageItemValue = totalItemValue = totalItemValue / slots;
363+
return MathHelper.floor(averageItemValue * 14.0F) + (nonEmptySlots > 0 ? 1 : 0);
364+
}
365+
366+
306367

307368
/**
308369
* Returns the blockstate with the given rotation from the passed blockstate. If

src/main/java/commoble/jumbofurnace/jumbo_furnace/JumboFurnaceCoreTileEntity.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,17 @@ public void craft()
414414
}
415415
}
416416
}
417+
418+
@Override
419+
public void markDirty()
420+
{
421+
super.markDirty();
422+
423+
// make sure comparators reading from exterior blocks are updated as well
424+
MultiBlockHelper.get3x3CubeAround(this.pos)
425+
.filter(exteriorPos -> !exteriorPos.equals(this.pos))
426+
.forEach(exteriorPos -> this.world.updateComparatorOutputLevel(exteriorPos, this.getBlockState().getBlock()));
427+
}
417428

418429

419430
}

src/main/java/commoble/jumbofurnace/jumbo_furnace/JumboFurnaceExteriorTileEntity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,7 @@ public JumboFurnaceCoreTileEntity getCoreTile()
5858
TileEntity te = world.getTileEntity(corePos);
5959
return te instanceof JumboFurnaceCoreTileEntity ? (JumboFurnaceCoreTileEntity)te : null;
6060
}
61+
62+
6163

6264
}

0 commit comments

Comments
 (0)