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 Aug 29, 2023
1 parent 8ac867f commit f9f9e69
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
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 @@ -3259,6 +3261,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 @@ -114,6 +114,8 @@ public static Entity fromJson(JsonObject json) {
return DecoratedPotModel.DecoratedPotSpoutEntity.fromJson(json);
case "calibratedSculkSensorAmethyst":
return CalibratedSculkSensorAmethyst.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);
}
}

0 comments on commit f9f9e69

Please sign in to comment.