Skip to content

Instantly share code, notes, and snippets.

@AjaxGb
Last active June 28, 2017 02:18
Show Gist options
  • Save AjaxGb/fbc000dc8422aa87cfba661ed4740225 to your computer and use it in GitHub Desktop.
Save AjaxGb/fbc000dc8422aa87cfba661ed4740225 to your computer and use it in GitHub Desktop.
MCEdit filter to do find and replace in command blocks, with support for regular expressions.
# coding unicode-escape
# Feel free to modify and use this filter however you wish. If you do,
# please give credit to SethBling.
# http://youtube.com/SethBling
# Updated to 1.11 by Onnowhere
# http://youtube.com/Onnowhere2
# Regex support added by AjaxGb
# https://github.com/AjaxGb
import re
from pymclevel import TAG_List
from pymclevel import TAG_Byte
from pymclevel import TAG_Int
from pymclevel import TAG_Compound
from pymclevel import TAG_Short
from pymclevel import TAG_Double
from pymclevel import TAG_String
displayName = "Find and Replace Regex"
inputs = (
("Find", "string"),
("Replace", "string"),
("Use RegEx", True),
)
def perform(level, box, options):
print("=== Running Find and Replace Regex ===")
find = options["Find"]
replace = options["Replace"]
use_regex = options["Use RegEx"]
for (chunk, slices, point) in level.getChunkSlices(box):
for t in chunk.TileEntities:
x = t["x"].value
y = t["y"].value
z = t["z"].value
if x >= box.minx and x < box.maxx and y >= box.miny and y < box.maxy and z >= box.minz and z < box.maxz:
if t["id"].value == "Control":
# Catch in case you have not updated all command blocks to 1.11
t["id"] = TAG_String("minecraft:command_block")
print("Updated block at", x, y, z, "to 1.11")
chunk.dirty = True
if t["id"].value == "minecraft:command_block":
# Find and replace
cmd = t["Command"].value
if use_regex:
newcmd = re.sub(find, replace, cmd)
else:
newcmd = cmd.replace(find, replace)
if newcmd != cmd:
print("<<", cmd)
print(">>", newcmd)
t["Command"] = TAG_String(newcmd)
chunk.dirty = True
@AjaxGb
Copy link
Author

AjaxGb commented Jun 28, 2017

Credit to SethBling for the original filter, and to Onnowhere for updating it to 1.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment