fixed blockstacker completely
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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<ItemHoarderBlockEntity> BLOCKENTITY_ITEMHOARDER = new BlockEntityRegister<ItemHoarderBlockEntity>()
|
||||
.registerBlockEntity("itemhoarderblockentity", ItemHoarderBlockEntity::new, ITEMHOARDER);
|
||||
public static final BlockEntityType<DrillBlockEntity> BLOCKENTITY_DRILL = new BlockEntityRegister<DrillBlockEntity>()
|
||||
.registerBlockEntity("drillblockentity", DrillBlockEntity::new, DRILL);
|
||||
public static final BlockEntityType<BlockStackerEntity> BLOCKSTACKER_BLOCKENTITY = new BlockEntityRegister<BlockStackerEntity>()
|
||||
.registerBlockEntity("blockstackerentity", BlockStackerEntity::new, STACKER);
|
||||
|
||||
private static final Block registerBlock(String name, Properties properties) {
|
||||
return QuicklyBlocks.registerBlock(name, properties, p -> new Block(p));
|
||||
}
|
||||
|
||||
@@ -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<ItemStack> inventory = new ArrayList<>();
|
||||
|
||||
public BlockStackerEntity(BlockPos pos, BlockState blockState) {
|
||||
super(QuicklyBlocks.BLOCKSTACKER_BLOCKENTITY, pos, blockState);
|
||||
super(QuicklyBlockEntity.BLOCKSTACKER_BLOCKENTITY, pos, blockState);
|
||||
}
|
||||
|
||||
public static <T extends BlockEntity> 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()) {
|
||||
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))) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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<T extends BlockEntity> {
|
||||
public class QuicklyBlockEntity<T extends BlockEntity> {
|
||||
|
||||
public static final BlockEntityType<ItemHoarderBlockEntity> BLOCKENTITY_ITEMHOARDER = new QuicklyBlockEntity<ItemHoarderBlockEntity>()
|
||||
.registerBlockEntity("itemhoarderblockentity", ItemHoarderBlockEntity::new, QuicklyBlocks.ITEMHOARDER);
|
||||
public static final BlockEntityType<DrillBlockEntity> BLOCKENTITY_DRILL = new QuicklyBlockEntity<DrillBlockEntity>()
|
||||
.registerBlockEntity("drillblockentity", DrillBlockEntity::new, QuicklyBlocks.DRILL);
|
||||
public static final BlockEntityType<BlockStackerEntity> BLOCKSTACKER_BLOCKENTITY = new QuicklyBlockEntity<BlockStackerEntity>()
|
||||
.registerBlockEntity("blockstackerentity", BlockStackerEntity::new, QuicklyBlocks.STACKER);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public BlockEntityType<T> registerBlockEntity(String name, Factory<? extends BlockEntity> factory, Block block) {
|
||||
@@ -25,4 +33,8 @@ public class BlockEntityRegister<T extends BlockEntity> {
|
||||
FabricBlockEntityTypeBuilder.create(factory, block).build()
|
||||
);
|
||||
}
|
||||
|
||||
public static final void registerBlockEntities() {
|
||||
// dummy
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user