Skip to content

Instantly share code, notes, and snippets.

@ci010
Created March 28, 2020 12:01
Show Gist options
  • Save ci010/27415d55a3e72399924433b759924f2e to your computer and use it in GitHub Desktop.
Save ci010/27415d55a3e72399924433b759924f2e to your computer and use it in GitHub Desktop.
static Random r = new Random();
static List<Block> blocks = GameRegistry.findRegistry(Block.class).getValues().stream()
.collect(Collectors.toList());
static int size = blocks.size();
static boolean isValidBlock(Block block) {
Item item = block.asItem();
ItemGroup group = item.getGroup();
if (group != ItemGroup.BUILDING_BLOCKS)
return false;
if (block == Blocks.WATER)
return false;
if (block instanceof ConcretePowderBlock)
return false;
if (block == Blocks.ICE)
return false;
if (block == Blocks.FROSTED_ICE)
return false;
if (block == Blocks.PACKED_ICE)
return false;
if (block == Blocks.GRAVEL)
return false;
if (block == Blocks.SAND)
return false;
return true;
}
static BlockState getBlockState() {
Block block;
do {
block = blocks.get(r.nextInt(size));
} while (!isValidBlock(block));
ImmutableList<BlockState> states = block.getStateContainer().getValidStates();
return states.get(r.nextInt(states.size()));
}
static int getIndex(int x, int y, int z) {
return y << 8 | z << 4 | x;
}
// generate random block in 0,0,0 chunk
static void generate(World world) {
for (int x = 0; x < 16; x += 2) {
for (int y = 0; y < 16; y += 2) {
for (int z = 0; z < 16; z += 2) {
BlockState state = getBlockState();
world.setBlockState(new BlockPos(x, y, z), state, 1);
}
}
}
}
// dump blocks in 0,0,0 chunk
static dump(World world) {
List<String> lines = new ArrayList<>();
Chunk chunk = world.getChunk(0, 0);
BlockStateContainer<BlockState> container = chunk.getSections()[0].getData();
BitArray arr = null;
// refect to get storage field in BlockStateContainer
try {
Field f;
f = container.getClass().getDeclaredField("storage");
f.setAccessible(true);
arr = (BitArray) f.get(container);
} catch (Exception e1) {
e1.printStackTrace();
}
lines.clear();
for (int x = 0; x < 16; x += 2) {
for (int y = 0; y < 16; y += 2) {
for (int z = 0; z < 16; z += 2) {
BlockState state = container.get(x, y, z);
// int persistId = Block.BLOCK_STATE_IDS.get(state);
int idx = getIndex(x, y, z);
int id = arr.getAt(idx);
lines.add(new StringBuilder().append(id).append(" ").append(x).append(" ").append(y)
.append(" ").append(z).append(" ").append(state).toString());
}
}
}
try {
Files.write(Paths.get("mock.txt"), lines, Charset.defaultCharset());
} catch (Exception e) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment