Comparing dyed leather armor

Discussion in 'Plugin Development' started by Polunom, Oct 6, 2013.

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

    Polunom

    I have a group of skeletons wearing red dyed leather caps, and another wearing blue caps. How can I check what color of cap their wearing?

    What I have:
    Code:java
    1.  
    2. ItemStack redhelm = colorLeatherArmor(Material.LEATHER_HELMET, Color.RED);
    3. ItemStack bluehelm =colorLeatherArmor(Material.LEATHER_HELMET, Color.BLUE);
    4.  
    5. if (e.getEquipment().getHelmet() == redhelm ){
    6. event.setCancelled(true);
    7. }
    8. if (e.getEquipment().getHelmet() == bluehelm ){
    9. event.setCancelled(true);
    10. }
    11.  
    12.  
    13. }
    14.  


    And the colorLeatherArmor class:

    Code:java
    1.  
    2. public ItemStack colorLeatherArmor(Material material, Color color) {
    3. ItemStack item = new ItemStack(material);
    4. LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
    5. meta.setColor(color);
    6. item.setItemMeta(meta);
    7.  
    8. return item;
    9.  
    10. }
    11.  
     
  2. Offline

    chasechocolate

    Code:java
    1. ItemStack helmet = entity.getEquipment().getHelmet();
    2.  
    3. if(helmet.getItemMeta() instanceof LeatherArmorMeta){
    4. LeatherArmorMeta meta = (LeatherArmorMeta) helmet.getItemMeta();
    5.  
    6. if(meta.getColor() == something){
    7. //Do something
    8. }
    9. }
     
  3. Offline

    Polunom

    chasechocolate

    The problem I'm having is comparing the colors. What would I replace with "something" here?

    Code:java
    1.  
    2.  
    3. if(meta.getColor() == something){
    4. //Do something
    5. }
    6.  
     
  4. Offline

    Vexil

    Code:java
    1.  
    2.  
    3. if(meta.getColor() == Color.BLUE){
    4. //Do something
    5. }
    6.  

    Or
    Code:java
    1.  
    2.  
    3. if(meta.getColor() == Color.RED){
    4. //Do something
    5. }
    6.  
     
  5. Offline

    Polunom

    It doesn't work. I'm getting this error:

    Code:
    08.10 19:55:54 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    08.10 19:55:54 [Server] INFO at me.Polunom.Plugin.Main.NoTarget(Main.java:267)
    08.10 19:55:54 [Server] INFO Caused by: java.lang.NullPointerException
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:574)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.WorldServer.tickEntities(WorldServer.java:480)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.World.tickEntities(World.java:1223)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.World.playerJoinedWorld(World.java:1335)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.World.entityJoinedWorld(World.java:1354)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.EntityMonster.l_(EntityMonster.java:24)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.EntityInsentient.l_(EntityInsentient.java:150)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.EntityLiving.l_(EntityLiving.java:1277)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.EntitySkeleton.c(EntitySkeleton.java:110)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.EntityMonster.c(EntityMonster.java:20)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.EntityInsentient.c(EntityInsentient.java:306)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.EntityLiving.c(EntityLiving.java:1414)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.EntityInsentient.bi(EntityInsentient.java:419)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.PathfinderGoalSelector.a(PathfinderGoalSelector.java:80)
    08.10 19:55:54 [Server] INFO at net.minecraft.server.v1_6_R3.PathfinderGoalArrowAttack.d(PathfinderGoalArrowAttack.java:56)
    08.10 19:55:54 [Server] INFO at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callEntityTargetEvent(CraftEventFactory.java:529)
    08.10 19:55:54 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    08.10 19:55:54 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    08.10 19:55:54 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    08.10 19:55:54 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    08.10 19:55:54 [Server] INFO org.bukkit.event.EventException
    08.10 19:55:54 [Server] SEVERE Could not pass event EntityTargetEvent to Plugin v1.0
    
    My code:
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.HIGHEST)
    3. public void NoTarget(EntityTargetEvent event) {
    4. if(event.getEntity() instanceof Skeleton && event.getTarget().getType() == EntityType.PLAYER){
    5. Player p = (Player) event.getTarget();
    6. Skeleton skeleton = (Skeleton) event.getEntity();
    7. ItemStack helmet = skeleton.getEquipment().getHelmet();
    8.  
    9. if(helmet.getItemMeta() instanceof LeatherArmorMeta){
    10. LeatherArmorMeta meta = (LeatherArmorMeta) helmet.getItemMeta();
    11.  
    12. if(blue.contains(p) && meta.getColor() == Color.BLUE){
    13. event.setCancelled(true);
    14. }
    15. if(red.contains(p) && meta.getColor() == Color.RED){
    16. event.setCancelled(true);
    17. }
    18. }
    19.  
    20.  
    21. }
    22.  


    I have added the players to the arrayList blue and red

    Can someone please help me on this. I've been stuck for the last week

    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  6. Isn't it wise to check if it actually has an itemmeta first? for NPE prevention. chasechocolate
     
  7. Offline

    Polunom

    Can anyone help? I'm still getting a NullPointerException
     
  8. Offline

    xTrollxDudex

    Are you importing org.bukkit.Color not java.awt.Color? Just making sure.
     
  9. Offline

    Polunom


    Yes, I'm using org.bukkit.Color
     
  10. Offline

    Plo124

    Polunom
    Code:java
    1.  
    2. Color.fromRGB(255, 0, 0)
    3.  

    Red

    Code:java
    1.  
    2. Color.fromRGB(0, 255, 0)
    3.  
    4.  
    5.  

    Blue
     
  11. Offline

    Polunom

    Plo124

    Doesn't work. Anyone know why I'm getting an NPE?
     
  12. Offline

    Worthless_Hobo

    I know its been a year and this wont help the original asker of this question, but it might help future users. You need to check if the item has item meta first and if the entity even has a helmet (this is the way I would do it, but not the only way): if(!(event.getEntity().getEquipment().getHelmet() != null || event.getEntity().getEquipment().getHelmet().hasItemMeta())) This is a one line check that I use all the time, it will stop that NPE
    Code:java
    1. if(!(event.getEntity().getEquipment().getHelmet() != null || event.getEntity().getEquipment().getHelmet().hasItemMeta())){return;}
    2. ItemStack helmet = entity.getEquipment().getHelmet();
    3.  
    4. if(helmet.getItemMeta() instanceof LeatherArmorMeta){
    5. LeatherArmorMeta meta = (LeatherArmorMeta) helmet.getItemMeta();
    6.  
    7. if(meta.getColor() == Color.RED){
    8. //Do something
    9. }
    10. }
     
Thread Status:
Not open for further replies.

Share This Page