added itemhoarder with block entity
This commit is contained in:
parent
451f19ea73
commit
7993693d9c
@ -6,6 +6,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import de.jottyfan.quickiemod.block.ModBlocks;
|
import de.jottyfan.quickiemod.block.ModBlocks;
|
||||||
|
import de.jottyfan.quickiemod.blockentity.ModBlockentity;
|
||||||
import de.jottyfan.quickiemod.item.ModItems;
|
import de.jottyfan.quickiemod.item.ModItems;
|
||||||
import de.jottyfan.quickiemod.itemgroup.ModItemGroup;
|
import de.jottyfan.quickiemod.itemgroup.ModItemGroup;
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
@ -25,6 +26,7 @@ public class Quickiemod implements ModInitializer {
|
|||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
List<Item> items = ModItems.registerModItems();
|
List<Item> items = ModItems.registerModItems();
|
||||||
List<Block> blocks = ModBlocks.registerModBlocks();
|
List<Block> blocks = ModBlocks.registerModBlocks();
|
||||||
|
ModBlockentity.registerModBlockentities();
|
||||||
ModItemGroup.registerItemGroup(items, blocks);
|
ModItemGroup.registerItemGroup(items, blocks);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
package de.jottyfan.quickiemod.block;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import de.jottyfan.quickiemod.blockentity.ModBlockentity;
|
||||||
|
import net.minecraft.block.AbstractBlock;
|
||||||
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.block.BlockEntityProvider;
|
||||||
|
import net.minecraft.block.BlockRenderType;
|
||||||
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.block.entity.BlockEntity;
|
||||||
|
import net.minecraft.block.entity.BlockEntityTicker;
|
||||||
|
import net.minecraft.block.entity.BlockEntityType;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.loot.context.LootWorldContext.Builder;
|
||||||
|
import net.minecraft.registry.RegistryKey;
|
||||||
|
import net.minecraft.registry.RegistryKeys;
|
||||||
|
import net.minecraft.text.Style;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.util.ActionResult;
|
||||||
|
import net.minecraft.util.Formatting;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import net.minecraft.util.ItemScatterer;
|
||||||
|
import net.minecraft.util.hit.BlockHitResult;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author jotty
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class BlockItemhoarder extends Block implements BlockEntityProvider {
|
||||||
|
|
||||||
|
public BlockItemhoarder(Identifier identifier) {
|
||||||
|
super(AbstractBlock.Settings.create().hardness(2.5f).registryKey(RegistryKey.of(RegistryKeys.BLOCK, identifier)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BlockEntity createBlockEntity(BlockPos pos, BlockState blockState) {
|
||||||
|
return ModBlockentity.ITEM_HOARDER_BLOCKENTITY.instantiate(pos, blockState);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BlockRenderType getRenderType(BlockState state) {
|
||||||
|
return BlockRenderType.MODEL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state,
|
||||||
|
BlockEntityType<T> type) {
|
||||||
|
return (world1, pos, state1, be) -> ItemHoarderBlockEntity.tick(world1, pos, state1, be);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) {
|
||||||
|
List<ItemStack> list = new ArrayList<>();
|
||||||
|
list.add(new ItemStack(ModBlocks.BLOCK_ITEMHOARDER));
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BlockState 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return super.onBreak(world, pos, state, player);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
|
||||||
|
if (!world.isClient) {
|
||||||
|
BlockEntity blockEntity = world.getBlockEntity(pos);
|
||||||
|
if (blockEntity instanceof ItemHoarderBlockEntity) {
|
||||||
|
ItemHoarderBlockEntity ihbe = (ItemHoarderBlockEntity) blockEntity;
|
||||||
|
player.sendMessage(
|
||||||
|
Text.translatable("msg.itemhoarder.summary", new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(new Date()))
|
||||||
|
.setStyle(Style.EMPTY.withColor(Formatting.BOLD)),
|
||||||
|
false);
|
||||||
|
for (Text text : ihbe.getSummary()) {
|
||||||
|
player.sendMessage(text, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ActionResult.SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,155 @@
|
|||||||
|
package de.jottyfan.quickiemod.block;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import de.jottyfan.quickiemod.blockentity.ModBlockentity;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -37,6 +37,8 @@ public class ModBlocks {
|
|||||||
new BlockLavahoarder(ModIdentifiers.BLOCK_LAVAHOARDER));
|
new BlockLavahoarder(ModIdentifiers.BLOCK_LAVAHOARDER));
|
||||||
public static final Block BLOCK_EMPTYLAVAHOARDER = registerBlock(ModIdentifiers.BLOCK_EMPTYLAVAHOARDER,
|
public static final Block BLOCK_EMPTYLAVAHOARDER = registerBlock(ModIdentifiers.BLOCK_EMPTYLAVAHOARDER,
|
||||||
new BlockEmptyLavahoarder(ModIdentifiers.BLOCK_EMPTYLAVAHOARDER));
|
new BlockEmptyLavahoarder(ModIdentifiers.BLOCK_EMPTYLAVAHOARDER));
|
||||||
|
public static final Block BLOCK_ITEMHOARDER = registerBlock(ModIdentifiers.BLOCK_ITEMHOARDER,
|
||||||
|
new BlockItemhoarder(ModIdentifiers.BLOCK_ITEMHOARDER));
|
||||||
|
|
||||||
private static final Block registerBlock(Identifier identifier, Block block) {
|
private static final Block registerBlock(Identifier identifier, Block block) {
|
||||||
Registry.register(Registries.ITEM, identifier, new BlockItem(block, new Item.Settings()
|
Registry.register(Registries.ITEM, identifier, new BlockItem(block, new Item.Settings()
|
||||||
@ -55,6 +57,7 @@ public class ModBlocks {
|
|||||||
blocks.add(BLOCK_MONSTERHOARDER);
|
blocks.add(BLOCK_MONSTERHOARDER);
|
||||||
blocks.add(BLOCK_EMPTYLAVAHOARDER);
|
blocks.add(BLOCK_EMPTYLAVAHOARDER);
|
||||||
blocks.add(BLOCK_LAVAHOARDER);
|
blocks.add(BLOCK_LAVAHOARDER);
|
||||||
|
blocks.add(BLOCK_ITEMHOARDER);
|
||||||
return blocks;
|
return blocks;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
package de.jottyfan.quickiemod.blockentity;
|
||||||
|
|
||||||
|
import de.jottyfan.quickiemod.Quickiemod;
|
||||||
|
import de.jottyfan.quickiemod.block.ItemHoarderBlockEntity;
|
||||||
|
import de.jottyfan.quickiemod.block.ModBlocks;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author jotty
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ModBlockentity {
|
||||||
|
public static final BlockEntityType<ItemHoarderBlockEntity> ITEM_HOARDER_BLOCKENTITY = Registry.register(
|
||||||
|
Registries.BLOCK_ENTITY_TYPE, Identifier.of(Quickiemod.MOD_ID, "itemhoarderblockentity"),
|
||||||
|
FabricBlockEntityTypeBuilder.create(ItemHoarderBlockEntity::new, ModBlocks.BLOCK_ITEMHOARDER).build());
|
||||||
|
|
||||||
|
public static final void registerModBlockentities() {
|
||||||
|
};
|
||||||
|
}
|
@ -27,4 +27,5 @@ public class ModIdentifiers {
|
|||||||
public static final Identifier BLOCK_MONSTERHOARDER = Identifier.of(Quickiemod.MOD_ID, "monsterhoarder");
|
public static final Identifier BLOCK_MONSTERHOARDER = Identifier.of(Quickiemod.MOD_ID, "monsterhoarder");
|
||||||
public static final Identifier BLOCK_LAVAHOARDER = Identifier.of(Quickiemod.MOD_ID, "lavahoarder");;
|
public static final Identifier BLOCK_LAVAHOARDER = Identifier.of(Quickiemod.MOD_ID, "lavahoarder");;
|
||||||
public static final Identifier BLOCK_EMPTYLAVAHOARDER = Identifier.of(Quickiemod.MOD_ID, "emptylavahoarder");;
|
public static final Identifier BLOCK_EMPTYLAVAHOARDER = Identifier.of(Quickiemod.MOD_ID, "emptylavahoarder");;
|
||||||
|
public static final Identifier BLOCK_ITEMHOARDER = Identifier.of(Quickiemod.MOD_ID, "itemhoarder");;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"variants": {
|
||||||
|
"": {
|
||||||
|
"model": "quickiemod:block/itemhoarder"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"parent": "block/cube_all",
|
||||||
|
"textures": {
|
||||||
|
"all": "quickiemod:block/itemhoarder"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"parent": "quickiemod:block/itemhoarder",
|
||||||
|
"display": {
|
||||||
|
"thirdperson": {
|
||||||
|
"rotation": [ 10, -45, 170 ],
|
||||||
|
"translation": [ 0, 1.5, -2.75 ],
|
||||||
|
"scale": [ 0.375, 0.375, 0.375 ]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"type": "minecraft:crafting_shaped",
|
||||||
|
"pattern": [
|
||||||
|
"ooo",
|
||||||
|
"oso",
|
||||||
|
"ooo"
|
||||||
|
],
|
||||||
|
"key": {
|
||||||
|
"o": "quickiemod:speedingot",
|
||||||
|
"s": "minecraft:barrel"
|
||||||
|
},
|
||||||
|
"result": {
|
||||||
|
"id": "quickiemod:itemhoarder",
|
||||||
|
"count": 1
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"type": "minecraft:crafting_shaped",
|
||||||
|
"pattern": [
|
||||||
|
"ooo",
|
||||||
|
"oso",
|
||||||
|
"ooo"
|
||||||
|
],
|
||||||
|
"key": {
|
||||||
|
"o": "quickiemod:speedingot",
|
||||||
|
"s": "c:chests"
|
||||||
|
},
|
||||||
|
"result": {
|
||||||
|
"id": "quickiemod:itemhoarder",
|
||||||
|
"count": 1
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user