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 org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import de.jottyfan.minecraft.block.QuicklyBlocks;
|
import de.jottyfan.minecraft.block.QuicklyBlocks;
|
||||||
|
import de.jottyfan.minecraft.blockentity.QuicklyBlockEntity;
|
||||||
import de.jottyfan.minecraft.composter.QuicklyComposter;
|
import de.jottyfan.minecraft.composter.QuicklyComposter;
|
||||||
import de.jottyfan.minecraft.feature.QuicklyFeatures;
|
import de.jottyfan.minecraft.feature.QuicklyFeatures;
|
||||||
import de.jottyfan.minecraft.item.QuicklyItems;
|
import de.jottyfan.minecraft.item.QuicklyItems;
|
||||||
@@ -27,6 +28,7 @@ public class Quickly implements ModInitializer {
|
|||||||
QuicklyTab.registerItemGroup();
|
QuicklyTab.registerItemGroup();
|
||||||
QuicklyItems.registerModItems();
|
QuicklyItems.registerModItems();
|
||||||
QuicklyBlocks.registerModBlocks();
|
QuicklyBlocks.registerModBlocks();
|
||||||
|
QuicklyBlockEntity.registerBlockEntities();
|
||||||
QuicklyFeatures.registerFeatures();
|
QuicklyFeatures.registerFeatures();
|
||||||
QuicklyComposter.registerComposterItems();
|
QuicklyComposter.registerComposterItems();
|
||||||
QuicklyLootTables.registerChanges();
|
QuicklyLootTables.registerChanges();
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package de.jottyfan.minecraft.block;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import de.jottyfan.minecraft.Quickly;
|
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.BlockStackerEntity;
|
||||||
import de.jottyfan.minecraft.blockentity.DrillBlockEntity;
|
import de.jottyfan.minecraft.blockentity.DrillBlockEntity;
|
||||||
import de.jottyfan.minecraft.blockentity.ItemHoarderBlockEntity;
|
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: 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
|
// 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) {
|
private static final Block registerBlock(String name, Properties properties) {
|
||||||
return QuicklyBlocks.registerBlock(name, properties, p -> new Block(p));
|
return QuicklyBlocks.registerBlock(name, properties, p -> new Block(p));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,19 +4,14 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.jspecify.annotations.Nullable;
|
|
||||||
|
|
||||||
import com.mojang.serialization.Codec;
|
import com.mojang.serialization.Codec;
|
||||||
|
|
||||||
import de.jottyfan.minecraft.Quickly;
|
|
||||||
import de.jottyfan.minecraft.block.BlockStacker;
|
import de.jottyfan.minecraft.block.BlockStacker;
|
||||||
import de.jottyfan.minecraft.block.QuicklyBlocks;
|
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.world.Container;
|
import net.minecraft.world.Container;
|
||||||
import net.minecraft.world.entity.SlotAccess;
|
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
|
import net.minecraft.world.item.Item;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
import net.minecraft.world.item.Items;
|
|
||||||
import net.minecraft.world.level.Level;
|
import net.minecraft.world.level.Level;
|
||||||
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
|
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
|
||||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
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<>();
|
private List<ItemStack> inventory = new ArrayList<>();
|
||||||
|
|
||||||
public BlockStackerEntity(BlockPos pos, BlockState blockState) {
|
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) {
|
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) {
|
public static final Boolean transferOneStack(BaseContainerBlockEntity source, BaseContainerBlockEntity dest) {
|
||||||
Boolean result = false;
|
Boolean result = false;
|
||||||
Integer sourceSlot = findItemStackPos(source, 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();
|
ItemStack sourceStack = source.getSlot(sourceSlot).get();
|
||||||
Integer destSlot = findItemStackPos(dest, sourceStack);
|
Integer destSlot = findItemStackPos(dest, sourceStack.getItem(), sourceStack.getCount());
|
||||||
if (destSlot != null) {
|
if (destSlot != null) {
|
||||||
ItemStack destStack = dest.getSlot(destSlot).get();
|
ItemStack destStack = dest.getSlot(destSlot).get();
|
||||||
Integer occupied = destStack.getCount();
|
Integer free = destStack.getItem().getDefaultMaxStackSize() - destStack.getCount();
|
||||||
Integer free = destStack.getCount() - occupied;
|
|
||||||
Integer candidates = sourceStack.getCount();
|
Integer candidates = sourceStack.getCount();
|
||||||
Integer travellers = candidates > free ? free : candidates;
|
Integer travellers = candidates > free ? free : candidates;
|
||||||
if (travellers > 0) {
|
if (travellers > 0) {
|
||||||
Quickly.LOGGER.debug("transfer {}/{} of {} from slot {} to slot {} on top of {} ones", travellers, candidates,
|
dest.setItem(destSlot, new ItemStack(sourceStack.getItem(), destStack.getCount() + travellers));
|
||||||
sourceStack.getItem().toString(), sourceSlot, destSlot, occupied);
|
source.removeItem(sourceSlot, travellers);
|
||||||
sourceStack.shrink(travellers);
|
|
||||||
destStack.grow(travellers);
|
|
||||||
result = true;
|
result = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Integer destFreeSlot = findItemStackPos(dest, true);
|
Integer destFreeSlot = findItemStackPos(dest, true);
|
||||||
if (destFreeSlot != null) {
|
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()));
|
dest.setItem(destFreeSlot, new ItemStack(sourceStack.getItem(), sourceStack.getCount()));
|
||||||
sourceStack.shrink(sourceSlot);
|
source.removeItem(sourceSlot, sourceStack.getCount());
|
||||||
result = true;
|
result = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,36 +76,35 @@ public class BlockStackerEntity extends BlockEntity implements Container {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Integer findItemStackPos(BaseContainerBlockEntity blockEntity, ItemStack sourceStack) {
|
private static final Integer findItemStackPos(BaseContainerBlockEntity blockEntity, Item sourceItem, Integer sourceCount) {
|
||||||
Integer counter = blockEntity.getContainerSize();
|
Integer counter = 0;
|
||||||
while (counter > 0) {
|
while (counter < blockEntity.getContainerSize()) {
|
||||||
counter--;
|
ItemStack stack = blockEntity.getItem(counter);
|
||||||
@Nullable
|
if (sourceItem.equals(stack.getItem())) {
|
||||||
SlotAccess slotAccess = blockEntity.getSlot(counter);
|
Integer spaceLeft = stack.getItem().getDefaultMaxStackSize() - stack.getCount();
|
||||||
if (slotAccess != null) {
|
if (sourceCount <= spaceLeft || spaceLeft > 0) {
|
||||||
ItemStack stack = slotAccess.get();
|
return counter;
|
||||||
if (sourceStack.getItem().equals(stack.getItem())) {
|
|
||||||
if (stack.getCount() < stack.getCount()) {
|
|
||||||
return counter;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
counter++;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Integer findItemStackPos(BaseContainerBlockEntity blockEntity, Boolean empty) {
|
private static final Integer findItemStackPos(BaseContainerBlockEntity blockEntity, Boolean seekEmptyStack) {
|
||||||
Integer counter = blockEntity.getContainerSize();
|
Integer counter = 0;
|
||||||
while (counter > 0) {
|
while (counter < blockEntity.getContainerSize()) {
|
||||||
counter--;
|
ItemStack stack = blockEntity.getItem(counter);
|
||||||
@Nullable
|
Boolean check = false;
|
||||||
SlotAccess slotAccess = blockEntity.getSlot(counter);
|
if (seekEmptyStack) {
|
||||||
if (slotAccess != null) {
|
check = ItemStack.EMPTY.equals(stack) || stack.getCount() < 1;
|
||||||
ItemStack stack = slotAccess.get();
|
} else {
|
||||||
if (empty.equals(ItemStack.EMPTY.equals(stack))) {
|
check = !ItemStack.EMPTY.equals(stack) && stack.getCount() > 0;
|
||||||
return counter;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (check) {
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
counter++;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import de.jottyfan.minecraft.block.BlockDrill;
|
import de.jottyfan.minecraft.block.BlockDrill;
|
||||||
import de.jottyfan.minecraft.block.QuicklyBlocks;
|
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.core.Direction;
|
import net.minecraft.core.Direction;
|
||||||
import net.minecraft.world.level.Level;
|
import net.minecraft.world.level.Level;
|
||||||
@@ -25,7 +24,7 @@ public class DrillBlockEntity extends BlockEntity {
|
|||||||
private final Integer maxDrillStep;
|
private final Integer maxDrillStep;
|
||||||
|
|
||||||
public DrillBlockEntity(BlockPos pos, BlockState state) {
|
public DrillBlockEntity(BlockPos pos, BlockState state) {
|
||||||
super(QuicklyBlocks.BLOCKENTITY_DRILL, pos, state);
|
super(QuicklyBlockEntity.BLOCKENTITY_DRILL, pos, state);
|
||||||
this.maxDrillStep = MAXDRILLSTEP;
|
this.maxDrillStep = MAXDRILLSTEP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import java.util.Map;
|
|||||||
import com.mojang.serialization.Codec;
|
import com.mojang.serialization.Codec;
|
||||||
|
|
||||||
import de.jottyfan.minecraft.Quickly;
|
import de.jottyfan.minecraft.Quickly;
|
||||||
import de.jottyfan.minecraft.block.QuicklyBlocks;
|
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.core.Direction;
|
import net.minecraft.core.Direction;
|
||||||
import net.minecraft.world.Container;
|
import net.minecraft.world.Container;
|
||||||
@@ -37,7 +36,7 @@ public class ItemHoarderBlockEntity extends BlockEntity implements Container {
|
|||||||
private static final Integer suckradius = 4;
|
private static final Integer suckradius = 4;
|
||||||
|
|
||||||
public ItemHoarderBlockEntity(BlockPos pos, BlockState state) {
|
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) {
|
private static final String getItemId(Item item) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package de.jottyfan.minecraft.blockentity;
|
package de.jottyfan.minecraft.blockentity;
|
||||||
|
|
||||||
import de.jottyfan.minecraft.Quickly;
|
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;
|
||||||
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder.Factory;
|
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder.Factory;
|
||||||
import net.minecraft.core.Registry;
|
import net.minecraft.core.Registry;
|
||||||
@@ -15,7 +16,14 @@ import net.minecraft.world.level.block.entity.BlockEntityType;
|
|||||||
* @author jotty
|
* @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")
|
@SuppressWarnings("unchecked")
|
||||||
public BlockEntityType<T> registerBlockEntity(String name, Factory<? extends BlockEntity> factory, Block block) {
|
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()
|
FabricBlockEntityTypeBuilder.create(factory, block).build()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final void registerBlockEntities() {
|
||||||
|
// dummy
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user