Need help making a weapon

Discussion in 'Plugin Development' started by QuadroRTX8000, Jun 21, 2022.

  1. Offline

    QuadroRTX8000

    I am trying to make a golden sword that places fireballs when shift right clicked, and fires them when normal right clicked. I have a method that on right click it will run an if statement where if you are shifting and holding a gold sword, it will summon a fireball, and if not shifting it sets the shooter to the fireball. The problem is the fireball variable defined inside the if statement can't be accessed by the else if statement, and when I shift right click it shoots the fireball, even though I want it to be stationary.
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    QuadroRTX8000

    Sorry for late response, here(dont mind the name):
    Code:
    package amogus.amogus.handlers;import amogus.amogus.Amogus;import org.bukkit.Bukkit;import org.bukkit.Location;import org.bukkit.Material;import org.bukkit.block.data.type.Fire;import org.bukkit.entity.Fireball;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.player.PlayerInteractEvent;import org.bukkit.util.Vector;public class FireSword implements Listener {
    Amogus plugin; public FireSword(Amogus plugin) {Bukkit.getPluginManager().registerEvents(this, plugin); this.plugin = plugin;}
    @EventHandlerpublic void FireSword(PlayerInteractEvent event) {
    Player p = event.getPlayer();Location loc = p.getLocation();Vector dir = loc.getDirection();dir.normalize();dir.multiply(2);loc.add(dir); if (p.getInventory().getItemInMainHand().getType() == Material.GOLDEN_SWORD && p.isSneaking()) {
    Fireball fire = p.getWorld().spawn(event.getPlayer().getLocation().add(new Vector(0.0d, 1.0d, 0.0d)), Fireball.class);} else if (p.getInventory().getItemInMainHand().getType() == Material.GOLDEN_SWORD) {
    fire.setShooter(p);}
    }
    }
     
  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    QuadroRTX8000

    How do I set the velocity to 0? Sorry if I'm dumb, but it shows a red line under a plain zero in .setVelocity, but if I add more arguments it says it only expected one.
     
  6. Online

    timtower Administrator Administrator Moderator

  7. Offline

    gochi9

    Use .setDirection(Vector);
    .setVelocity won't work
     
  8. Offline

    QuadroRTX8000

    Yes, a vector, but how would I set a vector to zero? Just make a vector variable and set it to zero?
     
  9. Offline

    gochi9

    Ah yes you can create it by doing new Vector(0,0,0)
     
  10. Offline

    QuadroRTX8000

    That still doesn't work, it still shoots the fireball. Here's my code:
    Sorry for late responses, mods have to approve my message EVERY SINGLE TIME and I have to wait about half an hour :/
    Code:
    package amogus.amogus.handlers;
    
    import amogus.amogus.Amogus;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.data.type.Fire;
    import org.bukkit.entity.Fireball;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.util.Vector;
    
    public class FireSword implements Listener {
        Amogus plugin;
        public FireSword(Amogus plugin) {Bukkit.getPluginManager().registerEvents(this, plugin); this.plugin = plugin;}
        @EventHandler
        public void FireSword(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            if (p.getInventory().getItemInMainHand().getType() == Material.GOLDEN_SWORD && p.isSneaking()) {
                Fireball fire = p.getWorld().spawn(event.getPlayer().getLocation().add(new Vector(0.0D,1.0D,0.0D)), Fireball.class);
                fire.setDirection(new Vector(0, 0, 0));
            }
        }
    } 
     
    Last edited: Jun 22, 2022 at 6:41 AM
  11. Offline

    gochi9

    Mods have to approve it only if you have less than 20 messages (i think) and if i'm not mistaken only timtower is the only mod left so don't be so hard on him, he does his job pretty good. Should be thankful there's still someone maintaining this place. Anyway...

    My bad, I tested my code in 1.8.8 and it worked fine even your code works as it should in 1.8.8 but then i hoped on to a newer version (1.17) and it indeed does not work. I tried setVelocity and setDirection along with other thing and nothing really work.

    Buuuuuuuut there is a method which isn't really pretty but I don't know anything else really. You could spawn an armour stand at the player's location and set invisible (maybe invincible too) and add the fireball as a passanger that way it looks like it is staying in place.
     
  12. Offline

    QuadroRTX8000

    Thank you, even though it doesn't work. Do you know of anyway to make a fireball go to a specific place(entity I'm looking at) or summon a fireball a little to the left of my cursor?
     
  13. Offline

    gochi9

    I tested it and the it does work, the fireball appears like it's in mid air without moving. Here I attached some images: https://imgur.com/a/c5MHTUn + I also made it so it launches in the direction I look at when I right click (maybe a problem with your code?)

    As for targeting an entity is kind of difficult because it can move and you can't really steer the fireball once it's been launched, you could change the direction but it doesn't look very good.

    Launching the fireball a little to your left you need to check the getYaw() from your player location to get your directions then modify the locations x and/or z values accordingly
     
  14. Offline

    QuadroRTX8000

    Can you give your EXACT main class and class for the fireball and whereever else it is used, and what version you are using?
     
  15. Offline

    gochi9

    I'll just say what you need to do and you code it, that's as much as we should do. No spoonfeeding code here.

    First and first the main class just registers the event in my case so there's nothing strange there. Also I used 1.17 but it should work with the newest versions just fine.

    You should have a separate class for the methods and map here but since this is just an example and you should know how to do that stuff.

    First create a HashMap<UUID ,UUID> or a HashMap<UUID, List<UUID>. The first map is used if you only want to be able to spawn one fireball at a time and the second map is used for having multiple fireballs at the same time. The key should be the player's uuid and the value should be the armorstand's uuid.

    Then in my in PlayerInteractListener I first check if the event action == to right click block or right click air , then I check if the event item is null or if it is the incorrect material. Then I create my Player and location variables and then check if the player is sneaking.

    If he is then (in case you have the 1st map you have to check if it containsKey the player's uuid and if it's true you have to get the old entity using it's uuid and check if it's still there and delete the passanger(which is the fireball) and then delete the entity itself while removing the player from the map at the end), you can spawn the armorstand and the fireball and after that add the fireball as a passanger for armorstand and finnaly put the player's uuid in the map along side the armorstand's uuid. (I don't have to say but you have to make the armorstand invisible, invicible and remove it's gravity).

    If he is not sneaking then check if the map contains the player and if it does then simply get the armorstand using the player's uuid (if you're using the second map than you'll have to loop through all uuid's), check if the entity is still there and get it's location, now you can delete the armorstand's passanger and then the armor stand. Using the saved location spawn another fireball and use setDirection and send it where you want and at the end remove the player's uuid from the map.
     

Share This Page