Last active
November 22, 2018 19:35
-
-
Save Phoenix616/8d781191141102067c27eed9b32d2cf2 to your computer and use it in GitHub Desktop.
Simple text replacements for properties inside of blocks, requires WorldEdit. MIT License
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
/** | |
* Copyright (c) 2018 Phoenix616 (Max Lee) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { | |
if (!(sender instanceof Player)) { | |
sender.sendMessage(ChatColor.RED + "This command can only be run by a player!"); | |
return true; | |
} | |
Player player = (Player) sender; | |
String[] input = getInput(args, 2); | |
if (input.length < 2) { | |
return false; | |
} | |
Selection s = we.getSelection(player); | |
if (s == null) { | |
sender.sendMessage(ChatColor.RED + "Please make a WorldEdit selection first!"); | |
return true; | |
} | |
if (s.getArea() > 1000000) { | |
sender.sendMessage(ChatColor.RED + "Can only search blocks in regions smaller then 1.000.000 blocks! Your selection is " + s.getArea()); | |
return true; | |
} | |
final Location min = s.getMinimumPoint(); | |
final Location max = s.getMaximumPoint(); | |
sender.sendMessage(ChatColor.AQUA + "Searching for blocks that contain " + ChatColor.YELLOW + input[0] + ChatColor.AQUA + " and replace with " + ChatColor.YELLOW + input[1]); | |
plugin.getServer().getScheduler().runTask(plugin, () -> { | |
int foundCount = 0; | |
boolean regex = input[0].startsWith("/") && input[0].endsWith("/"); | |
Pattern search = null; | |
if (regex) { | |
input[0] = input[0].substring(1, input[0].length() - 1); | |
try { | |
search = Pattern.compile(input[0]); | |
} catch (PatternSyntaxException e) { | |
sender.sendMessage(ChatColor.RED + "Invalid Regex pattern. " + e.getMessage()); | |
return; | |
} | |
} | |
for (int y = min.getBlockY(); y <= max.getBlockY(); y++) { | |
for (int x = min.getBlockX(); x <= max.getBlockX(); x++) { | |
for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) { | |
boolean found = false; | |
Block block = min.getWorld().getBlockAt(x, y, z); | |
if (block.getState() instanceof InventoryHolder) { | |
if (replaceInItems(((InventoryHolder) block.getState()).getInventory(), input[0], input[1], regex)) { | |
found = true; | |
} | |
} | |
if (block.getState() instanceof CommandBlock) { | |
CommandBlock cb = (CommandBlock) block.getState(); | |
if (search != null) { | |
Matcher m = search.matcher(cb.getCommand()); | |
if (m.matches()) { | |
cb.setCommand(m.replaceAll(input[1])); | |
found = cb.update(); | |
} | |
} else if (cb.getCommand().contains(input[0])){ | |
cb.setCommand(cb.getCommand().replace(input[0], input[1])); | |
found = cb.update(); | |
} | |
} | |
if (found) { | |
foundCount++; | |
} | |
} | |
} | |
} | |
sender.sendMessage(ChatColor.YELLOW + "" + foundCount + " " + ChatColor.AQUA + " blocks changed!"); | |
}); | |
return true; | |
} | |
private boolean replaceInItems(Inventory inventory, String search, String replace, boolean regex) { | |
replace = ChatColor.translateAlternateColorCodes('&', replace); | |
boolean changed = false; | |
for (int i = 0; i < inventory.getSize(); i++) { | |
ItemStack item = inventory.getItem(i); | |
if (item != null && item.hasItemMeta()) { | |
boolean itemChanged = false; | |
ItemMeta meta = item.getItemMeta(); | |
if (meta.hasDisplayName()) { | |
if (regex) { | |
if (meta.getDisplayName().matches(search)) { | |
meta.setDisplayName(meta.getDisplayName().replaceAll(search, replace)); | |
itemChanged = true; | |
} | |
} else if (meta.getDisplayName().contains(search)) { | |
meta.setDisplayName(meta.getDisplayName().replace(search, replace)); | |
itemChanged = true; | |
} | |
} | |
if (meta.hasLore()) { | |
boolean loreChanged = false; | |
List<String> lore = new ArrayList<>(); | |
for (String line : meta.getLore()) { | |
if (regex) { | |
if (line.matches(search)) { | |
lore.add(line.replaceAll(search, replace)); | |
loreChanged = true; | |
} else { | |
lore.add(line); | |
} | |
} else if (line.contains(search)) { | |
lore.add(line.replace(search, replace)); | |
loreChanged = true; | |
} else { | |
lore.add(line); | |
} | |
} | |
if (loreChanged) { | |
itemChanged = true; | |
meta.setLore(lore); | |
} | |
} | |
if (itemChanged) { | |
changed = true; | |
item.setItemMeta(meta); | |
inventory.setItem(i, item); | |
} | |
} | |
} | |
return changed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment