No chat to show

This project's files are all here to explore.

Files
src
build.gradle
README.txt
settings.gradle
Code
5 231
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 "lobby" -> { if (a.length < 2 || !a[1].equalsIgnoreCase("build")) throw new IllegalArgumentException("Use //lobby build"); buildLobby(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>, //lobby build"); }
    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 void buildLobby(Player p) {
        Location base = p.getLocation().getBlock().getLocation();
        World w = base.getWorld();
        int cx = base.getBlockX(), y = base.getBlockY(), cz = base.getBlockZ();
        if (y < w.getMinHeight() + 5 || y + 24 >= w.getMaxHeight()) throw new IllegalArgumentException("Stand at a safe height with at least 25 blocks above you.");
        // Place the lobby in a 65x65 area centered on the player. Back up your world first.
        Map<BlockKey, Material> before = new HashMap<>();
        int changed = 0;
        for (int dx = -32; dx <= 32; dx++) for (int dz = -32; dz <= 32; dz++) {
            double r = Math.sqrt(dx * dx + dz * dz);
            if (r <= 30) {
                changed += lobbyBlock(w, cx + dx, y - 1, cz + dz, r <= 27 ? Material.QUARTZ_BLOCK : Material.SMOOTH_QUARTZ, before);
                if (r > 27 && r <= 30) changed += lobbyBlock(w, cx + dx, y, cz + dz, Material.SEA_LANTERN, before);
            }
            // Four broad paths leading out from the plaza.
            if ((Math.abs(dx) <= 3 && Math.abs(dz) <= 32) || (Math.abs(dz) <= 3 && Math.abs(dx) <= 32)) {
                changed += lobbyBlock(w, cx + dx, y - 1, cz + dz, Material.QUARTZ_BLOCK, before);
            }
        }
        // Fountain in the center.
        for (int dx=-4; dx<=4; dx++) for (int dz=-4; dz<=4; dz++) {
            if (Math.max(Math.abs(dx),Math.abs(dz)) <= 4) changed += lobbyBlock(w,cx+dx,y,cz+dz,Material.QUARTZ_BRICKS,before);
            if (Math.max(Math.abs(dx),Math.abs(dz)) == 3) changed += lobbyBlock(w,cx+dx,y+1,cz+dz,Material.WATER,before);
        }
        for (int dy=1; dy<=4; dy++) changed += lobbyBlock(w,cx,y+dy,cz,Material.WATER,before);
        // Four decorative towers around the plaza.
        int[][] towers = {{-20,-20},{-20,20},{20,-20},{20,20}};
        for (int[] t : towers) {
            int tx=cx+t[0], tz=cz+t[1];
            for (int dy=0; dy<=7; dy++) for (int dx=-2; dx<=2; dx++) for (int dz=-2; dz<=2; dz++) {
                if (Math.abs(dx)==2 || Math.abs(dz)==2 || dy==0 || dy==7)
                    changed += lobbyBlock(w,tx+dx,y+dy,tz+dz,Material.QUARTZ_BRICKS,before);
            }
            changed += lobbyBlock(w,tx,y+5,tz,Material.SEA_LANTERN,before);
        }
        // Simple landscaped trees and flower beds near the outer edge.
        int[][] trees = {{-13,-25},{13,-25},{-13,25},{13,25},{-25,-12},{25,-12},{-25,12},{25,12}};
        for (int[] t : trees) {
            int tx=cx+t[0], tz=cz+t[1];
            for(int dy=0;dy<4;dy++) changed += lobbyBlock(w,tx,y+dy,tz,Material.OAK_LOG,before);
            for(int dx=-2;dx<=2;dx++) for(int dz=-2;dz<=2;dz++) for(int dy=3;dy<=5;dy++)
                if(Math.abs(dx)+Math.abs(dz)+(dy==5?1:0)<=4) changed += lobbyBlock(w,tx+dx,y+dy,tz+dz,Material.OAK_LEAVES,before);
        }
        undo.put(p.getUniqueId(), before);
        p.sendMessage(ChatColor.GREEN + "MaxPro lobby built! Changes: " + before.size() + " blocks. Use //undo to revert block types.");
        p.sendMessage(ChatColor.YELLOW + "The build is centered on where you stood. Back up your world before large edits.");
    }

    private int lobbyBlock(World w, int x, int y, int z, Material material, Map<BlockKey, Material> before) {
        if (y < w.getMinHeight() || y >= w.getMaxHeight()) return 0;
        Block b = w.getBlockAt(x,y,z);
        BlockKey key = new BlockKey(w.getUID(),x,y,z);
        before.putIfAbsent(key,b.getType());
        if (b.getType() != material) b.setType(material,false);
        return 1;
    }

    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."); }
}