|
4 | 4 | import cn.nukkit.block.Block; |
5 | 5 | import cn.nukkit.block.BlockDoor; |
6 | 6 | import cn.nukkit.block.BlockID; |
| 7 | +import cn.nukkit.level.format.leveldb.BlockStateMapping; |
7 | 8 | import cn.nukkit.level.format.leveldb.LevelDBConstants; |
8 | 9 | import cn.nukkit.nbt.tag.CompoundTag; |
9 | 10 | import cn.nukkit.network.protocol.ProtocolInfo; |
|
18 | 19 | import com.reider745.block.wall.BlockWall; |
19 | 20 | import it.unimi.dsi.fastutil.ints.Int2IntMap; |
20 | 21 | import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; |
| 22 | +import org.cloudburstmc.nbt.NbtMap; |
21 | 23 | import org.jetbrains.annotations.Nullable; |
22 | 24 | import org.json.JSONObject; |
23 | 25 |
|
24 | 26 | import java.util.*; |
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
25 | 28 |
|
26 | 29 | @Hooks(className = "cn.nukkit.level.GlobalBlockPalette") |
27 | 30 | public class BlockPalette { |
@@ -79,7 +82,7 @@ public static int getIdByName(String name) { |
79 | 82 | return stateIdByName.get(name); |
80 | 83 | } |
81 | 84 |
|
82 | | - private record BlockDescription(int id, int newId, JSONObject states, int data) { |
| 85 | + private record BlockDescription(int id, int newId, JSONObject states, int data, String identifier) { |
83 | 86 | } |
84 | 87 |
|
85 | 88 | private static JSONObject cloneJSON(JSONObject json) { |
@@ -121,7 +124,7 @@ private static void variantGenerations(List<Integer> states, List<JSONObject> va |
121 | 124 | } |
122 | 125 | } |
123 | 126 |
|
124 | | - private static List<JSONObject> variantGenerations(List<Integer> states) { |
| 127 | + public static List<JSONObject> variantGenerations(List<Integer> states) { |
125 | 128 | final JSONObject temp = new JSONObject(); |
126 | 129 | for(int state : states) { |
127 | 130 | temp.put(getNameForId(state), 0); |
@@ -158,16 +161,30 @@ public static void init() { |
158 | 161 | if(!doors.contains(newId) && e.getString("nameId").contains("_door")) |
159 | 162 | doors.add(newId); |
160 | 163 |
|
161 | | - sortStates.add(new BlockDescription(e.getInt("oldId"), newId, states, legacyData)); |
| 164 | + sortStates.add(new BlockDescription(e.getInt("oldId"), newId, states, legacyData, "minecraft:" + e.getString("nameId"))); |
162 | 165 | } |
163 | 166 |
|
164 | 167 | // custom runtime block |
| 168 | + |
| 169 | + AtomicReference<Integer> RUNTIME_ID = new AtomicReference<>(Integer.MAX_VALUE); |
165 | 170 | CustomBlock.blocks.forEach((id, manager) -> { |
166 | | - //final ArrayList<String> variants = CustomBlock.getVariants(manager); |
167 | 171 | final List<JSONObject> states = variantGenerations(CustomBlock.getStates(manager)); |
168 | 172 |
|
169 | 173 | for (int data = 0; data < states.size(); data++) { |
170 | | - sortStates.add(new BlockDescription(id, id, states.get(data), data)); |
| 174 | + var state = states.get(data); |
| 175 | + sortStates.add(new BlockDescription(id, id, state, data, manager.textId)); |
| 176 | + |
| 177 | + int runtimeId = RUNTIME_ID.getAndSet(RUNTIME_ID.get() - 1); |
| 178 | + blockPalette.registerState(id, data, runtimeId, null); |
| 179 | + var builder = NbtMap.builder(); |
| 180 | + |
| 181 | + state.keySet().forEach((name) -> { |
| 182 | + builder.putInt(name, state.getInt(name)); |
| 183 | + }); |
| 184 | + builder.putString("name", manager.textId); |
| 185 | + var nbt = builder.build(); |
| 186 | + nbt.hashCode(); |
| 187 | + BlockStateMapping.get().registerState(runtimeId, nbt); |
171 | 188 | } |
172 | 189 | }); |
173 | 190 |
|
|
0 commit comments