Skip to content

Commit f5a4b1f

Browse files
More block entity rewriters (#329)
1 parent 6c15760 commit f5a4b1f

7 files changed

Lines changed: 352 additions & 8 deletions

File tree

src/main/java/net/raphimc/viabedrock/protocol/rewriter/BlockEntityRewriter.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,13 @@ public class BlockEntityRewriter {
4646

4747
static {
4848
// TODO: Enhancement: Add missing block entities
49-
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.BREWING_STAND, NOOP_REWRITER);
5049
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.CALIBRATED_SCULK_SENSOR, NOOP_REWRITER);
51-
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.CAMPFIRE, NOOP_REWRITER);
5250
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.COPPER_GOLEM_STATUE, NOOP_REWRITER);
5351
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.NOTE_BLOCK, NULL_REWRITER);
5452
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.PISTON, NOOP_REWRITER);
5553
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.MOVING_BLOCK, NULL_REWRITER);
5654
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.SCULK_SENSOR, NOOP_REWRITER);
5755
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.SCULK_SHRIEKER, NOOP_REWRITER);
58-
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.SHELF, NOOP_REWRITER);
59-
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.TRIAL_SPAWNER, NOOP_REWRITER);
60-
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.VAULT, NOOP_REWRITER);
6156

6257
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.BANNER, new BannerBlockEntityRewriter());
6358
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.BARREL, new LootableContainerBlockEntityRewriter());
@@ -66,7 +61,9 @@ public class BlockEntityRewriter {
6661
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.BEEHIVE, new BeehiveBlockEntityRewriter());
6762
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.BELL, NOOP_REWRITER);
6863
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.BLAST_FURNACE, new FurnaceBlockEntityRewriter());
64+
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.BREWING_STAND, new BrewingStandBlockEntityRewriter());
6965
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.BRUSHABLE_BLOCK, new BrushableBlockBlockEntityRewriter());
66+
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.CAMPFIRE, new CampfireBlockEntityRewriter());
7067
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.CAULDRON, NULL_REWRITER);
7168
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.CHEST, new LootableContainerBlockEntityRewriter());
7269
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.CHISELED_BOOKSHELF, new ChiseledBookshelfBlockEntityRewriter());
@@ -95,13 +92,17 @@ public class BlockEntityRewriter {
9592
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.MOB_SPAWNER, new MobSpawnerBlockEntityRewriter());
9693
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.NETHER_REACTOR, NULL_REWRITER);
9794
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.SCULK_CATALYST, NOOP_REWRITER);
95+
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.SHELF, new ShelfBlockEntityRewriter());
9896
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.SHULKER_BOX, new LootableContainerBlockEntityRewriter());
9997
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.SIGN, new SignBlockEntityRewriter());
10098
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.SKULL, new SkullBlockEntityRewriter());
10199
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.SMOKER, new FurnaceBlockEntityRewriter());
102100
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.SPORE_BLOSSOM, NULL_REWRITER);
103101
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.STRUCTURE_BLOCK, new StructureBlockBlockEntityRewriter());
104102
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.TRAPPED_CHEST, new LootableContainerBlockEntityRewriter());
103+
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.TRIAL_SPAWNER, new TrialSpawnerBlockEntityRewriter());
104+
BLOCK_ENTITY_REWRITERS.put(CustomBlockTags.VAULT, new VaultBlockEntityRewriter());
105+
105106
}
106107

