diff options
| author | 2024-01-09 19:37:28 +0100 | |
|---|---|---|
| committer | 2024-01-09 19:37:28 +0100 | |
| commit | 4606c536a6260477870426234f748067240de3d1 (patch) | |
| tree | 52ecd35ab0a51dd84bbebb675f5433a85166b132 /src/main/java/lv/enes/mc/eris_alchemy/block/entity | |
| parent | Replace ItemMixin.java with proper ItemTooltipCallback usage. (diff) | |
| download | mc-eris-alchemy-4606c536a6260477870426234f748067240de3d1.tar.gz mc-eris-alchemy-4606c536a6260477870426234f748067240de3d1.tar.xz mc-eris-alchemy-4606c536a6260477870426234f748067240de3d1.zip | |
Added Alchemical Chest.
Diffstat (limited to 'src/main/java/lv/enes/mc/eris_alchemy/block/entity')
| -rw-r--r-- | src/main/java/lv/enes/mc/eris_alchemy/block/entity/AlchemicalChestBlockEntity.java | 198 | ||||
| -rw-r--r-- | src/main/java/lv/enes/mc/eris_alchemy/block/entity/ErisAlchemyBlockEntities.java | 33 |
2 files changed, 231 insertions, 0 deletions
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/block/entity/AlchemicalChestBlockEntity.java b/src/main/java/lv/enes/mc/eris_alchemy/block/entity/AlchemicalChestBlockEntity.java new file mode 100644 index 0000000..9c9942f --- /dev/null +++ b/src/main/java/lv/enes/mc/eris_alchemy/block/entity/AlchemicalChestBlockEntity.java | |||
| @@ -0,0 +1,198 @@ | |||
| 1 | package lv.enes.mc.eris_alchemy.block.entity; | ||
| 2 | |||
| 3 | import jakarta.annotation.Nonnull; | ||
| 4 | import lv.enes.mc.eris_alchemy.block.ErisAlchemyBlocks; | ||
| 5 | import lv.enes.mc.eris_alchemy.menu.AlchemicalChestMenu; | ||
| 6 | import net.minecraft.core.BlockPos; | ||
| 7 | import net.minecraft.core.NonNullList; | ||
| 8 | import net.minecraft.nbt.CompoundTag; | ||
| 9 | import net.minecraft.network.chat.Component; | ||
| 10 | import net.minecraft.sounds.SoundEvents; | ||
| 11 | import net.minecraft.sounds.SoundSource; | ||
| 12 | import net.minecraft.world.Container; | ||
| 13 | import net.minecraft.world.ContainerHelper; | ||
| 14 | import net.minecraft.world.entity.player.Inventory; | ||
| 15 | import net.minecraft.world.entity.player.Player; | ||
| 16 | import net.minecraft.world.inventory.AbstractContainerMenu; | ||
| 17 | import net.minecraft.world.item.ItemStack; | ||
| 18 | import net.minecraft.world.level.Level; | ||
| 19 | import net.minecraft.world.level.block.entity.BaseContainerBlockEntity; | ||
| 20 | import net.minecraft.world.level.block.entity.ChestLidController; | ||
| 21 | import net.minecraft.world.level.block.entity.ContainerOpenersCounter; | ||
| 22 | import net.minecraft.world.level.block.entity.LidBlockEntity; | ||
| 23 | import net.minecraft.world.level.block.state.BlockState; | ||
| 24 | |||
| 25 | public class AlchemicalChestBlockEntity extends BaseContainerBlockEntity implements LidBlockEntity { | ||
| 26 | private final static int WIDTH = 13; | ||
| 27 | private final static int HEIGHT = 8; | ||
| 28 | |||
| 29 | private final static int EVENT = 1; | ||
| 30 | |||
| 31 | private final NonNullList<ItemStack> items = NonNullList.withSize(WIDTH * HEIGHT, ItemStack.EMPTY); | ||
| 32 | private final ChestLidController lidController = new ChestLidController(); | ||
| 33 | private final ContainerOpenersCounter openersCounter = new ContainerOpenersCounter() { | ||
| 34 | @Override | ||
| 35 | protected void onOpen(Level world, BlockPos pos, BlockState state) { | ||
| 36 | // TODO: Sound effects? | ||
| 37 | world.playSound( | ||
| 38 | null, | ||
| 39 | pos.getX() + 0.5, | ||
| 40 | pos.getY() + 0.5, | ||
| 41 | pos.getZ() + 0.5, | ||
| 42 | SoundEvents.ENDER_CHEST_OPEN, | ||
| 43 | SoundSource.BLOCKS, | ||
| 44 | 0.5f, | ||
| 45 | world.random.nextFloat() * 0.1f + 0.9f | ||
| 46 | ); | ||
| 47 | } | ||
| 48 | |||
| 49 | @Override | ||
| 50 | protected void onClose(Level world, BlockPos pos, BlockState state) { | ||
| 51 | // TODO: Sound effects? | ||
| 52 | world.playSound( | ||
| 53 | null, | ||
| 54 | pos.getX() + 0.5, | ||
| 55 | pos.getY() + 0.5, | ||
| 56 | pos.getZ() + 0.5, | ||
| 57 | SoundEvents.ENDER_CHEST_CLOSE, | ||
| 58 | SoundSource.BLOCKS, | ||
| 59 | 0.5f, | ||
| 60 | world.random.nextFloat() * 0.1f + 0.9f | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | @Override | ||
| 65 | protected void openerCountChanged(Level world, BlockPos pos, BlockState state, int oldViewerCount, int newViewerCount) { | ||
| 66 | world.blockEvent(worldPosition, ErisAlchemyBlocks.ALCHEMICAL_CHEST, EVENT, newViewerCount); | ||
| 67 | } | ||
| 68 | |||
| 69 | @Override | ||
| 70 | protected boolean isOwnContainer(Player player) { | ||
| 71 | if (player.containerMenu instanceof AlchemicalChestMenu menu) { | ||
| 72 | return menu.getContainer() == AlchemicalChestBlockEntity.this; | ||
| 73 | } | ||
| 74 | return false; | ||
| 75 | } | ||
| 76 | }; | ||
| 77 | |||
| 78 | public AlchemicalChestBlockEntity(BlockPos pos, BlockState state) { | ||
| 79 | super(ErisAlchemyBlockEntities.ALCHEMICAL_CHEST, pos, state); | ||
| 80 | } | ||
| 81 | |||
| 82 | @Override | ||
| 83 | public void clearContent() { | ||
| 84 | items.clear(); | ||
| 85 | } | ||
| 86 | |||
| 87 | @Nonnull | ||
| 88 | @Override | ||
| 89 | protected AbstractContainerMenu createMenu(int syncId, Inventory playerInventory) { | ||
| 90 | return new AlchemicalChestMenu(syncId, playerInventory, this); | ||
| 91 | } | ||
| 92 | |||
| 93 | @Override | ||
| 94 | public int getContainerSize() { | ||
| 95 | return items.size(); | ||
| 96 | } | ||
| 97 | |||
| 98 | @Nonnull | ||
| 99 | @Override | ||
| 100 | protected Component getDefaultName() { | ||
| 101 | return Component.translatable("container.eris_alchemy.alchemical_chest"); | ||
| 102 | } | ||
| 103 | |||
| 104 | @Nonnull | ||
| 105 | @Override | ||
| 106 | public ItemStack getItem(int slot) { | ||
| 107 | return items.get(slot); | ||
| 108 | } | ||
| 109 | |||
| 110 | @Override | ||
| 111 | public float getOpenNess(float tickDelta) { | ||
| 112 | return lidController.getOpenness(tickDelta); | ||
| 113 | } | ||
| 114 | |||
| 115 | @Override | ||
| 116 | public boolean isEmpty() { | ||
| 117 | return items.stream().allMatch(ItemStack::isEmpty); | ||
| 118 | } | ||
| 119 | |||
| 120 | @Override | ||
| 121 | public void load(CompoundTag nbt) { | ||
| 122 | super.load(nbt); | ||
| 123 | ContainerHelper.loadAllItems(nbt, items); | ||
| 124 | } | ||
| 125 | |||
| 126 | private void recheckOpen() { | ||
| 127 | if (!remove) { | ||
| 128 | openersCounter.recheckOpeners(getLevel(), getBlockPos(), getBlockState()); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | @Nonnull | ||
| 133 | @Override | ||
| 134 | public ItemStack removeItem(int slot, int amount) { | ||
| 135 | var stack = ContainerHelper.removeItem(items, slot, amount); | ||
| 136 | if (!stack.isEmpty()) { | ||
| 137 | this.setChanged(); | ||
| 138 | } | ||
| 139 | return stack; | ||
| 140 | } | ||
| 141 | |||
| 142 | @Nonnull | ||
| 143 | @Override | ||
| 144 | public ItemStack removeItemNoUpdate(int slot) { | ||
| 145 | return ContainerHelper.takeItem(items, slot); | ||
| 146 | } | ||
| 147 | |||
| 148 | @Override | ||
| 149 | protected void saveAdditional(CompoundTag nbt) { | ||
| 150 | super.saveAdditional(nbt); | ||
| 151 | ContainerHelper.saveAllItems(nbt, items); | ||
| 152 | } | ||
| 153 | |||
| 154 | @Override | ||
| 155 | public void setItem(int slot, ItemStack stack) { | ||
| 156 | items.set(slot, stack); | ||
| 157 | if (stack.getCount() > getMaxStackSize()) { | ||
| 158 | stack.setCount(getMaxStackSize()); | ||
| 159 | } | ||
| 160 | this.setChanged(); | ||
| 161 | } | ||
| 162 | |||
| 163 | @Override | ||
| 164 | public void startOpen(Player player) { | ||
| 165 | if (!remove && !player.isSpectator()) { | ||
| 166 | openersCounter.incrementOpeners(player, getLevel(), getBlockPos(), getBlockState()); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | @Override | ||
| 171 | public boolean stillValid(Player player) { | ||
| 172 | return Container.stillValidBlockEntity(this, player); | ||
| 173 | } | ||
| 174 | |||
| 175 | @Override | ||
| 176 | public void stopOpen(Player player) { | ||
| 177 | if (!remove && !player.isSpectator()) { | ||
| 178 | openersCounter.decrementOpeners(player, getLevel(), getBlockPos(), getBlockState()); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | public void tick(Level world, BlockPos ignoredPos, BlockState ignoredState) { | ||
| 183 | if (world.isClientSide) { | ||
| 184 | lidController.tickLid(); | ||
| 185 | } | ||
| 186 | recheckOpen(); | ||
| 187 | } | ||
| 188 | |||
| 189 | @Override | ||
| 190 | public boolean triggerEvent(int type, int data) { | ||
| 191 | if (type == EVENT) { | ||
| 192 | lidController.shouldBeOpen(data > 0); | ||
| 193 | return true; | ||
| 194 | } | ||
| 195 | |||
| 196 | return super.triggerEvent(type, data); | ||
| 197 | } | ||
| 198 | } | ||
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/block/entity/ErisAlchemyBlockEntities.java b/src/main/java/lv/enes/mc/eris_alchemy/block/entity/ErisAlchemyBlockEntities.java new file mode 100644 index 0000000..a95ac58 --- /dev/null +++ b/src/main/java/lv/enes/mc/eris_alchemy/block/entity/ErisAlchemyBlockEntities.java | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | package lv.enes.mc.eris_alchemy.block.entity; | ||
| 2 | |||
| 3 | import lv.enes.mc.eris_alchemy.ErisAlchemy; | ||
| 4 | import lv.enes.mc.eris_alchemy.block.ErisAlchemyBlocks; | ||
| 5 | import net.minecraft.resources.ResourceLocation; | ||
| 6 | import net.minecraft.world.level.block.entity.BlockEntity; | ||
| 7 | import net.minecraft.world.level.block.entity.BlockEntityType; | ||
| 8 | import org.quiltmc.qsl.block.entity.api.QuiltBlockEntityTypeBuilder; | ||
| 9 | |||
| 10 | import java.util.LinkedHashMap; | ||
| 11 | import java.util.Map; | ||
| 12 | import java.util.function.BiConsumer; | ||
| 13 | |||
| 14 | public final class ErisAlchemyBlockEntities { | ||
| 15 | private static final Map<ResourceLocation, BlockEntityType<?>> entities = new LinkedHashMap<>(); | ||
| 16 | |||
| 17 | public static final BlockEntityType<AlchemicalChestBlockEntity> ALCHEMICAL_CHEST = register( | ||
| 18 | "alchemical_chest", | ||
| 19 | QuiltBlockEntityTypeBuilder.create(AlchemicalChestBlockEntity::new, ErisAlchemyBlocks.ALCHEMICAL_CHEST) | ||
| 20 | .build() | ||
| 21 | ); | ||
| 22 | |||
| 23 | public static void consumeBlockEntities(BiConsumer<? super ResourceLocation, ? super BlockEntityType<?>> consumer) { | ||
| 24 | entities.forEach(consumer); | ||
| 25 | } | ||
| 26 | |||
| 27 | private static <T extends BlockEntity> BlockEntityType<T> register(String id, BlockEntityType<T> type) { | ||
| 28 | entities.putIfAbsent(new ResourceLocation(ErisAlchemy.ID, id), type); | ||
| 29 | return type; | ||
| 30 | } | ||
| 31 | |||
| 32 | private ErisAlchemyBlockEntities() {} | ||
| 33 | } | ||