diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/BlockSpreaderEntity.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/BlockSpreaderEntity.java new file mode 100644 index 0000000..5c5b305 --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/BlockSpreaderEntity.java @@ -0,0 +1,95 @@ +package de.jottyfan.minecraft.quickiefabric.blockentity; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.LootableContainerBlockEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +/** + * + * @author jotty + * + */ +public class BlockSpreaderEntity extends BlockEntity { + private static final Logger LOGGER = LogManager.getLogger(BlockSpreaderEntity.class); + + public BlockSpreaderEntity(BlockPos blockPos, BlockState blockState) { + super(QuickieFabricBlockEntity.BLOCKSPREADER_ENTITY, blockPos, blockState); + } + + public static void tick(World world, BlockPos pos, BlockState state, BlockSpreaderEntity entity) { + if (!world.isClient) { + // TODO: source is a chest type below + BlockEntity source = world.getBlockEntity(pos.down()); + // dest is the block that fits to the filter of the gui (north, south, west, east only) + BlockEntity dest = world.getBlockEntity(pos.north()); + Boolean sourceIsLootable = source instanceof LootableContainerBlockEntity; + Boolean destIsLootable = dest instanceof LootableContainerBlockEntity; + if (sourceIsLootable && destIsLootable) { + LootableContainerBlockEntity lootableSource = (LootableContainerBlockEntity) source; + LootableContainerBlockEntity lootableDest = (LootableContainerBlockEntity) dest; + transferOneStack(lootableSource, lootableDest); + } + } + } + + private static final void transferOneStack(LootableContainerBlockEntity source, LootableContainerBlockEntity dest) { + Integer sourceSlot = findItemStackPos(source, false); + if (sourceSlot != null && !Items.AIR.equals(source.getStack(sourceSlot).getItem())) { + ItemStack sourceStack = source.getStack(sourceSlot); + Integer destSlot = findItemStackPos(dest, sourceStack); + if (destSlot != null) { + Integer occupied = dest.getStack(destSlot).getCount(); + Integer free = dest.getStack(destSlot).getMaxCount() - occupied; + Integer candidates = source.getStack(sourceSlot).getCount(); + Integer travellers = candidates > free ? free : candidates; + if (travellers > 0) { + LOGGER.debug("transfer {}/{} of {} from slot {} to slot {} on top of {} ones", travellers, candidates, source.getStack(sourceSlot).getItem().toString(), sourceSlot, destSlot, occupied); + source.getStack(sourceSlot).decrement(travellers); + if (source.getStack(sourceSlot).getCount() < 1) { + source.removeStack(sourceSlot); // make empty slots really empty + } + dest.getStack(destSlot).increment(travellers); + } + } else { + Integer destFreeSlot = findItemStackPos(dest, true); + if (destFreeSlot != null) { + LOGGER.debug("transfer all of {} from slot {} to slot {}", source.getStack(sourceSlot).getItem().toString(), sourceSlot, destSlot); + dest.setStack(destFreeSlot, source.removeStack(sourceSlot)); + } + } + } + } + + private static final Integer findItemStackPos(LootableContainerBlockEntity lcbe, ItemStack sourceStack) { + Integer counter = lcbe.size(); + while (counter > 0) { + counter--; + ItemStack stack = lcbe.getStack(counter); + if (stack.getItem().equals(sourceStack.getItem())) { + if (stack.getCount() < stack.getMaxCount()) { + return counter; + } + } + } + return null; + } + + private static final Integer findItemStackPos(LootableContainerBlockEntity lcbe, Boolean empty) { + Integer counter = lcbe.size(); + while (counter > 0) { + counter--; + ItemStack stack = lcbe.getStack(counter); + if (empty.equals(ItemStack.EMPTY.equals(stack))) { + return counter; + } + } + return null; + } +} diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/QuickieFabricBlockEntity.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/QuickieFabricBlockEntity.java index 7a0a51d..d437d7e 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/QuickieFabricBlockEntity.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/QuickieFabricBlockEntity.java @@ -8,6 +8,7 @@ import net.minecraft.block.entity.BlockEntityType; * */ public class QuickieFabricBlockEntity { + public static BlockEntityType BLOCKSPREADER_ENTITY; public static BlockEntityType BLOCKSTACKER_ENTITY; public static BlockEntityType ITEMHOARDER; public static BlockEntityType MONSTERHOARDER; diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/BlockSpreader.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/BlockSpreader.java new file mode 100644 index 0000000..197ac6d --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/BlockSpreader.java @@ -0,0 +1,43 @@ +package de.jottyfan.minecraft.quickiefabric.blocks; + +import de.jottyfan.minecraft.quickiefabric.blockentity.BlockSpreaderEntity; +import de.jottyfan.minecraft.quickiefabric.blockentity.QuickieFabricBlockEntity; +import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; +import net.minecraft.block.BlockRenderType; +import net.minecraft.block.BlockState; +import net.minecraft.block.BlockWithEntity; +import net.minecraft.block.Material; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityTicker; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +/** + * + * @author jotty + * + */ +public class BlockSpreader extends BlockWithEntity { + + public BlockSpreader() { + super(FabricBlockSettings.of(Material.STONE).hardness(2.5f)); + } + + @Override + public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { + return new BlockSpreaderEntity(pos, state); + } + + @Override + public BlockRenderType getRenderType(BlockState state) { + return BlockRenderType.MODEL; + } + + @Override + public BlockEntityTicker getTicker(World world, BlockState state, + BlockEntityType type) { + return checkType(type, QuickieFabricBlockEntity.BLOCKSPREADER_ENTITY, + (world1, pos, state1, be) -> BlockSpreaderEntity.tick(world1, pos, state1, be)); + } +} diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/QuickieBlocks.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/QuickieBlocks.java index 92e23df..40035bd 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/QuickieBlocks.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/QuickieBlocks.java @@ -26,4 +26,5 @@ public class QuickieBlocks { public static final BlockDrillNorth DRILL_NORTH = new BlockDrillNorth(); public static final BlockDrillstop DRILLSTOP = new BlockDrillstop(); public static final BlockStacker BLOCKSTACKER = new BlockStacker(); + public static final BlockSpreader BLOCKSPREADER = new BlockSpreader(); } diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java index c1a793c..39a3591 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java @@ -6,6 +6,7 @@ import java.util.function.Predicate; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import de.jottyfan.minecraft.quickiefabric.blockentity.BlockSpreaderEntity; import de.jottyfan.minecraft.quickiefabric.blockentity.BlockStackerEntity; import de.jottyfan.minecraft.quickiefabric.blockentity.DrillBlockDownEntity; import de.jottyfan.minecraft.quickiefabric.blockentity.DrillBlockEastEntity; @@ -165,6 +166,7 @@ public class RegistryManager { stacks.add(new ItemStack(QuickieBlocks.DRILL_NORTH)); stacks.add(new ItemStack(QuickieBlocks.DRILLSTOP)); stacks.add(new ItemStack(QuickieBlocks.BLOCKSTACKER)); + stacks.add(new ItemStack(QuickieBlocks.BLOCKSPREADER)); }).build(); private static final void registerBlock(Block block, String name) { @@ -207,6 +209,8 @@ public class RegistryManager { "drillblocknorthentity", DrillBlockNorthEntity::new, QuickieBlocks.DRILL_NORTH); QuickieFabricBlockEntity.BLOCKSTACKER_ENTITY = (BlockEntityType) registerBlockEntity( "blockstackerentity", BlockStackerEntity::new, QuickieBlocks.BLOCKSTACKER); + QuickieFabricBlockEntity.BLOCKSPREADER_ENTITY = (BlockEntityType) registerBlockEntity( + "blockspreaderentity", BlockSpreaderEntity::new, QuickieBlocks.BLOCKSPREADER); } public static final void registerBlocks() { @@ -231,6 +235,7 @@ public class RegistryManager { registerBlock(QuickieBlocks.DRILL_NORTH, "drillnorth"); registerBlock(QuickieBlocks.DRILLSTOP, "drillstop"); registerBlock(QuickieBlocks.BLOCKSTACKER, "blockstacker"); + registerBlock(QuickieBlocks.BLOCKSPREADER, "blockspreader"); } public static final void registerItems() { diff --git a/src/main/resources/assets/quickiefabric/blockstates/blockspreader.json b/src/main/resources/assets/quickiefabric/blockstates/blockspreader.json new file mode 100644 index 0000000..e8e6fd2 --- /dev/null +++ b/src/main/resources/assets/quickiefabric/blockstates/blockspreader.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "quickiefabric:block/blockspreader" + } + } +} diff --git a/src/main/resources/assets/quickiefabric/lang/de_de.json b/src/main/resources/assets/quickiefabric/lang/de_de.json index 6635ef4..4149b67 100644 --- a/src/main/resources/assets/quickiefabric/lang/de_de.json +++ b/src/main/resources/assets/quickiefabric/lang/de_de.json @@ -63,6 +63,7 @@ "block.quickiefabric.drillnorth": "Nord-Bohrer", "block.quickiefabric.drillstop": "Bohrerstopper", "block.quickiefabric.blockstacker": "Blockstapler", + "block.quickiefabric.blockspreader": "Blockverteiler", "container.quickiefabric.backpack": "Rucksack", "container.quickiefabric.blockstacker": "Blockstapler", "msg.buildingplan.start": "beginne Konstruktionsaufnahme bei %s,%s,%s", diff --git a/src/main/resources/assets/quickiefabric/lang/en_us.json b/src/main/resources/assets/quickiefabric/lang/en_us.json index f16e08d..ffff3be 100644 --- a/src/main/resources/assets/quickiefabric/lang/en_us.json +++ b/src/main/resources/assets/quickiefabric/lang/en_us.json @@ -63,6 +63,7 @@ "block.quickiefabric.drillnorth": "north drill", "block.quickiefabric.drillstop": "drill stopper", "block.quickiefabric.blockstacker": "block stacker", + "block.quickiefabric.blockspreader": "block spreader", "container.quickiefabric.backpack": "backpack", "container.quickiefabric.blockstacker": "block stacker", "msg.buildingplan.start": "started recording of construction at %s,%s,%s", diff --git a/src/main/resources/assets/quickiefabric/models/block/blockspreader.json b/src/main/resources/assets/quickiefabric/models/block/blockspreader.json new file mode 100644 index 0000000..e520faf --- /dev/null +++ b/src/main/resources/assets/quickiefabric/models/block/blockspreader.json @@ -0,0 +1,6 @@ +{ + "parent": "block/cube_all", + "textures": { + "all": "quickiefabric:block/blockspreader" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/quickiefabric/models/item/blockspreader.json b/src/main/resources/assets/quickiefabric/models/item/blockspreader.json new file mode 100644 index 0000000..da9b380 --- /dev/null +++ b/src/main/resources/assets/quickiefabric/models/item/blockspreader.json @@ -0,0 +1,10 @@ +{ + "parent": "quickiefabric:block/blockspreader", + "display": { + "thirdperson": { + "rotation": [ 10, -45, 170 ], + "translation": [ 0, 1.5, -2.75 ], + "scale": [ 0.375, 0.375, 0.375 ] + } + } +} \ No newline at end of file diff --git a/src/main/resources/assets/quickiefabric/textures/block/blockspreader.png b/src/main/resources/assets/quickiefabric/textures/block/blockspreader.png new file mode 100644 index 0000000..8de17d8 Binary files /dev/null and b/src/main/resources/assets/quickiefabric/textures/block/blockspreader.png differ diff --git a/src/main/resources/data/quickiefabric/recipes/blockspreader.json b/src/main/resources/data/quickiefabric/recipes/blockspreader.json new file mode 100644 index 0000000..169c171 --- /dev/null +++ b/src/main/resources/data/quickiefabric/recipes/blockspreader.json @@ -0,0 +1,17 @@ +{ + "type": "minecraft:crafting_shaped", + "pattern": [ + " s ", + "s s", + " s " + ], + "key": { + "s": { + "item": "quickiefabric:speedpowder" + } + }, + "result": { + "item": "quickiefabric:blockspreader", + "count": 1 + } +} \ No newline at end of file diff --git a/src/main/resources/data/quickiefabric/recipes/blockstacker.json b/src/main/resources/data/quickiefabric/recipes/blockstacker.json index 6741cde..b09aac8 100644 --- a/src/main/resources/data/quickiefabric/recipes/blockstacker.json +++ b/src/main/resources/data/quickiefabric/recipes/blockstacker.json @@ -1,13 +1,16 @@ { "type": "minecraft:crafting_shaped", "pattern": [ - " s ", - "s s", - "s s" + " w ", + "wsw", + "w w" ], "key": { "s": { "item": "quickiefabric:speedpowder" + }, + "w": { + "item": "minecraft:planks" } }, "result": {