Honestly I don't know and im sure there are. HOwever I recommend you to do a delayed task and remove the player in it after the time. Also, I recommend you to not store players in Hashmaps. Unless you remove them when they logoff.
Then I think it's alright?Not sure though xD Also for join events I recommend you to add a small delay. That's what I usually do XD
Added 1 sec delay on player join, still not working. Something funky is going on with the arraylist. Code: @EventHandler public void onJoin(final PlayerJoinEvent e){ final Player p = e.getPlayer(); this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() { public void run() { if(p.hasPermission("stick.fire")){ ps.add(p.getName()); } } }, 20L); }
Nope.. every time I shoot, it sends the cooldown message Code: p.sendMessage(ChatColor.RED+"The cooldown is on, please try again in 5 seconds!"); This doesnt really make any sense to me. Player joins the game, gets added to list Code: @EventHandler public void onJoin(final PlayerJoinEvent e){ final Player p = e.getPlayer(); this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() { public void run() { if(p.hasPermission("stick.fire")){ ps.add(p.getName()); } } }, 20L); } If player is in the list, and right clicks with a stick, it shoots a fireball Code: public void onInteract(final PlayerInteractEvent e){ final Player p = e.getPlayer(); if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){ if(e.getPlayer().getItemInHand().getType() == (Material.STICK)){ if(e.getPlayer().getInventory().contains(Material.SULPHUR)){ if(p.hasPermission("stick.fire")){ if(ps.contains(p.getName())){ Fireball fireball = e.getPlayer().launchProjectile(Fireball.class); fireball.setShooter(e.getPlayer()); fireball.setYield(7.5f); e.getPlayer().getInventory().removeItem(new ItemStack(Material.SULPHUR, 1)); e.getPlayer().updateInventory(); Also removes the player from the list Code: ps.remove(p); If the fireball is succesful (player was in the list, and had permission) it will trigger a delayed task, that will put the player back to the list after 5 seconds Code: this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() { public void run() { ps.add(p.getName()); } }, 100L); If player is not in the list (in this case, its caused by the cooldown, becouse player is not in the list yet) it will cancel the interact event, and send a message Code: @EventHandler public void onInteract2(PlayerInteractEvent e){ Player p = e.getPlayer(); if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){ if(e.getPlayer().getItemInHand().getType() == (Material.STICK)){ if(!(ps.contains(p))){ e.setCancelled(true); p.sendMessage(ChatColor.RED+"The cooldown is on, please try again in 5 seconds!"); } Very confused right now.
[quote uid=90572384 name="Vandrake" post=1380826]describe what happens. Does it shoot at least once? or what?[/quote] The fireball works.. i can spam the button and rape the land under me by shooting the ballz.. but not sure if the arraylists work, becouse the cooldown doesnt do anything. Oh, the permission works too. "every time I shoot, it sends the cooldown message" Here is the jar if you wanna test it <Edit by Moderator: Redacted mediafire url>
[quote uid=90712918 name="Woobie" post=1380840]The fireball works.. i can spam the button and rape the land under me by shooting the ballz.. but not sure if the arraylists work, becouse the cooldown doesnt do anything. Oh, the permission works too. "every time I shoot, it sends the cooldown message" Here is the jar if you wanna test it <Edit by Moderator: Redacted mediafire url> Can I see the source?I'll take al ook on my eclipse with my coding style
Yeah just a sec, trying another thing quickly. EDIT: Didnt work, this is what I tried Code: int cooldown = 60; if(!(ps.contains(p.getName()))) { e.setCancelled(true); long diff = (System.currentTimeMillis()); if (diff < cooldown) { p.sendMessage(ChatColor.RED + "You have to wait another "+(cooldown - diff)+" seconds to use this action!"); } }else{ ps.add(p.getName()); Vandrake Here is the code Code: package me.woobie.piratestick; import java.util.ArrayList; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Fireball; 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.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin implements Listener { public ArrayList <String> ps = new ArrayList <String> (); public void onEnable(){ System.out.println("[Piratestick] Plugin Enabled"); getServer().getPluginManager().registerEvents(this, this); } public void onDisable(){ System.out.println("[Piratestick] Plugin Disabled"); } @EventHandler public void onJoin(final PlayerJoinEvent e){ final Player p = e.getPlayer(); this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() { public void run() { if(p.hasPermission("stick.fire")){ ps.add(p.getName()); } } }, 20L); } @EventHandler public void onQuit(PlayerQuitEvent e){ Player p = e.getPlayer(); ps.remove(p.getName()); } @SuppressWarnings("deprecation") @EventHandler public void onInteract(final PlayerInteractEvent e){ final Player p = e.getPlayer(); if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){ if(e.getPlayer().getItemInHand().getType() == (Material.STICK)){ if(e.getPlayer().getInventory().contains(Material.SULPHUR)){ if(p.hasPermission("stick.fire")){ if(ps.contains(p.getName())){ Fireball fireball = e.getPlayer().launchProjectile(Fireball.class); fireball.setShooter(e.getPlayer()); fireball.setYield(7.5f); e.getPlayer().getInventory().removeItem(new ItemStack(Material.SULPHUR, 1)); e.getPlayer().updateInventory(); ps.remove(p); this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() { public void run() { ps.add(p.getName()); } }, 100L); } } else { p.sendMessage(ChatColor.RED+ "You dont have permission to perform this action!"); } } } else { e.getPlayer().sendMessage(ChatColor.RED+ "You need 1 gunpowder to shoot fireballs"); } } } @EventHandler public void onInteract2(PlayerInteractEvent e){ Player p = e.getPlayer(); if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){ if(e.getPlayer().getItemInHand().getType() == (Material.STICK)){ if(!(ps.contains(p))){ e.setCancelled(true); p.sendMessage(ChatColor.RED+"The cooldown is on, please try again in 5 seconds!"); } } } } } Pastebin version http://pastebin.com/6xLvFRBT
well I found 1 mistake. You need to check the {} coz I don't have a gunpowder and it stops there without message. anyways cehck that for now. I'm still testing.
Oops, I do have a message for it, but it was in the wrong place Code: e.getPlayer().sendMessage(ChatColor.RED+ "You need 1 gunpowder to shoot fireballs"); I'll fix that now.
ok it's done. Woobie Your problem is that you only add players to the array list on join event. That means if you reload the server it will delete everything in it and you need to relog. So here's my version in the pastebin. Remove my debug messages XD oops http://pastebin.com/aswAnQqP
oh you're right I forgot that XD hold on I found your error xD the arraylist is about Strings. Not Players. so ps.remove(p.getName()); line 67 EDIT: It works after I did what I wrote :3 have fun EDIT by Moderator: merged posts, please use the edit button instead of double posting.
[quote uid=90572384 name="Vandrake" post=1380909]I found your error xD the arraylist is about Strings. Not Players. so ps.remove(p.getName()); line 67 EDIT: It works after I did what I wrote :3 have fun[/quote] Sorry, I have to go. Could you test this for me? <Edit by Moderator: Redacted mediafire url>
[quote uid=90712918 name="Woobie" post=1380915]Sorry, I have to go. Could you test this for me? <Edit by Moderator: Redacted mediafire url> what did you do?It doesn't work XD
Not sure if I should've done this, butI changed the arraylist from <String> to <Player> and changed some of the ps.add stuff The problem is the on player join, it doesnt add the player to the list. The stick didnt work for me anymore, so that might mean i'm not in the list.
Why are you cancelling the event if they are not in the list. They will never be able to interact with anything whilst holding a stick. It's not necessary
Vandrake http://pastebin.com/HgmTyh4v This way you have no need for the onjoin and onquit event, and you can still open chests while holding a gun/stick EDIT: New link, tidied it up a bit EDIT2: I removed the rightclick block, so it only fires if your clicking air, made more sense to me but u can always re add it
Yeah, I wasnt excepting this to be so complicated. I earlier thought that cancelling the event would help, but no. Also, interacting by right clicking with stick doesnt do anything, so that is not a problem. EDIT: Chests, door etc of course, stupid me.
http://pastebin.com/YC63USHU Removed No permission message, this will only spam players who dont have permission.
Why are you not just using a HashMap<String, Interger> with playername and System.currentMillis() as value. Like this you just have to compare the current time with the HashMap's value of the playername. This is just the way I'm using delays.
Wont you still have to run a delayed task to check the hashmap? Oh wait, i see how that would work now.
Anything is good for me, that was just the way I tried doing it. EDIT by Moderator: merged posts, please use the edit button instead of double posting.