Get dye color

Discussion in 'Plugin Development' started by Moon_werewolf, Feb 4, 2011.

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

    Moon_werewolf

    Who do i get like dye color or wool color? for all wool and dye has the same TypeId
     
  2. Offline

    Cmars

    it's [id]:[color], like 35:1 = orange wool
     
  3. Offline

    Moon_werewolf

    yea. but how do i get that in code? i try get that color value but i don't know how to get it :(
     
  4. Offline

    Cmars

    Well the dyes and wool is stored in damage so if you would be adding it, it would look like
    http://www.minecraftwiki.net/wiki/Data_values#Wool
    Code:
    ItemStack s = new ItemStack(35, 1, 1); //id count damage
    player.getInventory().addItem(s);
     
  5. Offline

    Moon_werewolf

    is is not the adding that is hard i need to get what varibale. of wool like
    Code:
    ItemStack  item = event.getPlayer().getItemInHand();
    
    if(item.getTypeId == yellow wool)
    {
    ...code...
    }
     
  6. Offline

    Zero9195

    I need something like this too, would be great if someone knows that ;)
     
  7. Offline

    Tempelchat

    Code:
    short woolcolor = p.getItemInHand().getDurability();
    I've not testet it but if it is stored in damage...
     
  8. Offline

    Plague

    no, its Block.getData(), in the hand case p.getItemInHand.getData()
    Yes, it is damage for real, was accessed like that before, but I think bukkit now handles that internally.
     
  9. Offline

    Tempelchat

    Thanks for the correction!(not testet)
    And how do I set the color to a placed block?
    getData() gives MaterialData but block.setData() needs a byte...

    So how do I take the color from the wool in hand and put it onto a block in the world?

    (Sorry that I use this theard but I think that fits in here)
     
  10. Offline

    eisental

    You can use:
    block.setData(DyeColor.BLACK.getData());

    To get the data from the block at hand:
    block.setData(player.getItemInHand().getData().getData());
     
  11. Offline

    Tempelchat

    Thanks for your answer! When use this code, I get a NullPointer when data.getData() gets executed:
    Code:
    MaterialData data = p.getItemInHand().getData();
                    byte color = data.getData();
    NullPointerException (open)

    Code:
    05.02.2011 09:24:53 org.bukkit.plugin.SimplePluginManager callEvent
    SCHWERWIEGEND: Could not pass event BLOCK_DAMAGED to BlockChange
    java.lang.NullPointerException
            at com.bukkit.Tempelchat.BlockChange.BlockChangeBlockListener.onBlockDamage(BlockChangeBlockListener.java:34)
            at org.bukkit.plugin.java.JavaPluginLoader$18.execute(JavaPluginLoader.java:210)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:60)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:213)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:339)
            at net.minecraft.server.Packet14BlockDig.a(SourceFile:42)
            at net.minecraft.server.NetworkManager.a(SourceFile:232)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:71)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:104)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:283)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:209)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:512)
     
  12. Offline

    fullwall

    Hmmm... is that line of code line 34 of your plugin? That's where the crash is pointing to.
     
  13. Offline

    Tempelchat

    Yes, i thougt that would be clear :/
     
  14. Offline

    Redecouverte

    Code:
    if(p.getItemInHand() != null && p.getItemInHand().getData() != null)
    {
    MaterialData data = p.getItemInHand().getData();
    byte color = data.getData();
    }
     
  15. Offline

    Tempelchat

    A lot of thanks for your effort to help me! I'll test it when my plugin is repaired. (I can't get onCommand to work) now.
    --- merged: Feb 5, 2011 11:38 AM ---
    Sorry but that doesn't seem to work. When I use a colored wool on grass it gets white wool. Here is my code:
    Code:
    public void onBlockDamage (BlockDamageEvent event)
        {
            Player p = event.getPlayer();
    
            if(plugin.on.containsKey(p) && plugin.on.get(p))
            {
    
                Material mat = p.getItemInHand().getType();
                event.getBlock().setType(mat);
    
                if(p.getItemInHand() != null && p.getItemInHand().getData() != null)
                {
                    MaterialData data = p.getItemInHand().getData();
                    byte color = data.getData();
                    event.getBlock().setData(color);
                }
    
                event.setCancelled(true);
            }
        }
     
  16. Offline

    Redecouverte

    ill debug it, gime a few min
    --- merged: Feb 5, 2011 3:10 PM ---
    working code:
    Code:
            Block b = event.getBlock();
    
            if(b.getType() == Material.AIR)
            {
                return;
            }
    
            Player p = event.getPlayer();
    
            if(plugin.on.containsKey(p) && plugin.on.get(p))
            {
    
                Material toMat = p.getItemInHand().getType();
                byte toData = (byte)p.getItemInHand().getDurability();
    
                event.getBlock().setType(toMat);
                event.getBlock().setData(toData);
    
                event.setCancelled(true);
            }
    --- merged: Feb 5, 2011 3:13 PM ---
    -> getItemInHand().getData() returns a wrong value, that's a bukkit bug

    -> the event also gets called sometimes with Block.getType() == Material.Air, so i have added a check for that

    -> also another bug with wool currently is, when you add a colored wool to a player's inventory and he already has wool, he will not receive the colored wool, but his already existing wool stack will be raised by the amount of the colored wool stack
    workaround: drop it instead of adding to inventory:

    Code:
            Player p = event.getPlayer();
            Location l = p.getLocation();
    
            ItemStack is = new ItemStack(Material.WOOL, 1, DyeColor.BLUE.getData());
            p.getWorld().dropItem(l, is);
    
            is = new ItemStack(Material.WOOL, 1, DyeColor.RED.getData());
            p.getWorld().dropItem(l, is);
     
  17. Offline

    Tempelchat

    You are a genius! Works perfectly!
    Thanks for your help!
     
Thread Status:
Not open for further replies.

Share This Page