Skip to content

Commit 100febf

Browse files
Rework legacy item/block mappings format (#728)
1 parent c4c15fb commit 100febf

File tree

7 files changed

+125
-89
lines changed

7 files changed

+125
-89
lines changed

common/src/main/java/com/viaversion/viabackwards/api/data/MappedLegacyBlockItem.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ public class MappedLegacyBlockItem {
2727
private final short data;
2828
private final String name;
2929
private final IdAndData block;
30+
private final Type type;
3031
private BlockEntityHandler blockEntityHandler;
3132

32-
public MappedLegacyBlockItem(int id, short data, @Nullable String name, boolean block) {
33+
public MappedLegacyBlockItem(int id) {
34+
this(id, (short) -1, null, Type.ITEM);
35+
}
36+
37+
public MappedLegacyBlockItem(int id, short data, @Nullable String name, Type type) {
3338
this.id = id;
3439
this.data = data;
3540
this.name = name != null ? "§f" + name : null;
36-
this.block = block ? data != -1 ? new IdAndData(id, data) : new IdAndData(id) : null;
41+
this.block = type != Type.ITEM ? data != -1 ? new IdAndData(id, data) : new IdAndData(id) : null;
42+
this.type = type;
3743
}
3844

3945
public int getId() {
@@ -48,8 +54,8 @@ public String getName() {
4854
return name;
4955
}
5056

51-
public boolean isBlock() {
52-
return block != null;
57+
public Type getType() {
58+
return type;
5359
}
5460

5561
public IdAndData getBlock() {
@@ -73,4 +79,21 @@ public interface BlockEntityHandler {
7379

7480
CompoundTag handleOrNewCompoundTag(int block, CompoundTag tag);
7581
}
82+
83+
public enum Type {
84+
85+
ITEM("items"),
86+
BLOCK_ITEM("block-items"),
87+
BLOCK("blocks");
88+
89+
final String name;
90+
91+
Type(final String name) {
92+
this.name = name;
93+
}
94+
95+
public String getName() {
96+
return name;
97+
}
98+
}
7699
}

common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,25 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
6262
protected LegacyBlockItemRewriter(T protocol, String name) {
6363
super(protocol, Type.ITEM1_8, Type.ITEM1_8_SHORT_ARRAY, false);
6464
final JsonObject jsonObject = readMappingsFile("item-mappings-" + name + ".json");
65-
for (Map.Entry<String, JsonElement> dataEntry : jsonObject.entrySet()) {
66-
addMapping(dataEntry.getKey(), dataEntry.getValue().getAsJsonObject(), replacementData);
65+
for (final MappedLegacyBlockItem.Type value : MappedLegacyBlockItem.Type.values()) {
66+
addMappings(value, jsonObject, replacementData);
6767
}
6868
}
6969

70-
private void addMapping(String key, JsonObject object, Int2ObjectMap<MappedLegacyBlockItem> mappings) {
70+
private void addMappings(MappedLegacyBlockItem.Type type, JsonObject object, Int2ObjectMap<MappedLegacyBlockItem> mappings) {
71+
if (object.has(type.getName())) {
72+
final JsonObject mappingsObject = object.getAsJsonObject(type.getName());
73+
for (Map.Entry<String, JsonElement> dataEntry : mappingsObject.entrySet()) {
74+
addMapping(dataEntry.getKey(), dataEntry.getValue().getAsJsonObject(), type, mappings);
75+
}
76+
}
77+
}
78+
79+
private void addMapping(String key, JsonObject object, MappedLegacyBlockItem.Type type, Int2ObjectMap<MappedLegacyBlockItem> mappings) {
7180
int id = object.getAsJsonPrimitive("id").getAsInt();
7281
JsonPrimitive jsonData = object.getAsJsonPrimitive("data");
7382
short data = jsonData != null ? jsonData.getAsShort() : 0;
74-
String name = object.getAsJsonPrimitive("name").getAsString();
75-
JsonPrimitive blockField = object.getAsJsonPrimitive("block");
76-
boolean block = blockField != null && blockField.getAsBoolean();
83+
String name = type != MappedLegacyBlockItem.Type.BLOCK ? object.getAsJsonPrimitive("name").getAsString() : null;
7784

7885
if (key.indexOf('-') == -1) {
7986
int unmappedId;
@@ -87,7 +94,7 @@ private void addMapping(String key, JsonObject object, Int2ObjectMap<MappedLegac
8794
unmappedId = IdAndData.toRawData(Integer.parseInt(key));
8895
}
8996

90-
mappings.put(unmappedId, new MappedLegacyBlockItem(id, data, name, block));
97+
mappings.put(unmappedId, new MappedLegacyBlockItem(id, data, name, type));
9198
return;
9299
}
93100

@@ -97,12 +104,12 @@ private void addMapping(String key, JsonObject object, Int2ObjectMap<MappedLegac
97104
int to = Integer.parseInt(split[1]);
98105

99106
// Special block color handling
100-
if (name.contains("%color%")) {
107+
if (name != null && name.contains("%color%")) {
101108
for (int i = from; i <= to; i++) {
102-
mappings.put(IdAndData.toRawData(i), new MappedLegacyBlockItem(id, data, name.replace("%color%", BlockColors.get(i - from)), block));
109+
mappings.put(IdAndData.toRawData(i), new MappedLegacyBlockItem(id, data, name.replace("%color%", BlockColors.get(i - from)), type));
103110
}
104111
} else {
105-
MappedLegacyBlockItem mappedBlockItem = new MappedLegacyBlockItem(id, data, name, block);
112+
MappedLegacyBlockItem mappedBlockItem = new MappedLegacyBlockItem(id, data, name, type);
106113
for (int i = from; i <= to; i++) {
107114
mappings.put(IdAndData.toRawData(i), mappedBlockItem);
108115
}
@@ -146,7 +153,7 @@ public void register() {
146153
if (item == null) return null;
147154

148155
MappedLegacyBlockItem data = getMappedBlockItem(item.identifier(), item.data());
149-
if (data == null) {
156+
if (data == null || data.getType() == MappedLegacyBlockItem.Type.BLOCK) {
150157
// Just rewrite the id
151158
return super.handleItemToClient(connection, item);
152159
}
@@ -219,7 +226,9 @@ public PacketHandler getFallingBlockHandler() {
219226

220227
public @Nullable IdAndData handleBlock(int blockId, int data) {
221228
MappedLegacyBlockItem settings = getMappedBlockItem(blockId, data);
222-
if (settings == null || !settings.isBlock()) return null;
229+
if (settings == null || settings.getType() == MappedLegacyBlockItem.Type.ITEM) {
230+
return null;
231+
}
223232

224233
IdAndData block = settings.getBlock();
225234
// For some blocks, the data can still be useful (:

common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public void register() {
274274
@Override
275275
protected void registerRewrites() {
276276
// Handle spawner block entity (map to itself with custom handler)
277-
MappedLegacyBlockItem data = replacementData.computeIfAbsent(IdAndData.toRawData(52), s -> new MappedLegacyBlockItem(52, (short) -1, null, false));
277+
MappedLegacyBlockItem data = replacementData.computeIfAbsent(IdAndData.toRawData(52), s -> new MappedLegacyBlockItem(52));
278278
data.setBlockEntityHandler((b, tag) -> {
279279
EntityIdRewriter.toClientSpawner(tag, true);
280280
return tag;
Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
{
2-
"255": {
3-
"id": 217,
4-
"name": "1.10 Structure Block"
2+
"items": {
3+
"255": {
4+
"id": 217,
5+
"name": "1.10 Structure Block"
6+
}
57
},
6-
"217": {
7-
"id": 287,
8-
"name": "1.10 Structure Void",
9-
"block": true
10-
},
11-
"213": {
12-
"id": 159,
13-
"data": 1,
14-
"name": "1.10 Magma Block",
15-
"block": true
16-
},
17-
"214": {
18-
"id": 159,
19-
"data": 14,
20-
"name": "1.10 Nether Wart Block",
21-
"block": true
22-
},
23-
"215": {
24-
"id": 112,
25-
"name": "1.10 Red Nether Bricks",
26-
"block": true
27-
},
28-
"216": {
29-
"id": 155,
30-
"name": "1.10 Bone Block",
31-
"block": true
8+
"block-items": {
9+
"217": {
10+
"id": 287,
11+
"name": "1.10 Structure Void"
12+
},
13+
"213": {
14+
"id": 159,
15+
"data": 1,
16+
"name": "1.10 Magma Block"
17+
},
18+
"214": {
19+
"id": 159,
20+
"data": 14,
21+
"name": "1.10 Nether Wart Block"
22+
},
23+
"215": {
24+
"id": 112,
25+
"name": "1.10 Red Nether Bricks"
26+
},
27+
"216": {
28+
"id": 155,
29+
"name": "1.10 Bone Block"
30+
}
3231
}
3332
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
2-
"452": {
3-
"id": 265,
4-
"name": "1.11.1 Iron Nugget"
2+
"items": {
3+
"452": {
4+
"id": 265,
5+
"name": "1.11.1 Iron Nugget"
6+
}
57
}
68
}
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
{
2-
"218": {
3-
"id": 23,
4-
"data": -1,
5-
"name": "1.11 Observer",
6-
"block": true
2+
"items": {
3+
"449": {
4+
"id": 418,
5+
"name": "1.11 Totem of Undying"
6+
},
7+
"450": {
8+
"id": 433,
9+
"name": "1.11 Shulker Shell"
10+
}
711
},
8-
"449": {
9-
"id": 418,
10-
"name": "1.11 Totem of Undying"
11-
},
12-
"450": {
13-
"id": 433,
14-
"name": "1.11 Shulker Shell"
15-
},
16-
"219-234": {
17-
"id": 158,
18-
"name": "1.11 %color% Shulker Box",
19-
"block": true
12+
"block-items": {
13+
"218": {
14+
"id": 23,
15+
"data": -1,
16+
"name": "1.11 Observer"
17+
},
18+
"219-234": {
19+
"id": 158,
20+
"name": "1.11 %color% Shulker Box"
21+
}
2022
}
2123
}
Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
{
2-
"251": {
3-
"id": 159,
4-
"data": -1,
5-
"name": "1.12 %vb_color% Concrete",
6-
"block": true
2+
"items": {
3+
"453": {
4+
"id": 340,
5+
"name": "1.12 Knowledge Book"
6+
},
7+
"355": {
8+
"id": 355,
9+
"name": "1.12 %vb_color% Bed"
10+
}
711
},
8-
"252": {
9-
"id": 35,
10-
"data": -1,
11-
"name": "1.12 %vb_color% Concrete Powder",
12-
"block": true
13-
},
14-
"453": {
15-
"id": 340,
16-
"name": "1.12 Knowledge Book"
17-
},
18-
"355": {
19-
"id": 355,
20-
"name": "1.12 %vb_color% Bed"
21-
},
22-
"235-250": {
23-
"id": 159,
24-
"name": "1.12 %color% Glazed Terracotta",
25-
"block": true
12+
"block-items": {
13+
"251": {
14+
"id": 159,
15+
"data": -1,
16+
"name": "1.12 %vb_color% Concrete"
17+
},
18+
"252": {
19+
"id": 35,
20+
"data": -1,
21+
"name": "1.12 %vb_color% Concrete Powder"
22+
},
23+
"235-250": {
24+
"id": 159,
25+
"name": "1.12 %color% Glazed Terracotta"
26+
}
2627
}
2728
}

0 commit comments

Comments
 (0)