Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pr0methean committed May 21, 2018
1 parent b34d3b5 commit 604863c
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 118 deletions.
13 changes: 4 additions & 9 deletions src/main/java/net/glowstone/block/entity/FlowerPotEntity.java
@@ -1,5 +1,6 @@
package net.glowstone.block.entity;

import java.util.Optional;
import lombok.Getter;
import lombok.Setter;
import net.glowstone.block.GlowBlock;
Expand All @@ -25,15 +26,9 @@ public FlowerPotEntity(GlowBlock block) {
@Override
public void loadNbt(CompoundTag tag) {
super.loadNbt(tag);
final Material[] item = {null};
// NBT data uses material ID names (post-1.8).
if (!tag.readString("Item", itemString -> item[0] = ItemIds.getItem(itemString))) {
// NBT data uses material IDs (pre-1.8).
tag.readInt("Item", itemInt -> item[0] = Material.getMaterial(itemInt));
}
if (item[0] != null) {
tag.readInt("Data", data -> this.contents = item[0].getNewData((byte) data));
}
tag.tryGetMaterial("Item").ifPresent(
item -> this.contents = item.getNewData(
(byte) (int) (tag.tryGetInt("Data").orElse(0))));
}

@Override
Expand Down
Expand Up @@ -27,10 +27,7 @@ public MobSpawnerEntity(GlowBlock block) {
@Override
public void loadNbt(CompoundTag tag) {
super.loadNbt(tag);
tag.readString("EntityId", name -> spawning = EntityType.fromName(name));
if (spawning == null) {
spawning = DEFAULT;
}
spawning = tag.tryGetString("EntityId").map(EntityType::fromName).orElse(DEFAULT);
if (!tag.readInt("Delay", this::setDelay)) {
delay = 0;
}
Expand Down
Expand Up @@ -49,14 +49,12 @@ public UnlockRecipesMessage createInitMessage() {
* @param playerData an NBT tag containing a compound subtag named recipeBook
*/
public void read(CompoundTag playerData) {
CompoundTag recipeBook = playerData.tryGetCompound("recipeBook");
if (recipeBook == null) {
return;
}
recipeBook.readBoolean("isFilteringCraftable", this::setFilterCraftable);
recipeBook.readBoolean("isGuiOpen", this::setBookOpen);
recipeBook.readStringList("recipes", recipes::addAll);
recipeBook.readStringList("toBeDisplayed", toBeDisplayed::addAll);
playerData.tryGetCompound("recipeBook").ifPresent(recipeBook -> {
recipeBook.readBoolean("isFilteringCraftable", this::setFilterCraftable);
recipeBook.readBoolean("isGuiOpen", this::setBookOpen);
recipeBook.readStringList("recipes", recipes::addAll);
recipeBook.readStringList("toBeDisplayed", toBeDisplayed::addAll);
});
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/glowstone/io/entity/ArmorStandStore.java
Expand Up @@ -29,15 +29,14 @@ public void load(GlowArmorStand entity, CompoundTag tag) {
tag.readBooleanNegated("NoGravity", entity::setGravity);
tag.readBoolean("ShowArms", entity::setArms);
tag.readBoolean("Small", entity::setSmall);
CompoundTag pose = tag.tryGetCompound("Pose");
if (pose != null) {
tag.tryGetCompound("Pose").ifPresent(pose -> {
entity.setBodyPose(readSafeAngle(pose, "Body"));
entity.setLeftArmPose(readSafeAngle(pose, "LeftArm"));
entity.setRightArmPose(readSafeAngle(pose, "RightArm"));
entity.setLeftLegPose(readSafeAngle(pose, "LeftLeg"));
entity.setRightLegPose(readSafeAngle(pose, "RightLeg"));
entity.setHeadPose(readSafeAngle(pose, "Head"));
}
});
}

@Override
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/glowstone/io/entity/EnderCrystalStore.java
Expand Up @@ -20,13 +20,12 @@ public GlowEnderCrystal createEntity(Location location, CompoundTag compound) {
public void load(GlowEnderCrystal entity, CompoundTag tag) {
super.load(entity, tag);
tag.readBoolean("ShowBottom", entity::setShowingBottom);
CompoundTag beamTarget = tag.tryGetCompound("BeamTarget");
if (beamTarget != null) {
tag.tryGetCompound("BeamTarget").ifPresent(beamTarget -> {
int x = beamTarget.getInt("X");
int y = beamTarget.getInt("Y");
int z = beamTarget.getInt("Z");
entity.setBeamTarget(new Location(entity.getWorld(), x, y, z));
}
});
}

@Override
Expand Down
35 changes: 7 additions & 28 deletions src/main/java/net/glowstone/io/entity/EndermanStore.java
@@ -1,5 +1,6 @@
package net.glowstone.io.entity;

import java.util.Optional;
import net.glowstone.constants.ItemIds;
import net.glowstone.entity.monster.GlowEnderman;
import net.glowstone.util.nbt.CompoundTag;
Expand All @@ -16,36 +17,14 @@ public EndermanStore() {
@Override
public void load(GlowEnderman entity, CompoundTag compound) {
super.load(entity, compound);
final MaterialData[] carried = {null};
// Load carried block. May be saved as a String or short ID.
// If the id is 0 or AIR, it is ignored.
if (!compound.readShort("carried", id -> {
Material type = Material.getMaterial(id);
if (type != null && type != Material.AIR) {
carried[0] = new MaterialData(type);
}
})) {
compound.readString("carried", id -> {
if (!id.isEmpty()) {
if (!id.contains(":")) {
// There is no namespace, so prepend the default minecraft: namespace
id = "minecraft:" + id;
}
Material type = ItemIds.getBlock(id);
if (type == null) {
// Not a block, might be an item
type = ItemIds.getItem(id);
}
if (type != null && type != Material.AIR) {
carried[0] = new MaterialData(type);
}
}
});
}
if (carried[0] != null) {
compound.readShort("carriedData", data -> carried[0].setData((byte) data));
entity.setCarriedMaterial(carried[0]);
}
compound.tryGetMaterial("carried")
.map(MaterialData::new)
.ifPresent(carried -> {
compound.readShort("carriedData", data -> carried.setData((byte) data));
entity.setCarriedMaterial(carried);
});
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/net/glowstone/io/entity/EntityStore.java
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;
import lombok.Data;
Expand Down Expand Up @@ -84,9 +85,10 @@ public void load(T entity, CompoundTag tag) {
entity.getCustomTags().addAll(list);
});
tag.readInt("PortalCooldown", entity::setPortalCooldown);
if (!tag.readUuid("UUIDMost", "UUIDLeast", entity::setUniqueId)) {
tag.readString("UUID", uuidString -> entity.setUniqueId(UUID.fromString(uuidString)));
}
Optional.ofNullable(
tag.tryGetUuid("UUIDMost", "UUIDLeast")
.orElseGet(() -> tag.tryGetString("UUID").map(UUID::fromString).orElse(null)))
.ifPresent(entity::setUniqueId);
tag.iterateCompoundList("Passengers", entityTag -> {
Entity passenger = loadPassenger(entity, entityTag);
if (passenger != null) {
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/net/glowstone/io/entity/LivingEntityStore.java
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;
import net.glowstone.entity.AttributeManager;
import net.glowstone.entity.AttributeManager.Modifier;
Expand Down Expand Up @@ -106,17 +107,9 @@ public void load(T entity, CompoundTag compound) {
});
am.setProperty(tag.getString("Name"), tag.getDouble("Base"), modifiers);
});
CompoundTag leash = compound.tryGetCompound("Leash");
if (leash == null) {
compound.readBoolean("Leashed", leashSet -> {
if (leashSet) {
// We know that there was something leashed, but not what entity it was
// This can happen, when for example Minecart got leashed
// We still have to make sure that we drop a Leash Item
entity.setLeashHolderUniqueId(UUID.randomUUID());
}
});
} else {
Optional<CompoundTag> maybeLeash = compound.tryGetCompound("Leash");
if (maybeLeash.isPresent()) {
CompoundTag leash = maybeLeash.get();
if (!leash.readUuid("UUIDMost", "UUIDLeast", entity::setLeashHolderUniqueId)
&& leash.isInt("X") && leash.isInt("Y") && leash.isInt("Z")) {
int x = leash.getInt("X");
Expand All @@ -127,6 +120,15 @@ public void load(T entity, CompoundTag compound) {
.getLeashHitchAt(new Location(entity.getWorld(), x, y, z).getBlock());
entity.setLeashHolder(leashHitch);
}
} else {
compound.readBoolean("Leashed", leashSet -> {
if (leashSet) {
// We know that there was something leashed, but not what entity it was
// This can happen, when for example Minecart got leashed
// We still have to make sure that we drop a Leash Item
entity.setLeashHolderUniqueId(UUID.randomUUID());
}
});
}
}

Expand Down
33 changes: 16 additions & 17 deletions src/main/java/net/glowstone/io/nbt/NbtSerialization.java
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import net.glowstone.GlowServer;
import net.glowstone.constants.ItemIds;
Expand Down Expand Up @@ -124,24 +125,22 @@ public static List<CompoundTag> writeInventory(ItemStack[] items, int start) {
* @return The world, or null if none could be found.
*/
public static World readWorld(GlowServer server, CompoundTag compound) {
final World[] world = {null};
compound.readUuid("WorldUUIDMost", "WorldUUIDLeast",
uuid -> world[0] = server.getWorld(uuid));
if (world[0] == null) {
compound.readString("World", name -> world[0] = server.getWorld(name));
World world = compound
.tryGetUuid("WorldUUIDMost", "WorldUUIDLeast")
.map(server::getWorld)
.orElseGet(() -> compound.tryGetString("World")
.map(server::getWorld)
.orElse(null));
if (world == null) {
world = compound
.tryGetInt("Dimension")
.map(World.Environment::getEnvironment)
.flatMap(env -> server.getWorlds().stream()
.filter(serverWorld -> env == serverWorld.getEnvironment())
.findFirst())
.orElse(null);
}
if (world[0] == null) {
compound.readInt("Dimension", dim -> {
// FIXME: Linear time
for (World serverWorld : server.getWorlds()) {
if (serverWorld.getEnvironment().getId() == dim) {
world[0] = serverWorld;
break;
}
}
});
}
return world[0];
return world;
}

/**
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/net/glowstone/io/nbt/NbtStructureDataService.java
Expand Up @@ -75,8 +75,8 @@ public Map<Integer, GlowStructure> readStructuresData() {
public void writeStructuresData(Map<Integer, GlowStructure> structures) {
for (GlowStructure structure : structures.values()) {
if (structure.isDirty()) {
CompoundTag data = null;
CompoundTag features = new CompoundTag();
CompoundTag data;
CompoundTag features;
CompoundTag feature = new CompoundTag();
CompoundTag inputRoot;
StructureStore<GlowStructure> store = StructureStorage
Expand All @@ -86,20 +86,16 @@ public void writeStructuresData(Map<Integer, GlowStructure> structures) {
try (NbtInputStream in = new NbtInputStream(
new FileInputStream(structureFile))) {
inputRoot = in.readCompound();
data = inputRoot.tryGetCompound("data");
if (data != null) {
CompoundTag maybeFeatures = data.tryGetCompound("Features");
if (maybeFeatures != null) {
features = maybeFeatures;
}
}
data = inputRoot.tryGetCompound("data").orElseGet(CompoundTag::new);
} catch (IOException e) {
server.getLogger().log(Level.SEVERE,
"Failed to read structure data from " + structureFile, e);
data = new CompoundTag();
}
}
if (data == null) {
features = data.tryGetCompound("Features").orElseGet(CompoundTag::new);
} else {
data = new CompoundTag();
features = new CompoundTag();
}
String key = "[" + structure.getChunkX() + "," + structure.getChunkZ() + "]";
features.putCompound(key, feature);
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/net/glowstone/io/nbt/NbtWorldMetadataService.java
Expand Up @@ -7,6 +7,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;
import net.glowstone.GlowWorld;
Expand Down Expand Up @@ -66,14 +67,13 @@ public WorldFinalValues readWorldData() {
File levelFile = new File(dir, "level.dat");
if (levelFile.exists()) {
try (NbtInputStream in = new NbtInputStream(new FileInputStream(levelFile))) {
level = in.readCompound();
CompoundTag data = level.tryGetCompound("Data");
if (data != null) {
level = data;
} else {
CompoundTag levelOuter = in.readCompound();
level = levelOuter.tryGetCompound("Data").orElseGet(() -> {
server.getLogger().warning(
"Loading world \"" + world.getName() + "\": reading from root, not Data");
}
"Loading world \"" + world.getName()
+ "\": reading from root, not Data");
return levelOuter;
});
} catch (IOException e) {
handleWorldException("level.dat", e);
}
Expand Down

0 comments on commit 604863c

Please sign in to comment.