generated from LCLPYT/fabric-mod-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
321 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...chematic-fabric/src/main/java/work/lclpnet/kibu/schematic/mixin/MinecraftServerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package work.lclpnet.kibu.schematic.mixin; | ||
|
||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.structure.StructureTemplateManager; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import work.lclpnet.kibu.schematic.type.KibuServerView; | ||
import work.lclpnet.kibu.schematic.vanilla.VanillaStructureFormat; | ||
|
||
@Mixin(MinecraftServer.class) | ||
public abstract class MinecraftServerMixin implements KibuServerView { | ||
|
||
@Shadow public abstract StructureTemplateManager getStructureTemplateManager(); | ||
|
||
@Unique | ||
private final Object vanillaStructureFormatLock = new Object(); | ||
@Unique @Nullable | ||
private volatile VanillaStructureFormat vanillaStructureFormat = null; | ||
|
||
@Override | ||
public VanillaStructureFormat kibu$getVanillaStructureFormat() { | ||
if (vanillaStructureFormat != null) { | ||
return vanillaStructureFormat; | ||
} | ||
|
||
synchronized (vanillaStructureFormatLock) { | ||
if (vanillaStructureFormat == null) { | ||
var manager = getStructureTemplateManager(); | ||
vanillaStructureFormat = new VanillaStructureFormat(manager); | ||
} | ||
} | ||
|
||
return vanillaStructureFormat; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...tic-fabric/src/main/java/work/lclpnet/kibu/schematic/mixin/StructureTemplateAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package work.lclpnet.kibu.schematic.mixin; | ||
|
||
import net.minecraft.structure.StructureTemplate; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
import java.util.List; | ||
|
||
@Mixin(StructureTemplate.class) | ||
public interface StructureTemplateAccessor { | ||
|
||
@Accessor | ||
List<StructureTemplate.PalettedBlockInfoList> getBlockInfoLists(); | ||
|
||
@Accessor | ||
List<StructureTemplate.StructureEntityInfo> getEntities(); | ||
} |
8 changes: 8 additions & 0 deletions
8
kibu-schematic-fabric/src/main/java/work/lclpnet/kibu/schematic/type/KibuServerView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package work.lclpnet.kibu.schematic.type; | ||
|
||
import work.lclpnet.kibu.schematic.vanilla.VanillaStructureFormat; | ||
|
||
public interface KibuServerView { | ||
|
||
VanillaStructureFormat kibu$getVanillaStructureFormat(); | ||
} |
114 changes: 114 additions & 0 deletions
114
kibu-schematic-fabric/src/main/java/work/lclpnet/kibu/schematic/vanilla/Deserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package work.lclpnet.kibu.schematic.vanilla; | ||
|
||
import net.minecraft.block.BlockState; | ||
import net.minecraft.entity.EntityType; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.registry.Registries; | ||
import net.minecraft.structure.StructureTemplate; | ||
import net.minecraft.structure.StructureTemplateManager; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.Vec3i; | ||
import work.lclpnet.kibu.jnbt.CompoundTag; | ||
import work.lclpnet.kibu.jnbt.NBTConstants; | ||
import work.lclpnet.kibu.mc.BlockStateAdapter; | ||
import work.lclpnet.kibu.mc.KibuBlockPos; | ||
import work.lclpnet.kibu.nbt.FabricNbtConversion; | ||
import work.lclpnet.kibu.schematic.FabricBlockStateAdapter; | ||
import work.lclpnet.kibu.schematic.FabricKibuBlockEntity; | ||
import work.lclpnet.kibu.schematic.FabricKibuEntity; | ||
import work.lclpnet.kibu.schematic.FabricStructureWrapper; | ||
import work.lclpnet.kibu.schematic.api.BlockStructureFactory; | ||
import work.lclpnet.kibu.schematic.api.SchematicDeserializer; | ||
import work.lclpnet.kibu.schematic.mixin.StructureTemplateAccessor; | ||
import work.lclpnet.kibu.structure.BlockStructure; | ||
|
||
import java.util.List; | ||
|
||
class Deserializer implements SchematicDeserializer { | ||
|
||
private final StructureTemplateManager manager; | ||
|
||
Deserializer(StructureTemplateManager manager) { | ||
this.manager = manager; | ||
} | ||
|
||
@Override | ||
public BlockStructure deserialize(CompoundTag tag, BlockStateAdapter _adapter, BlockStructureFactory factory) { | ||
NbtCompound nbt = FabricNbtConversion.convert(tag, NbtCompound.class); | ||
StructureTemplate template = manager.createTemplate(nbt); | ||
|
||
Vec3i size = template.getSize(); | ||
var origin = new KibuBlockPos(0, 0, 0); | ||
int dataVersion = FabricStructureWrapper.getDataVersion(); | ||
|
||
BlockStructure struct = factory.create(size.getX(), size.getY(), size.getZ(), origin, dataVersion); | ||
|
||
var accessor = (StructureTemplateAccessor) template; | ||
var blockInfoLists = accessor.getBlockInfoLists(); | ||
|
||
var adapter = FabricBlockStateAdapter.getInstance(); | ||
|
||
if (!blockInfoLists.isEmpty()) { | ||
// blockInfoLists can contain multiple palettes (e.g. ship wreck structure files) | ||
// this deserializer only chooses the first one | ||
addBlocks(struct, blockInfoLists.getFirst().getAll(), adapter); | ||
} | ||
|
||
addEntities(struct, accessor.getEntities()); | ||
|
||
return struct; | ||
} | ||
|
||
private void addBlocks(BlockStructure struct, List<StructureTemplate.StructureBlockInfo> blocks, FabricBlockStateAdapter adapter) { | ||
for (StructureTemplate.StructureBlockInfo block : blocks) { | ||
BlockPos pos = block.pos(); | ||
BlockState state = block.state(); | ||
|
||
var kibuPos = adapter.adapt(pos); | ||
var kibuState = adapter.adapt(state); | ||
|
||
struct.setBlockState(kibuPos, kibuState); | ||
|
||
NbtCompound nbt = block.nbt(); | ||
|
||
if (nbt == null) continue; | ||
|
||
addBlockEntity(struct, kibuPos, pos, state, nbt); | ||
} | ||
} | ||
|
||
private void addBlockEntity(BlockStructure struct, KibuBlockPos kibuPos, BlockPos pos, BlockState state, NbtCompound nbt) { | ||
if (!state.hasBlockEntity()) return; | ||
|
||
var type = Registries.BLOCK_ENTITY_TYPE.getOrEmpty(Identifier.of(nbt.getString("id"))) | ||
.orElse(null); | ||
|
||
if (type == null) return; | ||
|
||
var blockEntity = new FabricKibuBlockEntity(type, pos, nbt); | ||
|
||
struct.setBlockEntity(kibuPos, blockEntity); | ||
} | ||
|
||
private void addEntities(BlockStructure struct, List<StructureTemplate.StructureEntityInfo> entities) { | ||
for (StructureTemplate.StructureEntityInfo entity : entities) { | ||
NbtCompound nbt = entity.nbt; | ||
|
||
if (nbt.contains("TileX", NBTConstants.TYPE_INT) | ||
&& nbt.contains("TileY", NBTConstants.TYPE_INT) | ||
&& nbt.contains("TileZ", NBTConstants.TYPE_INT)) { | ||
nbt.putInt("TileX", entity.blockPos.getX()); | ||
nbt.putInt("TileY", entity.blockPos.getY()); | ||
nbt.putInt("TileZ", entity.blockPos.getZ()); | ||
} | ||
|
||
var type = EntityType.fromNbt(nbt).orElse(null); | ||
|
||
if (type == null) continue; | ||
|
||
var kibuEntity = new FabricKibuEntity(type, entity.pos, nbt); | ||
struct.addEntity(kibuEntity); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
kibu-schematic-fabric/src/main/java/work/lclpnet/kibu/schematic/vanilla/Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package work.lclpnet.kibu.schematic.vanilla; | ||
|
||
import work.lclpnet.kibu.jnbt.CompoundTag; | ||
import work.lclpnet.kibu.jnbt.io.NbtIOHelper; | ||
import work.lclpnet.kibu.mc.BlockStateAdapter; | ||
import work.lclpnet.kibu.schematic.api.BlockStructureFactory; | ||
import work.lclpnet.kibu.schematic.api.SchematicDeserializer; | ||
import work.lclpnet.kibu.schematic.api.SchematicReader; | ||
import work.lclpnet.kibu.structure.BlockStructure; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Objects; | ||
|
||
class Reader implements SchematicReader { | ||
|
||
private final SchematicDeserializer deserializer; | ||
|
||
Reader(SchematicDeserializer deserializer) { | ||
this.deserializer = Objects.requireNonNull(deserializer); | ||
} | ||
|
||
@Override | ||
public BlockStructure read(InputStream in, BlockStateAdapter adapter, BlockStructureFactory factory) throws IOException { | ||
var tag = NbtIOHelper.read(in); | ||
|
||
if (!"".equals(tag.name())) throw new IOException("Invalid nbt"); | ||
|
||
var nbt = tag.tag(); | ||
|
||
if (!(nbt instanceof CompoundTag compoundNbt)) throw new IOException("Invalid nbt"); | ||
|
||
return deserializer.deserialize(compoundNbt, adapter, factory); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...atic-fabric/src/main/java/work/lclpnet/kibu/schematic/vanilla/VanillaStructureFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package work.lclpnet.kibu.schematic.vanilla; | ||
|
||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.structure.StructureTemplateManager; | ||
import work.lclpnet.kibu.schematic.api.*; | ||
import work.lclpnet.kibu.schematic.type.KibuServerView; | ||
|
||
public class VanillaStructureFormat implements SchematicFormat { | ||
|
||
private final StructureTemplateManager manager; | ||
private volatile SchematicSerializer serializer = null; | ||
private volatile SchematicDeserializer deserializer = null; | ||
private volatile SchematicWriter writer = null; | ||
private volatile SchematicReader reader = null; | ||
|
||
public VanillaStructureFormat(StructureTemplateManager manager) { | ||
this.manager = manager; | ||
} | ||
|
||
@Override | ||
public SchematicSerializer serializer() { | ||
throw new UnsupportedOperationException("Not implemented"); | ||
} | ||
|
||
@Override | ||
public SchematicDeserializer deserializer() { | ||
if (deserializer != null) return deserializer; | ||
|
||
synchronized (this) { | ||
if (deserializer == null) { | ||
deserializer = new Deserializer(manager); | ||
} | ||
} | ||
|
||
return deserializer; | ||
} | ||
|
||
@Override | ||
public SchematicWriter writer() { | ||
throw new UnsupportedOperationException("Not implemented"); | ||
} | ||
|
||
@Override | ||
public SchematicReader reader() { | ||
if (reader != null) return reader; | ||
|
||
synchronized (this) { | ||
if (reader == null) { | ||
reader = new Reader(deserializer()); | ||
} | ||
} | ||
|
||
return reader; | ||
} | ||
|
||
public static VanillaStructureFormat get(MinecraftServer server) { | ||
return ((KibuServerView) server).kibu$getVanillaStructureFormat(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.