preparation for item spreader

This commit is contained in:
Jörg Henke 2022-08-22 00:48:13 +02:00
parent 9e36a08660
commit a4f2f3185f
13 changed files with 193 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -8,6 +8,7 @@ import net.minecraft.block.entity.BlockEntityType;
* *
*/ */
public class QuickieFabricBlockEntity { public class QuickieFabricBlockEntity {
public static BlockEntityType<BlockSpreaderEntity> BLOCKSPREADER_ENTITY;
public static BlockEntityType<BlockStackerEntity> BLOCKSTACKER_ENTITY; public static BlockEntityType<BlockStackerEntity> BLOCKSTACKER_ENTITY;
public static BlockEntityType<ItemHoarderBlockEntity> ITEMHOARDER; public static BlockEntityType<ItemHoarderBlockEntity> ITEMHOARDER;
public static BlockEntityType<MonsterHoarderBlockEntity> MONSTERHOARDER; public static BlockEntityType<MonsterHoarderBlockEntity> MONSTERHOARDER;

View File

@ -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 <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state,
BlockEntityType<T> type) {
return checkType(type, QuickieFabricBlockEntity.BLOCKSPREADER_ENTITY,
(world1, pos, state1, be) -> BlockSpreaderEntity.tick(world1, pos, state1, be));
}
}

View File

@ -26,4 +26,5 @@ public class QuickieBlocks {
public static final BlockDrillNorth DRILL_NORTH = new BlockDrillNorth(); public static final BlockDrillNorth DRILL_NORTH = new BlockDrillNorth();
public static final BlockDrillstop DRILLSTOP = new BlockDrillstop(); public static final BlockDrillstop DRILLSTOP = new BlockDrillstop();
public static final BlockStacker BLOCKSTACKER = new BlockStacker(); public static final BlockStacker BLOCKSTACKER = new BlockStacker();
public static final BlockSpreader BLOCKSPREADER = new BlockSpreader();
} }

View File

@ -6,6 +6,7 @@ import java.util.function.Predicate;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; 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.BlockStackerEntity;
import de.jottyfan.minecraft.quickiefabric.blockentity.DrillBlockDownEntity; import de.jottyfan.minecraft.quickiefabric.blockentity.DrillBlockDownEntity;
import de.jottyfan.minecraft.quickiefabric.blockentity.DrillBlockEastEntity; 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.DRILL_NORTH));
stacks.add(new ItemStack(QuickieBlocks.DRILLSTOP)); stacks.add(new ItemStack(QuickieBlocks.DRILLSTOP));
stacks.add(new ItemStack(QuickieBlocks.BLOCKSTACKER)); stacks.add(new ItemStack(QuickieBlocks.BLOCKSTACKER));
stacks.add(new ItemStack(QuickieBlocks.BLOCKSPREADER));
}).build(); }).build();
private static final void registerBlock(Block block, String name) { private static final void registerBlock(Block block, String name) {
@ -207,6 +209,8 @@ public class RegistryManager {
"drillblocknorthentity", DrillBlockNorthEntity::new, QuickieBlocks.DRILL_NORTH); "drillblocknorthentity", DrillBlockNorthEntity::new, QuickieBlocks.DRILL_NORTH);
QuickieFabricBlockEntity.BLOCKSTACKER_ENTITY = (BlockEntityType<BlockStackerEntity>) registerBlockEntity( QuickieFabricBlockEntity.BLOCKSTACKER_ENTITY = (BlockEntityType<BlockStackerEntity>) registerBlockEntity(
"blockstackerentity", BlockStackerEntity::new, QuickieBlocks.BLOCKSTACKER); "blockstackerentity", BlockStackerEntity::new, QuickieBlocks.BLOCKSTACKER);
QuickieFabricBlockEntity.BLOCKSPREADER_ENTITY = (BlockEntityType<BlockSpreaderEntity>) registerBlockEntity(
"blockspreaderentity", BlockSpreaderEntity::new, QuickieBlocks.BLOCKSPREADER);
} }
public static final void registerBlocks() { public static final void registerBlocks() {
@ -231,6 +235,7 @@ public class RegistryManager {
registerBlock(QuickieBlocks.DRILL_NORTH, "drillnorth"); registerBlock(QuickieBlocks.DRILL_NORTH, "drillnorth");
registerBlock(QuickieBlocks.DRILLSTOP, "drillstop"); registerBlock(QuickieBlocks.DRILLSTOP, "drillstop");
registerBlock(QuickieBlocks.BLOCKSTACKER, "blockstacker"); registerBlock(QuickieBlocks.BLOCKSTACKER, "blockstacker");
registerBlock(QuickieBlocks.BLOCKSPREADER, "blockspreader");
} }
public static final void registerItems() { public static final void registerItems() {

View File

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "quickiefabric:block/blockspreader"
}
}
}

View File

@ -63,6 +63,7 @@
"block.quickiefabric.drillnorth": "Nord-Bohrer", "block.quickiefabric.drillnorth": "Nord-Bohrer",
"block.quickiefabric.drillstop": "Bohrerstopper", "block.quickiefabric.drillstop": "Bohrerstopper",
"block.quickiefabric.blockstacker": "Blockstapler", "block.quickiefabric.blockstacker": "Blockstapler",
"block.quickiefabric.blockspreader": "Blockverteiler",
"container.quickiefabric.backpack": "Rucksack", "container.quickiefabric.backpack": "Rucksack",
"container.quickiefabric.blockstacker": "Blockstapler", "container.quickiefabric.blockstacker": "Blockstapler",
"msg.buildingplan.start": "beginne Konstruktionsaufnahme bei %s,%s,%s", "msg.buildingplan.start": "beginne Konstruktionsaufnahme bei %s,%s,%s",

View File

@ -63,6 +63,7 @@
"block.quickiefabric.drillnorth": "north drill", "block.quickiefabric.drillnorth": "north drill",
"block.quickiefabric.drillstop": "drill stopper", "block.quickiefabric.drillstop": "drill stopper",
"block.quickiefabric.blockstacker": "block stacker", "block.quickiefabric.blockstacker": "block stacker",
"block.quickiefabric.blockspreader": "block spreader",
"container.quickiefabric.backpack": "backpack", "container.quickiefabric.backpack": "backpack",
"container.quickiefabric.blockstacker": "block stacker", "container.quickiefabric.blockstacker": "block stacker",
"msg.buildingplan.start": "started recording of construction at %s,%s,%s", "msg.buildingplan.start": "started recording of construction at %s,%s,%s",

View File

@ -0,0 +1,6 @@
{
"parent": "block/cube_all",
"textures": {
"all": "quickiefabric:block/blockspreader"
}
}

View File

@ -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 ]
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 240 B

View File

@ -0,0 +1,17 @@
{
"type": "minecraft:crafting_shaped",
"pattern": [
" s ",
"s s",
" s "
],
"key": {
"s": {
"item": "quickiefabric:speedpowder"
}
},
"result": {
"item": "quickiefabric:blockspreader",
"count": 1
}
}

View File

@ -1,13 +1,16 @@
{ {
"type": "minecraft:crafting_shaped", "type": "minecraft:crafting_shaped",
"pattern": [ "pattern": [
" s ", " w ",
"s s", "wsw",
"s s" "w w"
], ],
"key": { "key": {
"s": { "s": {
"item": "quickiefabric:speedpowder" "item": "quickiefabric:speedpowder"
},
"w": {
"item": "minecraft:planks"
} }
}, },
"result": { "result": {