diff --git a/src/main/java/de/jottyfan/minecraft/Quickly.java b/src/main/java/de/jottyfan/minecraft/Quickly.java index 9992954..7714249 100644 --- a/src/main/java/de/jottyfan/minecraft/Quickly.java +++ b/src/main/java/de/jottyfan/minecraft/Quickly.java @@ -4,6 +4,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import de.jottyfan.minecraft.block.QuicklyBlocks; +import de.jottyfan.minecraft.blockentity.QuicklyBlockEntity; import de.jottyfan.minecraft.composter.QuicklyComposter; import de.jottyfan.minecraft.feature.QuicklyFeatures; import de.jottyfan.minecraft.item.QuicklyItems; @@ -27,6 +28,7 @@ public class Quickly implements ModInitializer { QuicklyTab.registerItemGroup(); QuicklyItems.registerModItems(); QuicklyBlocks.registerModBlocks(); + QuicklyBlockEntity.registerBlockEntities(); QuicklyFeatures.registerFeatures(); QuicklyComposter.registerComposterItems(); QuicklyLootTables.registerChanges(); diff --git a/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java b/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java index bc6c286..deeec29 100644 --- a/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java +++ b/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java @@ -3,7 +3,7 @@ package de.jottyfan.minecraft.block; import java.util.function.Function; import de.jottyfan.minecraft.Quickly; -import de.jottyfan.minecraft.blockentity.BlockEntityRegister; +import de.jottyfan.minecraft.blockentity.QuicklyBlockEntity; import de.jottyfan.minecraft.blockentity.BlockStackerEntity; import de.jottyfan.minecraft.blockentity.DrillBlockEntity; import de.jottyfan.minecraft.blockentity.ItemHoarderBlockEntity; @@ -62,13 +62,6 @@ public class QuicklyBlocks { // TODO: merge lavahoarder and emptylavahoarder into one block using a BooleanProperty for the lava fill state // TODO: repair block stacker bugs; transport seems to only use the max container size slot - public static final BlockEntityType BLOCKENTITY_ITEMHOARDER = new BlockEntityRegister() - .registerBlockEntity("itemhoarderblockentity", ItemHoarderBlockEntity::new, ITEMHOARDER); - public static final BlockEntityType BLOCKENTITY_DRILL = new BlockEntityRegister() - .registerBlockEntity("drillblockentity", DrillBlockEntity::new, DRILL); - public static final BlockEntityType BLOCKSTACKER_BLOCKENTITY = new BlockEntityRegister() - .registerBlockEntity("blockstackerentity", BlockStackerEntity::new, STACKER); - private static final Block registerBlock(String name, Properties properties) { return QuicklyBlocks.registerBlock(name, properties, p -> new Block(p)); } diff --git a/src/main/java/de/jottyfan/minecraft/blockentity/BlockStackerEntity.java b/src/main/java/de/jottyfan/minecraft/blockentity/BlockStackerEntity.java index dcc780a..3f9e571 100644 --- a/src/main/java/de/jottyfan/minecraft/blockentity/BlockStackerEntity.java +++ b/src/main/java/de/jottyfan/minecraft/blockentity/BlockStackerEntity.java @@ -4,19 +4,14 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.jspecify.annotations.Nullable; - import com.mojang.serialization.Codec; -import de.jottyfan.minecraft.Quickly; import de.jottyfan.minecraft.block.BlockStacker; -import de.jottyfan.minecraft.block.QuicklyBlocks; import net.minecraft.core.BlockPos; import net.minecraft.world.Container; -import net.minecraft.world.entity.SlotAccess; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BaseContainerBlockEntity; import net.minecraft.world.level.block.entity.BlockEntity; @@ -35,7 +30,7 @@ public class BlockStackerEntity extends BlockEntity implements Container { private List inventory = new ArrayList<>(); public BlockStackerEntity(BlockPos pos, BlockState blockState) { - super(QuicklyBlocks.BLOCKSTACKER_BLOCKENTITY, pos, blockState); + super(QuicklyBlockEntity.BLOCKSTACKER_BLOCKENTITY, pos, blockState); } public static Object tick(Level level, BlockPos pos, BlockState state, T entity) { @@ -56,29 +51,24 @@ public class BlockStackerEntity extends BlockEntity implements Container { public static final Boolean transferOneStack(BaseContainerBlockEntity source, BaseContainerBlockEntity dest) { Boolean result = false; Integer sourceSlot = findItemStackPos(source, false); - if (sourceSlot != null && !Items.AIR.equals(source.getSlot(sourceSlot).get().getItem())) { + if (sourceSlot != null) { ItemStack sourceStack = source.getSlot(sourceSlot).get(); - Integer destSlot = findItemStackPos(dest, sourceStack); + Integer destSlot = findItemStackPos(dest, sourceStack.getItem(), sourceStack.getCount()); if (destSlot != null) { ItemStack destStack = dest.getSlot(destSlot).get(); - Integer occupied = destStack.getCount(); - Integer free = destStack.getCount() - occupied; + Integer free = destStack.getItem().getDefaultMaxStackSize() - destStack.getCount(); Integer candidates = sourceStack.getCount(); Integer travellers = candidates > free ? free : candidates; if (travellers > 0) { - Quickly.LOGGER.debug("transfer {}/{} of {} from slot {} to slot {} on top of {} ones", travellers, candidates, - sourceStack.getItem().toString(), sourceSlot, destSlot, occupied); - sourceStack.shrink(travellers); - destStack.grow(travellers); + dest.setItem(destSlot, new ItemStack(sourceStack.getItem(), destStack.getCount() + travellers)); + source.removeItem(sourceSlot, travellers); result = true; } } else { Integer destFreeSlot = findItemStackPos(dest, true); if (destFreeSlot != null) { - Quickly.LOGGER.debug("transfer all of {} from slot {} to slot {}", sourceStack.getItem().toString(), - sourceSlot, destSlot); dest.setItem(destFreeSlot, new ItemStack(sourceStack.getItem(), sourceStack.getCount())); - sourceStack.shrink(sourceSlot); + source.removeItem(sourceSlot, sourceStack.getCount()); result = true; } } @@ -86,36 +76,35 @@ public class BlockStackerEntity extends BlockEntity implements Container { return result; } - private static final Integer findItemStackPos(BaseContainerBlockEntity blockEntity, ItemStack sourceStack) { - Integer counter = blockEntity.getContainerSize(); - while (counter > 0) { - counter--; - @Nullable - SlotAccess slotAccess = blockEntity.getSlot(counter); - if (slotAccess != null) { - ItemStack stack = slotAccess.get(); - if (sourceStack.getItem().equals(stack.getItem())) { - if (stack.getCount() < stack.getCount()) { - return counter; - } + private static final Integer findItemStackPos(BaseContainerBlockEntity blockEntity, Item sourceItem, Integer sourceCount) { + Integer counter = 0; + while (counter < blockEntity.getContainerSize()) { + ItemStack stack = blockEntity.getItem(counter); + if (sourceItem.equals(stack.getItem())) { + Integer spaceLeft = stack.getItem().getDefaultMaxStackSize() - stack.getCount(); + if (sourceCount <= spaceLeft || spaceLeft > 0) { + return counter; } } + counter++; } return null; } - private static final Integer findItemStackPos(BaseContainerBlockEntity blockEntity, Boolean empty) { - Integer counter = blockEntity.getContainerSize(); - while (counter > 0) { - counter--; - @Nullable - SlotAccess slotAccess = blockEntity.getSlot(counter); - if (slotAccess != null) { - ItemStack stack = slotAccess.get(); - if (empty.equals(ItemStack.EMPTY.equals(stack))) { - return counter; - } + private static final Integer findItemStackPos(BaseContainerBlockEntity blockEntity, Boolean seekEmptyStack) { + Integer counter = 0; + while (counter < blockEntity.getContainerSize()) { + ItemStack stack = blockEntity.getItem(counter); + Boolean check = false; + if (seekEmptyStack) { + check = ItemStack.EMPTY.equals(stack) || stack.getCount() < 1; + } else { + check = !ItemStack.EMPTY.equals(stack) && stack.getCount() > 0; } + if (check) { + return counter; + } + counter++; } return null; } diff --git a/src/main/java/de/jottyfan/minecraft/blockentity/DrillBlockEntity.java b/src/main/java/de/jottyfan/minecraft/blockentity/DrillBlockEntity.java index 8d0fdd4..81e2717 100644 --- a/src/main/java/de/jottyfan/minecraft/blockentity/DrillBlockEntity.java +++ b/src/main/java/de/jottyfan/minecraft/blockentity/DrillBlockEntity.java @@ -4,7 +4,6 @@ import java.util.ArrayList; import java.util.List; import de.jottyfan.minecraft.block.BlockDrill; -import de.jottyfan.minecraft.block.QuicklyBlocks; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.world.level.Level; @@ -25,7 +24,7 @@ public class DrillBlockEntity extends BlockEntity { private final Integer maxDrillStep; public DrillBlockEntity(BlockPos pos, BlockState state) { - super(QuicklyBlocks.BLOCKENTITY_DRILL, pos, state); + super(QuicklyBlockEntity.BLOCKENTITY_DRILL, pos, state); this.maxDrillStep = MAXDRILLSTEP; } diff --git a/src/main/java/de/jottyfan/minecraft/blockentity/ItemHoarderBlockEntity.java b/src/main/java/de/jottyfan/minecraft/blockentity/ItemHoarderBlockEntity.java index bb74055..6985393 100644 --- a/src/main/java/de/jottyfan/minecraft/blockentity/ItemHoarderBlockEntity.java +++ b/src/main/java/de/jottyfan/minecraft/blockentity/ItemHoarderBlockEntity.java @@ -6,7 +6,6 @@ import java.util.Map; import com.mojang.serialization.Codec; import de.jottyfan.minecraft.Quickly; -import de.jottyfan.minecraft.block.QuicklyBlocks; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.world.Container; @@ -37,7 +36,7 @@ public class ItemHoarderBlockEntity extends BlockEntity implements Container { private static final Integer suckradius = 4; public ItemHoarderBlockEntity(BlockPos pos, BlockState state) { - super(QuicklyBlocks.BLOCKENTITY_ITEMHOARDER, pos, state); + super(QuicklyBlockEntity.BLOCKENTITY_ITEMHOARDER, pos, state); } private static final String getItemId(Item item) { diff --git a/src/main/java/de/jottyfan/minecraft/blockentity/BlockEntityRegister.java b/src/main/java/de/jottyfan/minecraft/blockentity/QuicklyBlockEntity.java similarity index 53% rename from src/main/java/de/jottyfan/minecraft/blockentity/BlockEntityRegister.java rename to src/main/java/de/jottyfan/minecraft/blockentity/QuicklyBlockEntity.java index 683028b..fd21649 100644 --- a/src/main/java/de/jottyfan/minecraft/blockentity/BlockEntityRegister.java +++ b/src/main/java/de/jottyfan/minecraft/blockentity/QuicklyBlockEntity.java @@ -1,6 +1,7 @@ package de.jottyfan.minecraft.blockentity; import de.jottyfan.minecraft.Quickly; +import de.jottyfan.minecraft.block.QuicklyBlocks; import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder.Factory; import net.minecraft.core.Registry; @@ -15,7 +16,14 @@ import net.minecraft.world.level.block.entity.BlockEntityType; * @author jotty * */ -public class BlockEntityRegister { +public class QuicklyBlockEntity { + + public static final BlockEntityType BLOCKENTITY_ITEMHOARDER = new QuicklyBlockEntity() + .registerBlockEntity("itemhoarderblockentity", ItemHoarderBlockEntity::new, QuicklyBlocks.ITEMHOARDER); + public static final BlockEntityType BLOCKENTITY_DRILL = new QuicklyBlockEntity() + .registerBlockEntity("drillblockentity", DrillBlockEntity::new, QuicklyBlocks.DRILL); + public static final BlockEntityType BLOCKSTACKER_BLOCKENTITY = new QuicklyBlockEntity() + .registerBlockEntity("blockstackerentity", BlockStackerEntity::new, QuicklyBlocks.STACKER); @SuppressWarnings("unchecked") public BlockEntityType registerBlockEntity(String name, Factory factory, Block block) { @@ -25,4 +33,8 @@ public class BlockEntityRegister { FabricBlockEntityTypeBuilder.create(factory, block).build() ); } + + public static final void registerBlockEntities() { + // dummy + } }