Last active
June 28, 2017 02:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit to SethBling for the original filter, and to Onnowhere for updating it to 1.11.