From 600482aba989feeb2e1b83ca22e5ac809fb8a57e Mon Sep 17 00:00:00 2001 From: jottyfan Date: Fri, 31 Jul 2020 23:38:19 +0200 Subject: [PATCH] working itemhoarder --- .../blockentity/ItemHoarderBlockEntity.java | 167 +++++++++--------- .../blocks/BlockItemhoarder.java | 33 +++- .../blocks/BlockLavahoarder.java | 2 +- 3 files changed, 112 insertions(+), 90 deletions(-) diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/ItemHoarderBlockEntity.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/ItemHoarderBlockEntity.java index 6fab56b..d809d3a 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/ItemHoarderBlockEntity.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/ItemHoarderBlockEntity.java @@ -1,130 +1,123 @@ package de.jottyfan.minecraft.quickiefabric.blockentity; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; -import java.util.ListIterator; -import java.util.Map; import net.minecraft.block.BlockState; -import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.LootableContainerBlockEntity; +import net.minecraft.entity.Entity; +import net.minecraft.entity.ItemEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.inventory.Inventories; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.Tag; +import net.minecraft.screen.ScreenHandler; import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; +import net.minecraft.util.Tickable; +import net.minecraft.util.collection.DefaultedList; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Box; +import net.minecraft.world.World; /** * * @author jotty * */ -public class ItemHoarderBlockEntity extends BlockEntity { - - private final String NBT_STACKS = "stacks"; - private final List stacks; +public class ItemHoarderBlockEntity extends LootableContainerBlockEntity implements Tickable { + private DefaultedList stacks; public ItemHoarderBlockEntity() { super(QuickieFabricBlockEntity.ITEMHOARDER); - stacks = new ArrayList<>(); + stacks = DefaultedList.ofSize(54, ItemStack.EMPTY); } - private final Integer getNextFreeEmptyStackPosition() { - Integer emptyStackPosition = null; - Iterator it = stacks.iterator(); - Integer index = 0; - while (emptyStackPosition == null && it.hasNext()) { - ItemStack s = it.next(); - if (s.isEmpty()) { - emptyStackPosition = index; - } - index++; - } - return emptyStackPosition; - } - // TODO: see https://fabricmc.net/wiki/tutorial:containers for a real chest - // TODO: find a way to suck in the surrounding items -// @Override -// public void tick() { -// BlockPos pos = getPos(); -// World world = getWorld(); -// // list all items around here -// AxisAlignedBB aabb = new AxisAlignedBB(pos).grow(4); -// List itemEntities = world.getEntitiesWithinAABB(ItemEntity.class, aabb); -// boolean dirty = false; -// for (ItemEntity itemEntity : itemEntities) { -// if (itemEntity.isAlive()) { -// ItemStack stack = itemEntity.getItem(); -// if (stack != null) { -// Integer emptyStackPosition = getNextFreeEmptyStackPosition(); -// if (emptyStackPosition == null) { -// } else { -// stacks.set(emptyStackPosition, stack); -// itemEntity.remove(); -// dirty = true; -// } -// } -// } else { -// itemEntity.getItem().getItem().getRegistryName().getPath()); -// } -// } -// if (dirty) { -// resortStacks(); -// } -// } - - /** - * resort item stacks to be more efficient - */ - private void resortStacks() { - Map map = new HashMap<>(); - Integer clearAmount = stacks.size(); - for (ItemStack stack : stacks) { - ItemStack found = map.get(stack.getName()); - if (found == null) { - found = stack; - map.put(stack.getName(), found); - } else { - found.setCount(found.getCount() + stack.getCount()); + private final boolean setStackToSlots(ItemStack stack) { + for (ItemStack slot : stacks) { + if (slot.getItem().equals(stack.getItem())) { + slot.increment(stack.getCount()); + return true; + } + } // if not found, seek for an empty stack instead + for (ItemStack slot : stacks) { + if (slot.isEmpty()) { + Integer index = stacks.indexOf(slot); + stacks.set(index, stack.copy()); + return true; } } - for (Integer i = 0; i < clearAmount; i++) { - stacks.set(i, ItemStack.EMPTY); - } - Integer index = 0; - for (ItemStack stack : map.values()) { - stacks.set(index++, stack); + return false; + } + + @Override + public void tick() { + BlockPos pos = getPos(); + World world = getWorld(); + float suckradius = 4f; // TODO: make it level up - able and start with 2 + Box box = new Box(pos).expand(suckradius); + List entities = world.getEntities(null, box); + for (Entity entity : entities) { + if (entity instanceof ItemEntity) { + ItemEntity itemEntity = (ItemEntity) entity; + if (itemEntity.isAlive()) { + ItemStack stack = itemEntity.getStack(); + if (stack != null) { + if (setStackToSlots(stack)) { + itemEntity.remove(); + } // else inventory is full + } + } + } } } @Override public CompoundTag toTag(CompoundTag tag) { super.toTag(tag); - ListTag listTag = new ListTag(); - for (ItemStack stack : stacks) { - listTag.add(stack.toTag(tag)); + if (!this.serializeLootTable(tag)) { + Inventories.toTag(tag, this.stacks); } - tag.put(NBT_STACKS, listTag); return tag; } @Override public void fromTag(BlockState state, CompoundTag tag) { super.fromTag(state, tag); - ListTag listTag = (ListTag) tag.get(NBT_STACKS); - ListIterator i = listTag.listIterator(); - stacks.clear(); - while (i.hasNext()) { - CompoundTag foundTag = (CompoundTag) i.next(); - ItemStack stack = ItemStack.fromTag(foundTag); - stacks.add(stack); + this.stacks = DefaultedList.ofSize(this.size(), ItemStack.EMPTY); + if (!this.deserializeLootTable(tag)) { + Inventories.fromTag(tag, this.stacks); } } public List getStacks() { return stacks; } + + @Override + public int size() { + return 54; // container chest size (9 * 6) + } + + @Override + protected DefaultedList getInvStackList() { + return stacks; + } + + @Override + protected void setInvStackList(DefaultedList list) { + this.stacks = list; + } + + @Override + protected ScreenHandler createScreenHandler(int syncId, PlayerInventory playerInventory) { + // TODO: implement, see https://fabricmc.net/wiki/tutorial:containers + return null; + } + + @Override + protected Text getContainerName() { + return new TranslatableText("container.itemhoarder"); + } } diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/BlockItemhoarder.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/BlockItemhoarder.java index b7e9955..8d3186d 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/BlockItemhoarder.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/BlockItemhoarder.java @@ -1,12 +1,22 @@ package de.jottyfan.minecraft.quickiefabric.blocks; +import java.util.ArrayList; +import java.util.List; + import de.jottyfan.minecraft.quickiefabric.blockentity.ItemHoarderBlockEntity; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.minecraft.block.Block; import net.minecraft.block.BlockEntityProvider; +import net.minecraft.block.BlockState; import net.minecraft.block.Material; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.loot.context.LootContext.Builder; +import net.minecraft.util.ItemScatterer; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.BlockView; +import net.minecraft.world.World; /** * @@ -18,9 +28,28 @@ public class BlockItemhoarder extends Block implements BlockEntityProvider { public BlockItemhoarder() { super(FabricBlockSettings.of(Material.WOOD).hardness(2.5f)); } - + @Override - public BlockEntity createBlockEntity(BlockView arg0) { + public BlockEntity createBlockEntity(BlockView blockView) { return new ItemHoarderBlockEntity(); } + + @Override + public List getDroppedStacks(BlockState state, Builder builder) { + List list = new ArrayList<>(); + list.add(new ItemStack(QuickieBlocks.ITEMHOARDER)); + return list; + } + + @Override + public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) { + BlockEntity blockEntity = world.getBlockEntity(pos); + if (blockEntity instanceof ItemHoarderBlockEntity) { + ItemHoarderBlockEntity ihbe = (ItemHoarderBlockEntity) blockEntity; + for (ItemStack stack : ihbe.getStacks()) { + ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), stack); + } + } + super.onBreak(world, pos, state, player); + } } diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/BlockLavahoarder.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/BlockLavahoarder.java index 2e67ece..b83a6cc 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/BlockLavahoarder.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/blocks/BlockLavahoarder.java @@ -73,7 +73,7 @@ public class BlockLavahoarder extends Block { @Override public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) { Set positions = new HashSet<>(); - Integer counter = 8; + Integer counter = 8; // TODO: make it level up - able findAllAttachedLavaBlocks(positions, pos.up(), world, counter); findAllAttachedLavaBlocks(positions, pos.down(), world, counter); findAllAttachedLavaBlocks(positions, pos.north(), world, counter);