Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions chunky/src/java/se/llbit/chunky/block/BlockProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface BlockProvider {
Collection<String> getSupportedBlocks();

static String facing(Tag tag, String defaultValue) {
return tag.get("Properties").get("facing").stringValue(defaultValue);
return BlockSpec.getPropertiesTag(tag).get("facing").stringValue(defaultValue);
}

static String facing(Tag tag) {
Expand Down Expand Up @@ -51,7 +51,20 @@ static boolean stringToBoolean(Tag tag) {
}

static String blockName(Tag tag) {
String name = tag.get("Name").stringValue();
// See BlockSpec#toBlock for why this is needed.
String name;
if (tag instanceof StringTag) {
name = tag.stringValue();
} else {
Tag idTag = tag.get("Name");
if (idTag.isError()) {
idTag = tag.get("id");
}
if (idTag.isError()) {
idTag = tag.get("");
}
name = idTag.stringValue();
}
if (name.startsWith("minecraft:")) {
name = name.substring(10); // Remove "minecraft:" prefix.
}
Expand Down
31 changes: 29 additions & 2 deletions chunky/src/java/se/llbit/chunky/block/BlockSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import se.llbit.chunky.block.minecraft.UnknownBlock;
import se.llbit.nbt.CompoundTag;
import se.llbit.nbt.SpecificTag;
import se.llbit.nbt.StringTag;
import se.llbit.nbt.Tag;
import se.llbit.util.NbtUtil;
import se.llbit.util.annotation.NotNull;
Expand Down Expand Up @@ -48,7 +50,24 @@ public boolean equals(Object obj) {
* Converts NBT block data to Chunky block object.
*/
public Block toBlock() {
String name = tag.get("Name").stringValue("unknown:unknown");
// Versions before 26.3 snapshot 7 used "Name" for the block name and "Properties" for the block state data.
// Versions 26.3 snapshot 7 and following use a StringTag for a block with default block state information, or
// a CompoundTag containing a NamedTag with an empty name to store the block name if the block is in a list with
// other CompundTags. If the block has non-default block state data, then "id" is used for the block name, and
// "properties" is used for the block state information.
String name;
if (tag instanceof StringTag) {
name = tag.stringValue("unknown:unknown");
} else {
Tag idTag = tag.get("Name");
if (idTag.isError()) {
idTag = tag.get("id");
}
if (idTag.isError()) {
idTag = tag.get("");
}
name = idTag.stringValue("unknown:unknown");
}
for (BlockProvider provider : blockProviders) {
Block block = provider.getBlockByTag(name, tag);
if (block != null) {
Expand All @@ -59,7 +78,7 @@ public Block toBlock() {
}

private Block maybeWaterlogged(Block block) {
if (tag.get("Properties").get("waterlogged").stringValue("").equals("true")) {
if (getPropertiesTag(tag).get("waterlogged").stringValue("").equals("true")) {
return block.applyWaterlogging();
}
return block;
Expand All @@ -68,4 +87,12 @@ private Block maybeWaterlogged(Block block) {
public Tag getTag() {
return tag;
}

public static Tag getPropertiesTag(Tag tag) {
Tag properties = tag.get("properties");
if (properties.isError()) {
return tag.get("Properties");
}
return properties;
}
}
466 changes: 249 additions & 217 deletions chunky/src/java/se/llbit/chunky/block/MinecraftBlockProvider.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package se.llbit.chunky.block.minecraft;

import se.llbit.chunky.block.AbstractModelBlock;
import se.llbit.chunky.block.BlockSpec;
import se.llbit.chunky.model.minecraft.DecoratedPotModel;
import se.llbit.chunky.resources.Texture;
import se.llbit.chunky.entity.Entity;
Expand Down Expand Up @@ -71,11 +72,11 @@ public Tag getNewTagWithBlockEntity(Tag blockTag, CompoundTag entityTag) {
CompoundTag newBlockTag = new CompoundTag();
// create a copy of the block tag
StreamSupport.stream(blockTag.asCompound().spliterator(), false)
.filter(namedTag -> !namedTag.name.equals("Properties"))
.filter(namedTag -> !namedTag.name.equalsIgnoreCase("Properties"))
.forEach(newBlockTag::add);
CompoundTag properties = new CompoundTag();
newBlockTag.add("Properties", properties); // because set is not implemented
blockTag.get("Properties").asCompound().iterator().forEachRemaining(properties::add);
BlockSpec.getPropertiesTag(blockTag).asCompound().iterator().forEachRemaining(properties::add);

properties.add("blockEntity#sherds", entityTag.get("sherds").asList());
properties.add("chunky#debugCoordinates", new StringTag(
Expand Down
3 changes: 2 additions & 1 deletion chunky/src/java/se/llbit/chunky/block/minecraft/Head.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package se.llbit.chunky.block.minecraft;

import se.llbit.chunky.block.BlockSpec;
import se.llbit.chunky.block.MinecraftBlockTranslucent;
import se.llbit.chunky.entity.Entity;
import se.llbit.chunky.entity.HeadEntity;
Expand Down Expand Up @@ -98,7 +99,7 @@ public static String getTextureUrl(CompoundTag entityTag) throws IOException {
ownerTag = entityTag.get("SkullOwner"); // used by player heads (1.20 or earlier)
}
if (ownerTag.isCompoundTag()) {
String textureBase64 = ownerTag.get("Properties").get("textures").get(0)
String textureBase64 = BlockSpec.getPropertiesTag(ownerTag).get("textures").get(0)
.get("Value").stringValue();
if (!textureBase64.isEmpty()) {
return MinecraftSkin.getSkinFromEncodedTextures(textureBase64).map(MinecraftSkin::getSkinUrl).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package se.llbit.chunky.block.minecraft;

import se.llbit.chunky.block.BlockSpec;
import se.llbit.chunky.renderer.scene.Scene;
import se.llbit.chunky.resources.Texture;
import se.llbit.math.Ray;
Expand All @@ -36,7 +37,7 @@ public UnknownBlock(String name) {
public UnknownBlock(String name, Tag tag) {
super(name, Texture.unknown);
StringBuilder descriptionBuilder = new StringBuilder();
for (NamedTag property : tag.get("Properties").asCompound()) {
for (NamedTag property : BlockSpec.getPropertiesTag(tag).asCompound()) {
if (!descriptionBuilder.isEmpty()) {
descriptionBuilder.append(", ");
}
Expand Down
11 changes: 5 additions & 6 deletions chunky/src/java/se/llbit/chunky/chunk/BlockPalette.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import se.llbit.chunky.block.*;
import se.llbit.chunky.block.minecraft.*;
import se.llbit.chunky.plugin.PluginApi;
import se.llbit.chunky.resources.Texture;
import se.llbit.math.Octree;
import se.llbit.nbt.CompoundTag;
import se.llbit.nbt.IntTag;
Expand Down Expand Up @@ -73,11 +72,11 @@ public BlockPalette(Map<BlockSpec, Integer> initialMap, List<Block> initialList)
this.palette = initialList;
this.materialProperties = getDefaultMaterialProperties();
CompoundTag airTag = new CompoundTag();
airTag.add("Name", new StringTag("minecraft:air"));
airTag.add("id", new StringTag("minecraft:air"));
CompoundTag stoneTag = new CompoundTag();
stoneTag.add("Name", new StringTag("minecraft:stone"));
stoneTag.add("id", new StringTag("minecraft:stone"));
CompoundTag waterTag = new CompoundTag();
waterTag.add("Name", new StringTag("minecraft:water"));
waterTag.add("id", new StringTag("minecraft:water"));
airId = put(airTag);
stoneId = put(stoneTag);
waterId = put(waterTag);
Expand Down Expand Up @@ -186,7 +185,7 @@ public int getWaterId(int level, int data) {
throw new IllegalArgumentException("Invalid water level " + level);
}
CompoundTag tag = new CompoundTag();
tag.add("Name", new StringTag("minecraft:water$chunky"));
tag.add("id", new StringTag("minecraft:water$chunky"));
tag.add("level", new IntTag(level));
tag.add("data", new IntTag(data));
BlockSpec spec = new BlockSpec(tag);
Expand All @@ -207,7 +206,7 @@ public int getLavaId(int level, int data) {
throw new IllegalArgumentException("Invalid lava level " + level);
}
CompoundTag tag = new CompoundTag();
tag.add("Name", new StringTag("minecraft:lava$chunky"));
tag.add("id", new StringTag("minecraft:lava$chunky"));
tag.add("level", new IntTag(level));
tag.add("data", new IntTag(data));
BlockSpec spec = new BlockSpec(tag);
Expand Down
3 changes: 2 additions & 1 deletion chunky/src/java/se/llbit/chunky/entity/PlayerEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package se.llbit.chunky.entity;

import se.llbit.chunky.block.BlockSpec;
import se.llbit.chunky.block.minecraft.Head;
import se.llbit.chunky.entity.SkullEntity.Kind;
import se.llbit.chunky.renderer.scene.PlayerModel;
Expand Down Expand Up @@ -130,7 +131,7 @@ static JsonObject parseItem(CompoundTag tag) {
int damage = tag.get("Damage").shortValue();
item.add("type", damage);
} else if (id.equals("minecraft:player_head")) {
Tag skinTag = tag.get("tag").get("SkullOwner").get("Properties").get("textures").get(0).get("Value");
Tag skinTag = BlockSpec.getPropertiesTag(tag.get("tag").get("SkullOwner")).get("textures").get(0).get("Value");
if (!skinTag.isError()) {
try {
String skinUrl = Head.getTextureUrl(tag.get("tag").asCompound());
Expand Down
8 changes: 4 additions & 4 deletions chunky/src/test/se/llbit/chunky/chunk/BlockPaletteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class BlockPaletteTest {
@Test
public void testBlockReuse() {
CompoundTag t1 = new CompoundTag();
t1.add("Name", new StringTag("some block"));
t1.add("id", new StringTag("some block"));
CompoundTag t2 = new CompoundTag();
t2.add("Name", new StringTag("some block"));
t2.add("id", new StringTag("some block"));
BlockPalette palette = new BlockPalette();
assertEquals(palette.put(t1), palette.put(t2),
"Block palette does not reuse block IDs for duplicate tags");
Expand All @@ -23,15 +23,15 @@ public void testBlockReuse() {
// Test that the block palette reuses the existing air id.
@Test public void testAir() {
CompoundTag air = new CompoundTag();
air.add("Name", new StringTag("minecraft:air"));
air.add("id", new StringTag("minecraft:air"));
BlockPalette palette = new BlockPalette();
assertEquals(palette.airId, palette.put(air));
}

// Test that the block palette reuses the existing water id.
@Test public void testWater() {
CompoundTag water = new CompoundTag();
water.add("Name", new StringTag("minecraft:water"));
water.add("id", new StringTag("minecraft:water"));
BlockPalette palette = new BlockPalette();
assertEquals(palette.waterId, palette.put(water));
}
Expand Down
Loading