| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.HolderLookup;
|
| 7 | import net.minecraft.core.component.DataComponentGetter;
|
| 8 | import net.minecraft.core.component.DataComponentMap;
|
| 9 | import net.minecraft.core.component.DataComponentPatch;
|
| 10 | import net.minecraft.core.component.DataComponents;
|
| 11 | import net.minecraft.nbt.CompoundTag;
|
| 12 | import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
| 13 | import net.minecraft.resources.ResourceKey;
|
| 14 | import net.minecraft.world.RandomizableContainer;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.ItemStackTemplate;
|
| 17 | import net.minecraft.world.item.Items;
|
| 18 | import net.minecraft.world.item.component.ItemContainerContents;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 21 | import net.minecraft.world.level.storage.ValueInput;
|
| 22 | import net.minecraft.world.level.storage.ValueOutput;
|
| 23 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 24 | import net.minecraft.world.ticks.ContainerSingleItem;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class DecoratedPotBlockEntity extends BlockEntity implements ContainerSingleItem.BlockContainerSingleItem, RandomizableContainer {
|
| 28 | public static final String TAG_SHERDS = "sherds";
|
| 29 | public static final String TAG_ITEM = "item";
|
| 30 | public static final int EVENT_POT_WOBBLES = 1;
|
| 31 | public long wobbleStartedAtTick;
|
| 32 | public DecoratedPotBlockEntity.@Nullable WobbleStyle lastWobbleStyle;
|
| 33 | private PotDecorations decorations;
|
| 34 | private ItemStack item = ItemStack.EMPTY;
|
| 35 | protected @Nullable ResourceKey<LootTable> lootTable;
|
| 36 | protected long lootTableSeed;
|
| 37 |
|
| 38 | public DecoratedPotBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 39 | super(BlockEntityTypes.DECORATED_POT, worldPosition, blockState);
|
| 40 | this.decorations = PotDecorations.EMPTY;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected void saveAdditional(final ValueOutput output) {
|
| 45 | super.saveAdditional(output);
|
| 46 | if (!this.decorations.equals(PotDecorations.EMPTY)) {
|
| 47 | output.store("sherds", PotDecorations.CODEC, this.decorations);
|
| 48 | }
|
| 49 |
|
| 50 | if (!this.trySaveLootTable(output) && !this.item.isEmpty()) {
|
| 51 | output.store("item", ItemStack.CODEC, this.item);
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected void loadAdditional(final ValueInput input) {
|
| 57 | super.loadAdditional(input);
|
| 58 | this.decorations = input.read("sherds", PotDecorations.CODEC).orElse(PotDecorations.EMPTY);
|
| 59 | if (!this.tryLoadLootTable(input)) {
|
| 60 | this.item = input.read("item", ItemStack.CODEC).orElse(ItemStack.EMPTY);
|
| 61 | } else {
|
| 62 | this.item = ItemStack.EMPTY;
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
| 67 | return ClientboundBlockEntityDataPacket.create(this);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 72 | return this.saveCustomOnly(registries);
|
| 73 | }
|
| 74 |
|
| 75 | public Direction getDirection() {
|
| 76 | return this.getBlockState().getValue(BlockStateProperties.HORIZONTAL_FACING);
|
| 77 | }
|
| 78 |
|
| 79 | public PotDecorations getDecorations() {
|
| 80 | return this.decorations;
|
| 81 | }
|
| 82 |
|
| 83 | public static ItemStackTemplate createDecoratedPotTemplate(final PotDecorations decorations) {
|
| 84 | return new ItemStackTemplate(Items.DECORATED_POT, DataComponentPatch.builder().set(DataComponents.POT_DECORATIONS, decorations).build());
|
| 85 | }
|
| 86 |
|
| 87 | public static ItemStack createDecoratedPotInstance(final PotDecorations decorations) {
|
| 88 | return createDecoratedPotTemplate(decorations).create();
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public @Nullable ResourceKey<LootTable> getLootTable() {
|
| 93 | return this.lootTable;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public void setLootTable(final @Nullable ResourceKey<LootTable> lootTable) {
|
| 98 | this.lootTable = lootTable;
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public long getLootTableSeed() {
|
| 103 | return this.lootTableSeed;
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public void setLootTableSeed(final long lootTableSeed) {
|
| 108 | this.lootTableSeed = lootTableSeed;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 113 | super.collectImplicitComponents(components);
|
| 114 | components.set(DataComponents.POT_DECORATIONS, this.decorations);
|
| 115 | components.set(DataComponents.CONTAINER, ItemContainerContents.fromItems(List.of(this.item)));
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 120 | super.applyImplicitComponents(components);
|
| 121 | this.decorations = components.getOrDefault(DataComponents.POT_DECORATIONS, PotDecorations.EMPTY);
|
| 122 | this.item = components.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY).copyOne();
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 127 | super.removeComponentsFromTag(output);
|
| 128 | output.discard("sherds");
|
| 129 | output.discard("item");
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public ItemStack getTheItem() {
|
| 134 | this.unpackLootTable(null);
|
| 135 | return this.item;
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | public ItemStack splitTheItem(final int count) {
|
| 140 | this.unpackLootTable(null);
|
| 141 | ItemStack result = this.item.split(count);
|
| 142 | if (this.item.isEmpty()) {
|
| 143 | this.item = ItemStack.EMPTY;
|
| 144 | }
|
| 145 |
|
| 146 | return result;
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | public void setTheItem(final ItemStack itemStack) {
|
| 151 | this.unpackLootTable(null);
|
| 152 | this.item = itemStack;
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public BlockEntity getContainerBlockEntity() {
|
| 157 | return this;
|
| 158 | }
|
| 159 |
|
| 160 | public void wobble(final DecoratedPotBlockEntity.WobbleStyle wobbleStyle) {
|
| 161 | if (this.level != null && !this.level.isClientSide()) {
|
| 162 | this.level.blockEvent(this.getBlockPos(), this.getBlockState().getBlock(), 1, wobbleStyle.ordinal());
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public boolean triggerEvent(final int event, final int data) {
|
| 168 | if (this.level != null && event == 1 && data >= 0 && data < DecoratedPotBlockEntity.WobbleStyle.values().length) {
|
| 169 | this.wobbleStartedAtTick = this.level.getGameTime();
|
| 170 | this.lastWobbleStyle = DecoratedPotBlockEntity.WobbleStyle.values()[data];
|
| 171 | return true;
|
| 172 | } else {
|
| 173 | return super.triggerEvent(event, data);
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | public enum WobbleStyle {
|
| 178 | POSITIVE(7),
|
| 179 | NEGATIVE(10);
|
| 180 |
|
| 181 | public final int duration;
|
| 182 |
|
| 183 | WobbleStyle(final int duration) {
|
| 184 | this.duration = duration;
|
| 185 | }
|
| 186 | }
|
| 187 | }
|
| 188 |
|