Util Simple schematics

Discussion in 'Resources' started by Plugers11, Jan 6, 2015.

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

    Plugers11

    If u have any problems say

    This is the code:

    Code:
    
    public class Frst extends JavaPlugin implements Listener{
     
        public Location first;
        public Location second;
     
        public List<String> locations = new ArrayList<String>();
     
        public void onEnable(){
            getCommand("saveTest").setExecutor(this);
            getCommand("loadTest").setExecutor(this);
            Bukkit.getPluginManager().registerEvents(this, this);
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args) {
            if(!(sender instanceof Player)){
                return false;
            }
            Player p = (Player) sender;
         
            if(command.getName().equalsIgnoreCase("saveTest")){
                saveCalculate(first, second, p.getLocation());
            }else if(command.getName().equalsIgnoreCase("loadTest")){
                loadCalculate(p);
            }
         
            return true;
        }
     
        @EventHandler
        public void onClick(PlayerInteractEvent e){
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                first = e.getClickedBlock().getLocation();
            }else if(e.getAction() == Action.LEFT_CLICK_BLOCK){
                second = e.getClickedBlock().getLocation();
            }
        }
     
        public void loadCalculated(Player p){
            for(String calculated : locations){
                String[] calSplit = calculated.split("#");
                for(String calSplited : calSplit){
                    String[] infoSplit = calSplited.split(";");
                    int xDi = Integer.parseInt(infoSplit[0]);
                    int yDi = Integer.parseInt(infoSplit[1]);
                    int zDi = Integer.parseInt(infoSplit[2]);
                    Material m = Material.valueOf(infoSplit[3]);
                 
                    int coordX = p.getLocation().getBlockX() + xDi;
                    int coordY = p.getLocation().getBlockY() + yDi;
                    int coordZ = p.getLocation().getBlockZ() + zDi;
                 
                    p.getWorld().getBlockAt(new Location(p.getWorld(), coordX, coordY, coordZ)).setType(m);;
                }
            }
        }
     
        public void saveCalculate(Location loc1, Location loc2, Location player){
            List<Block> blocks = new ArrayList<Block>();
          
            int topBlockX = (loc1.getBlockX() < loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
            int bottomBlockX = (loc1.getBlockX() > loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
            int topBlockY = (loc1.getBlockY() < loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
            int bottomBlockY = (loc1.getBlockY() > loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
            int topBlockZ = (loc1.getBlockZ() < loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
            int bottomBlockZ = (loc1.getBlockZ() > loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
         
            for(int x = bottomBlockX; x <= topBlockX; x++)
            {
                for(int z = bottomBlockZ; z <= topBlockZ; z++)
                {
                    for(int y = bottomBlockY; y <= topBlockY; y++)
                    {
                        int distanceX = 0;
                        int distanceY = 0;
                        int distanceZ = 0;
                        Block block = loc1.getWorld().getBlockAt(x, y, z);
                     
                        int blockX = block.getLocation().getBlockX();
                        int blockY = block.getLocation().getBlockY();
                        int blockZ = block.getLocation().getBlockZ();
                     
                        int pX = player.getBlockX();
                        int pY = player.getBlockY();
                        int pZ = player.getBlockZ();
                     
                        if(pX > blockX){
                            distanceX = -(pX - blockX);
                        }
                        if(pY > blockY){
                            distanceY = -(pY - blockY);
                        }
                        if(pZ > blockZ){
                            distanceZ = -(pZ - blockZ);
                        }
                     
                     
                        if(pX < blockX){
                            distanceX = blockX - pX;
                        }
                        if(pX < blockY){
                            distanceY = blockY - pY;
                        }
                        if(pZ < blockZ){
                            distanceZ = blockZ - pZ;
                        }
                     
                        locations.add(distanceX + ";" + distanceY + ";" + distanceZ + ";" + block.getType() + "#");
                     
                        blocks.add(block);
                    }
                }
            }
        }
     
    }
    
     
    Ganga and xXBeLkAXx like this.
  2. Offline

    xXBeLkAXx

    Plugers11 likes this.
  3. Offline

    Plugers11

    @xXBeLkAXx
    Your welcome :)

    You need to change getting and settings locations in click block :_:
    Cuz it's little buggy idk why :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  4. Offline

    Skionz

    @Plugers11 Isn't this just temporarily copying an area of blocks? I believe a schematic is an area of blocks that are saved in a file.
     
  5. Offline

    Plugers11

    @Skionz
    Working on it
    Maybe in next weekend i will upload it

    I need now to repair Y coordinate bug.
    Also
    Do u want copy and paste without adding air to schematic? ?

    And there it is !!

    Code:

    Code:
    import static yourpackage.classMain.map;
    import static yourpackage.classMain.locations;
    public class UtilSchematics implements Listener, CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args) {
            if(!(sender instanceof Player)){
                return false;
            }
            Player p = (Player) sender;
      
            if(command.getName().equalsIgnoreCase("saveTest")){
                    List<Block> blocks = new ArrayList<Block>();
                
                locations.clear();
            
                    Location first = map.get("first");
                    Location second = map.get("second");
                
                    int topBlockX = (first.getBlockX() < second.getBlockX() ? second.getBlockX() : first.getBlockX());
                    int bottomBlockX = (first.getBlockX() > second.getBlockX() ? second.getBlockX() : first.getBlockX());
          
                    int topBlockY = (first.getBlockY() < second.getBlockY() ? second.getBlockY() : first.getBlockY());
                    int bottomBlockY = (first.getBlockY() > second.getBlockY() ? second.getBlockY() : first.getBlockY());
          
                    int topBlockZ = (first.getBlockZ() < second.getBlockZ() ? second.getBlockZ() : first.getBlockZ());
                    int bottomBlockZ = (first.getBlockZ() > second.getBlockZ() ? second.getBlockZ() : first.getBlockZ());
                
                    for(int x = bottomBlockX; x <= topBlockX; x++)
                    {
                        for(int z = bottomBlockZ; z <= topBlockZ; z++)
                        {
                            for(int y = bottomBlockY; y <= topBlockY; y++)
                            {
                             int distanceX = 0;
                             int distanceY = 0;
                             int distanceZ = 0;
                                Block block = first.getWorld().getBlockAt(x, y, z);
                            
                                int blockX = block.getLocation().getBlockX();
                                int blockY = block.getLocation().getBlockY();
                                int blockZ = block.getLocation().getBlockZ();
                            
                                int pX = p.getLocation().getBlockX();
                                int pY = p.getLocation().getBlockY();
                                int pZ = p.getLocation().getBlockZ();
                            
                                if(pX > blockX){
                                 distanceX = -(pX - blockX);
                                }
                                if(pY > blockY){
                                 distanceY = -(pY - blockY);
                                }
                                if(pZ > blockZ){
                                 distanceZ = -(pZ - blockZ);
                                }
                            
                            
                                if(pX < blockX){
                                 distanceX = blockX - pX;
                                }
                                if(pX < blockY){
                                 distanceY = blockY - pY;
                                }
                                if(pZ < blockZ){
                                 distanceZ = blockZ - pZ;
                                }
                            
                                p.sendMessage(distanceX + ";" + distanceY + ";" + distanceZ + ";" + block.getType() + "#");
                            
                                locations.add(distanceX + ";" + distanceY + ";" + distanceZ + ";" + block.getType() + "#");
                            
                                blocks.add(block);
                            }
                        }
                    }
            }else if(command.getName().equalsIgnoreCase("loadTest")){
                loadCalculated(p);
            }
      
            return true;
        }
        @EventHandler
        public void onClick(PlayerInteractEvent e){
            if(e.getPlayer().getItemInHand().getType() == Material.BED) {
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                    e.getPlayer().sendMessage("1");
                map.put("first", e.getClickedBlock().getLocation());
            }else if(e.getAction() == Action.LEFT_CLICK_BLOCK){
                    e.getPlayer().sendMessage("2");
                map.put("second", e.getClickedBlock().getLocation());
            }
            }
        }
        public void loadCalculated(Player p){
            p.sendMessage(locations.size() + "");
            for(String calculated : locations){
                    p.sendMessage(calculated.toString());
                String[] calSplit = calculated.split("#");
                for(String calSplited : calSplit){
                    String[] infoSplit = calSplited.split(";");
                    int xDi = Integer.parseInt(infoSplit[0]);
                    int yDi = Integer.parseInt(infoSplit[1]);
                    int zDi = Integer.parseInt(infoSplit[2]);
                    Material m = Material.valueOf(infoSplit[3]);
              
                    int coordX = p.getLocation().getBlockX() + xDi;
                    int coordY = p.getLocation().getBlockY() + yDi;
                    int coordZ = p.getLocation().getBlockZ() + zDi;
              
                    p.getWorld().getBlockAt(new Location(p.getWorld(), coordX, coordY, coordZ)).setType(m);
                
                    p.sendMessage(coordY + "");
                }
            }
        }
    }
    Main class:

    Code:
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Jav extends JavaPlugin{
    
        public static Map<String, Location> map = new HashMap<>();
        public static List<String> locations = new ArrayList<String>();
    
        public void onEnable(){
            getCommand("saveTest").setExecutor(new Frst());
            getCommand("loadTest").setExecutor(new Frst());
            Bukkit.getPluginManager().registerEvents(new Frst(), this);
        }
    
    }
    Also
    Change imports
    +
    Tomorrow will be saving and loading :)
    In next weekend will be undo :p [And pasting without air :D]

    EDIT by Timtower: merged double post
     
    Last edited: Jan 7, 2015
    Skionz likes this.
  6. Offline

    ChipDev

    Naming conventions please.
    And I hope java does not allow 'FuckingNiceUtil' :p
     
  7. Offline

    Hex_27

  8. @ChipDev I'm not sure what convention you're referring to. So far as the official Java naming convention goes, there's not technically much wrong with "FuckingNiceUtil"... although that doesn't mean it's not a terrible choice for a name that should be immediately changed.


    @Plugers11 I've not had a properly look at this, but one thing I'd like to point out is that this is distinctly lacking in encapsulation and has needless static fields.
     
    ChipDev likes this.
  9. Offline

    Plugers11

    @AdamQpzm
    Sorry i @xXBeLkAXx helping me and he change name to this :D
    Today after 7 pm i will probably show code to put all to files
     
  10. Offline

    Plugers11

    Sorry for no updating code but i don't have so much time :/
     
Thread Status:
Not open for further replies.

Share This Page