Skip to content

Commit

Permalink
Add hanging signs (model still missing).
Browse files Browse the repository at this point in the history
  • Loading branch information
leMaik committed Jul 2, 2023
1 parent e8905bc commit 61eef33
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
40 changes: 40 additions & 0 deletions chunky/src/java/se/llbit/chunky/block/HangingSign.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions chunky/src/java/se/llbit/chunky/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
87 changes: 87 additions & 0 deletions chunky/src/java/se/llbit/chunky/entity/HangingSignEntity.java
Original file line number Diff line number Diff line change
@@ -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<Primitive> 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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 61eef33

Please sign in to comment.