WorldEdit Schematic saveing Pasting schift region2 blocks

Discussion in 'Plugin Development' started by Helfull, Sep 3, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Helfull

    hey i have a dumb problem when i dont stay exactly on a block my schematic gets shifted 2 block in the direction where i stand near.

    WorldEdit Schematic Handler class
    Code:
    package de.helfull.terrasasu.Farms;
     
    import java.io.File;
    import java.io.IOException;
     
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
     
    import com.sk89q.worldedit.CuboidClipboard;
    import com.sk89q.worldedit.EditSession;
    import com.sk89q.worldedit.EmptyClipboardException;
    import com.sk89q.worldedit.FilenameException;
    import com.sk89q.worldedit.IncompleteRegionException;
    import com.sk89q.worldedit.LocalPlayer;
    import com.sk89q.worldedit.LocalSession;
    import com.sk89q.worldedit.MaxChangedBlocksException;
    import com.sk89q.worldedit.Vector;
    import com.sk89q.worldedit.WorldEdit;
    import com.sk89q.worldedit.bukkit.BukkitUtil;
    import com.sk89q.worldedit.bukkit.WorldEditPlugin;
    import com.sk89q.worldedit.data.DataException;
    import com.sk89q.worldedit.schematic.SchematicFormat;
     
    public class FarmSchematicHandler {
        public static final String EXTENSION = "farm";
     
        private final WorldEdit we;
     
        private final LocalSession ls;
        private final LocalPlayer lp;
        private final EditSession es;
        public Vector playerPos;
     
        public FarmSchematicHandler(WorldEditPlugin wep, Player player){
            we = wep.getWorldEdit();
            lp = wep.wrapPlayer(player);
            ls = we.getSession(lp);
            es = ls.createEditSession(lp);
        }
     
        public void saveFarm(File saveFile, Location loc1, Location loc2) throws FilenameException, IOException, DataException, IncompleteRegionException{
            Vector min = getMin(loc1, loc2);
            Vector max = getMax(loc1, loc2);
            Vector pos = ls.getPlacementPosition(lp);
     
            saveFile = we.getSafeSaveFile(lp, saveFile.getParentFile(), saveFile.getName(), EXTENSION, new String[] { EXTENSION });
     
            es.enableQueue();
            CuboidClipboard clip = new CuboidClipboard(max.subtract(min).add(new Vector(1, 1, 1)), min, min.subtract(pos));
            clip.copy(es);
            SchematicFormat.MCEDIT.save(clip, saveFile);
            es.flushQueue();
        }
     
        public void loadFarm(File saveFile) throws FilenameException, IOException, DataException, MaxChangedBlocksException, EmptyClipboardException, IncompleteRegionException{
            Vector pos = playerPos = ls.getPlacementPosition(lp);
     
            saveFile = we.getSafeSaveFile(lp, saveFile.getParentFile(), saveFile.getName(), EXTENSION, new String[] { EXTENSION });
            es.enableQueue();
            ls.setClipboard(SchematicFormat.MCEDIT.load(saveFile));
            ls.getClipboard().paste(es, pos, false, true);
            es.flushQueue();
            we.flushBlockBag(lp, es);
        }
     
        public void loadFarm(File saveFile, Location loc) throws FilenameException, IOException, DataException, MaxChangedBlocksException, EmptyClipboardException, IncompleteRegionException{
            Vector pos = BukkitUtil.toVector(loc);
     
            saveFile = we.getSafeSaveFile(lp, saveFile.getParentFile(), saveFile.getName(), EXTENSION, new String[] { EXTENSION });
            es.enableQueue();
            ls.setClipboard(SchematicFormat.MCEDIT.load(saveFile));
            ls.getClipboard().paste(es, pos, false, true);
            es.flushQueue();
            we.flushBlockBag(lp, es);
        }
     
        public static Vector getMax(Location loc1, Location loc2){
            return new Vector(
                                Math.max(loc1.getBlockX(), loc2.getBlockX()),
                                Math.max(loc1.getBlockY(), loc2.getBlockY()),
                                Math.max(loc1.getBlockZ(), loc2.getBlockZ())
                            );
        }
     
        public static Vector getMin(Location loc1, Location loc2){
            return new Vector(
                                Math.min(loc1.getBlockX(), loc2.getBlockX()),
                                Math.min(loc1.getBlockY(), loc2.getBlockY()),
                                Math.min(loc1.getBlockZ(), loc2.getBlockZ())
                            );
        }
    }
    
    Methods where they get called
    Code:
        public void spawnFarm(Player player, String farmID) {
            Farm f = getFarm(farmID);
            if (f != null) {
                if (f.SCHEMATIC_FARM) {
                    String path = this.plugin.getDataFolder()
                            + File.separator + "Farms" + File.separator + farmID + "." + FarmSchematicHandler.EXTENSION;
                    File farm = new File(path);
                    if (farm.exists()) {
                        FarmSchematicHandler fsh = new FarmSchematicHandler(
                                this.plugin.getWorldEditPlugin(), player);
                        try {
                            if(f.loc1 == null){
                                player.sendMessage("NULL");
                            }
                            fsh.loadFarm(farm, f.loc1.toLocation(f.world));
                        } catch (Exception e) {
                            e.printStackTrace();
                            player.sendMessage(Message.err);
                        }
                    } else {
                        plugin.log.info(Message.fileNotFound(path));
                        player.sendMessage(Message.fileNotFound(path));
                    }
                } else {
                    setMat(f.world, listLoc(f.world, f.loc1, f.loc2), f.mat);
                }
            } else {
                player.sendMessage(Message.farmNotFound(farmID));
            }
        }
       
        public void loadFarmFromSchematic(Player player, String farmID){
            File farm = new File(plugin.getDataFolder()+File.separator+"Farms"+File.separator+farmID+"."+FarmSchematicHandler.EXTENSION);
            if(farm.exists()){
                FarmSchematicHandler fc = new FarmSchematicHandler(plugin.getWorldEditPlugin(), player);
                try {
                    fc.loadFarm(farm);
                } catch (Exception e) {
                    e.printStackTrace();
                    player.sendMessage(Message.err);
                }
            } else {
                player.sendMessage(Message.farmNotFound(farmID));
            }
        }
       
        public void saveFarmToSchematic(Player player, String farmID){
            File farm = new File(plugin.getDataFolder()+File.separator+"Farms"+File.separator+farmID+"."+FarmSchematicHandler.EXTENSION);
            if(!farm.exists()){
                try {
                    farm.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            FarmSchematicHandler fc = new FarmSchematicHandler(plugin.getWorldEditPlugin(), player);
            try {
                Selection sel = plugin.getWorldEditPlugin().getSelection(player);
               
                fc.saveFarm(farm, sel.getMinimumPoint(), sel.getMaximumPoint());
                setFarm(farmID , new Farm(player.getLocation().toVector(), player.getWorld()), player);
                save();
                player.sendMessage(Message.farmCreated(farmID));
            } catch (Exception e) {
                player.sendMessage(Message.err);
                e.printStackTrace();
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page