Skip to content

Instantly share code, notes, and snippets.

@reedacartwright
Last active October 26, 2021 02:55
Show Gist options
  • Save reedacartwright/1a7a81f35eef493e1418cb28b176ad72 to your computer and use it in GitHub Desktop.
Save reedacartwright/1a7a81f35eef493e1418cb28b176ad72 to your computer and use it in GitHub Desktop.
Introduction to Analyzing a Minecraft World, LIA 194 - Class 2
# Introduction to Analyzing a Minecraft World
# LIA 194 - Class #2
# We will be using the development branch of the rbedrock library,
# and will use devtools to install or update it.
if (!requireNamespace("devtools", quietly = TRUE)) {
install.packages("devtools")
}
devtools::install_github("reedacartwright/rbedrock")
# Load required packages for class
library(tidyr)
library(purrr)
library(dplyr)
library(readr)
library(rbedrock)
# Download and unzip the world we want to analyze
download.file("https://bit.ly/3pFeE39", destfile="ClassSMP.zip", mode="wb")
unzip("ClassSMP.zip")
# Open a database connection to our world
dbpath <- "ClassSMP"
db <- bedrockdb(dbpath)
# Minecraft stores data in chunks. Each chunk is a 16x16 area of the world.
# Chunk x=0, z=0 refers to the area from block x=0 to x=15 and z=0 to z=16.
# Dimension=0 is the overworld.
# EXERCISE #1: FINDING DIAMONDS
# Here we retrieve all blocks in chunk 0,0
blocks <- get_chunk_blocks_value(db, x=0, z=0, dimension=0, names_only=TRUE)
# count how often each block occurs in this chunk
table(blocks)
# locate all blocks with 'diamond' in their name in this chunk
dat <- locate_blocks(blocks, "diamond")
# create a grid of chunk coordinates in the overworld
g <- expand_grid(x=-4:4,z=-4:4,dimension=0)
# Retrieve all blocks in this chunk grid
# note the use of a different function than before
blocks <- get_chunk_blocks_data(db, g$x, g$z, g$dimension, names_only=TRUE)
# Apply locate_blocks to every chuck in our grid and combined the results
pos <- map_dfr(blocks, locate_blocks, "diamond")
# save our located positions
write_csv(pos, "block_pos.csv")
# We're done. Close our database
close(db)
# EXERCISE #2: FINDING MOB SPAWNERS
# Open our world database again
db <- bedrockdb(dbpath)
# fetch a list of all keys in the world
keys <- get_keys(db)
# read all block entity data in the world
block_ent <- get_block_entities_data(db,keys)
# Flatten our list of lists into a single list and
# strip unneeded metadata with unnbt.
block_ent <- unnbt(flatten(block_ent))
# Filter our list, keeping only elements that are MobSpawners
mob_spawners <- keep(block_ent, ~.$id == "MobSpawner")
# Transform our list into a table, and extact 4 columns
dat <- bind_rows(mob_spawners)
dat <- select(dat, x, y, z, EntityIdentifier)
# save our results to a file
write_csv(dat, "spawner_pos.csv")
# And we're done
close(db)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment