Solved The stupidest bukkit question ever.

Discussion in 'Plugin Development' started by DrTURTLE2, Aug 15, 2013.

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

    DrTURTLE2

    So how would I define a Player to p in this event handler?

    Code:java
    1.  
    2. @EventHandler
    3. public void onEntityDamage(EntityDamageByEntityEvent e) {
    4. if (e.getDamager() instanceof Fireball) {
    5. Fireball f = (Fireball) e.getDamager();
    6. if (Shooter.contains(Bukkit.getName()));
    7. if (f.getShooter() instanceof Player) {
    8. Player shooter = (Player) f.getShooter();
    9. if (shooter.getItemInHand().getType() == Material.IRON_SPADE) {
    10. e.setDamage(10.0);
    11. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    12. public void run() {
    13. Cooldown.remove(p);
    14. }
    15. }, 100);
     
  2. Offline

    Joager

    like, you want the shooter to be P?

    if that's the case, just do as below. dunno tho, i'm kinda new in this thing.
    Player p = (Player) f.getShooter();
     
  3. Offline

    DrTURTLE2

    Yes I do want the shooter to be p.

    That doesn't work though.

    thanks for trying.
     
  4. Offline

    PreFiXAUT

    Joager
    The shooter is allready the exact same thing.
     
  5. Offline

    xTrollxDudex

    DrTURTLE2
    You have a semicolon after the if statement
     
  6. Offline

    DrTURTLE2

  7. Offline

    xTrollxDudex

    DrTURTLE2
     
  8. Offline

    DrTURTLE2

    Okay, bit off topic. How would I make it have like a cooldown of 10 seconds before the player can shoot again? xTrollxDudex
     
  9. Offline

    xTrollxDudex

    DrTURTLE2
    I this like a fireball shooting thing? Because when the fireball is fired, add the player to the list and start a scheduler to remove them after time.
     
  10. Offline

    DrTURTLE2

    Im not asking you too feed me code, but could you walk me through this? xTrollxDudex
     
  11. Offline

    xTrollxDudex

    DrTURTLE2
    How is the fireball being shot? What are you doing so that the fire ball hits a player?
     
  12. Offline

    DrTURTLE2

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    4.  
    5. if (!(e.getItem().getType() == Material.IRON_SPADE)) return;
    6.  
    7. Fireball f = e.getPlayer().launchProjectile(Fireball.class);
    8. f.setIsIncendiary(false);
    9. f.setYield(0);
    10. }

    xTrollxDudex
     
  13. Offline

    xTrollxDudex

    DrTURTLE2
    Create an ArrayList, lets call it players. It will contain String. Then in the event, you would add the player name to the list. Copy paste 11-15 in the OP after that, replace "Cooldown" with "players" since the ArrayList name is players. This assumes that you have this in the main class
     
  14. Offline

    Quantix

    Are you trying to add the cool-down after the fireball hits a target or right after the player shoots it? In either scenario you would put their name and the time when they can shoot the fireball again in a HashMap and every time they try to shoot check if they are allowed to by doing this:
    Code:java
    1. public HashMap<String, Long> cooldown = new HashMap<String, Long>();
    This next code is inserted into the part where you have the player shoot the fireball:
    Code:java
    1. //When the player tries to shoot a fireball
    2. if (cooldown.containsKey(player.getName())) {
    3. long removeCooldown = cooldown.get(player.getName());
    4. if (removeCooldown >= System.currentTimeMillis()) {
    5. //Don't shoot fireball
    6. player.sendMessage("You have to wait a little to use this feature again!");
    7. } else {
    8. //Shoot the fireball
    9. cooldown.put(player.getName(), (System.currentTimeMillis() + (1000 * 10)));
    10. }
    11. } else {
    12. //Shoot the fireball
    13. cooldown.put(player.getName(), (System.currentTimeMillis() + (1000 * 10)));
    14. }
     
  15. Offline

    DrTURTLE2

    Okay how would I add the player name to the list when they launch the fireball?

    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageByEntityEvent e) {
    3. if (e.getDamager() instanceof Fireball) {
    4. Fireball f = (Fireball) e.getDamager();
    5. if (Shooter.contains(Bukkit.getName())) {
    6. if (f.getShooter() instanceof Player) {
    7. Player shooter = (Player) f.getShooter();
    8. Player player = (Player) player.getPlayer();
    9. if (cooldown.containsKey(player.getName())) {
    10. long removeCooldown = cooldown.get(player.getName());
    11. if (removeCooldown >= System.currentTimeMillis()) {
    12. player.sendMessage(ChatColor.RED + "You have to wait to do this again");
    13. }
    14. }else{
    15.  
    16. if (shooter.getItemInHand().getType() == Material.IRON_SPADE) {
    17. e.setDamage(10.0);
    18. cooldown.put(player.getName(), (System.currentTimeMillis() + (1000 * 10)));
    19.  
    20. }else{
    21. if (shooter.getItemInHand().getType() == Material.IRON_SPADE) {
    22. e.setDamage(10.0);
    23. cooldown.put(player.getName(), (System.currentTimeMillis() + (1000 * 10)));
    24. }
    25.  

    This is code, doesn't work
    Quantix

    Bump

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

    Door Knob

    Make a BukkitRunnable or a scheduler. When the player shoots the fireball, add it to the List/Map/Whatever.
    Kinda like this:

    Code:java
    1.  
    2. //when the player shoots a fireball do this:
    3. players.add(player.getName);
    4. new BukkitRunnable(){
    5. public void run(){
    6. players.remove(player.getName);
    7. }
    8. }.runTaskLater(this, 200); //this, assuming your class extends JavaPlugin. 200 is 20x[seconds you want]. 200 ticks is 10 seconds.
    9.  
    10. //each time he fires, check if players.contains(player.getName);

    I recommend you use a HashSet. When making it, simply do this:
    Code:java
    1. HashSet<String> players = new HashSet<String>();

    It should work fine now.
     
  17. Offline

    DrTURTLE2

    Door Knob

    Okay to get this to work I have to define the player, which still isn't working.
     
  18. Offline

    Door Knob

    What do you mean?

    Player player = (Player)e.getDamager();
     
  19. Offline

    Muffin89

    This is far far away not even close to the most stupid question on the bukkit forums.
     
  20. Offline

    DrTURTLE2

    I fixed it, but how would I send the player a message if he tries to right click again while hes on cooldown saying "You cannot do this again?"


    It bounced off. lol

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

    Door Knob

    When checking if player is in the players HashSet do this:
    Code:java
    1. if players.contains(player){
    2. player.sendMessage("You have to wait 10 seconds!");
    3. } else {
    4. //shooting stuff
    5. }
    6.  
     
  22. Offline

    DrTURTLE2

    Door Knob
    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageByEntityEvent e) {
    3. if (e.getDamager() instanceof Fireball) {
    4. Fireball f = (Fireball) e.getDamager();
    5. if (Shooter.contains(Bukkit.getName())) {
    6. if (f.getShooter() instanceof Player) {
    7. final Player player = (Player)e.getDamager();
    8. Player shooter = (Player) f.getShooter();
    9. if (shooter.getItemInHand().getType() == Material.IRON_SPADE) {
    10. e.setDamage(10.0);
    11.  
    12. players.add(player.getName());
    13. new BukkitRunnable(){
    14. public void run(){
    15. players.remove(player.getName());
    16. }
    17. }.runTaskLater(this, 200);
    18. } else {
    19. if players.contains(player){
    20. player.sendMessage("You have to wait 10 seconds!");
    21.  

    This is my code, doesn't work.

    Thanks for your continuous help.
     
  23. Offline

    Door Knob

    Is that all of the code? Because it's missing lots of things. What's Shooter? Why do you use Bukkit.getName()? Also, the shooting code should be when the HashSet does NOT contain player.getName();. Also, what's not working? Can you please elaborate?
     
  24. Offline

    DrTURTLE2


    This is the shooting part.
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    4.  
    5. if (!(e.getItem().getType() == Material.IRON_SPADE)) return;
    6.  
    7. Fireball f = e.getPlayer().launchProjectile(Fireball.class);
    8. f.setIsIncendiary(false);
    9. f.setYield(0);
    10.  
    11.  
    12.  
    13.  

    I used bukkit.getName because I couldn't figure out how to define the player at the start.
    Edit: I changed to player.getName, thanks
     
  25. Offline

    Door Knob

    Shouldn't the BukkitRunnable/putting the player in the HashSet be done in onPlayerInteract? Because right now all it does is doesn't let you use it for 10 seconds ONLY if you hit a player. You can shoot indefinelty in the air...
     
  26. Offline

    DrTURTLE2

    yes, thats what I want to fix, so you can only shoot every 10 seconds.

    Im getting so many errors that I don't know how to fix.
     
  27. Offline

    Door Knob

    Shouldn't be that hard. Just move the check that if he's in the HashSet to onPlayerInteract. and also add() the player there, and also put the bukkitrunnable there.
     
  28. Offline

    DrTURTLE2

Thread Status:
Not open for further replies.

Share This Page