OK, I'm new hear,working with Threads

Discussion in 'Plugin Development' started by Anthony Pray, Jul 7, 2011.

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

    Anthony Pray

    Ok,Big edit.. I fixed the not running NullPointer.

    I am tryig to make a plugin that looks for instanceof Arrow in getEntities();
    I have crafted the Thread in the mane plugin class with


    in my
    Code:
    public class OddArrow extends JavaPlugin{
    public static OddArrow plugin;
    	public Thread Looper;
    PbEntityListener playerListener =  new PbEntityListener(this);
    	......
    	@Override
    	public void onEnable() {
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvent(Type.PLAYER_INTERACT, playerListener, Priority.Normal, this);
    		Looper = new Arrowlooper(getServer().getWorlds().get(0));
    		.....
    	}
    	void lookforarrows(Player Arrowshooter){
    		if (Looper.getState() == State.TERMINATED || Looper.getState() == State.NEW ){
    			Arrowshooter.sendMessage("Start looking for arrows");
    			Looper.run();
    		}else{
    			Arrowshooter.sendMessage(Looper.getState().toString());
    		}
    	}
    	public boolean onCommand (CommandSender sender, Command cmd, String commandLable, String[] args){
    		if (commandLable.equalsIgnoreCase("oa")){
    			Entity player = (Entity) sender;
    			List<org.bukkit.entity.Entity> arrowList = player.getNearbyEntities(500, 500, 500);
    			for(int i = 0; i<arrowList.size(); i++) {
    				Entity curr = arrowList.get(i);
    				if(curr instanceof Arrow){
    					org.bukkit.craftbukkit.entity.CraftArrow Arrow = (CraftArrow) curr;
    					sender.sendMessage("Arrow " + Arrow.getMomentum().toString());
    						player.getWorld().createExplosion(Arrow.getLocation(), (float) 1);
    						Arrow.remove();
    				}
    			}
    			sender.sendMessage("!!BOOM!!");
    			return true;
    		}
    		return false;
    	}
    
    public class PbEntityListener
    Code:
    public class PbEntityListener extends PlayerListener {
    	private OddArrow plugin;
    	public PbEntityListener(final OddArrow instantince) {
    		// TODO Auto-generated constructor stub
    		this.plugin = instantince;
    	}
    
    	public void onPlayerInteract (PlayerInteractEvent event)
    	{
    		if (event.getItem().getType() == Material.BOW){
    			plugin.lookforarrows(event.getPlayer());
    		}
        }
    }
    
    public class Arrowlooper
    Code:
    public class Arrowlooper extends  Thread {
    	World arrowShouter;
    List<org.bukkit.entity.Entity> arrowList;
    	public int SearchSize = 1000;
    	public boolean counter = false;
    	public final Logger logger = Logger.getLogger("Minecraft");
    	public Arrowlooper(World instantince) {
    		arrowShouter =  instantince;
    		counter = true;
    	}
    
    	public void run() {
    		int thiscounter = 0;
    		this.logger.info("[OddArrow] is looping!");
    		while(counter == true)
    		{
    			thiscounter++;
    			try {
    				sleep(1);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			if (thiscounter >1000 ) {
    				counter = false;
    				this.logger.info("[OddArrow] is notfound!" + arrowList.toString());
    			}
    			arrowList = arrowShouter.getEntities();
    			for(int i = 0; i<arrowList.size(); i++) {
    				Entity curr = arrowList.get(i);
    				if(curr instanceof Arrow){
    					this.logger.info("Arrow...");
    					//double temp1 = curr.getLocation().getY();
    					//double temp2 = curr.getLocation().getY();
    					//if (temp1 == temp2){
    					//	arrowShouter.getWorld().createExplosion(curr.getLocation(), (float) 1);
    					//	curr.remove();
    					//	counter = false;
    					//}
    				}
    	        }
    		}
    		this.logger.info("[OddArrow] is Finihed!");
    	}
    }
    
    When i call /oa the arrowList in public boolean onCommand() contantes CraftArrow
    When void lookforarrows() is called the arrowList dusent have CraftArrow?
    When void lookforarrows() is called for the first time arrowList dues not have CraftArrow?
    but seems to have all outer Enterys.

    the PbEntityListener locks tell Looper.run(); finished.
    thus not crating the arrow.

    if you shoot a nother arrow it finds the fist but not the new one.
    how do i call lookforarrows() affter PbEntityListener() ends?

    Code:
    [INFO] [OddArrow] is looping!
    [INFO] [OddArrow] is notfound![CraftPlayer{name=odwdinc}, CraftSquid, CraftCow, CraftChicken, CraftSquid, CraftChicken, CraftSquid, CraftSquid, CraftSquid, CraftChicken, CraftCow, CraftCow, CraftCow, CraftChicken, CraftChicken, CraftSquid, CraftPig, CraftChicken, CraftChicken, CraftPig, CraftSquid, CraftSquid, CraftSquid, CraftSquid, CraftSquid, CraftSheep, CraftCow, CraftCow, CraftCow, CraftPig, CraftCow, CraftCow, CraftSheep]
    [INFO] [OddArrow] is Finihed!
    
    why is the Arrowlooper thread not able to see CraftArrow class?
     
Thread Status:
Not open for further replies.

Share This Page