Files
src
build.gradle
README.txt
settings.gradle
Code
5 169
src/main/java/com/maxpro/maxworldedit/MaxWorldEdit.java
package com.maxpro.maxworldedit;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public final class MaxWorldEdit extends JavaPlugin implements Listener {
private static final int MAX_BLOCKS = 100_000;
private final Map<UUID, Selection> selections = new HashMap<>();
private final Map<UUID, Map<BlockKey, Material>> undo = new HashMap<>();
private final Map<UUID, List<ClipboardBlock>> clipboards = new HashMap<>();
private final Map<UUID, Location> clipboardOrigins = new HashMap<>();
@Override public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
getDataFolder().mkdirs();
new File(getDataFolder(), "schematics").mkdirs();
getLogger().info("MaxWorldEdit enabled. Editing commands are OP-only.");
}
// Keep the implementation independent of external WorldEdit libraries.
private static final class File extends java.io.File { File(java.io.File parent, String child) { super(parent, child); } }
private record Selection(Location p1, Location p2) {}
private record BlockKey(UUID world, int x, int y, int z) {}
private record ClipboardBlock(int x, int y, int z, Material material) {}
@EventHandler public void onCommand(PlayerCommandPreprocessEvent event) {
String raw = event.getMessage();
if (!raw.startsWith("//")) return;
event.setCancelled(true);
Player p = event.getPlayer();
if (!p.isOp()) { p.sendMessage(ChatColor.RED + "MaxWorldEdit is for OPs/admins only."); return; }
String[] a = raw.substring(2).trim().split("\\s+");
if (a.length == 0 || a[0].isBlank()) { help(p); return; }
String cmd = a[0].toLowerCase(Locale.ROOT);
try {
switch (cmd) {
case "help" -> help(p);
case "wand" -> { p.getInventory().addItem(new org.bukkit.inventory.ItemStack(Material.WOODEN_AXE)); p.sendMessage(ChatColor.GREEN + "Left-click = point 1; right-click = point 2."); }
case "pos1" -> setPoint(p, 1, p.getLocation());
case "pos2" -> setPoint(p, 2, p.getLocation());
case "set" -> { requireArgs(p, a, 2); Material m = material(a[1]); edit(p, (b, x,y,z) -> b.setType(m, false)); }
case "replace" -> { requireArgs(p, a, 3); Material from = material(a[1]), to = material(a[2]); edit(p, (b,x,y,z) -> { if (b.getType() == from) b.setType(to, false); }); }
case "walls" -> { requireArgs(p, a, 2); Material m = material(a[1]); edit(p, (b,x,y,z) -> { Selection s = selection(p); if (x == Math.min(s.p1.getBlockX(),s.p2.getBlockX()) || x == Math.max(s.p1.getBlockX(),s.p2.getBlockX()) || z == Math.min(s.p1.getBlockZ(),s.p2.getBlockZ()) || z == Math.max(s.p1.getBlockZ(),s.p2.getBlockZ())) b.setType(m,false); }); }
case "copy" -> copy(p, false);
case "cut" -> copy(p, true);
case "paste" -> paste(p);
case "undo" -> undo(p);
case "expand" -> { requireArgs(p,a,3); expand(p, a[1], a[2]); }
case "schematic" -> { requireArgs(p,a,3); if (a[1].equalsIgnoreCase("save")) saveSchematic(p,a[2]); else if (a[1].equalsIgnoreCase("load")) loadSchematic(p,a[2]); else p.sendMessage(ChatColor.RED + "Use //schematic save <name> or //schematic load <name>"); }
default -> { p.sendMessage(ChatColor.RED + "Unknown command. Use //help"); }
}
} catch (IllegalArgumentException ex) { p.sendMessage(ChatColor.RED + ex.getMessage()); }
catch (Exception ex) { p.sendMessage(ChatColor.RED + "MaxWorldEdit error: " + ex.getMessage()); getLogger().warning("Command error: " + ex); }
}
@EventHandler public void onInteract(PlayerInteractEvent e) {
if (e.getHand() != EquipmentSlot.HAND || e.getItem() == null || e.getItem().getType() != Material.WOODEN_AXE) return;
if (!e.getPlayer().isOp()) return;
if (e.getAction() != Action.LEFT_CLICK_BLOCK && e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
e.setCancelled(true);
setPoint(e.getPlayer(), e.getAction() == Action.LEFT_CLICK_BLOCK ? 1 : 2, e.getClickedBlock().getLocation());
}
private void help(Player p) { p.sendMessage(ChatColor.GOLD + "MaxWorldEdit commands (OP only): //wand, //pos1, //pos2, //set <block>, //replace <from> <to>, //walls <block>, //copy, //cut, //paste, //undo, //expand <direction> <amount>, //schematic save/load <name>"); }
private void requireArgs(Player p, String[] a, int n) { if (a.length < n) throw new IllegalArgumentException("Missing arguments. Use //help."); }
private Material material(String name) { Material m = Material.matchMaterial(name); if (m == null || !m.isBlock()) throw new IllegalArgumentException("Unknown block: " + name); return m; }
private Selection selection(Player p) { Selection s = selections.get(p.getUniqueId()); if (s == null || s.p1 == null || s.p2 == null) throw new IllegalArgumentException("Select two points first with //wand."); if (!s.p1.getWorld().equals(s.p2.getWorld())) throw new IllegalArgumentException("Both points must be in the same world."); return s; }
private void setPoint(Player p, int n, Location l) { Selection old = selections.get(p.getUniqueId()); Location p1 = old == null ? null : old.p1, p2 = old == null ? null : old.p2; if(n==1)p1=l.clone();else p2=l.clone(); selections.put(p.getUniqueId(), new Selection(p1,p2)); p.sendMessage(ChatColor.GREEN + "Point " + n + " set to " + l.getBlockX()+", "+l.getBlockY()+", "+l.getBlockZ()); }
@FunctionalInterface private interface BlockEdit { void apply(Block b,int x,int y,int z); }
private void edit(Player p, BlockEdit op) { Selection s=selection(p); int minX=Math.min(s.p1.getBlockX(),s.p2.getBlockX()), maxX=Math.max(s.p1.getBlockX(),s.p2.getBlockX()); int minY=Math.min(s.p1.getBlockY(),s.p2.getBlockY()), maxY=Math.max(s.p1.getBlockY(),s.p2.getBlockY()); int minZ=Math.min(s.p1.getBlockZ(),s.p2.getBlockZ()), maxZ=Math.max(s.p1.getBlockZ(),s.p2.getBlockZ()); long volume=(long)(maxX-minX+1)*(maxY-minY+1)*(maxZ-minZ+1); if(volume>MAX_BLOCKS)throw new IllegalArgumentException("Selection is too large. Limit: "+MAX_BLOCKS+" blocks."); World w=s.p1.getWorld(); Map<BlockKey,Material> before=new HashMap<>(); for(int x=minX;x<=maxX;x++)for(int y=minY;y<=maxY;y++)for(int z=minZ;z<=maxZ;z++){ Block b=w.getBlockAt(x,y,z); before.put(new BlockKey(w.getUID(),x,y,z),b.getType()); op.apply(b,x,y,z); } undo.put(p.getUniqueId(),before); p.sendMessage(ChatColor.GREEN+"Edit complete ("+volume+" blocks checked). Use //undo to revert block types."); }
private void undo(Player p) { Map<BlockKey,Material> old=undo.remove(p.getUniqueId()); if(old==null){p.sendMessage(ChatColor.RED+"Nothing to undo.");return;} for(var e:old.entrySet()){World w=Bukkit.getWorld(e.getKey().world); if(w!=null)w.getBlockAt(e.getKey().x,e.getKey().y,e.getKey().z).setType(e.getValue(),false);} p.sendMessage(ChatColor.GREEN+"Last edit undone."); }
private void copy(Player p, boolean cut) { Selection s=selection(p); int minX=Math.min(s.p1.getBlockX(),s.p2.getBlockX()),maxX=Math.max(s.p1.getBlockX(),s.p2.getBlockX()),minY=Math.min(s.p1.getBlockY(),s.p2.getBlockY()),maxY=Math.max(s.p1.getBlockY(),s.p2.getBlockY()),minZ=Math.min(s.p1.getBlockZ(),s.p2.getBlockZ()),maxZ=Math.max(s.p1.getBlockZ(),s.p2.getBlockZ()); long v=(long)(maxX-minX+1)*(maxY-minY+1)*(maxZ-minZ+1); if(v>MAX_BLOCKS)throw new IllegalArgumentException("Selection is too large. Limit: "+MAX_BLOCKS+" blocks."); List<ClipboardBlock> list=new ArrayList<>(); Map<BlockKey,Material> before=new HashMap<>(); World w=s.p1.getWorld(); for(int x=minX;x<=maxX;x++)for(int y=minY;y<=maxY;y++)for(int z=minZ;z<=maxZ;z++){Block b=w.getBlockAt(x,y,z); list.add(new ClipboardBlock(x-minX,y-minY,z-minZ,b.getType())); if(cut){before.put(new BlockKey(w.getUID(),x,y,z),b.getType());b.setType(Material.AIR,false);}} clipboards.put(p.getUniqueId(),list); clipboardOrigins.put(p.getUniqueId(),new Location(w,minX,minY,minZ)); if(cut)undo.put(p.getUniqueId(),before); p.sendMessage(ChatColor.GREEN+(cut?"Cut":"Copied")+" "+list.size()+" blocks."); }
private void paste(Player p) { List<ClipboardBlock> list=clipboards.get(p.getUniqueId()); if(list==null){throw new IllegalArgumentException("Clipboard is empty. Use //copy first.");} if(list.size()>MAX_BLOCKS)throw new IllegalArgumentException("Clipboard exceeds block limit."); Location l=p.getLocation().getBlock().getLocation(); World w=l.getWorld(); Map<BlockKey,Material> before=new HashMap<>(); for(ClipboardBlock c:list){int x=l.getBlockX()+c.x,y=l.getBlockY()+c.y,z=l.getBlockZ()+c.z;if(y<w.getMinHeight()||y>=w.getMaxHeight())continue;Block b=w.getBlockAt(x,y,z);before.put(new BlockKey(w.getUID(),x,y,z),b.getType());b.setType(c.material,false);}undo.put(p.getUniqueId(),before);p.sendMessage(ChatColor.GREEN+"Pasted "+before.size()+" blocks."); }
private void expand(Player p,String direction,String amountText) { Selection s=selection(p); int n;try{n=Integer.parseInt(amountText);}catch(NumberFormatException e){throw new IllegalArgumentException("Amount must be a number.");} if(n<1||n>1000)throw new IllegalArgumentException("Amount must be 1-1000."); Location a=s.p1.clone(),b=s.p2.clone(); switch(direction.toLowerCase(Locale.ROOT)){case "north" -> {a.setZ(a.getBlockZ()-n);b.setZ(b.getBlockZ()-n);}case "south" -> {a.setZ(a.getBlockZ()+n);b.setZ(b.getBlockZ()+n);}case "east" -> {a.setX(a.getBlockX()+n);b.setX(b.getBlockX()+n);}case "west" -> {a.setX(a.getBlockX()-n);b.setX(b.getBlockX()-n);}case "up" -> {a.setY(a.getBlockY()+n);b.setY(b.getBlockY()+n);}case "down" -> {a.setY(a.getBlockY()-n);b.setY(b.getBlockY()-n);}default -> throw new IllegalArgumentException("Direction: north/south/east/west/up/down");} selections.put(p.getUniqueId(),new Selection(a,b));p.sendMessage(ChatColor.GREEN+"Selection expanded "+n+" blocks toward "+direction+"."); }
private Path schematicPath(String name) { if(!name.matches("[A-Za-z0-9_-]{1,40}"))throw new IllegalArgumentException("Name can use letters, numbers, _ and - only."); return getDataFolder().toPath().resolve("schematics").resolve(name+".mwe"); }
private void saveSchematic(Player p,String name)throws IOException { List<ClipboardBlock> list=clipboards.get(p.getUniqueId()); if(list==null){copy(p,false);list=clipboards.get(p.getUniqueId());} Path path=schematicPath(name);try(BufferedWriter w=Files.newBufferedWriter(path)){for(ClipboardBlock b:list)w.write(b.x+","+b.y+","+b.z+","+b.material.name()+"\n");}p.sendMessage(ChatColor.GREEN+"Saved "+list.size()+" blocks to plugins/MaxWorldEdit/schematics/"+name+".mwe"); }
private void loadSchematic(Player p,String name)throws IOException { Path path=schematicPath(name);if(!Files.exists(path))throw new IllegalArgumentException("Schematic not found: "+name+".mwe");List<ClipboardBlock> list=new ArrayList<>();try(BufferedReader r=Files.newBufferedReader(path)){String line;while((line=r.readLine())!=null){String[] v=line.split(",");if(v.length!=4)continue;Material m=Material.matchMaterial(v[3]);if(m==null)continue;list.add(new ClipboardBlock(Integer.parseInt(v[0]),Integer.parseInt(v[1]),Integer.parseInt(v[2]),m));if(list.size()>MAX_BLOCKS)throw new IllegalArgumentException("Schematic exceeds block limit.");}}clipboards.put(p.getUniqueId(),list);p.sendMessage(ChatColor.GREEN+"Loaded "+list.size()+" blocks. Stand where you want the build and run //paste."); }
}