| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.core.HolderLookup;
|
| 7 | import net.minecraft.core.Registry;
|
| 8 | import net.minecraft.core.registries.Registries;
|
| 9 | import net.minecraft.data.worldgen.Pools;
|
| 10 | import net.minecraft.nbt.CompoundTag;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.resources.ResourceKey;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.util.StringRepresentable;
|
| 17 | import net.minecraft.world.level.block.JigsawBlock;
|
| 18 | import net.minecraft.world.level.block.state.BlockState;
|
| 19 | import net.minecraft.world.level.levelgen.structure.pools.JigsawPlacement;
|
| 20 | import net.minecraft.world.level.levelgen.structure.pools.StructureTemplatePool;
|
| 21 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 22 | import net.minecraft.world.level.storage.ValueInput;
|
| 23 | import net.minecraft.world.level.storage.ValueOutput;
|
| 24 |
|
| 25 | public class JigsawBlockEntity extends BlockEntity {
|
| 26 | public static final Codec<ResourceKey<StructureTemplatePool>> POOL_CODEC = ResourceKey.codec(Registries.TEMPLATE_POOL);
|
| 27 | public static final Identifier EMPTY_ID = Identifier.withDefaultNamespace("empty");
|
| 28 | private static final int DEFAULT_PLACEMENT_PRIORITY = 0;
|
| 29 | private static final int DEFAULT_SELECTION_PRIORITY = 0;
|
| 30 | public static final String TARGET = "target";
|
| 31 | public static final String POOL = "pool";
|
| 32 | public static final String JOINT = "joint";
|
| 33 | public static final String PLACEMENT_PRIORITY = "placement_priority";
|
| 34 | public static final String SELECTION_PRIORITY = "selection_priority";
|
| 35 | public static final String NAME = "name";
|
| 36 | public static final String FINAL_STATE = "final_state";
|
| 37 | public static final String DEFAULT_FINAL_STATE = "minecraft:air";
|
| 38 | private Identifier name = EMPTY_ID;
|
| 39 | private Identifier target = EMPTY_ID;
|
| 40 | private ResourceKey<StructureTemplatePool> pool = Pools.EMPTY;
|
| 41 | private JigsawBlockEntity.JointType joint = JigsawBlockEntity.JointType.ROLLABLE;
|
| 42 | private String finalState = "minecraft:air";
|
| 43 | private int placementPriority = 0;
|
| 44 | private int selectionPriority = 0;
|
| 45 |
|
| 46 | public JigsawBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 47 | super(BlockEntityTypes.JIGSAW, worldPosition, blockState);
|
| 48 | }
|
| 49 |
|
| 50 | public Identifier getName() {
|
| 51 | return this.name;
|
| 52 | }
|
| 53 |
|
| 54 | public Identifier getTarget() {
|
| 55 | return this.target;
|
| 56 | }
|
| 57 |
|
| 58 | public ResourceKey<StructureTemplatePool> getPool() {
|
| 59 | return this.pool;
|
| 60 | }
|
| 61 |
|
| 62 | public String getFinalState() {
|
| 63 | return this.finalState;
|
| 64 | }
|
| 65 |
|
| 66 | public JigsawBlockEntity.JointType getJoint() {
|
| 67 | return this.joint;
|
| 68 | }
|
| 69 |
|
| 70 | public int getPlacementPriority() {
|
| 71 | return this.placementPriority;
|
| 72 | }
|
| 73 |
|
| 74 | public int getSelectionPriority() {
|
| 75 | return this.selectionPriority;
|
| 76 | }
|
| 77 |
|
| 78 | public void setName(final Identifier name) {
|
| 79 | this.name = name;
|
| 80 | }
|
| 81 |
|
| 82 | public void setTarget(final Identifier target) {
|
| 83 | this.target = target;
|
| 84 | }
|
| 85 |
|
| 86 | public void setPool(final ResourceKey<StructureTemplatePool> pool) {
|
| 87 | this.pool = pool;
|
| 88 | }
|
| 89 |
|
| 90 | public void setFinalState(final String finalState) {
|
| 91 | this.finalState = finalState;
|
| 92 | }
|
| 93 |
|
| 94 | public void setJoint(final JigsawBlockEntity.JointType joint) {
|
| 95 | this.joint = joint;
|
| 96 | }
|
| 97 |
|
| 98 | public void setPlacementPriority(final int placementPriority) {
|
| 99 | this.placementPriority = placementPriority;
|
| 100 | }
|
| 101 |
|
| 102 | public void setSelectionPriority(final int selectionPriority) {
|
| 103 | this.selectionPriority = selectionPriority;
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | protected void saveAdditional(final ValueOutput output) {
|
| 108 | super.saveAdditional(output);
|
| 109 | output.store("name", Identifier.CODEC, this.name);
|
| 110 | output.store("target", Identifier.CODEC, this.target);
|
| 111 | output.store("pool", POOL_CODEC, this.pool);
|
| 112 | output.putString("final_state", this.finalState);
|
| 113 | output.store("joint", JigsawBlockEntity.JointType.CODEC, this.joint);
|
| 114 | output.putInt("placement_priority", this.placementPriority);
|
| 115 | output.putInt("selection_priority", this.selectionPriority);
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | protected void loadAdditional(final ValueInput input) {
|
| 120 | super.loadAdditional(input);
|
| 121 | this.name = input.read("name", Identifier.CODEC).orElse(EMPTY_ID);
|
| 122 | this.target = input.read("target", Identifier.CODEC).orElse(EMPTY_ID);
|
| 123 | this.pool = input.read("pool", POOL_CODEC).orElse(Pools.EMPTY);
|
| 124 | this.finalState = input.getStringOr("final_state", "minecraft:air");
|
| 125 | this.joint = input.read("joint", JigsawBlockEntity.JointType.CODEC).orElseGet(() -> StructureTemplate.getDefaultJointType(this.getBlockState()));
|
| 126 | this.placementPriority = input.getIntOr("placement_priority", 0);
|
| 127 | this.selectionPriority = input.getIntOr("selection_priority", 0);
|
| 128 | }
|
| 129 |
|
| 130 | public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
| 131 | return ClientboundBlockEntityDataPacket.create(this);
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 136 | return this.saveCustomOnly(registries);
|
| 137 | }
|
| 138 |
|
| 139 | public void generate(final ServerLevel level, final int levels, final boolean keepJigsaws) {
|
| 140 | BlockPos position = this.getBlockPos().relative(this.getBlockState().getValue(JigsawBlock.ORIENTATION).front());
|
| 141 | Registry<StructureTemplatePool> poolRegistry = level.registryAccess().lookupOrThrow(Registries.TEMPLATE_POOL);
|
| 142 | Holder<StructureTemplatePool> pool = poolRegistry.getOrThrow(this.pool);
|
| 143 | JigsawPlacement.generateJigsaw(level, pool, this.target, levels, position, keepJigsaws);
|
| 144 | }
|
| 145 |
|
| 146 | public enum JointType implements StringRepresentable {
|
| 147 | ROLLABLE("rollable"),
|
| 148 | ALIGNED("aligned");
|
| 149 |
|
| 150 | public static final StringRepresentable.EnumCodec<JigsawBlockEntity.JointType> CODEC = StringRepresentable.fromEnum(JigsawBlockEntity.JointType::values);
|
| 151 | private final String name;
|
| 152 |
|
| 153 | JointType(final String name) {
|
| 154 | this.name = name;
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | public String getSerializedName() {
|
| 159 | return this.name;
|
| 160 | }
|
| 161 |
|
| 162 | public Component getTranslatedName() {
|
| 163 | return Component.translatable("jigsaw_block.joint." + this.name);
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 |
|