From 61eef3386e214aeaf866c90f0612ac0d1e742ea6 Mon Sep 17 00:00:00 2001 From: Maik Marschner Date: Sun, 2 Jul 2023 15:41:09 +0200 Subject: [PATCH] Add hanging signs (model still missing). --- .../se/llbit/chunky/block/HangingSign.java | 40 +++++++++ .../chunky/block/MinecraftBlockProvider.java | 9 ++ .../java/se/llbit/chunky/entity/Entity.java | 2 + .../chunky/entity/HangingSignEntity.java | 87 +++++++++++++++++++ .../chunky/world/ChunkSelectionTracker.java | 2 +- 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 chunky/src/java/se/llbit/chunky/block/HangingSign.java create mode 100644 chunky/src/java/se/llbit/chunky/entity/HangingSignEntity.java diff --git a/chunky/src/java/se/llbit/chunky/block/HangingSign.java b/chunky/src/java/se/llbit/chunky/block/HangingSign.java new file mode 100644 index 0000000000..ef6e5ac786 --- /dev/null +++ b/chunky/src/java/se/llbit/chunky/block/HangingSign.java @@ -0,0 +1,40 @@ +package se.llbit.chunky.block; + +import se.llbit.chunky.entity.Entity; +import se.llbit.chunky.entity.HangingSignEntity; +import se.llbit.chunky.entity.SignEntity; +import se.llbit.chunky.renderer.scene.Scene; +import se.llbit.math.Ray; +import se.llbit.math.Vector3; +import se.llbit.nbt.CompoundTag; + +public class HangingSign extends MinecraftBlockTranslucent { + private final String material; + private final int rotation; + private final boolean attached; + + public HangingSign(String name, String material, int rotation, boolean attached) { + super(name, SignEntity.textureFromMaterial(material)); + this.material = material; + this.rotation = rotation; + this.attached = attached; + invisible = true; + solid = false; + localIntersect = true; + } + + @Override + public boolean intersect(Ray ray, Scene scene) { + return false; + } + + @Override + public boolean isBlockEntity() { + return true; + } + + @Override + public Entity toBlockEntity(Vector3 position, CompoundTag entityTag) { + return new HangingSignEntity(position, entityTag, rotation, attached, material); + } +} diff --git a/chunky/src/java/se/llbit/chunky/block/MinecraftBlockProvider.java b/chunky/src/java/se/llbit/chunky/block/MinecraftBlockProvider.java index 709fb175dc..45ed913f89 100644 --- a/chunky/src/java/se/llbit/chunky/block/MinecraftBlockProvider.java +++ b/chunky/src/java/se/llbit/chunky/block/MinecraftBlockProvider.java @@ -1012,7 +1012,9 @@ private static void addBlocks(Texture texture, String... names) { addBlock("cherry_planks", Texture.cherryPlanks); addBlock("cherry_pressure_plate", (name, tag) -> new PressurePlate(name, Texture.cherryPlanks)); addBlock("cherry_sign", (name, tag) -> sign(tag, "cherry")); + addBlock("cherry_hanging_sign", (name, tag) -> hangingSign(tag, "cherry")); addBlock("cherry_wall_sign", (name, tag) -> wallSign(tag, "cherry")); + addBlock("cherry_wall_hanging_sign", (name, tag) -> hangingSign(tag, "cherry")); addBlock("cherry_slab", (name, tag) -> slab(tag, Texture.cherryPlanks)); addBlock("cherry_stairs", (name, tag) -> stairs(tag, Texture.cherryPlanks)); addBlock("cherry_trapdoor", (name, tag) -> trapdoor(tag, Texture.cherryTrapdoor)); @@ -3248,6 +3250,13 @@ private static Block sign(Tag tag, String material) { return new Sign(name, material, rotation); } + private static Block hangingSign(Tag tag, String material) { + String name = BlockProvider.blockName(tag); + int rotation = BlockProvider.stringToInt(tag.get("Properties").get("rotation"), 0); + boolean attached = tag.get("Properties").get("attached").stringValue("false").equals("true"); + return new HangingSign(name, material, rotation, attached); + } + private static Block banner(Tag tag, Texture texture, int color) { String name = BlockProvider.blockName(tag); int rotation = BlockProvider.stringToInt(tag.get("Properties").get("rotation"), 0); diff --git a/chunky/src/java/se/llbit/chunky/entity/Entity.java b/chunky/src/java/se/llbit/chunky/entity/Entity.java index 541509c0ad..100a82fb7c 100644 --- a/chunky/src/java/se/llbit/chunky/entity/Entity.java +++ b/chunky/src/java/se/llbit/chunky/entity/Entity.java @@ -110,6 +110,8 @@ public static Entity fromJson(JsonObject json) { return SporeBlossom.fromJson(json); case "decoratedPotSpout": return DecoratedPotModel.DecoratedPotSpoutEntity.fromJson(json); + case "hangingSign": + return HangingSignEntity.fromJson(json); } return null; } diff --git a/chunky/src/java/se/llbit/chunky/entity/HangingSignEntity.java b/chunky/src/java/se/llbit/chunky/entity/HangingSignEntity.java new file mode 100644 index 0000000000..b7ba8aed57 --- /dev/null +++ b/chunky/src/java/se/llbit/chunky/entity/HangingSignEntity.java @@ -0,0 +1,87 @@ +package se.llbit.chunky.entity; + +import se.llbit.chunky.resources.SignTexture; +import se.llbit.chunky.resources.Texture; +import se.llbit.json.JsonArray; +import se.llbit.json.JsonObject; +import se.llbit.json.JsonValue; +import se.llbit.math.Vector3; +import se.llbit.math.primitive.Primitive; +import se.llbit.nbt.CompoundTag; + +import java.util.Collection; +import java.util.LinkedHashSet; + +import static se.llbit.chunky.entity.SignEntity.*; + +public class HangingSignEntity extends Entity { + private final JsonArray[] frontText; + private final JsonArray[] backText; + private final int angle; + private final boolean attached; + private final SignTexture frontTexture; + private final SignTexture backTexture; + private final Texture texture; + private final String material; + + public HangingSignEntity(Vector3 position, CompoundTag entityTag, int rotation, boolean attached, String material) { + this(position, getFrontTextLines(entityTag), getBackTextLines(entityTag), rotation, attached, material); + } + + public HangingSignEntity(Vector3 position, JsonArray[] frontText, JsonArray[] backText, int rotation, boolean attached, String material) { + super(position); + Texture signTexture = SignEntity.textureFromMaterial(material); + this.frontText = frontText; + this.backText = backText; + this.angle = rotation; + this.attached = attached; + this.frontTexture = frontText != null ? new SignTexture(frontText, signTexture, false) : null; + this.backTexture = backText != null ? new SignTexture(backText, signTexture, true) : null; + this.texture = signTexture; + this.material = material; + } + + @Override + public Collection primitives(Vector3 offset) { + // TODO + return new LinkedHashSet<>(); + } + + @Override + public JsonValue toJson() { + JsonObject json = new JsonObject(); + json.add("kind", "hangingSign"); + json.add("position", position.toJson()); + if (frontText != null) { + json.add("text", SignEntity.textToJson(frontText)); + } + if (backText != null) { + json.add("backText", SignEntity.textToJson(backText)); + } + json.add("direction", angle); + json.add("attached", attached); + json.add("material", material); + return json; + } + + /** + * Unmarshalls a sign entity from JSON data. + */ + public static Entity fromJson(JsonObject json) { + Vector3 position = new Vector3(); + position.fromJson(json.get("position").object()); + JsonArray[] frontText = null; + if (json.get("text").isArray()) { + frontText = textFromJson(json.get("text")); + } + JsonArray[] backText = null; + if (json.get("backText").isArray()) { + backText = textFromJson(json.get("backText")); + } + int direction = json.get("direction").intValue(0); + boolean attached = json.get("attached").boolValue(false); + String material = json.get("material").stringValue("oak"); + return new HangingSignEntity(position, frontText, backText, direction, attached, material); + } +} + diff --git a/chunky/src/java/se/llbit/chunky/world/ChunkSelectionTracker.java b/chunky/src/java/se/llbit/chunky/world/ChunkSelectionTracker.java index d0ea93ae1a..3a39d99b94 100644 --- a/chunky/src/java/se/llbit/chunky/world/ChunkSelectionTracker.java +++ b/chunky/src/java/se/llbit/chunky/world/ChunkSelectionTracker.java @@ -333,7 +333,7 @@ public synchronized boolean setChunkRadius(World world, int centerChunkX, int ce int chunkXOffset = chunkX - centerChunkX; int chunkZOffset = chunkZ - centerChunkZ; if (chunkXOffset * chunkXOffset + chunkZOffset * chunkZOffset < radiusSquared) { - selectionChanged |= setChunk(world, ChunkPosition.get(chunkX, chunkZ), selected); + selectionChanged |= setChunk(world, new ChunkPosition(chunkX, chunkZ), selected); } } }