|
1 | 1 | package io.izzel.arclight.common.mixin.vanilla.world.level.levelgen.structure.templatesystem;
|
2 | 2 |
|
| 3 | +import com.google.common.collect.Lists; |
| 4 | +import com.mojang.datafixers.util.Pair; |
3 | 5 | import io.izzel.arclight.common.bridge.core.world.level.levelgen.structure.templatesystem.StructureTemplateBridge;
|
4 | 6 | import net.minecraft.core.BlockPos;
|
| 7 | +import net.minecraft.core.Direction; |
| 8 | +import net.minecraft.core.Vec3i; |
| 9 | +import net.minecraft.nbt.CompoundTag; |
| 10 | +import net.minecraft.util.RandomSource; |
| 11 | +import net.minecraft.world.Clearable; |
| 12 | +import net.minecraft.world.level.LevelAccessor; |
5 | 13 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 14 | +import net.minecraft.world.level.block.Block; |
| 15 | +import net.minecraft.world.level.block.Blocks; |
| 16 | +import net.minecraft.world.level.block.LiquidBlockContainer; |
6 | 17 | import net.minecraft.world.level.block.Mirror;
|
7 | 18 | import net.minecraft.world.level.block.Rotation;
|
| 19 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 20 | +import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity; |
| 21 | +import net.minecraft.world.level.block.state.BlockState; |
8 | 22 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
9 | 23 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
|
10 | 24 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 25 | +import net.minecraft.world.level.material.FluidState; |
| 26 | +import net.minecraft.world.phys.shapes.BitSetDiscreteVoxelShape; |
| 27 | +import net.minecraft.world.phys.shapes.DiscreteVoxelShape; |
| 28 | +import org.bukkit.craftbukkit.v.block.CraftBlockEntityState; |
| 29 | +import org.bukkit.craftbukkit.v.block.CraftBlockState; |
| 30 | +import org.bukkit.craftbukkit.v.block.CraftBlockStates; |
| 31 | +import org.bukkit.craftbukkit.v.block.CraftLootable; |
| 32 | +import org.bukkit.craftbukkit.v.util.CraftStructureTransformer; |
| 33 | +import org.bukkit.craftbukkit.v.util.TransformerGeneratorAccess; |
11 | 34 | import org.jetbrains.annotations.Nullable;
|
| 35 | +import org.spongepowered.asm.mixin.Final; |
12 | 36 | import org.spongepowered.asm.mixin.Mixin;
|
| 37 | +import org.spongepowered.asm.mixin.Overwrite; |
13 | 38 | import org.spongepowered.asm.mixin.Shadow;
|
14 | 39 |
|
15 |
| -@Mixin(StructureTemplate.class) |
| 40 | +import java.util.Iterator; |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +@Mixin(value = StructureTemplate.class) |
16 | 44 | public abstract class StructureTemplateMixin_Vanilla implements StructureTemplateBridge {
|
17 | 45 |
|
18 | 46 | // @formatter:off
|
19 | 47 | @Shadow protected abstract void placeEntities(ServerLevelAccessor serverLevelAccessor, BlockPos blockPos, Mirror mirror, Rotation rotation, BlockPos blockPos2, @Nullable BoundingBox boundingBox, boolean bl);
|
| 48 | + @Shadow @Final public List<StructureTemplate.Palette> palettes; |
| 49 | + @Shadow @Final public List<StructureTemplate.StructureEntityInfo> entityInfoList; |
| 50 | + @Shadow private Vec3i size; |
| 51 | + @Shadow public static void updateShapeAtEdge(LevelAccessor p_74511_, int p_74512_, DiscreteVoxelShape p_74513_, int p_74514_, int p_74515_, int p_74516_) { } |
20 | 52 | // @formatter:on
|
21 | 53 |
|
22 |
| - @Override |
23 |
| - public void bridge$platform$placeEntities(ServerLevelAccessor serverLevelAccessor, BlockPos blockPos, Mirror mirror, Rotation rotation, BlockPos blockPos2, @Nullable BoundingBox boundingBox, boolean bl, StructurePlaceSettings placementIn) { |
24 |
| - this.placeEntities(serverLevelAccessor, blockPos, mirror, rotation, blockPos2, boundingBox, bl); |
| 54 | + /** |
| 55 | + * @author IzzelAliz |
| 56 | + * @reason |
| 57 | + */ |
| 58 | + @Overwrite |
| 59 | + public boolean placeInWorld(ServerLevelAccessor p_230329_, BlockPos pos, BlockPos p_230331_, StructurePlaceSettings placeSettings, RandomSource p_230333_, int p_230334_) { |
| 60 | + if (this.palettes.isEmpty()) { |
| 61 | + return false; |
| 62 | + } else { |
| 63 | + // CraftBukkit start |
| 64 | + // We only want the TransformerGeneratorAccess at certain locations because in here are many "block update" calls that shouldn't be transformed |
| 65 | + var wrappedAccess = p_230329_; |
| 66 | + CraftStructureTransformer structureTransformer = null; |
| 67 | + if (wrappedAccess instanceof TransformerGeneratorAccess transformerAccess) { |
| 68 | + p_230329_ = transformerAccess.getHandle(); |
| 69 | + structureTransformer = transformerAccess.getStructureTransformer(); |
| 70 | + // The structureTransformer is not needed if we can not transform blocks therefore we can save a little bit of performance doing this |
| 71 | + if (structureTransformer != null && !structureTransformer.canTransformBlocks()) { |
| 72 | + structureTransformer = null; |
| 73 | + } |
| 74 | + } |
| 75 | + // CraftBukkit end |
| 76 | + List<StructureTemplate.StructureBlockInfo> list = placeSettings.getRandomPalette(this.palettes, pos).blocks(); |
| 77 | + if ((!list.isEmpty() || !placeSettings.isIgnoreEntities() && !this.entityInfoList.isEmpty()) && this.size.getX() >= 1 && this.size.getY() >= 1 && this.size.getZ() >= 1) { |
| 78 | + BoundingBox boundingbox = placeSettings.getBoundingBox(); |
| 79 | + List<BlockPos> list1 = Lists.newArrayListWithCapacity(placeSettings.shouldKeepLiquids() ? list.size() : 0); |
| 80 | + List<BlockPos> list2 = Lists.newArrayListWithCapacity(placeSettings.shouldKeepLiquids() ? list.size() : 0); |
| 81 | + List<Pair<BlockPos, CompoundTag>> list3 = Lists.newArrayListWithCapacity(list.size()); |
| 82 | + int i = Integer.MAX_VALUE; |
| 83 | + int j = Integer.MAX_VALUE; |
| 84 | + int k = Integer.MAX_VALUE; |
| 85 | + int l = Integer.MIN_VALUE; |
| 86 | + int i1 = Integer.MIN_VALUE; |
| 87 | + int j1 = Integer.MIN_VALUE; |
| 88 | + |
| 89 | + for (StructureTemplate.StructureBlockInfo structuretemplate$structureblockinfo : StructureTemplate.processBlockInfos(p_230329_, pos, p_230331_, placeSettings, list)) { |
| 90 | + BlockPos blockpos = structuretemplate$structureblockinfo.pos(); |
| 91 | + if (boundingbox == null || boundingbox.isInside(blockpos)) { |
| 92 | + FluidState fluidstate = placeSettings.shouldKeepLiquids() ? p_230329_.getFluidState(blockpos) : null; |
| 93 | + BlockState blockstate = structuretemplate$structureblockinfo.state().mirror(placeSettings.getMirror()).rotate(placeSettings.getRotation()); |
| 94 | + if (structuretemplate$structureblockinfo.nbt() != null) { |
| 95 | + BlockEntity blockentity = p_230329_.getBlockEntity(blockpos); |
| 96 | + Clearable.tryClear(blockentity); |
| 97 | + p_230329_.setBlock(blockpos, Blocks.BARRIER.defaultBlockState(), 20); |
| 98 | + } |
| 99 | + // CraftBukkit start |
| 100 | + if (structureTransformer != null) { |
| 101 | + var craftBlockState = (CraftBlockState) CraftBlockStates.getBlockState(blockpos, blockstate, null); |
| 102 | + if (structuretemplate$structureblockinfo.nbt() != null && craftBlockState instanceof CraftBlockEntityState<?> entityState) { |
| 103 | + entityState.loadData(structuretemplate$structureblockinfo.nbt()); |
| 104 | + if (craftBlockState instanceof CraftLootable<?> craftLootable) { |
| 105 | + craftLootable.setSeed(p_230333_.nextLong()); |
| 106 | + } |
| 107 | + } |
| 108 | + craftBlockState = structureTransformer.transformCraftState(craftBlockState); |
| 109 | + blockstate = craftBlockState.getHandle(); |
| 110 | + structuretemplate$structureblockinfo = new StructureTemplate.StructureBlockInfo(blockpos, blockstate, (craftBlockState instanceof CraftBlockEntityState<?> craftBlockEntityState ? craftBlockEntityState.getSnapshotNBT() : null)); |
| 111 | + } |
| 112 | + // CraftBukkit end |
| 113 | + |
| 114 | + if (p_230329_.setBlock(blockpos, blockstate, p_230334_)) { |
| 115 | + i = Math.min(i, blockpos.getX()); |
| 116 | + j = Math.min(j, blockpos.getY()); |
| 117 | + k = Math.min(k, blockpos.getZ()); |
| 118 | + l = Math.max(l, blockpos.getX()); |
| 119 | + i1 = Math.max(i1, blockpos.getY()); |
| 120 | + j1 = Math.max(j1, blockpos.getZ()); |
| 121 | + list3.add(Pair.of(blockpos, structuretemplate$structureblockinfo.nbt())); |
| 122 | + if (structuretemplate$structureblockinfo.nbt() != null) { |
| 123 | + BlockEntity blockentity1 = p_230329_.getBlockEntity(blockpos); |
| 124 | + if (blockentity1 != null) { |
| 125 | + if (structureTransformer == null && blockentity1 instanceof RandomizableContainerBlockEntity) { |
| 126 | + structuretemplate$structureblockinfo.nbt().putLong("LootTableSeed", p_230333_.nextLong()); |
| 127 | + } |
| 128 | + |
| 129 | + blockentity1.load(structuretemplate$structureblockinfo.nbt()); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (fluidstate != null) { |
| 134 | + if (blockstate.getFluidState().isSource()) { |
| 135 | + list2.add(blockpos); |
| 136 | + } else if (blockstate.getBlock() instanceof LiquidBlockContainer) { |
| 137 | + ((LiquidBlockContainer) blockstate.getBlock()).placeLiquid(p_230329_, blockpos, blockstate, fluidstate); |
| 138 | + if (!fluidstate.isSource()) { |
| 139 | + list1.add(blockpos); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + boolean flag = true; |
| 148 | + Direction[] adirection = new Direction[]{Direction.UP, Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST}; |
| 149 | + |
| 150 | + while (flag && !list1.isEmpty()) { |
| 151 | + flag = false; |
| 152 | + Iterator<BlockPos> iterator = list1.iterator(); |
| 153 | + |
| 154 | + while (iterator.hasNext()) { |
| 155 | + BlockPos blockpos3 = iterator.next(); |
| 156 | + FluidState fluidstate2 = p_230329_.getFluidState(blockpos3); |
| 157 | + |
| 158 | + for (int i2 = 0; i2 < adirection.length && !fluidstate2.isSource(); ++i2) { |
| 159 | + BlockPos blockpos1 = blockpos3.relative(adirection[i2]); |
| 160 | + FluidState fluidstate1 = p_230329_.getFluidState(blockpos1); |
| 161 | + if (fluidstate1.isSource() && !list2.contains(blockpos1)) { |
| 162 | + fluidstate2 = fluidstate1; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (fluidstate2.isSource()) { |
| 167 | + BlockState blockstate1 = p_230329_.getBlockState(blockpos3); |
| 168 | + Block block = blockstate1.getBlock(); |
| 169 | + if (block instanceof LiquidBlockContainer) { |
| 170 | + ((LiquidBlockContainer) block).placeLiquid(p_230329_, blockpos3, blockstate1, fluidstate2); |
| 171 | + flag = true; |
| 172 | + iterator.remove(); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + if (i <= l) { |
| 179 | + if (!placeSettings.getKnownShape()) { |
| 180 | + DiscreteVoxelShape discretevoxelshape = new BitSetDiscreteVoxelShape(l - i + 1, i1 - j + 1, j1 - k + 1); |
| 181 | + int k1 = i; |
| 182 | + int l1 = j; |
| 183 | + int j2 = k; |
| 184 | + |
| 185 | + for (Pair<BlockPos, CompoundTag> pair1 : list3) { |
| 186 | + BlockPos blockpos2 = pair1.getFirst(); |
| 187 | + discretevoxelshape.fill(blockpos2.getX() - k1, blockpos2.getY() - l1, blockpos2.getZ() - j2); |
| 188 | + } |
| 189 | + |
| 190 | + updateShapeAtEdge(p_230329_, p_230334_, discretevoxelshape, k1, l1, j2); |
| 191 | + } |
| 192 | + |
| 193 | + for (Pair<BlockPos, CompoundTag> pair : list3) { |
| 194 | + BlockPos blockpos4 = pair.getFirst(); |
| 195 | + if (!placeSettings.getKnownShape()) { |
| 196 | + BlockState blockstate2 = p_230329_.getBlockState(blockpos4); |
| 197 | + BlockState blockstate3 = Block.updateFromNeighbourShapes(blockstate2, p_230329_, blockpos4); |
| 198 | + if (blockstate2 != blockstate3) { |
| 199 | + p_230329_.setBlock(blockpos4, blockstate3, p_230334_ & -2 | 16); |
| 200 | + } |
| 201 | + |
| 202 | + p_230329_.blockUpdated(blockpos4, blockstate3.getBlock()); |
| 203 | + } |
| 204 | + |
| 205 | + if (pair.getSecond() != null) { |
| 206 | + BlockEntity blockentity2 = p_230329_.getBlockEntity(blockpos4); |
| 207 | + if (blockentity2 != null) { |
| 208 | + blockentity2.setChanged(); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + if (!placeSettings.isIgnoreEntities()) { |
| 215 | + this.placeEntities(wrappedAccess, pos, placeSettings.getMirror(), placeSettings.getRotation(), placeSettings.getRotationPivot(), placeSettings.getBoundingBox(), placeSettings.shouldFinalizeEntities()); |
| 216 | + } |
| 217 | + |
| 218 | + return true; |
| 219 | + } else { |
| 220 | + return false; |
| 221 | + } |
| 222 | + } |
25 | 223 | }
|
26 | 224 | }
|
0 commit comments