[REQ] Pirate Stick

Discussion in 'Archived: Plugin Requests' started by RumbleIV, Oct 18, 2012.

  1. Offline

    Vandrake

    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.
     
  2. Offline

    Woobie

    Arraylist* :D
    Also.. didnt work, not sure what the problem is
     
  3. Offline

    Vandrake

    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
     
  4. Offline

    Woobie

    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);
            }
     
  5. Offline

    Vandrake

    erm...doesn't the second interact need an EventHandler?
     
  6. Offline

    Woobie

    oops XD
     
  7. Offline

    Vandrake

    is it fixed?
     
  8. Offline

    Woobie

    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.
     
  9. Offline

    Vandrake

    describe what happens. Does it shoot at least once? or what?
     
  10. Offline

    Woobie

    [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>
     
    Last edited by a moderator: Nov 9, 2016
  11. Offline

    Vandrake

    [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
     
    Last edited by a moderator: Nov 9, 2016
  12. Offline

    Woobie

    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
     
  13. Offline

    Vandrake

    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.
     
  14. Offline

    Woobie

    Oops, I do have a message for it, but it was in the wrong place :D
    Code:
    e.getPlayer().sendMessage(ChatColor.RED+ "You need 1 gunpowder to shoot fireballs");
    I'll fix that now.
     
  15. Offline

    Vandrake

    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
     
  16. Offline

    Woobie

    I know I had that problem.. but how does that have anything to do with the delay?
     
  17. Offline

    Vandrake

    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.
     
    Last edited by a moderator: May 29, 2016
  18. Offline

    Woobie

    [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>
     
    Last edited by a moderator: Nov 9, 2016
  19. Offline

    Vandrake

    [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
     
    Last edited by a moderator: Nov 9, 2016
  20. Offline

    Woobie

    Not sure if I should've done this, butI changed the arraylist from <String> to <Player> and changed some of the ps.add stuff :D

    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.
     
  21. Offline

    Vandrake

    I'll just show you my code. :3
    http://pastebin.com/sQrbJSDy
     
  22. Offline

    the_merciless

    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
     
  23. Offline

    RumbleIV

    holy craps. went from 5 lines to BOOM.
     
  24. Offline

    the_merciless

    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
     
  25. Offline

    Woobie

    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.
     
  26. Offline

    the_merciless

  27. Offline

    Postkutsche

    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.
     
  28. Offline

    Vandrake

    ye and its the easiest too lol but he wants to use it like this so I won't change that
     
  29. Offline

    the_merciless

    Wont you still have to run a delayed task to check the hashmap?

    Oh wait, i see how that would work now.
     
  30. Offline

    Woobie

    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.
     
    Last edited by a moderator: May 29, 2016

Share This Page