-
Notifications
You must be signed in to change notification settings - Fork 0
/
access-world.py
81 lines (64 loc) · 2.82 KB
/
access-world.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Loads a Minecraft level file and displays, for the chunk located at (0, 0),
the 2D NumPy arrays for the slices at each altitude that contains blocks.
"""
from __future__ import print_function
import mclevel
import numpy as np
__author__ = "Cosmo Harrigan"
# Load the level data: set the path using level_filename here
# level_filename = "/Users/cosmo/minecraft-dataset-generation/world/level.dat" # Unprocessed repository version
level_filename = "/Users/cosmo/minecraft-server/world/level.dat" # Server version
# level_filename = "/Users/cosmo/minecraft-dataset-generation/world-processed/level.dat" # Processed repository version
# level_filename = "/Users/cosmo/Library/Application Support/minecraft/saves/test6b/level.dat" # MCEdit saved version
READ_ONLY = False
world = mclevel.loadWorld(level_filename)
# Check how many chunks are defined in the world
chunks = world.getChunks()
print("Number of chunks: {0}".format(len(list(chunks))))
# # Print the position of each chunk
# chunk_positions = list(world.allChunks)
# print("Chunk positions:")
# for chunk_position in chunk_positions:
# print(chunk_position)
# In this case, we will only work with the chunk located at (0, 0) since we
# are defining a small world that fits within one chunk.
chunk = world.getChunk(-1, 0)
# Erase any blocks above the grass
chunk.Blocks[:, :, 2:255] = 0
# Example: How to modify a block
# Set coordinate (0, 0) at altitude 2 to be a mushroom
chunk.Blocks[0, 0, 2] = 40
# Randomly place 10 blocks according to certain constraints
num_blocks = 10
for i in range(num_blocks):
# Randomly choose x and y coordinates within the range of 0 to 15
x = np.random.randint(0, 15)
y = np.random.randint(0, 15)
# Randomly choose an altitude coordinate between 2 (the first level above the grass) and 10
altitude = np.random.randint(2, 10)
# Randomly choose a block type from 1 (stone), 17 (wood), 40 (mushroom)
# block_type = np.random.choice([1, 17, 40])
block_type = 40
# Place the block
chunk.Blocks[x, y, altitude] = block_type
# Set the spawn point
# world.setPlayerPosition((4, 4, 5)) # add 3 to make sure his head isn't in the ground.
# world.setPlayerSpawnPosition((4, 4, 2))
# Save the updated world
if READ_ONLY == False:
chunk.chunkChanged()
world.saveInPlace()
# Display the blocks at each slice that is non-empty
block_count = 0
for altitude in range(256):
# Print the slice of this altitude
altitude_slice = chunk.Blocks[:, :, altitude]
# If the altitude slice contains any blocks, print it
if np.sum(altitude_slice):
print("---- Altitude: {0}".format(altitude))
print(altitude_slice)
block_count += np.count_nonzero(altitude_slice)
print("Total block count: {0}".format(int(block_count)))
# # Check what methods and attributes the world exposes
# [print(elem) for elem in dir(world)]