added tools
This commit is contained in:
@ -0,0 +1,154 @@
|
||||
package de.jottyfan.quickiemod.blockentity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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.Entity.RemovalReason;
|
||||
import net.minecraft.entity.ItemEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.inventory.Inventories;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.Items;
|
||||
import net.minecraft.nbt.NbtCompound;
|
||||
import net.minecraft.registry.RegistryWrapper.WrapperLookup;
|
||||
import net.minecraft.screen.ScreenHandler;
|
||||
import net.minecraft.text.Text;
|
||||
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 LootableContainerBlockEntity {
|
||||
|
||||
|
||||
private DefaultedList<ItemStack> stacks;
|
||||
private float suckradius;
|
||||
|
||||
public ItemHoarderBlockEntity(BlockPos pos, BlockState state) {
|
||||
super(ModBlockentity.ITEM_HOARDER_BLOCKENTITY, pos, state);
|
||||
stacks = DefaultedList.ofSize(54, ItemStack.EMPTY);
|
||||
setSuckradius(4f);
|
||||
}
|
||||
|
||||
// TODO: see https://fabricmc.net/wiki/tutorial:containers for a real chest
|
||||
|
||||
public final static boolean setStackToSlots(ItemStack stack, List<ItemStack> stacks) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void tick(World world, BlockPos pos, BlockState state, BlockEntity be) {
|
||||
if (be instanceof ItemHoarderBlockEntity) {
|
||||
ItemHoarderBlockEntity ihbe = (ItemHoarderBlockEntity) be;
|
||||
Box box = new Box(pos).expand(ihbe.getSuckradius());
|
||||
List<Entity> entities = world.getOtherEntities(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 (ItemHoarderBlockEntity.setStackToSlots(stack, ihbe.getStacks())) {
|
||||
itemEntity.remove(RemovalReason.DISCARDED);
|
||||
} // else inventory is full
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeNbt(NbtCompound nbt, WrapperLookup registryLookup) {
|
||||
super.writeNbt(nbt, registryLookup);
|
||||
if (!this.writeLootTable(nbt)) {
|
||||
Inventories.writeNbt(nbt, this.stacks, registryLookup);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void readNbt(NbtCompound nbt, WrapperLookup registryLookup) {
|
||||
super.readNbt(nbt, registryLookup);
|
||||
this.stacks = DefaultedList.ofSize(this.size(), ItemStack.EMPTY);
|
||||
if (!this.readLootTable(nbt)) {
|
||||
Inventories.readNbt(nbt, this.stacks, registryLookup);
|
||||
}
|
||||
}
|
||||
|
||||
public List<ItemStack> getStacks() {
|
||||
return stacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return 54; // container chest size (9 * 6)
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DefaultedList<ItemStack> getHeldStacks() {
|
||||
return stacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setHeldStacks(DefaultedList<ItemStack> 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 Text.translatable("container.itemhoarder");
|
||||
}
|
||||
|
||||
/**
|
||||
* get a summary of content for the chat box
|
||||
*
|
||||
* @return the summary
|
||||
*/
|
||||
public List<Text> getSummary() {
|
||||
List<Text> list = new ArrayList<>();
|
||||
for (ItemStack stack : stacks) {
|
||||
Item item = stack.getItem();
|
||||
if (item != Items.AIR) {
|
||||
Text text = Text.of(String.format("%dx %s", stack.getCount(), Text.translatable(stack.getItem().getTranslationKey())));
|
||||
list.add(text);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public float getSuckradius() {
|
||||
return suckradius;
|
||||
}
|
||||
|
||||
public void setSuckradius(float suckradius) {
|
||||
this.suckradius = suckradius;
|
||||
}
|
||||
}
|
@ -1,13 +1,11 @@
|
||||
package de.jottyfan.quickiemod.blockentity;
|
||||
|
||||
import de.jottyfan.quickiemod.Quickiemod;
|
||||
import de.jottyfan.quickiemod.block.ItemHoarderBlockEntity;
|
||||
import de.jottyfan.quickiemod.block.ModBlocks;
|
||||
import de.jottyfan.quickiemod.identifier.ModIdentifiers;
|
||||
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.registry.Registries;
|
||||
import net.minecraft.registry.Registry;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -16,7 +14,7 @@ import net.minecraft.util.Identifier;
|
||||
*/
|
||||
public class ModBlockentity {
|
||||
public static final BlockEntityType<ItemHoarderBlockEntity> ITEM_HOARDER_BLOCKENTITY = Registry.register(
|
||||
Registries.BLOCK_ENTITY_TYPE, Identifier.of(Quickiemod.MOD_ID, "itemhoarderblockentity"),
|
||||
Registries.BLOCK_ENTITY_TYPE, ModIdentifiers.BLOCKENTITY_ITEMHOARDER,
|
||||
FabricBlockEntityTypeBuilder.create(ItemHoarderBlockEntity::new, ModBlocks.BLOCK_ITEMHOARDER).build());
|
||||
|
||||
public static final void registerModBlockentities() {
|
||||
|
Reference in New Issue
Block a user