107108
public static BlockEntity toJava(final UserConnection user, final int bedrockBlockStateId, final BedrockBlockEntity bedrockBlockEntity) {
@@ -166,7 +167,10 @@ default void copyItem(final UserConnection user, final CompoundTag fromTag, fina
166167

167168
default void copyItem(final UserConnection user, final CompoundTag fromTag, final CompoundTag toTag, final String fromKey, final String toKey) {
168169
if (fromTag.get(fromKey) instanceof CompoundTag itemTag) {
169-
toTag.put(toKey, this.rewriteItem(user, itemTag));
170+
CompoundTag item = this.rewriteItem(user, itemTag);
171+
if (item != null) {
172+
toTag.put(toKey, item);
173+
}
170174
}
171175
}
172176

@@ -179,6 +183,7 @@ default ListTag<?> rewriteItemList(final UserConnection user, final ListTag<Comp
179183
final ItemRewriter itemRewriter = user.get(ItemRewriter.class);
180184
for (CompoundTag bedrockItemTag : bedrockItemList) {
181185
final CompoundTag javaItemTag = itemRewriter.javaItem(bedrockItemTag);
186+
if (javaItemTag == null) continue;
182187
this.copy(bedrockItemTag, javaItemTag, "Slot", ByteTag.class);
183188
javaItemList.add(javaItemTag);
184189
}

src/main/java/net/raphimc/viabedrock/protocol/rewriter/ItemRewriter.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.google.common.collect.BiMap;
2121
import com.google.common.collect.HashBiMap;
2222
import com.viaversion.nbt.tag.CompoundTag;
23+
import com.viaversion.nbt.tag.IntTag;
24+
import com.viaversion.nbt.tag.StringTag;
2325
import com.viaversion.nbt.tag.Tag;
2426
import com.viaversion.viaversion.api.Via;
2527
import com.viaversion.viaversion.api.connection.StoredObject;
@@ -231,8 +233,43 @@ public Item javaItem(final BedrockItem bedrockItem) {
231233

232234
public CompoundTag javaItem(final CompoundTag bedrockTag) {
233235
final CompoundTag javaTag = new CompoundTag();
234-
javaTag.putString("id", "minecraft:stone");
235-
return javaTag; // TODO: Support converting nbt items
236+
237+
if (bedrockTag == null) {
238+
return null;
239+
}
240+
241+
String bedrockId = bedrockTag.getString("Name");
242+
if (bedrockId == null || bedrockId.isEmpty()) {
243+
return null;
244+
}
245+
246+
Integer id = this.items.get(bedrockId);
247+
if (id == null) {
248+
ViaBedrock.getPlatform().getLogger().warning("Could not find item " + bedrockId);
249+
return null;
250+
}
251+
252+
BedrockItem item = new BedrockItem(
253+
id,
254+
(short) 0,
255+
bedrockTag.getByte("Count"),
256+
bedrockTag.getCompoundTag("tag")
257+
);
258+
if (item.isEmpty()) {
259+
return null;
260+
}
261+
262+
Item javaItem = this.javaItem(item);
263+
264+
String javaId = BedrockProtocol.MAPPINGS.getJavaItems().inverse().get(javaItem.identifier());
265+
javaTag.put("id", new StringTag(javaId));
266+
267+
javaTag.put("count",new IntTag(javaItem.amount()));
268+
if (javaItem.tag() != null) {
269+
javaTag.put("components", javaItem.tag());
270+
}
271+
272+
return javaTag;
236273
}
237274

238275
public Item[] javaItems(final BedrockItem[] bedrockItems) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock
3+
* Copyright (C) 2023-2026 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.viabedrock.protocol.rewriter.blockentity;
19+
20+
import com.viaversion.nbt.tag.CompoundTag;
21+
import com.viaversion.nbt.tag.ListTag;
22+
import com.viaversion.nbt.tag.ShortTag;
23+
import com.viaversion.viaversion.api.connection.UserConnection;
24+
import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity;
25+
import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntityImpl;
26+
import net.raphimc.viabedrock.api.chunk.BedrockBlockEntity;
27+
import net.raphimc.viabedrock.protocol.rewriter.BlockEntityRewriter;
28+
29+
import java.util.List;
30+
31+
public class BrewingStandBlockEntityRewriter implements BlockEntityRewriter.Rewriter {
32+
33+
@Override
34+
public BlockEntity toJava(UserConnection user, BedrockBlockEntity bedrockBlockEntity) {
35+
final CompoundTag bedrockTag = bedrockBlockEntity.tag();
36+
final CompoundTag javaTag = new CompoundTag();
37+
38+
List<CompoundTag> items = bedrockTag.getListTag("Items", CompoundTag.class).getValue();
39+
ListTag<CompoundTag> javaItems = new ListTag<>(CompoundTag.class);
40+
for (CompoundTag item : items) {
41+
CompoundTag javaItem = this.rewriteItem(user, item);
42+
byte newSlot = switch (item.getByte("Slot")) {
43+
case 0 -> 3; // Ingredient Slot
44+
case 1 -> 0; // Potion slots
45+
case 2 -> 1;
46+
case 3 -> 2;
47+
case 4 -> 4; // Fuel slot
48+
default -> -1; // Invalid slot, should not happen
49+
};
50+
51+
javaItem.putByte("Slot", newSlot);
52+
javaItems.add(javaItem);
53+
}
54+
javaTag.put("Items", javaItems);
55+
56+
this.copy(bedrockTag, javaTag, "CookTime", "BrewTime", ShortTag.class);
57+
byte fuel = (byte) bedrockTag.getShort("FuelAmount");
58+
javaTag.putByte("Fuel", fuel);
59+
60+
return new BlockEntityImpl(bedrockBlockEntity.packedXZ(), bedrockBlockEntity.y(), -1, javaTag);
61+
}
62+
63+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock
3+
* Copyright (C) 2023-2026 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.viabedrock.protocol.rewriter.blockentity;
19+
20+
import com.viaversion.nbt.tag.ByteTag;
21+
import com.viaversion.nbt.tag.CompoundTag;
22+
import com.viaversion.nbt.tag.ListTag;
23+
import com.viaversion.viaversion.api.connection.UserConnection;
24+
import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity;
25+
import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntityImpl;
26+
import net.raphimc.viabedrock.ViaBedrock;
27+
import net.raphimc.viabedrock.api.chunk.BedrockBlockEntity;
28+
import net.raphimc.viabedrock.protocol.rewriter.BlockEntityRewriter;
29+
import net.raphimc.viabedrock.protocol.rewriter.ItemRewriter;
30+
31+
public class CampfireBlockEntityRewriter implements BlockEntityRewriter.Rewriter {
32+
33+
@Override
34+
public BlockEntity toJava(UserConnection user, BedrockBlockEntity bedrockBlockEntity) {
35+
final CompoundTag bedrockTag = bedrockBlockEntity.tag();
36+
final CompoundTag javaTag = new CompoundTag();
37+
38+
final ListTag<CompoundTag> javaItemList = new ListTag<>(CompoundTag.class);
39+
final ItemRewriter itemRewriter = user.get(ItemRewriter.class);
40+
for (int i = 0; i < 4; i++) {
41+
String name = "Item" + (i + 1);
42+
if (!bedrockTag.contains(name)) continue;
43+
final CompoundTag bedrockItemTag = bedrockTag.getCompoundTag(name);
44+
final CompoundTag javaItemTag = itemRewriter.javaItem(bedrockItemTag);
45+
if (javaItemTag == null) continue;
46+
javaItemTag.put("Slot", new ByteTag((byte) i));
47+
javaItemList.add(javaItemTag);
48+
}
49+
javaTag.put("Items", javaItemList);
50+
51+
// TODO: ItemTime1-4 seem to be booleans not the actual cook time ?????????
52+
53+
return new BlockEntityImpl(bedrockBlockEntity.packedXZ(), bedrockBlockEntity.y(), -1, javaTag);
54+
}
55+
56+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock
3+
* Copyright (C) 2023-2026 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.viabedrock.protocol.rewriter.blockentity;
19+
20+
import com.viaversion.nbt.tag.ByteTag;
21+
import com.viaversion.nbt.tag.CompoundTag;
22+
import com.viaversion.nbt.tag.ListTag;
23+
import com.viaversion.viaversion.api.connection.UserConnection;
24+
import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity;
25+
import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntityImpl;
26+
import net.raphimc.viabedrock.ViaBedrock;
27+
import net.raphimc.viabedrock.api.chunk.BedrockBlockEntity;
28+
import net.raphimc.viabedrock.protocol.rewriter.BlockEntityRewriter;
29+
import net.raphimc.viabedrock.protocol.rewriter.ItemRewriter;
30+
31+
public class ShelfBlockEntityRewriter implements BlockEntityRewriter.Rewriter {
32+
33+
@Override
34+
public BlockEntity toJava(UserConnection user, BedrockBlockEntity bedrockBlockEntity) {
35+
final CompoundTag bedrockTag = bedrockBlockEntity.tag();
36+
final CompoundTag javaTag = new CompoundTag();
37+
38+
final ListTag<CompoundTag> bedrockItemList = bedrockTag.getListTag("Items", CompoundTag.class);
39+
final ListTag<CompoundTag> javaItemList = new ListTag<>(CompoundTag.class);
40+
final ItemRewriter itemRewriter = user.get(ItemRewriter.class);
41+
for (int i = 0; i < 3; i++) {
42+
final CompoundTag bedrockItemTag = bedrockItemList.get(i);
43+
final CompoundTag javaItemTag = itemRewriter.javaItem(bedrockItemTag);
44+
if (javaItemTag == null) continue;
45+
javaItemTag.put("Slot", new ByteTag((byte) i));
46+
javaItemList.add(javaItemTag);
47+
}
48+
javaTag.put("Items", javaItemList);
49+
50+
return new BlockEntityImpl(bedrockBlockEntity.packedXZ(), bedrockBlockEntity.y(), -1, javaTag);
51+
}
52+
53+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock
3+
* Copyright (C) 2023-2026 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.viabedrock.protocol.rewriter.blockentity;
19+
20+
import com.viaversion.nbt.tag.CompoundTag;
21+
import com.viaversion.nbt.tag.StringTag;
22+
import com.viaversion.viaversion.api.connection.UserConnection;
23+
import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity;
24+
import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntityImpl;
25+
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_21_11;
26+
import com.viaversion.viaversion.util.Key;
27+
import net.raphimc.viabedrock.ViaBedrock;
28+
import net.raphimc.viabedrock.api.chunk.BedrockBlockEntity;
29+
import net.raphimc.viabedrock.protocol.BedrockProtocol;
30+
import net.raphimc.viabedrock.protocol.rewriter.BlockEntityRewriter;
31+
32+
import java.util.logging.Level;
33+
34+
public class TrialSpawnerBlockEntityRewriter implements BlockEntityRewriter.Rewriter {
35+
36+
@Override
37+
public BlockEntity toJava(UserConnection user, BedrockBlockEntity bedrockBlockEntity) {
38+
final CompoundTag bedrockTag = bedrockBlockEntity.tag();
39+
final CompoundTag javaTag = new CompoundTag();
40+
41+
// TODO: More testing needed
42+
43+
if (bedrockTag.getCompoundTag("spawn_data").get("TypeId") instanceof StringTag entityIdentifierTag) {
44+
final String bedrockEntityIdentifier = entityIdentifierTag.getValue();
45+
final EntityTypes1_21_11 javaEntityType = BedrockProtocol.MAPPINGS.getBedrockToJavaEntities().get(Key.namespaced(bedrockEntityIdentifier));
46+
if (javaEntityType != null) {
47+
final CompoundTag spawnData = new CompoundTag();
48+
final CompoundTag entityTag = new CompoundTag();
49+
entityTag.putString("id", javaEntityType.identifier());
50+
spawnData.put("entity", entityTag);
51+
javaTag.put("spawn_data", spawnData);
52+
} else if (!Key.stripNamespace(bedrockEntityIdentifier).isEmpty()) {
53+
ViaBedrock.getPlatform().getLogger().log(Level.WARNING, "Unknown bedrock entity identifier: " + bedrockEntityIdentifier);
54+
final CompoundTag spawnData = new CompoundTag();
55+
spawnData.put("entity", new CompoundTag());
56+
javaTag.put("spawn_data", spawnData);
57+
}
58+
}
59+
60+
return new BlockEntityImpl(bedrockBlockEntity.packedXZ(), bedrockBlockEntity.y(), -1, javaTag);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)