Skip to content

Instantly share code, notes, and snippets.

@gamesbyangelina
Created January 7, 2020 13:15
Show Gist options
  • Save gamesbyangelina/908e216724cfea90560ca79be002683b to your computer and use it in GitHub Desktop.
Save gamesbyangelina/908e216724cfea90560ca79be002683b to your computer and use it in GitHub Desktop.
Sign Creation Example, MCEdit/GDMC/pymclevel
from pymclevel import nbt
import time # for timing
from math import sqrt, tan, sin, cos, pi, ceil, floor, acos, atan, asin, degrees, radians, log, atan2, acos, asin
from random import *
from numpy import *
from pymclevel import alphaMaterials, MCSchematic, MCLevel, BoundingBox
from mcplatform import *
from pymclevel import TAG_String
from pymclevel import TAG_Int
from pymclevel import TAG_List
from pymclevel import TAG_Compound
from pymclevel import TileEntity
import utilityFunctions as utilityFunctions
displayName = "Add Signs"
inputs = (
("Sign Creation Example", "label"), # A label with the name of our filter
("Adds a sign to every tile location.", "label"),
("Creator: Mike Cook", "label"), # A label with the author of the filter
("Thanks to Rodrigo Canaan for example filter code", "label"),
)
# Class storing some properties of a tile in the game
class tile:
def __init__(self, x, y, z, material):
self.x = x
self.y = y
self.z = z
self.material = material
xmin = 0
xmax = 0
zmin = 0
zmax = 0
#this is some convenience stuff that is overkill for this filter, you can skip over this bit
from pymclevel import ChunkNotPresent
GlobalChunkCache = {}
GlobalLevel = None
def getChunk(x, z, level):
global GlobalChunkCache
chunkCoords = (x>>4, z>>4)
if chunkCoords not in GlobalChunkCache:
try:
GlobalChunkCache[chunkCoords] = level.getChunk(x>>4, z>>4)
except ChunkNotPresent:
return None
return GlobalChunkCache[chunkCoords]
def perform(level, box, options):
tileMap = getTileMap (level, box, 255, 0)
# this creates a sign in every open tile you've selected
# really this filter is here as example code, i don't think you'd want to run this >_>
# FYI the way getTileMap works (which I cribbed from a GDMC example filter) is basically to find surface blocks
for x in range(0,xmax-xmin):
for y in range(0, zmax-zmin):
tile = tileMap[x,y]
# first we need to add the sign itself
level.setBlockAt(tile.x, tile.y+1, tile.z, 63) # 63 is the code for a regular sign. 68 is a wall-mounted sign.
# and some block data setting which way it faces
level.setBlockDataAt(tile.x, tile.y+1, tile.z, 0) # This is the direction the sign is facing
# then we grab the chunk the sign is in
chunk = getChunk(tile.x, tile.z, level)
# and mark it dirty so the game knows we changed it
chunk.dirty = True
# finally, create the sign
sign = createSign(tile.x, tile.y+1, tile.z, "You are here:","("+str(tile.x)+","+str(tile.z)+")","Love,","The Sign Painter")
# and add it to the tile entities list (so the sign block knows how to display itself)
level.addTileEntity(sign)
return;
def createSign(x,y,z,t1,t2,t3,t4):
# signs are TAG_Compounds containing text data and co-ordinates
sign = TAG_Compound()
# this is very important - it's a sign
sign["id"] = TAG_String(u'minecraft:sign')
# each one of these are the four text lines, note it must be wrapped in this json format
sign["Text1"] = TAG_String("{\"text\":\""+t1+"\"}")
sign["Text2"] = TAG_String("{\"text\":\""+t2+"\"}")
sign["Text3"] = TAG_String("{\"text\":\""+t3+"\"}")
sign["Text4"] = TAG_String("{\"text\":\""+t4+"\"}")
# finally, the co-ordinates
sign["x"] = TAG_Int(x)
sign["y"] = TAG_Int(y)
sign["z"] = TAG_Int(z)
return sign
def getTileMap (level, box, maxHeight, minHeight):
global xmin, xmax, zmin, zmax;
xmin = box.minx
xmax = box.maxx
zmin = box.minz
zmax = box.maxz
width = xmax-xmin+1
depth = zmax-zmin+1
tileMap = empty( (width,depth) ,dtype=object)
for x in range(xmin,xmax+1):
for z in range(zmin,zmax+1):
for y in range(maxHeight, minHeight-1,-1):
material = level.blockAt(x,y,z)
if (material != 0):
tileMap[x-xmin,z-zmin] = tile(x,y,z,material)
break
return tileMap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment