Stick

Discussion in 'Plugin Development' started by JavaNoob, Sep 13, 2020.

  1. Offline

    JavaNoob

    Hi! I don't really know how to create this. I am trying to basically recreate the toy stick on Hypixel. I know how to create a new crafting recipe that I can basically make a new item with. But the rest of it I need to code the item so that whenever you right click, it launches a small projectile that when on contact with blocks or entities, it launches entities and blocks into the air. I know how to make the blocks fly and all that, but i don't know how to set the range of the projectile effect, or how to launch the projectile. I could spawn it on my location and set the velocity, but I don't think that would be the best way to do it. Any thoughts? I messed around with it a while back, but gave up on it. I want to revisit it again.
     
  2. Offline

    mAndAle

    @JavaNoob
    You can use PlayerInteractEvent and when right click, do your job:

    Code:
    public InteractPlayerListener   implements Listener{
    
    public void onInteract(PlayerIntereactEvent event){
       if (event.getAction() == action.rightClick){
       //do your job
      //sorry but im not in ide
      }
    }
    
    }
     
  3. Offline

    JavaNoob

    Thank you!
    Edit: I started out with just trying to make it so when you right-click with it, it creates an explosion. I already have a plugin that i made earlier that makes the blocks fly everywhere on explosions. But it won't let me cast the item type to a stick when doing the right click event


    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Item;
    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.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {
    @Override
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
    }


    @EventHandler
    public void onPlayer(PlayerInteractEvent event) {
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    Item item = (Item) event.getItem();
    if (item.equals(Material.STICK)) {
    Player player = (Player) event.getPlayer();
    Block block = (Block) event.getClickedBlock();
    player.getWorld().createExplosion(block.getLocation(), 6F);

    }
    }

    }

    }
     
    Last edited: Sep 14, 2020 at 10:41 AM
  4. Offline

    KarimAKL

    @JavaNoob That's not how you check if the item is a stick. You need to get the ItemStack from the Item entity, then check if that ItemStack's type (Material) is STICK.
     
    JavaNoob likes this.
  5. Offline

    JavaNoob

    So like this? ItemStack item = new ItemStack(event.getItem());
    Edit: Okay, now im confused what I just did. I tried to modify the code so that it would work just when I left clicked any block, and something strange happened. It worked until I died from the explosions, and then it stopped working. I have another plugin that where I break a block it spawns a random block in. It spawned in a tnt, and when I left clicked the tnt, it lit. Im confused as to why it did that.

    import org.bukkit.Material;
    import org.bukkit.block.Block;
    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.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {
    @Override
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
    }


    @EventHandler
    public void onPlayer(PlayerInteractEvent event) {
    if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
    ItemStack item = new ItemStack(event.getItem());
    Player player = (Player) event.getPlayer();
    Block block = (Block) event.getClickedBlock();
    player.getWorld().createExplosion(block.getLocation(), 6F);


    }

    }

    }
    Edit2: Nevermind, it's not lightning tnt anymore, I don't know why it was doing it before. Anyways, the explosions are very erratic, not happening often, and certainly not every time. I also created another event that triggers a lightning strike when I right-click. I also spawn an explosion on it. It works. But I cannot seem to be able to access the blocklist for the explosion so I cannot make the blocks fly around instead of break. Also, the plugin that I had previously do that for explosions still works, just not for summoned explosions for some reason.
     
    Last edited: Sep 14, 2020 at 12:09 PM
  6. Offline

    KarimAKL

    No. Read what i posted. You need to get the ItemStack from the Item entity.
     
  7. Offline

    JavaNoob

    Wait, maybe im just stupid. But you can call Item from Entity?
    Would this work?
    if (event.getPlayer().getInventory().getItemInHand.getType() == Material.STICK) {

    }
    Edit: It works! It works every time too.
    Now the next thing, I cannot find out how to get the blocklist of the effected blocks by the created explosion. How would I get it?
     
    Last edited: Sep 14, 2020 at 8:57 PM
  8. Offline

    KarimAKL

    No. You cannot.

    Probably. But you can get the item from the event instead of the player's inventory.

    I'm pretty sure there's an explosion event that lets you do that. Have you tried searching the Javadocs?
     
  9. Offline

    JavaNoob

    I just did, and I found the EntityExplodeEvent, which is the one I used for the other block plugin I made, and then I found one that I think will work. The BlockExplodeEvent. So I will try that one.
    That worked! But the other problem im having is that I want it to fling entities too. So I tried using a EntityDamageEvent, but I don't know how to check if they just take damage from an explosion. I have this. if (event.getCause().equals(explosion)) That is where I think it is wrong; what should i put in for explosion?

    Edit: It doesn't work for any type of damage even without the if statement.
     
    Last edited: Sep 15, 2020 at 7:10 AM
  10. Offline

    KarimAKL

    @JavaNoob What do you mean by "i want it to fling entities too"? As in give items a velocity away from the center of the explosion?
     
  11. Offline

    JavaNoob

    Sorry, I should have been more specific. So for example, if I were to fire off the explosion at my feet or in the middle of a herd of cows, then the cows/I would go flying much like the blocks do. I know how to set velocity, and I already have a small algorithm I made to make a random number every time. I just don't know how I would fling the animals/players. I tried the EntityDamageEvent, but it didn't work.
     
  12. Offline

    KarimAKL

    @JavaNoob Set their velocity if they're in range?
     
  13. Offline

    JavaNoob

    Yes, that is what I'm going for. If you know about the Toy Stick victory dance on the Hypixel network, that is kind of what I'm going for. I am also going to set the player who set the explosion invulnerable until they land, but I think that that was introduced in like version 1.12, so I'll have to upgrade my spigot server.
     
  14. Offline

    KarimAKL

    Okay. Which part are you having trouble with?
     
  15. Offline

    JavaNoob

    Well, I tried to use the EntityDamage event to trigger the velocity, as in when they take damage they would go flying. But it wouldn't work. So I'm just wondering if there is a better way to do that, or if the setVelocity even works on entities.
     
  16. Offline

    KarimAKL

    @JavaNoob Define "wouldn't work." Does it just not set their velocity? Did you try debugging?
     
  17. Offline

    JavaNoob

    What I mean by won't work is that even when the entities take damage, they will not go flying. I did try to debug, but I cannot see a single thing wrong. I tried all types of damage, not just explosions.
     
  18. Offline

    KarimAKL

    JavaNoob likes this.
  19. Offline

    JavaNoob

    public void onHurt(EntityDamageEvent event) {
    Entity entity = (Entity) event.getEntity();
    float x2 = (float) -2 + (float) Math.random() * ((2 - -2) + 1);
    float y2 = (float) -1 + (float) Math.random() * ((1 - -1) + 1);
    float z2 = (float) -2 + (float) Math.random() * ((2 - -2) + 1);
    entity.setVelocity(new Vector(x2, y2, z2));

    }
    This is just the entity fling code
     
    Last edited: Sep 18, 2020 at 4:23 PM

Share This Page