how to set a ItemMeta in a Material

Discussion in 'Plugin Development' started by MartijnWoddema, Apr 28, 2014.

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

    MartijnWoddema

    i am Making a Speed Wand Plugin for a server but got a problem

    if you Right/Left Click on a block/air you Enable and Disable the Speed potion

    The material is a Stick, so it works on every stick
    i want to set the Onplayerinteract [Speed Potion] on a ItemMeta but i dont know how to set it
    I have search on google but it dosnt help
    Maybe can someone help me how i set the itemmeta in a Material

    CODE
    Code:
    package me.martijntjee10;
     
    import java.util.ArrayList;
     
    import org.bukkit.Material;
    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;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Main extends JavaPlugin implements Listener {
     
        ArrayList<Player> playerArrayList = new ArrayList<Player>();
     
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
        }
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (!(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK ||
            e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK)) return;
            p.sendMessage("§7[§6PROXCRAFT§7]§4Speed stick disabled!");
            if(!(e.getItem().getType() == Material.STICK)) return;
            if (playerArrayList.contains(p)) {
                playerArrayList.remove(p);
             
                for (PotionEffect effect : p.getActivePotionEffects()) {
                    p.removePotionEffect(effect.getType());
                    return;
                 
                }
                return;
             
            }
            if(!(playerArrayList.contains(p))) {
                playerArrayList.add(p);
                p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999, 2));
                p.sendMessage("§7[§6PROXCRAFT§7]§2Speed stick enabled!");
            }
        }
     
    }
    
     
  2. Offline

    DxDy

    Could you clarify that question? What exactly are you trying to achieve? Should the wands have a special text/whatever?

    Generally you can get an Items Metadata by ItemStack.getItemMeta and set it with the similarly named setter. Also I believe this post here will solve your troubles ;)
     
  3. Offline

    MCForger

    MartijnWoddema
    Couple things I recommend. One would be not to store the Player object in a list rather store that players UUID. To init ItemMeta from an item use this:
    Code:java
    1. ItemMeta itemMeta = itemstack.getItemMeta();

    Then modify the lore or displayname (In your case you want to probably add something to the lore)
    Code:java
    1. itemMeta.setLore(Arrays.asList("Speed Stick"));

    Once done modifying the lore and display name, set the new itemmeta back to the item by doing this:
    Code:java
    1. itemstack.setItemMeta(itemMeta);

    Then when you want to check if the stick has the ability, just get the lore if the stick has any lore and if it contains "Speed Stick" or whatever else you use there.
     
  4. Offline

    MartijnWoddema

    Okay i was missing a ItemMeta Sorry for that

    The question is

    i want to change the material to the ItemMeta
    Does someone know what i need to change ?
    Code:
    if(!(e.getItem().getType() == Material.STICK)) return;
    ItemMeta
    Code:
            ItemStack item = new ItemStack(Material.STICK);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("Speed Wand");
            meta.setLore(Arrays.asList("Lore 1", "Lore 2", "Lore 3", "So on and so on"));
            item.setItemMeta(meta);
     
  5. Offline

    MCForger

    MartijnWoddema
    If you want to change the itemstacks material use this:
    Code:java
    1. itemstack.setMaterial(Material.BLAZE_ROD);
     
  6. Offline

    MartijnWoddema

    No i want to let the Speed Buff work on the ItemMeta
     
  7. Offline

    DxDy

    How can a speed buff work on the ItemMeta?

    Or are you asking how to filter out items that don't have a specific meta? In that case just get the ItemMeta and compare the relevant elements against your expected input ;)
     
  8. Offline

    MartijnWoddema

    I want that the Speed Buff works on the itemmeta and not on the Material.STICK
     
  9. Offline

    DxDy

    Code:java
    1. // Filter out non-sticks and normal sticks
    2. if(e.getItem() != null && (e.getItem().getType() != Material.STICK || !e.getItem().hasItemMeta())
    3. return;
    4.  
    5. ItemMeta itemMeta = e.getItem().getItemMeta();
    6.  
    7. // Filter out sticks which aren't wands but have metadata
    8. if(!itemMeta.getDisplayName().equals("Speed Wand"))
    9. return;
     
  10. Offline

    MartijnWoddema

    the itemmeta name = item
    Do i need to put that in the hasItemMeat())
    or change something

    Code:
            ItemStack item = new ItemStack(Material.DIAMOND);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("Speed wand");
            meta.setLore(Arrays.asList("Lore 1", "Lore 2", "Lore 3", "So on and so on"));
            item.setItemMeta(meta);
     
  11. Offline

    DxDy

    I'm afraid I don't really understand your question.

    If you're asking where the code I posted belongs, then: In your PlayerInteract-Event
     
  12. Offline

    MartijnWoddema

    Okay it workes Thxx!
    But i got something new :p
    i have if i right click the item it removed and changes to another Item [enable and disable]
    but what is not working he removes the Glowstone dust but he need to delete the glowstone dust with the Displayname and the same with the adding of the redstone can someone help me?

    This is my code now
    Code:
     @EventHandler
                    public void onPlayerInteract(PlayerInteractEvent e) {
                        if (e.getPlayer().hasPermission("proxcraft.use.speedwand")) {
                            Player player = e.getPlayer();
                            if (!(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK ||
                            e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK)) return;
                            if(!(e.getItem().getType() == Material.GLOWSTONE_DUST)) return;
                            ItemMeta itemMeta = e.getItem().getItemMeta();
                            if(!itemMeta.getDisplayName().equals("§6Speed §7[§2On&7]"))
                            player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999, 2));
                            player.sendMessage("§7[§6PROXCRAFT§7]§3Speed stick §2Enabled!");
                                        player.playSound(player.getLocation(), Sound.ANVIL_LAND, 10, 1);
                                        player.getInventory().remove(new ItemStack(Material.GLOWSTONE_DUST));
                                        player.getInventory().addItem(new ItemStack(Material.REDSTONE));
                                        return;
                        }
                        return;
                    }
                    @EventHandler
                    public void onPlayerInteract11(PlayerInteractEvent e) {
                        if (e.getPlayer().hasPermission("proxcraft.use.speedwand")) {
                            Player player = e.getPlayer();
                            if (!(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK ||
                            e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK)) return;
                            if(!(e.getItem().getType() == Material.REDSTONE)) return;
                            ItemMeta itemMeta = e.getItem().getItemMeta();
                            if(!itemMeta.getDisplayName().equals("§6Speed §7[§4OFF&7]"))
                            player.removePotionEffect(PotionEffectType.SPEED);
                            player.sendMessage("§7[§6PROXCRAFT§7]§3Speed stick §4Disabled!");
                                        player.playSound(player.getLocation(), Sound.ANVIL_LAND, 10, 1);
                                        player.getInventory().remove(new ItemStack(Material.REDSTONE));
                                        player.getInventory().addItem(new ItemStack(Material.GLOWSTONE_DUST));
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        Player player = (Player) sender;
        if (cmd.getName().equalsIgnoreCase("supersword")) {
            ItemStack speedwand = new ItemStack(Material.GLOWSTONE_DUST);
            ItemMeta meta = speedwand.getItemMeta();
            meta.setDisplayName("§6Speed §7[§2On§7]");
            meta.setLore(Arrays.asList(ChatColor.BLUE + "This sword is awesome!"));
            speedwand.setItemMeta(meta);
            player.getInventory().addItem(speedwand);
            return true;
        }
     
Thread Status:
Not open for further replies.

Share This Page