Development Assistance Performance improvement

Discussion in 'Plugin Help/Development/Requests' started by ScorixEar, May 3, 2016.

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

    ScorixEar

    Hi guys,
    I wrote a plugin, which sets a wool lane under the player, if he moves. So, 3 woolblocks in front of the player and 3 woolblocks behind the player and 3 directly under the player will be set with different colors dependent on yaw. Also all Woolblocks above, directly in front of the player and directly behind the player will be set to air. In the end, all woolblocks, set as path will be removed after 5 seconds.

    My pc-server (offline) does not come afterwards with setting and removing blocks by one player and I want to ask you, if you have any ideas, to make this not so performance heavy
    Thank you, Paul :)

    Code:
    import de.minecrafthaifl.nyanfighters.Nyanfighters;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    
    
    /**
    * Created by Paul on 02.05.2016.
    */
    public class MoveListener implements Listener
    {
    
        @EventHandler
         public void onMove(PlayerMoveEvent e)
         {
             Player p = e.getPlayer();
    
             Location l= e.getTo();
            if(Nyanfighters.getInstance().getGame())
            {
                if(l.getPitch()>55)
                {
                    setAir("Start", 0,0,l,4);
                }
                else
                {
                    setBlocks("Start", 0,0,l,4);
                }
    
                if((l.getYaw()<=-45&&l.getYaw()>-135.0)||(l.getYaw()<=305&&l.getYaw()>215))
                {
                    //Bukkit.broadcastMessage("-135<x<=-45");
                    if(e.getPlayer().getLocation().getPitch()>55)
                    {
                        setAir("left"      ,0,-1,l,1);
                        setAir("lefttwice" ,0,-2,l,14);
                        setAir("right"     ,0,1,l,5);
                        setAir("righttwice",0,2,l,9);
    
                        setAir("front"          ,1,0,l,4);
                        setAir("frontleft"      ,1,-1,l,1);
                        setAir("frontlefttwice" ,1,-2,l,14);
                        setAir("frontright"     ,1,1,l,5);
                        setAir("frontrighttwice",1,2,l,9);
    
                        setAir("back"          ,-1,0,l,4);
                        setAir("backleft"      ,-1,-1,l,1);
                        setAir("backlefttwice" ,-1,-2,l,14);
                        setAir("backright"     ,-1,1,l,5);
                        setAir("backrighttwice",-1,2,l,9);
                    }
                    else if(l.getPitch()<-50)
                    {
    
                    }
                    else
                    {
                        setBlocks("left"      ,0,-1,l,1);
                        setBlocks("lefttwice" ,0,-2,l,14);
                        setBlocks("right"     ,0,1,l,5);
                        setBlocks("righttwice",0,2,l,9);
    
                        setBlocks("front"          ,1,0,l,4);
                        setBlocks("frontleft"      ,1,-1,l,1);
                        setBlocks("frontlefttwice" ,1,-2,l,14);
                        setBlocks("frontright"     ,1,1,l,5);
                        setBlocks("frontrighttwice",1,2,l,9);
    
                        setBlocks("back"          ,-1,0,l,4);
                        setBlocks("backleft"      ,-1,-1,l,1);
                        setBlocks("backlefttwice" ,-1,-2,l,14);
                        setBlocks("backright"     ,-1,1,l,5);
                        setBlocks("backrighttwice",-1,2,l,9);
                    }
    
    
                }
                else if((l.getYaw()<=-305||(l.getYaw()>-45&&l.getYaw()<=0))||((l.getYaw()<=45&&l.getYaw()>=0)||l.getYaw()>305))
                {
                    if(e.getPlayer().getLocation().getPitch()>55)
                    {
                        setAir("left"      ,1,0,l,1);
                        setAir("lefttwice" ,2,0,l,14);
                        setAir("right"     ,-1,0,l,5);
                        setAir("righttwice",-2,0,l,9);
    
                        setAir("front"          ,0,+1,l,4);
                        setAir("frontleft"      ,1,+1,l,1);
                        setAir("frontlefttwice" ,2,+1,l,14);
                        setAir("frontright"     ,-1,+1,l,5);
                        setAir("frontrighttwice",-2,+1,l,9);
    
                        setAir("back"          ,0,-1,l,4);
                        setAir("backleft"      ,1,-1,l,1);
                        setAir("backlefttwice" ,2,-1,l,14);
                        setAir("backright"     ,-1,-1,l,5);
                        setAir("backrighttwice",-2,-1,l,9);
                    }
                    else
                    {
                        setBlocks("left"      ,1,0,l,1);
                        setBlocks("lefttwice" ,2,0,l,14);
                        setBlocks("right"     ,-1,0,l,5);
                        setBlocks("righttwice",-2,0,l,9);
    
                        setBlocks("front"          ,0,+1,l,4);
                        setBlocks("frontleft"      ,1,+1,l,1);
                        setBlocks("frontlefttwice" ,2,+1,l,14);
                        setBlocks("frontright"     ,-1,+1,l,5);
                        setBlocks("frontrighttwice",-2,+1,l,9);
    
                        setBlocks("back"          ,0,-1,l,4);
                        setBlocks("backleft"      ,1,-1,l,1);
                        setBlocks("backlefttwice" ,2,-1,l,14);
                        setBlocks("backright"     ,-1,-1,l,5);
                        setBlocks("backrighttwice",-2,-1,l,9);
                    }
                    //Bukkit.broadcastMessage("-45<x<=45");
                }
                else if((l.getYaw()<=-215&&l.getYaw()>-305)||(l.getYaw()<=135&&l.getYaw()>45))
                {
                    if(e.getPlayer().getLocation().getPitch()>55)
                    {
                        setAir("left"      ,0,1,l,1);
                        setAir("lefttwice" ,0,2,l,14);
                        setAir("right"     ,0,-1,l,5);
                        setAir("righttwice",0,-2,l,9);
    
                        setAir("front"          ,-1,0,l,4);
                        setAir("frontleft"      ,-1,1,l,1);
                        setAir("frontlefttwice" ,-1,2,l,14);
                        setAir("frontright"     ,-1,-1,l,5);
                        setAir("frontrighttwice",-1,-2,l,9);
    
                        setAir("back"          ,1,0,l,4);
                        setAir("backleft"      ,1,1,l,1);
                        setAir("backlefttwice" ,1,2,l,14);
                        setAir("backright"     ,1,-1,l,5);
                        setAir("backrighttwice",1,-2,l,9);
                    }
                    else
                    {
                        setBlocks("left"      ,0,1,l,1);
                        setBlocks("lefttwice" ,0,2,l,14);
                        setBlocks("right"     ,0,-1,l,5);
                        setBlocks("righttwice",0,-2,l,9);
    
                        setBlocks("front"          ,-1,0,l,4);
                        setBlocks("frontleft"      ,-1,1,l,1);
                        setBlocks("frontlefttwice" ,-1,2,l,14);
                        setBlocks("frontright"     ,-1,-1,l,5);
                        setBlocks("frontrighttwice",-1,-2,l,9);
    
                        setBlocks("back"          ,1,0,l,4);
                        setBlocks("backleft"      ,1,1,l,1);
                        setBlocks("backlefttwice" ,1,2,l,14);
                        setBlocks("backright"     ,1,-1,l,5);
                        setBlocks("backrighttwice",1,-2,l,9);
                    }
                    //Bukkit.broadcastMessage("45<x<=135");
    
                }
                else
                {
                    //Bukkit.broadcastMessage("135<x<=180");
                    if(e.getPlayer().getLocation().getPitch()>55)
                    {
                        //Bukkit.broadcastMessage("AIR");
                        setAir("left"      ,-1,0,l,1);
                        setAir("lefttwice" ,-2,0,l,14);
                        setAir("right"     ,1,0,l,5);
                        setAir("righttwice",2,0,l,9);
    
                        setAir("front"          ,0,-1,l,4);
                        setAir("frontleft"      ,-1,-1,l,1);
                        setAir("frontlefttwice" ,-2,-1,l,14);
                        setAir("frontright"     ,1,-1,l,5);
                        setAir("frontrighttwice",2,-1,l,9);
    
                        setAir("back"          ,0,1,l,4);
                        setAir("backleft"      ,-1,1,l,1);
                        setAir("backlefttwice" ,-2,1,l,14);
                        setAir("backright"     ,1,1,l,5);
                        setAir("backrighttwice",2,1,l,9);
                    }
                    else
                    {
                        setBlocks("left"      ,-1,0,l,1);
                        setBlocks("lefttwice" ,-2,0,l,14);
                        setBlocks("right"     ,1,0,l,5);
                        setBlocks("righttwice",2,0,l,9);
    
                        setBlocks("front"          ,0,-1,l,4);
                        setBlocks("frontleft"      ,-1,-1,l,1);
                        setBlocks("frontlefttwice" ,-2,-1,l,14);
                        setBlocks("frontright"     ,1,-1,l,5);
                        setBlocks("frontrighttwice",2,-1,l,9);
    
                        setBlocks("back"          ,0,1,l,4);
                        setBlocks("backleft"      ,-1,1,l,1);
                        setBlocks("backlefttwice" ,-2,1,l,14);
                        setBlocks("backright"     ,1,1,l,5);
                        setBlocks("backrighttwice",2,1,l,9);
                    }
    
                }
                //Bukkit.broadcastMessage("Pitch:"+l.getPitch());
                //Bukkit.broadcastMessage("First: "+(e.getPlayer().getLocation().getPitch()<18));
                //Bukkit.broadcastMessage("Second: "+(l.getYaw()>-45));
                //Bukkit.broadcastMessage("Combined: "+(l.getYaw()<=-305||l.getYaw()>-45));
            }
    
    
        }
    
    
        public void setBlocks(String s, int x, int z, Location start, int id ) {
            Location left = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY() -1, start.getBlockZ() + z);
            Location leftair = new Location(start.getWorld(), start.getBlockX()+x, start.getBlockY(), start.getBlockZ()+z);
            if(leftair.getBlock().getType()==Material.WOOL)
            {
                leftair.getBlock().setType(Material.AIR);
            }
            Location leftair2 = new Location(start.getWorld(), start.getBlockX()+x, start.getBlockY()+1, start.getBlockZ()+z);
            if(leftair2.getBlock().getType()==Material.WOOL)
            {
                leftair2.getBlock().setType(Material.AIR);
            }
            Location leftair3 = new Location(start.getWorld(), start.getBlockX()+x, start.getBlockY()+2, start.getBlockZ());
            if(leftair3.getBlock().getType()==Material.WOOL)
            {
                leftair3.getBlock().setType(Material.AIR);
            }
            Material block = left.getBlock().getType();
            byte data =left.getBlock().getData();
            if(block==Material.AIR||block==Material.WOOL)
            {
                left.getBlock().setType(Material.WOOL);
                left.getBlock().setData((byte) id);
                removeBlock(start, left, x, -1,z,block, data);
    
            }
    
        }
        public void setAir(String s, int x, int z, Location start, int id) {
    
            Location left = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY() - 1, start.getBlockZ() + z);
            if (left.getBlock().getType() == Material.WOOL)
                left.getBlock().setType(Material.AIR);
            Location leftair = new Location(start.getWorld(), start.getBlockX()+x, start.getBlockY(), start.getBlockZ()+z);
            if(leftair.getBlock().getType()==Material.WOOL)
            {
                leftair.getBlock().setType(Material.AIR);
            }
            Location leftair2 = new Location(start.getWorld(), start.getBlockX()+x, start.getBlockY()+1, start.getBlockZ()+z);
            if(leftair2.getBlock().getType()==Material.WOOL)
            {
                leftair2.getBlock().setType(Material.AIR);
            }
            Location leftair3 = new Location(start.getWorld(), start.getBlockX()+x, start.getBlockY()+2, start.getBlockZ());
            if(leftair3.getBlock().getType()==Material.WOOL)
            {
                leftair3.getBlock().setType(Material.AIR);
            }
            Location right = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY() - 2, start.getBlockZ() + z);
            Material block = right.getBlock().getType();
            byte data = right.getBlock().getData();
            if (block == Material.AIR) {
                right.getBlock().setType(Material.WOOL);
                right.getBlock().setData((byte) id);
                removeBlock(start, right, x, -2,z,block, data);
    
    
            }
        }
        public void removeBlock(Location start,Location remove, int x, int y, int z, Material block, byte data)
        {
            Bukkit.getScheduler().scheduleSyncDelayedTask(Nyanfighters.getInstance(), new Runnable() {
                @Override
                public void run() {
                    //Location right = new Location(start.getWorld(), start.getBlockX()+x, start.getBlockY()+y, start.getBlockZ()+z);
                    //Bukkit.broadcastMessage("starting to remove");
                    //if(!(start.getWorld().getName().equals(remove.getWorld().getName())&&(start.getBlockX()+x)==remove.getBlockX()&&(start.getBlockY()+y)==remove.getBlockY()&&(start.getBlockZ()+z)==remove.getBlockZ()))
                    //{
                       // Bukkit.broadcastMessage("removed");
                        remove.getBlock().setType(Material.AIR);
    
                    //}
                    /*else
                    {
                        Bukkit.broadcastMessage("NOP :(");
                        Bukkit.getScheduler().cancelTasks(Nyanfighters.getInstance());
                        removeBlock(start, remove, x, y, z, block, data);
                    }*/
                }
            },20*5);
        }
    
    
    }
    
     
    Last edited: May 4, 2016
  2. Offline

    ScorixEar

    #push if this is allowed
     
Thread Status:
Not open for further replies.

Share This Page