GlowstoneMC / Glowstone-Legacy

An open-source server for the Bukkit Minecraft modding interface
Other
363 stars 122 forks source link

GlowBlock requires world to not be null #625

Closed PaulBGD closed 9 years ago

PaulBGD commented 9 years ago

Glowstone in itself is a great server software, however it contains utils that can be used in other ways. I've been experimenting with using it to load chunks from a world, without loading the actual world. For instance, the following script can be run to print out all the types of a block in the spawn chunk of a world:

AnvilChunkIoService service = new AnvilChunkIoService(worldDir);
NBTInputStream dataInputStream = new NBTInputStream(new FileInputStream(new File(worldDir, "level.dat")));
CompoundTag data = dataInputStream.readCompound().getCompound("Data");
dataInputStream.close();
GlowChunk spawn = loadChunk(data.getInt("SpawnX") >> 4, data.getInt("SpawnZ") >> 4, service);
HashMap<Material, Integer> types = new HashMap<Material, Integer>();
for (int x = 0; x < 16; x++) {
    for (int z = 0; z < 16; z++) {
        for (int y = 0; y < 256; y++) {
            Block block = spawn.getBlock(x, y, z);
            if (!types.containsKey(block.getType())) {
                types.put(block.getType(), 0);
            }
            types.put(block.getType(), types.get(block.getType()) + 1);
        }
    }
}
System.out.println(Joiner.on('\n').withKeyValueSeparator(": ").join(types));

Which produces for me:

DIAMOND_ORE: 4
AIR: 46411
STATIONARY_WATER: 53
REDSTONE_ORE: 11
GOLD_ORE: 20
STONE: 15276
GRAVEL: 91
LEAVES: 884
LONG_GRASS: 49
COAL_ORE: 246
DIRT: 901
COCOA: 3
GRASS: 245
STATIONARY_LAVA: 97
BEDROCK: 778
IRON_ORE: 112
LOG: 150
VINE: 204
RED_ROSE: 1

However if I wanted to set a block's type, block.setType throws an NPE because there is no world (and there can't be, because there's no server).

turt2live commented 9 years ago

This sounds like you aren't trying to improve the server beyond personal purposes. Is there a reason why you can't write your own library for whatever you're trying to do?

PaulBGD commented 9 years ago

@turt2live I made a PR due to the fact that I had already coded it, and assumed someone else may also want to use it in the future. If you don't think that will happen, I'll close these.

turt2live commented 9 years ago

I honestly don't know how @SpaceManiac feels about it (it's his project), but personally I don't see a benefit in it if it doesn't improve the server operation in some way. As far as I can tell, the enhancement would be used by things that aren't servers or by plugins which shouldn't be seeing it anyways.

SpaceManiac commented 9 years ago

While I don't see any pressing need to perform this particular change, it would certainly be beneficial in the long run to reduce the coupling between the world I/O code and the live world code (which would make things like this possible).

If you hadn't been willing to patch GlowBlock, you could have worked around this by using the methods on GlowChunk directly.

PaulBGD commented 9 years ago

I realized this, however I thought it'd be a simple enough change. I'll close this and the PR, I suppose it's not needed in Glowstone (especially at this stage).