Skip to content

Commit bdd95bd

Browse files
committed
Make Entities#fromJson return a single entity, legacy entities now handled by caller
1 parent bf2f67e commit bdd95bd

30 files changed

Lines changed: 101 additions & 128 deletions

chunky/src/java/se/llbit/chunky/entity/ArmorStand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,8 @@ public ArmorStand(Vector3 position, Tag tag) {
587587
*
588588
* @return deserialized entity, or {@code null} if it was not a valid entity
589589
*/
590-
public static Collection<Entity> fromJson(JsonObject json) {
591-
return Collections.singleton(new ArmorStand(json));
590+
public static Entity fromJson(JsonObject json) {
591+
return new ArmorStand(json);
592592
}
593593

594594
@Override public String[] partNames() {

chunky/src/java/se/llbit/chunky/entity/BeaconBeam.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ public JsonValue toJson() {
227227
return json;
228228
}
229229

230-
public static Collection<Entity> fromJson(JsonObject json) {
231-
return Collections.singleton(new BeaconBeam(json));
230+
public static Entity fromJson(JsonObject json) {
231+
return new BeaconBeam(json);
232232
}
233233

234234
@Override

chunky/src/java/se/llbit/chunky/entity/Book.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ public JsonValue toJson() {
296296
return json;
297297
}
298298

299-
public static Collection<Entity> fromJson(JsonObject json) {
300-
return Collections.singleton(new Book(json));
299+
public static Entity fromJson(JsonObject json) {
300+
return new Book(json);
301301
}
302302

303303
@Override

chunky/src/java/se/llbit/chunky/entity/CalibratedSculkSensorAmethyst.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public JsonValue toJson() {
113113
return json;
114114
}
115115

116-
public static Collection<Entity> fromJson(JsonObject json) {
117-
return Collections.singleton(new CalibratedSculkSensorAmethyst(json));
116+
public static Entity fromJson(JsonObject json) {
117+
return new CalibratedSculkSensorAmethyst(json);
118118
}
119119

120120
@Override

chunky/src/java/se/llbit/chunky/entity/Campfire.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ public JsonValue toJson() {
329329
return json;
330330
}
331331

332-
public static Collection<Entity> fromJson(JsonObject json) {
333-
return Collections.singleton(new Campfire(json));
332+
public static Entity fromJson(JsonObject json) {
333+
return new Campfire(json);
334334
}
335335

336336
private static int getOrientationIndex(String facing) {

chunky/src/java/se/llbit/chunky/entity/ChickenEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ public JsonValue toJson() {
243243
return json;
244244
}
245245

246-
public static Collection<Entity> fromJson(JsonObject json) {
247-
return Collections.singleton(new ChickenEntity(json));
246+
public static Entity fromJson(JsonObject json) {
247+
return new ChickenEntity(json);
248248
}
249249

250250
@Override

chunky/src/java/se/llbit/chunky/entity/CoralFanEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ public CoralFanEntity(Vector3 position, String coralType) {
107107
return json;
108108
}
109109

110-
public static Collection<Entity> fromJson(JsonObject json) {
110+
public static Entity fromJson(JsonObject json) {
111111
Vector3 position = new Vector3();
112112
position.fromJson(json.get("position").object());
113-
return Collections.singleton(new CoralFanEntity(position, json.get("coral_type").stringValue("tube")));
113+
return new CoralFanEntity(position, json.get("coral_type").stringValue("tube"));
114114
}
115115

116116

chunky/src/java/se/llbit/chunky/entity/CowEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ public JsonValue toJson() {
288288
return json;
289289
}
290290

291-
public static Collection<Entity> fromJson(JsonObject json) {
292-
return Collections.singleton(new CowEntity(json));
291+
public static Entity fromJson(JsonObject json) {
292+
return new CowEntity(json);
293293
}
294294

295295
@Override

chunky/src/java/se/llbit/chunky/entity/Entity.java

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
import se.llbit.chunky.model.minecraft.DecoratedPotModel;
2222
import se.llbit.json.JsonObject;
2323
import se.llbit.json.JsonValue;
24+
import se.llbit.log.Log;
2425
import se.llbit.math.Grid;
2526
import se.llbit.math.Octree;
2627
import se.llbit.math.Vector3;
2728
import se.llbit.math.Vector3i;
2829
import se.llbit.math.primitive.Primitive;
2930

3031
import java.util.Collection;
32+
import java.util.LinkedList;
3133

3234
/**
3335
* Represents Minecraft entities that are not stored in the octree.
@@ -68,70 +70,51 @@ public void loadDataFromOctree(Octree octree, BlockPalette palette, Vector3i ori
6870
/**
6971
* Unmarshalls an entity object from JSON data.
7072
*
73+
* <p>This method only returns a {@link Collection} to support legacy scenes.</p>
74+
*
7175
* @param json json data.
72-
* @return unmarshalled entity, or {@code null} if it was not a valid entity.
76+
* @return The entities, or an empty collection if no entity was found.
7377
*/
74-
public static Collection<Entity> fromJson(JsonObject json) {
78+
public static Collection<Entity> entitiesFromJson(JsonObject json) {
7579
String kind = json.get("kind").stringValue("");
80+
Collection<Entity> entities = new LinkedList<>();
7681
switch (kind) {
77-
case "painting":
78-
return PaintingEntity.fromJson(json);
79-
case "sign":
80-
return SignEntity.fromJson(json);
81-
case "wallsign":
82-
return WallSignEntity.fromJson(json);
83-
case "skull":
84-
return SkullEntity.fromJson(json);
85-
case "head":
86-
return HeadEntity.fromJson(json);
87-
case "player":
88-
return PlayerEntity.fromJson(json);
89-
case "standing_banner":
90-
return StandingBanner.fromJson(json);
91-
case "wall_banner":
92-
return WallBanner.fromJson(json);
93-
case "armor_stand":
94-
return ArmorStand.fromJson(json);
95-
case "lily_pad":
96-
return LilyPadEntity.fromJson(json);
97-
case "coral_fan":
98-
return CoralFanEntity.fromJson(json);
99-
case "wall_coral_fan":
100-
return WallCoralFanEntity.fromJson(json);
101-
case "lectern":
102-
return Lectern.fromJson(json);
103-
case "campfire":
104-
return Campfire.fromJson(json);
105-
case "book":
106-
return Book.fromJson(json);
107-
case "flameParticles":
108-
return FlameParticles.fromJson(json);
109-
case "beaconBeam":
110-
return BeaconBeam.fromJson(json);
111-
case "sporeBlossom":
112-
return SporeBlossom.fromJson(json);
113-
case "decoratedPotSpout":
114-
return DecoratedPotModel.DecoratedPotSpoutEntity.fromJson(json);
115-
case "calibratedSculkSensorAmethyst":
116-
return CalibratedSculkSensorAmethyst.fromJson(json);
117-
case "hangingSign":
118-
return HangingSignEntity.fromJson(json);
119-
case "wallHangingSign":
120-
return WallHangingSignEntity.fromJson(json);
121-
case "sheep":
122-
return SheepEntity.fromJson(json);
123-
case "cow":
124-
return CowEntity.fromJson(json);
125-
case "chicken":
126-
return ChickenEntity.fromJson(json);
127-
case "pig":
128-
return PigEntity.fromJson(json);
129-
case "mooshroom":
130-
return MooshroomEntity.fromJson(json);
131-
case "squid":
132-
return SquidEntity.fromJson(json);
82+
case "painting" -> entities.add(PaintingEntity.fromJson(json));
83+
case "sign" -> entities.add(SignEntity.fromJson(json));
84+
case "wallsign" -> entities.add(WallSignEntity.fromJson(json));
85+
case "skull" -> entities.add(SkullEntity.fromJson(json));
86+
case "head" -> entities.add(HeadEntity.fromJson(json));
87+
case "player" -> entities.add(PlayerEntity.fromJson(json));
88+
case "standing_banner" -> entities.add(StandingBanner.fromJson(json));
89+
case "wall_banner" -> entities.add(WallBanner.fromJson(json));
90+
case "armor_stand" -> entities.add(ArmorStand.fromJson(json));
91+
case "lily_pad" -> entities.add(LilyPadEntity.fromJson(json));
92+
case "coral_fan" -> entities.add(CoralFanEntity.fromJson(json));
93+
case "wall_coral_fan" -> entities.add(WallCoralFanEntity.fromJson(json));
94+
case "lectern" -> {
95+
if (json.get("book").isObject()) { // we still get the book from the lectern json to be compatible with the old format
96+
entities.add(Book.fromJson(json.get("book").object()));
97+
}
98+
entities.add(Lectern.fromJson(json));
99+
}
100+
case "campfire" -> entities.add(Campfire.fromJson(json));
101+
case "book" -> entities.add(Book.fromJson(json));
102+
case "flameParticles" -> entities.add(FlameParticles.fromJson(json));
103+
case "beaconBeam" -> entities.add(BeaconBeam.fromJson(json));
104+
case "sporeBlossom" -> entities.add(SporeBlossom.fromJson(json));
105+
case "decoratedPotSpout" -> entities.add(DecoratedPotModel.DecoratedPotSpoutEntity.fromJson(json));
106+
case "calibratedSculkSensorAmethyst" -> entities.add(CalibratedSculkSensorAmethyst.fromJson(json));
107+
case "hangingSign" -> entities.add(HangingSignEntity.fromJson(json));
108+
case "wallHangingSign" -> entities.add(WallHangingSignEntity.fromJson(json));
109+
case "sheep" -> entities.add(SheepEntity.fromJson(json));
110+
case "cow" -> entities.add(CowEntity.fromJson(json));
111+
case "chicken" -> entities.add(ChickenEntity.fromJson(json));
112+
case "pig" -> entities.add(PigEntity.fromJson(json));
113+
case "mooshroom" -> entities.add(MooshroomEntity.fromJson(json));
114+
case "squid" -> entities.add(SquidEntity.fromJson(json));
115+
default -> Log.errorf("Found unknown entity %s when loading from scene.", kind);
133116
}
134-
return null;
117+
return entities;
135118
}
136119

137120
public Vector3 getPosition() {

chunky/src/java/se/llbit/chunky/entity/FlameParticles.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ public JsonValue toJson() {
120120
return json;
121121
}
122122

123-
public static Collection<Entity> fromJson(JsonObject json) {
124-
return Collections.singleton(new FlameParticles(json));
123+
public static Entity fromJson(JsonObject json) {
124+
return new FlameParticles(json);
125125
}
126126

127127
@Override

0 commit comments

Comments
 (0)