]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/interop/NBTStructureLoader.java
c15b2e1eb7c6b6d8fe8da7358c286919ee87c744
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / interop / NBTStructureLoader.java
1 package com.irtimaled.bbor.client.interop;
2
3 import com.irtimaled.bbor.common.EventBus;
4 import com.irtimaled.bbor.common.events.StructuresLoaded;
5 import net.minecraft.nbt.CompressedStreamTools;
6 import net.minecraft.nbt.NBTTagCompound;
7 import net.minecraft.nbt.NBTTagList;
8 import net.minecraft.util.math.ChunkPos;
9 import net.minecraft.util.math.MutableBoundingBox;
10 import net.minecraft.world.IWorld;
11 import net.minecraft.world.chunk.storage.RegionFileCache;
12 import net.minecraft.world.dimension.DimensionType;
13 import net.minecraft.world.gen.feature.structure.LegacyStructureDataUtil;
14 import net.minecraft.world.gen.feature.structure.StructurePiece;
15 import net.minecraft.world.gen.feature.structure.StructureStart;
16 import net.minecraft.world.gen.feature.template.TemplateManager;
17 import net.minecraft.world.storage.ISaveHandler;
18 import net.minecraft.world.storage.WorldSavedDataStorage;
19
20 import java.io.DataInputStream;
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.*;
24
25 class NBTStructureLoader {
26     private final int dimensionId;
27     private final Set<String> loadedChunks = new HashSet<>();
28
29     private LegacyStructureDataUtil legacyStructureDataUtil = null;
30     private ISaveHandler saveHandler = null;
31     private File chunkSaveLocation = null;
32
33     NBTStructureLoader(int dimensionId, ISaveHandler saveHandler, File worldDirectory) {
34         this.dimensionId = dimensionId;
35         this.configure(saveHandler, worldDirectory);
36     }
37
38     void clear() {
39         this.saveHandler = null;
40         this.chunkSaveLocation = null;
41         this.loadedChunks.clear();
42     }
43
44     void configure(ISaveHandler saveHandler, File worldDirectory) {
45         this.saveHandler = saveHandler;
46         if(worldDirectory != null) {
47             this.chunkSaveLocation = DimensionType.getById(dimensionId).getDirectory(worldDirectory);
48         }
49     }
50
51     private LegacyStructureDataUtil getLegacyStructureDataUtil() {
52         if (this.legacyStructureDataUtil == null) {
53             this.legacyStructureDataUtil = LegacyStructureDataUtil.func_212183_a(DimensionType.getById(dimensionId), new WorldSavedDataStorage(saveHandler));
54         }
55         return this.legacyStructureDataUtil;
56     }
57
58     private NBTTagCompound loadStructureStarts(int chunkX, int chunkZ) {
59         try {
60             DataInputStream stream = RegionFileCache.getChunkInputStream(chunkSaveLocation, chunkX, chunkZ);
61             if (stream != null) {
62                 NBTTagCompound compound = CompressedStreamTools.read(stream);
63                 stream.close();
64                 int dataVersion = compound.contains("DataVersion", 99) ? compound.getInt("DataVersion") : -1;
65                 if (dataVersion < 1493) {
66                     if (compound.getCompound("Level").getBoolean("hasLegacyStructureData")) {
67                         compound = getLegacyStructureDataUtil().func_212181_a(compound);
68                     }
69                 }
70                 return compound.getCompound("Level").getCompound("Structures").getCompound("Starts");
71             }
72         } catch (IOException ignored) {
73         }
74         return null;
75     }
76
77     void loadStructures(int chunkX, int chunkZ) {
78         if (saveHandler == null) return;
79
80         if (!loadedChunks.add(String.format("%s,%s", chunkX, chunkZ))) return;
81
82         NBTTagCompound structureStarts = loadStructureStarts(chunkX, chunkZ);
83         if (structureStarts == null || structureStarts.size() == 0) return;
84
85         Map<String, StructureStart> structureStartMap = new HashMap<>();
86         for (String key : structureStarts.keySet()) {
87             NBTTagCompound compound = structureStarts.getCompound(key);
88             if (compound.contains("BB")) {
89                 structureStartMap.put(key, new SimpleStructureStart(compound));
90             }
91         }
92
93         EventBus.publish(new StructuresLoaded(structureStartMap, dimensionId));
94     }
95
96     private static class SimpleStructureStart extends StructureStart {
97         SimpleStructureStart(NBTTagCompound compound) {
98             this.boundingBox = new MutableBoundingBox(compound.getIntArray("BB"));
99
100             NBTTagList children = compound.getList("Children", 10);
101             for (int index = 0; index < children.size(); ++index) {
102                 NBTTagCompound child = children.getCompound(index);
103                 if (child.contains("BB")) this.components.add(new SimpleStructurePiece(child));
104             }
105         }
106     }
107
108     private static class SimpleStructurePiece extends StructurePiece {
109         SimpleStructurePiece(NBTTagCompound compound) {
110             this.boundingBox = new MutableBoundingBox(compound.getIntArray("BB"));
111         }
112
113         @Override
114         protected void writeAdditional(NBTTagCompound nbtTagCompound) {
115         }
116
117         @Override
118         protected void readAdditional(NBTTagCompound nbtTagCompound, TemplateManager templateManager) {
119         }
120
121         @Override
122         public boolean addComponentParts(IWorld iWorld, Random random, MutableBoundingBox mutableBoundingBox, ChunkPos chunkPos) {
123             return false;
124         }
125     }
126 }