Solved Throwing an item while keeping it's texture

Discussion in 'Plugin Development' started by BDKing88, Apr 24, 2014.

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

    BDKing88

    Hi all, I need some help with a plugin I'm making. I want to be able to throw a bottle while keeping it's bottle texture in the air. If anyone could help, that'd be amazing!
    Code:
    package me.bdking00.mc;
     
    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    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 {
       
        List<UUID> droppedItems = new ArrayList<UUID>();
     
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
     
        public void throwMolotov(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            World w = p.getWorld();
            if(p.hasPermission("molotov.throw")) {
                if(p.getInventory().contains(Material.GLASS_BOTTLE, 1)) {
                    if(p.getItemInHand().getType() == Material.GLASS_BOTTLE) {
                        if(e.getAction() == Action.RIGHT_CLICK_AIR) {
                            final Item molotov = w.dropItem(p.getEyeLocation(), new ItemStack(Material.GLASS_BOTTLE));
                            droppedItems.add(molotov.getUniqueId());
                            molotov.setVelocity(p.getEyeLocation().getDirection());
                            Bukkit.getScheduler().runTaskLater(this, new Runnable() {
                                @Override
                                public void run() {
                                    molotov.getLocation().getBlock().setType(Material.FIRE);
                                    molotov.remove();       
                                }
                            }, 40);
                        }
                    }
                }
            } else {
                return;
            }
        }
    }
    
     
  2. Offline

    Slikey

    I'd like to help, but I don't unterstand what you mean.. Can you describe that better?
     
  3. Offline

    BDKing88

    Slikey I want to be able to throw a glass potion bottle like a snowball or an egg while keeping it's texture (Still looking like a glass potion bottle in the air). I know how to make a glass bottle thrown like a snowball, but the way I know it looks like a snowball while in the air.
     
  4. Offline

    Slikey

    BDKing88 Oh okay.
    Code:java
    1. ItemMeta im = is.getItemMeta();
    2. im.setDisplayName("molotov" + ChatColor.RED + RandomUtils.random.nextInt(10000));
    3. is.setItemMeta(im);
    4. Item i = loc.getWorld().dropItem(loc, is);
    5. i.setMetadata("molotov", new FixedMetadataValue(plugin, 0));


    Maybe you should spawn an entity-item, when someone rightclicks the water-bottle. Replace "RandomUtils.random.nextInt(10000)" with a random value to 10000. Then you have a unique item which won't merge with another... Now add a Listener to prevent someone from picking the molotov up.

    Code:java
    1. @EventHandler
    2. public void onItemPickup(PlayerPickupItemEvent event) {
    3. if (event.getItem().hasMetadata(ITEM_IDENTIFIER)) {
    4. event.setCancelled(true);
    5. return;
    6. }
    7. }


    Now you have an unstackable and unpickable glass-bottle.. Set the velocity of the Item (It's an entity) and run a task that is called after 40 ticks, to remove the glassbottle and spread the fire.
     
    BDKing88 likes this.
  5. Offline

    BDKing88

    Slikey Okay, what do you mean where you put the "is.getItemMeta()"? What does the is stand for?
     
  6. Offline

    Slikey

    I was bored. So I coded it.. Is untested though.. Should work anyway. ;)
    Code:java
    1. import java.util.Random;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.World;
    7. import org.bukkit.entity.Item;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.HandlerList;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.event.player.PlayerPickupItemEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.metadata.FixedMetadataValue;
    18. import org.bukkit.plugin.Plugin;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public final class Molotov extends JavaPlugin implements Listener {
    22.  
    23. public static final String ITEM_IDENTIFIER = "molotov";
    24. private Random random;
    25.  
    26. @Override
    27. public void onEnable() {
    28. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    29. random = new Random(System.currentTimeMillis());
    30. }
    31.  
    32. @Override
    33. public void onDisable() {
    34. HandlerList.unregisterAll((Plugin) this);
    35. }
    36.  
    37. @EventHandler
    38. public void onMolotov(PlayerInteractEvent e) {
    39. Player p = e.getPlayer();
    40. if (!p.hasPermission("molotov.throw"))
    41. return;
    42. if (!p.getInventory().contains(Material.GLASS_BOTTLE, 1))
    43. return;
    44. if (p.getItemInHand().getType() != Material.GLASS_BOTTLE)
    45. return;
    46. if (e.getAction() != Action.RIGHT_CLICK_AIR)
    47. return;
    48.  
    49. World w = p.getWorld();
    50. ItemStack is = new ItemStack(Material.GLASS_BOTTLE);
    51. ItemMeta im = is.getItemMeta();
    52. im.setDisplayName(ITEM_IDENTIFIER + ChatColor.RED + random.nextInt(10000));
    53. is.setItemMeta(im);
    54. final Item i = w.dropItem(p.getEyeLocation(), is);
    55. i.setMetadata(ITEM_IDENTIFIER, new FixedMetadataValue(this, 0));
    56. i.setVelocity(p.getEyeLocation().getDirection());
    57. Bukkit.getScheduler().runTaskLater(this, new Runnable() {
    58. @Override
    59. public void run() {
    60. i.getLocation().getBlock().setType(Material.FIRE);
    61. i.remove();
    62. }
    63. }, 40);
    64. }
    65.  
    66. @EventHandler
    67. public void onItemPickup(PlayerPickupItemEvent event) {
    68. if (event.getItem().hasMetadata(ITEM_IDENTIFIER)) {
    69. event.setCancelled(true);
    70. return;
    71. }
    72. }
    73.  
    74. }


    BDKing88 I tested it and it works perfectly.. just go into gamemode and rightclick the glass bottle ;)

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

    BDKing88

    Slikey How would I go about making a command for getting the molotov? Because I can't make the itemstack public so therefor I can't access it from a separate boolean. Or am I just missing something very simple?

    And yes, it works amazingly!! Thanks a ton!
     
  8. Offline

    Slikey

    BDKing88 Eh... You don't need to give someone a molotov.. Just give someone a simple glass bottle.. The ItemStack in the method is just a workaround to create a unique item for the entity.
     
  9. Offline

    BDKing88

    Slikey Yeah... maybe. I was thinking about adding a crafting recipe and all but I'm getting ahead of myself. And thank you so much for the help!

    Slikey By the way, if I were to add this plugin to bukkit, would you want to be added as an author?

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

    Slikey

    BDKing88 Just put a credit to me ;) It's your choince^^ Glad, it works ;)
     
  11. Offline

    BDKing88

    Slikey Okay, I most certainly will give you credit :) and thanks again for the help!
     
Thread Status:
Not open for further replies.

Share This Page