1.7.10 Snowball change positions

Discussion in 'Spigot Plugin Development' started by BloodChain, Mar 18, 2020.

  1. Hello, I need help to make my plugin, what I want to do is the following:

    When player 1 throws a snowball with a customname to another player 2, they exchange positions.

    I need detailed explanation on how to do this since I am not a java professional.


    --------------------------------------------------------------------------------------------------------------------------------------

    I also need other help, and that is that netbeans does not show me Spigot's suggestions, for example import event, import Player, etc.
     
    #1 BloodChain, Mar 18, 2020
    Last edited: Mar 18, 2020
  2. Listen to the EntityDamageByEntityEvent and check whether EntityDamageByEntityEvent#getEntity() is a Player. If yes, cast it to a Player. Then check whether EntityDamageByEntityEvent#getDamager() is a Snowball. If yes, cast it to a Snowball and check whether Snowball#getShooter() is a Player. If yes, cast it to a Player. Then, exchange the location of the two players.
     
  3. I don't know why it won't let me get the snowball shooter, when I try to do snowball#getShooter() the getShooter() method is marked in red.

    Code (Text):
    @EventHandler
        public void snowballHit(EntityDamageByEntityEvent e){
           
            FileConfiguration messages = plugin.getCustomMessagesConfig();
           
            Entity entity = e.getEntity();
           
            if(entity instanceof Player){
                Player hitBySnowball = (Player) entity;
               
                if(e.getDamager() instanceof Snowball){
                   
                    Snowball snowball = (Snowball) e.getDamager();
                   
                    if(snowball.getShooter() instanceof Player){
                       
                    }
                }
               
            }
           
        }
     
  4. The method probably does not exist because you use 1.7. That is the problem when you use a medieval version...
     
  5. Code (Java):
    @EventHandler
        public void snowballHit(EntityDamageByEntityEvent e){
         
            FileConfiguration messages = plugin.getCustomMessagesConfig();
         
            Entity entity = e.getEntity();
         
            if(entity instanceof Player){
                Player hitBySnowball = (Player) entity;
             
                if(e.getDamager() instanceof Snowball){
                 
                    Projectile snowball = (Projectile) e.getDamager();
                 
                    if(snowball.getShooter() instanceof Player){
                     
                        Player shooter = (Player) snowball.getShooter();
                     
                        hitBySnowball.teleport(shooter);
                        shooter.teleport(hitBySnowball);
                    }
                }
             
            }
         
        }

    This is how my code was, it does not give me errors or anything, but when I put it on my server and try it with another person, nothing happens at all, not even console errors.

    I am testing it in 1.8 but my goal is to make it work in 1.7 (but I have not yet tested it in that version)
     
  6. Ah, I also need to check that the snowball is equal to a custom name, but I don't know how since I think the name of the item in the player's hand is different from the entity generated when launching it.
     
  7. can anyone help me?
     
  8. Two potential sources of error I can spot for why this wouldn't be working.

    1) Make sure you annotated this method as an EventHandler. (Just put @EventHandler on the line above "public void snowballHit......")
    2) Make sure you registered events in your onEnable section:
    Bukkit.getServer().getPluginManager().registerEvents(this, this);

    I'd also point out that your teleportation method won't quite do what you want it to do. As written, you're teleporting the target to the shooter and then teleporting the shooter to the target, which is now at the shooter location, so the shooter won't change positions. Try this instead:

    Code (Java):
    Location targetLoc = hitBySnowball.getLocation().clone();
    hitBySnowball.teleport(shooter);
    shooter.teleport(targetLoc);
     
    #8 UniverseCraft, Mar 18, 2020
    Last edited: Mar 18, 2020
  9. I just reviewed and everything is as you just indicated, but everything remains the same. The truth is weird

    And yes, I also changed my code to:
    Code (Java):
    Location targetLoc = hitBySnowball.getLocation().clone();
    hitBySnowball.teleport(shooter);
    shooter.teleport(targetLoc);
     
  10. Then I would recommend adding debug statements after each if statement, i.e. Bukkit.broadcastMessage("debug 1"); etc.
    Then if the last thing broadcasted is "debug 2", for example, then you know it didn't get past the second if statement.
    Let me know where it stops working for you.
     
  11. only reaches the condition:
    Code (Java):
    if(entity instanceof Player){
    Code (Java):
    @EventHandler
        public void snowballHit(EntityDamageByEntityEvent e){
       
            FileConfiguration messages = plugin.getCustomMessagesConfig();
       
            Entity entity = e.getEntity();
       
            if(entity instanceof Player){
                Player hitBySnowball = (Player) entity;
           
                Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "entity instanceof Player"));
           
    //                 The following does not run VVVV

                if(e.getDamager() instanceof Snowball){
               
                    Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "e.getDamager() instanceof Snowball"));
               
                    Projectile snowball;
                    snowball = (Projectile) e.getDamager();
               
                    if(snowball.getShooter() instanceof Player){
                   
                        Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "snowball.getShooter() instanceof Player"));
                   
                        e.setCancelled(true);
                        Player shooter = (Player) snowball.getShooter();
                   
                        Location targetLoc = hitBySnowball.getLocation().clone();
                        hitBySnowball.teleport(shooter);
                        shooter.teleport(targetLoc);
                    }
                }
            }
        }
    maybe it work if I declare the variable "snowball" like this:

    Code (Java):
    Snowball snowball;
    snowball = (Snowball) e.getDamager();
    But when changing that variable to "Snowball" I can't use the getShooter() method, is marked in red

    ddsdsd.PNG
     
    #11 BloodChain, Mar 19, 2020
    Last edited: Mar 19, 2020
  12. I hope your help. thanks!
     
  13. Weird. This works for me on the 1.8 Spigot API, so you must be using 1.7 or earlier. I know you said you want to use 1.7, but I would strongly recommend upgrading to at least 1.8 if not later. Keep in mind you can always use ViaVersion or something similar to allow players from different versions to join.

    If none of that's an option, then you can either go into OBC or NMS and try to find something there that logs the shooter, or you could create a really hack-ish solution like adding a handler for ProjectileLaunchEvent and doing some advanced ray-tracing to try to find the nearest player that will be hit when the projectile is a snowball. I would strongly caution against this approach, however, but if you must stick to 1.7, you'll have to get creative.
     
  14. I tried downloading Spigot 1.15 / 1.8 / 1.7 and none worked for me. Can you give me the Spigot API you used?
     
  15. If I'm remembering correctly it's illegal to share the *actual JARs* of the API because they contain Mojang (Microsoft)'s copyrighted code, so instead you have to download the BuildTools which will take care of everything for you and download the JAR legally to your computer. A step-by-step tutorial is located here. (It's much less difficult than it looks)
     
  16. Ok I already followed the whole tutorial and downloaded version 1.8.8 but I have no idea where the JAR is that I should use in netbeans
    Captura.PNG
     
  17. The jar should show up in the folder you're in right now. It looks like you targeted the latest version instead of 1.8.8. The proper build command would be:
    Code (Text):
    java -jar BuildTools.jar --rev 1.8.8
    (If it gives you a memory-related error, like it did for me, try this:
    Code (Text):
    java -Xmx1024M -jar BuildTools.jar --rev 1.8.8
    The spigot-1.8.8.jar file will then show up in this directory.