Snippets for 1.4

Discussion in 'Resources' started by stirante, Oct 29, 2012.

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

    stirante

    I think I write too much threads so I created this one.If there will be any errors or problems PM me or post it here.

    Changing color of wolf's collar:
    Code:java
    1. package com.stirante.PrettyScaryLib;
    2. import java.awt.Color;
    3. import net.minecraft.server.EntityWolf;
    4. import org.bukkit.DyeColor;
    5. import org.bukkit.craftbukkit.entity.CraftLivingEntity;
    6. import org.bukkit.entity.Wolf;
    7. /**
    8.  * Class, that allows setting and getting color of wolf's collar.
    9.  */
    10. public class WolfCollar {
    11.  
    12. /**
    13.   * Sets color.
    14.   *
    15.   * @param wolf wolf
    16.   * @param color color
    17.   * @return wolf
    18.   */
    19. public static Wolf setColor(Wolf wolf, int color){
    20. EntityWolf ent = (EntityWolf) ((CraftLivingEntity)wolf).getHandle();
    21. ent.setCollarColor(color);
    22. return wolf;
    23. }
    24.  
    25. /**
    26.   * Sets color.
    27.   *
    28.   * @param wolf wolf
    29.   * @param color color
    30.   * @return wolf
    31.   */
    32. public static Wolf setColor(Wolf wolf, Color color){
    33. EntityWolf ent = (EntityWolf) ((CraftLivingEntity)wolf).getHandle();
    34. ent.setCollarColor(color.getRGB());
    35. return wolf;
    36. }
    37.  
    38. /**
    39.   * Sets color.
    40.   *
    41.   * @param wolf wolf
    42.   * @param color color
    43.   * @return wolf
    44.   */
    45. public static Wolf setColor(Wolf wolf, DyeColor color){
    46. EntityWolf ent = (EntityWolf) ((CraftLivingEntity)wolf).getHandle();
    47. ent.setCollarColor(color.getData());
    48. return wolf;
    49. }
    50.  
    51. /**
    52.   * Sets color.
    53.   *
    54.   * @param wolf wolf
    55.   * @param colorStr color str
    56.   * @return wolf
    57.   */
    58. public static Wolf setColor(Wolf wolf, String colorStr){
    59. int color = Integer.decode(colorStr);
    60. EntityWolf ent = (EntityWolf) ((CraftLivingEntity)wolf).getHandle();
    61. ent.setCollarColor(color);
    62. return wolf;
    63. }
    64.  
    65. /**
    66.   * Sets color.
    67.   *
    68.   * @param wolf wolf
    69.   * @param colorR amount of red
    70.   * @param colorG amount of green
    71.   * @param colorB amount of blue
    72.   * @return wolf
    73.   */
    74. public static Wolf setColor(Wolf wolf, int colorR, int colorG, int colorB){
    75. int color = Integer.decode(ColorConverter.toHex(colorR, colorG, colorB));
    76. EntityWolf ent = (EntityWolf) ((CraftLivingEntity)wolf).getHandle();
    77. ent.setCollarColor(color);
    78. return wolf;
    79. }
    80.  
    81. /**
    82.   * Gets color.
    83.   *
    84.   * @param wolf wolf
    85.   * @return color
    86.   */
    87. public static int getColor(Wolf wolf){
    88. EntityWolf ent = (EntityWolf) ((CraftLivingEntity)wolf).getHandle();
    89. return ent.getCollarColor();
    90. }
    91.  
    92. /**
    93.   * Gets dye color.
    94.   *
    95.   * @param wolf wolf
    96.   * @return dye color
    97.   */
    98. public static DyeColor getDyeColor(Wolf wolf){
    99. EntityWolf ent = (EntityWolf) ((CraftLivingEntity)wolf).getHandle();
    100. return DyeColor.getByData((byte) ent.getCollarColor());
    101. }
    102. }
    103.  


    Changing color of armor:
    Code:java
    1. package com.stirante.PrettyScaryLib;
    2. import java.awt.Color;
    3. import net.minecraft.server.NBTTagCompound;
    4. import org.bukkit.craftbukkit.inventory.CraftItemStack;
    5. import org.bukkit.inventory.ItemStack;
    6. /**
    7.  * Class, that allows setting and getting color of the leather armor.
    8.  */
    9. public class Armor {
    10. /**
    11.   * Sets the color.
    12.   *
    13.   * @param item item to color
    14.   * @param color color
    15.   * @return colored item
    16.   * @throws Exception thrown, when item is not applicable
    17.   */
    18. public static ItemStack setColor(ItemStack item, int color) throws Exception {
    19. if (!isApplicable(item))
    20. throw new Exception("Item is not applicable!");
    21. CraftItemStack craftStack = null;
    22. net.minecraft.server.ItemStack itemStack = null;
    23. if (item instanceof CraftItemStack) {
    24. craftStack = (CraftItemStack) item;
    25. itemStack = craftStack.getHandle();
    26. }
    27. else if (item instanceof ItemStack) {
    28. craftStack = new CraftItemStack(item);
    29. itemStack = craftStack.getHandle();
    30. }
    31. NBTTagCompound tag = itemStack.tag;
    32. if (tag == null) {
    33. tag = new NBTTagCompound();
    34. tag.setCompound("display", new NBTTagCompound());
    35. itemStack.tag = tag;
    36. }
    37.  
    38. tag = itemStack.tag.getCompound("display");
    39. tag.setInt("color", color);
    40. itemStack.tag.setCompound("display", tag);
    41. return craftStack;
    42. }
    43.  
    44. /**
    45.   * Checks if item is applicable.
    46.   *
    47.   * @param item the item to check
    48.   * @return true, if is applicable
    49.   */
    50. private static boolean isApplicable(ItemStack item) {
    51. switch (item.getType()) {
    52. case LEATHER_BOOTS:
    53. case LEATHER_CHESTPLATE:
    54. case LEATHER_HELMET:
    55. case LEATHER_LEGGINGS:
    56. return true;
    57. default:
    58. return false;
    59. }
    60. }
    61.  
    62. /**
    63.   * Sets the color.
    64.   *
    65.   * @param item item to color
    66.   * @param color color
    67.   * @return colored item
    68.   * @throws Exception thrown, when item is not applicable
    69.   */
    70. public static ItemStack setColor(ItemStack item, Color color) throws Exception {
    71. if (!isApplicable(item))
    72. throw new Exception("Item is not applicable!");
    73. CraftItemStack craftStack = null;
    74. net.minecraft.server.ItemStack itemStack = null;
    75. if (item instanceof CraftItemStack) {
    76. craftStack = (CraftItemStack) item;
    77. itemStack = craftStack.getHandle();
    78. }
    79. else if (item instanceof ItemStack) {
    80. craftStack = new CraftItemStack(item);
    81. itemStack = craftStack.getHandle();
    82. }
    83. NBTTagCompound tag = itemStack.tag;
    84. if (tag == null) {
    85. tag = new NBTTagCompound();
    86. tag.setCompound("display", new NBTTagCompound());
    87. itemStack.tag = tag;
    88. }
    89.  
    90. tag = itemStack.tag.getCompound("display");
    91. tag.setInt("color", color.getRGB());
    92. itemStack.tag.setCompound("display", tag);
    93. return craftStack;
    94. }
    95. /**
    96.   * Sets the color.
    97.   *
    98.   * @param item item to color
    99.   * @param color color
    100.   * @return colored item
    101.   */
    102. public static ItemStack setColor(ItemStack item, ArmorColor color) {
    103. if (!isApplicable(item))
    104. return null;
    105. CraftItemStack craftStack = null;
    106. net.minecraft.server.ItemStack itemStack = null;
    107. if (item instanceof CraftItemStack) {
    108. craftStack = (CraftItemStack) item;
    109. itemStack = craftStack.getHandle();
    110. }
    111. else if (item instanceof ItemStack) {
    112. craftStack = new CraftItemStack(item);
    113. itemStack = craftStack.getHandle();
    114. }
    115. NBTTagCompound tag = itemStack.tag;
    116. if (tag == null) {
    117. tag = new NBTTagCompound();
    118. tag.setCompound("display", new NBTTagCompound());
    119. itemStack.tag = tag;
    120. }
    121.  
    122. tag = itemStack.tag.getCompound("display");
    123. tag.setInt("color", color.getColor());
    124. itemStack.tag.setCompound("display", tag);
    125. return craftStack;
    126. }
    127. /**
    128.   * Sets the color.
    129.   *
    130.   * @param item item to color
    131.   * @param colorStr color
    132.   * @return colored item
    133.   */
    134. public static ItemStack setColor(ItemStack item, String colorStr) {
    135. if (!isApplicable(item))
    136. return null;
    137. int color = Integer.decode(colorStr);
    138. CraftItemStack craftStack = null;
    139. net.minecraft.server.ItemStack itemStack = null;
    140. if (item instanceof CraftItemStack) {
    141. craftStack = (CraftItemStack) item;
    142. itemStack = craftStack.getHandle();
    143. }
    144. else if (item instanceof ItemStack) {
    145. craftStack = new CraftItemStack(item);
    146. itemStack = craftStack.getHandle();
    147. }
    148. NBTTagCompound tag = itemStack.tag;
    149. if (tag == null) {
    150. tag = new NBTTagCompound();
    151. tag.setCompound("display", new NBTTagCompound());
    152. itemStack.tag = tag;
    153. }
    154.  
    155. tag = itemStack.tag.getCompound("display");
    156. tag.setInt("color", color);
    157. itemStack.tag.setCompound("display", tag);
    158. return craftStack;
    159. }
    160.  
    161. /**
    162.   * Sets the color.
    163.   *
    164.   * @param item item to color
    165.   * @param colorR amount of red
    166.   * @param colorG amount of green
    167.   * @param colorB amount of blue
    168.   * @return colored item
    169.   */
    170. public static ItemStack setColor(ItemStack item, int colorR, int colorG, int colorB) {
    171. if (!isApplicable(item))
    172. return null;
    173. int color = Integer.decode(ColorConverter.toHex(colorR, colorG, colorB));
    174. CraftItemStack craftStack = null;
    175. net.minecraft.server.ItemStack itemStack = null;
    176. if (item instanceof CraftItemStack) {
    177. craftStack = (CraftItemStack) item;
    178. itemStack = craftStack.getHandle();
    179. }
    180. else if (item instanceof ItemStack) {
    181. craftStack = new CraftItemStack(item);
    182. itemStack = craftStack.getHandle();
    183. }
    184. NBTTagCompound tag = itemStack.tag;
    185. if (tag == null) {
    186. tag = new NBTTagCompound();
    187. tag.setCompound("display", new NBTTagCompound());
    188. itemStack.tag = tag;
    189. }
    190.  
    191. tag = itemStack.tag.getCompound("display");
    192. tag.setInt("color", color);
    193. itemStack.tag.setCompound("display", tag);
    194. return craftStack;
    195. }
    196.  
    197. /**
    198.   * Gets the color.
    199.   *
    200.   * @param item colored item
    201.   * @return color
    202.   */
    203. public static int getColor(ItemStack item) {
    204. if (!isApplicable(item))
    205. return -1;
    206. CraftItemStack craftStack = null;
    207. net.minecraft.server.ItemStack itemStack = null;
    208. if (item instanceof CraftItemStack) {
    209. craftStack = (CraftItemStack) item;
    210. itemStack = craftStack.getHandle();
    211. }
    212. else if (item instanceof ItemStack) {
    213. craftStack = new CraftItemStack(item);
    214. itemStack = craftStack.getHandle();
    215. }
    216. NBTTagCompound tag = itemStack.tag;
    217. if (tag == null) {
    218. tag = new NBTTagCompound();
    219. tag.setCompound("display", new NBTTagCompound());
    220. itemStack.tag = tag;
    221. return -1;
    222. }
    223.  
    224. tag = itemStack.tag.getCompound("display");
    225. return tag.getInt("color");
    226. }
    227. }
    228.  


    ArmorColor enum(used in Armor class):
    Code:java
    1. package com.stirante.PrettyScaryLib;
    2. /**
    3.  * Enum ArmorColor.
    4.  */
    5. public enum ArmorColor {
    6.  
    7. /** black. */
    8. BLACK(1973019),
    9.  
    10. /** red. */
    11. RED(11743532),
    12.  
    13. /** green. */
    14. GREEN(3887386),
    15.  
    16. /** brown. */
    17. BROWN(5320730),
    18.  
    19. /** blue. */
    20. BLUE(2437522),
    21.  
    22. /** purple. */
    23. PURPLE(8073150),
    24.  
    25. /** cyan. */
    26. CYAN(2651799),
    27.  
    28. /** silver. */
    29. SILVER(2651799),
    30.  
    31. /** gray. */
    32. GRAY(4408131),
    33.  
    34. /** pink. */
    35. PINK(14188952),
    36.  
    37. /** lime. */
    38. LIME(4312372),
    39.  
    40. /** yellow. */
    41. YELLOW(14602026),
    42.  
    43. /** light blue. */
    44. LIGHT_BLUE(6719955),
    45.  
    46. /** magenta. */
    47. MAGENTA(12801229),
    48.  
    49. /** orange. */
    50. ORANGE(15435844),
    51.  
    52. /** white. */
    53. WHITE(15790320);
    54.  
    55. private int color;
    56.  
    57. private ArmorColor(int color){
    58. this.color = color;
    59. }
    60.  
    61. /**
    62.   * Gets the color.
    63.   *
    64.   * @return color
    65.   */
    66. public int getColor(){
    67. return color;
    68. }
    69. }
    70.  



    ColorConverter(needed in code above, I don't take credit for it):
    Code:java
    1. package com.stirante.PrettyScaryLib;
    2. /**
    3.  * ColorConverter. I don't take credit for this class.
    4.  */
    5. public class ColorConverter {
    6.  
    7. /**
    8.   * To hex.
    9.   *
    10.   * @param r the r
    11.   * @param g the g
    12.   * @param b the b
    13.   * @return the string
    14.   */
    15. public static String toHex(int r, int g, int b) {
    16. return "0x" + toBrowserHexValue(r) + toBrowserHexValue(g)
    17. + toBrowserHexValue(b);
    18. }
    19.  
    20. /**
    21.   * To browser hex value.
    22.   *
    23.   * @param number the number
    24.   * @return the string
    25.   */
    26. private static String toBrowserHexValue(int number) {
    27. StringBuilder builder = new StringBuilder(
    28. Integer.toHexString(number & 0xff));
    29. while (builder.length() < 2) {
    30. builder.append("0");
    31. }
    32. return builder.toString().toUpperCase();
    33. }
    34.  
    35. }
    36.  


    Changing effects and level of beacon:
    Code:java
    1. package com.stirante.PrettyScaryLib;
    2. import java.lang.reflect.Field;
    3. import net.minecraft.server.TileEntityBeacon;
    4. import org.bukkit.block.Block;
    5. import org.bukkit.craftbukkit.CraftWorld;
    6. import org.bukkit.potion.PotionEffectType;
    7. /**
    8.  * Class, that allows modifying beacon block's effects.
    9.  */
    10. public class BeaconHelper {
    11.  
    12. /**
    13.   * Sets the level.
    14.   *
    15.   * @param block beacon
    16.   * @param level level
    17.   */
    18. public static void setLevel(Block block, int level) {
    19. if (!isApplicable(block))
    20. return ;
    21. TileEntityBeacon beacon = (TileEntityBeacon)((CraftWorld)block.getWorld()).getHandle().getTileEntity(block.getX(), block.getY(), block.getZ());
    22. Field lvl = null;
    23. try {
    24. lvl = TileEntityBeacon.class.getDeclaredField("e");
    25. lvl.setAccessible(true);
    26. lvl.set(beacon, level);
    27. }
    28. e.printStackTrace();
    29. }
    30. catch (SecurityException e) {
    31. e.printStackTrace();
    32. }
    33. e.printStackTrace();
    34. }
    35. e.printStackTrace();
    36. }
    37. }
    38.  
    39. /**
    40.   * Gets the level.
    41.   *
    42.   * @param block beacon
    43.   * @return level
    44.   */
    45. public static int getLevel(Block block) {
    46. if (!isApplicable(block))
    47. return -1;
    48. TileEntityBeacon beacon = (TileEntityBeacon)((CraftWorld)block.getWorld()).getHandle().getTileEntity(block.getX(), block.getY(), block.getZ());
    49. Field lvl = null;
    50. try {
    51. lvl = TileEntityBeacon.class.getDeclaredField("e");
    52. lvl.setAccessible(true);
    53. int level = lvl.getInt(beacon);
    54. return level;
    55. }
    56. e.printStackTrace();
    57. }
    58. catch (SecurityException e) {
    59. e.printStackTrace();
    60. }
    61. e.printStackTrace();
    62. }
    63. e.printStackTrace();
    64. }
    65. return 0;
    66. }
    67.  
    68. /**
    69.   * Sets the primary effect.
    70.   *
    71.   * @param block beacon
    72.   * @param effect PotionEffectType to apply
    73.   */
    74. public static void setPrimaryEffect(Block block, PotionEffectType effect) {
    75. if (!isApplicable(block))
    76. return ;
    77. TileEntityBeacon beacon = (TileEntityBeacon)((CraftWorld)block.getWorld()).getHandle().getTileEntity(block.getX(), block.getY(), block.getZ());
    78. Field eff = null;
    79. try {
    80. eff = TileEntityBeacon.class.getDeclaredField("f");
    81. eff.setAccessible(true);
    82. eff.set(beacon, effect.getId());
    83. }
    84. e.printStackTrace();
    85. }
    86. catch (SecurityException e) {
    87. e.printStackTrace();
    88. }
    89. e.printStackTrace();
    90. }
    91. e.printStackTrace();
    92. }
    93. }
    94.  
    95. /**
    96.   * Gets the primary effect.
    97.   *
    98.   * @param block beacon
    99.   * @return effect
    100.   */
    101. public static PotionEffectType getPrimaryEffect(Block block) {
    102. if (!isApplicable(block))
    103. return null;
    104. TileEntityBeacon beacon = (TileEntityBeacon)((CraftWorld)block.getWorld()).getHandle().getTileEntity(block.getX(), block.getY(), block.getZ());
    105. Field eff = null;
    106. try {
    107. eff = TileEntityBeacon.class.getDeclaredField("f");
    108. eff.setAccessible(true);
    109. PotionEffectType effect = PotionEffectType.getById(eff.getInt(beacon));
    110. return effect;
    111. }
    112. e.printStackTrace();
    113. }
    114. catch (SecurityException e) {
    115. e.printStackTrace();
    116. }
    117. e.printStackTrace();
    118. }
    119. e.printStackTrace();
    120. }
    121. return null;
    122. }
    123.  
    124. /**
    125.   * Sets the secondary effect.
    126.   *
    127.   * @param block beacon
    128.   * @param effect PotionEffectType to apply
    129.   */
    130. public static void setSecondaryEffect(Block block, PotionEffectType effect) {
    131. if (!isApplicable(block))
    132. return ;
    133. TileEntityBeacon beacon = (TileEntityBeacon)((CraftWorld)block.getWorld()).getHandle().getTileEntity(block.getX(), block.getY(), block.getZ());
    134. Field eff = null;
    135. try {
    136. eff = TileEntityBeacon.class.getDeclaredField("g");
    137. eff.setAccessible(true);
    138. eff.set(beacon, effect.getId());
    139. }
    140. e.printStackTrace();
    141. }
    142. catch (SecurityException e) {
    143. e.printStackTrace();
    144. }
    145. e.printStackTrace();
    146. }
    147. e.printStackTrace();
    148. }
    149. }
    150.  
    151. /**
    152.   * Gets the secondary effect.
    153.   *
    154.   * @param block beacon
    155.   * @return effect
    156.   */
    157. public static PotionEffectType getSecondaryEffect(Block block) {
    158. if (!isApplicable(block))
    159. return null;
    160. TileEntityBeacon beacon = (TileEntityBeacon)((CraftWorld)block.getWorld()).getHandle().getTileEntity(block.getX(), block.getY(), block.getZ());
    161. Field eff = null;
    162. try {
    163. eff = TileEntityBeacon.class.getDeclaredField("g");
    164. eff.setAccessible(true);
    165. PotionEffectType effect = PotionEffectType.getById(eff.getInt(beacon));
    166. return effect;
    167. }
    168. e.printStackTrace();
    169. }
    170. catch (SecurityException e) {
    171. e.printStackTrace();
    172. }
    173. e.printStackTrace();
    174. }
    175. e.printStackTrace();
    176. }
    177. return null;
    178. }
    179.  
    180. /**
    181.   * Checks if is applicable.
    182.   *
    183.   * @param block the block
    184.   * @return true, if is applicable
    185.   */
    186. private static boolean isApplicable(Block block) {
    187. switch (block.getType()) {
    188. case BEACON:
    189. return true;
    190. default:
    191. return false;
    192. }
    193. }
    194. }
    195.  


    Changing mobs' equipment:
    Code:java
    1. package com.stirante.PrettyScaryLib;
    2. import net.minecraft.server.EntityLiving;
    3. import org.bukkit.craftbukkit.entity.CraftLivingEntity;
    4. import org.bukkit.craftbukkit.inventory.CraftItemStack;
    5. import org.bukkit.entity.LivingEntity;
    6. import org.bukkit.inventory.ItemStack;
    7. /**
    8.  * Class, that allows setting and getting equipment of mob.
    9.  */
    10. public class EntityEquipment {
    11.  
    12. /**
    13.   * Sets equipment.
    14.   *
    15.   * @param mob mob
    16.   * @param item item
    17.   * @param slot slot
    18.   */
    19. public static void setEquipment(LivingEntity mob, ItemStack item, int slot) {
    20. if (!isApplicable(mob))
    21. return ;
    22. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    23. net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();
    24. ent.setEquipment(slot, itemStack);
    25. }
    26.  
    27. /**
    28.   * Gets equipment.
    29.   *
    30.   * @param mob mob
    31.   * @param slot slot
    32.   * @return equiped item
    33.   */
    34. public static ItemStack getEquipment(LivingEntity mob, int slot) {
    35. if (!isApplicable(mob))
    36. return null;
    37. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    38. return new CraftItemStack(ent.getEquipment(slot));
    39. }
    40.  
    41. /**
    42.   * Sets weapon.
    43.   *
    44.   * @param mob mob
    45.   * @param item item
    46.   */
    47. public static void setWeapon(LivingEntity mob, ItemStack item) {
    48. if (!isApplicable(mob))
    49. return ;
    50. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    51. net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();
    52. ent.setEquipment(0, itemStack);
    53. }
    54.  
    55. /**
    56.   * Gets weapon.
    57.   *
    58.   * @param mob mob
    59.   * @return weapon
    60.   */
    61. public static ItemStack getWeapon(LivingEntity mob) {
    62. if (!isApplicable(mob))
    63. return null;
    64. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    65. return new CraftItemStack(ent.getEquipment(0));
    66. }
    67.  
    68. /**
    69.   * Sets helmet.
    70.   *
    71.   * @param mob mob
    72.   * @param item item
    73.   */
    74. public static void setHelmet(LivingEntity mob, ItemStack item) {
    75. if (!isApplicable(mob))
    76. return ;
    77. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    78. net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();
    79. ent.setEquipment(1, itemStack);
    80. }
    81.  
    82. /**
    83.   * Gets helmet.
    84.   *
    85.   * @param mob mob
    86.   * @return helmet
    87.   */
    88. public static ItemStack getHelmet(LivingEntity mob) {
    89. if (!isApplicable(mob))
    90. return null;
    91. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    92. return new CraftItemStack(ent.getEquipment(1));
    93. }
    94.  
    95. /**
    96.   * Sets chestplate.
    97.   *
    98.   * @param mob mob
    99.   * @param item item
    100.   */
    101. public static void setChestplate(LivingEntity mob, ItemStack item) {
    102. if (!isApplicable(mob))
    103. return ;
    104. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    105. net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();
    106. ent.setEquipment(2, itemStack);
    107. }
    108.  
    109. /**
    110.   * Gets chestplate.
    111.   *
    112.   * @param mob mob
    113.   * @return chestplate
    114.   */
    115. public static ItemStack getChestplate(LivingEntity mob) {
    116. if (!isApplicable(mob))
    117. return null;
    118. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    119. return new CraftItemStack(ent.getEquipment(2));
    120. }
    121.  
    122. /**
    123.   * Sets pants.
    124.   *
    125.   * @param mob mob
    126.   * @param item item
    127.   */
    128. public static void setPants(LivingEntity mob, ItemStack item) {
    129. if (!isApplicable(mob))
    130. return ;
    131. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    132. net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();
    133. ent.setEquipment(3, itemStack);
    134. }
    135.  
    136. /**
    137.   * Gets pants.
    138.   *
    139.   * @param mob mob
    140.   * @return pants
    141.   */
    142. public static ItemStack getPants(LivingEntity mob) {
    143. if (!isApplicable(mob))
    144. return null;
    145. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    146. return new CraftItemStack(ent.getEquipment(3));
    147. }
    148.  
    149. /**
    150.   * Sets boots.
    151.   *
    152.   * @param mob mob
    153.   * @param item item
    154.   */
    155. public static void setBoots(LivingEntity mob, ItemStack item) {
    156. if (!isApplicable(mob))
    157. return ;
    158. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    159. net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();
    160. ent.setEquipment(4, itemStack);
    161. }
    162.  
    163. /**
    164.   * Gets boots.
    165.   *
    166.   * @param mob mob
    167.   * @return boots
    168.   */
    169. public static ItemStack getBoots(LivingEntity mob) {
    170. if (!isApplicable(mob))
    171. return null;
    172. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    173. return new CraftItemStack(ent.getEquipment(4));
    174. }
    175.  
    176. /**
    177.   * Checks if is applicable.
    178.   *
    179.   * @param entity entity
    180.   * @return true, if is applicable
    181.   */
    182. private static boolean isApplicable(LivingEntity entity) {
    183. switch (entity.getType()) {
    184. case ZOMBIE:
    185. case SKELETON:
    186. return true;
    187. default:
    188. return false;
    189. }
    190. }
    191. }
    192.  


    Changing skull's skin:
    Code:java
    1.  
    2. package com.stirante.NBTTagAPI;
    3.  
    4. import net.minecraft.server.NBTTagCompound;
    5.  
    6. import org.bukkit.craftbukkit.inventory.CraftItemStack;
    7. import org.bukkit.inventory.ItemStack;
    8.  
    9. public class Skull {
    10. public static ItemStack setSkin(ItemStack item, String nick){
    11. CraftItemStack craftStack = null;
    12. net.minecraft.server.ItemStack itemStack = null;
    13. if (item instanceof CraftItemStack) {
    14. craftStack = (CraftItemStack) item;
    15. itemStack = craftStack.getHandle();
    16. }
    17. else if (item instanceof ItemStack) {
    18. craftStack = new CraftItemStack(item);
    19. itemStack = craftStack.getHandle();
    20. }
    21. NBTTagCompound tag = itemStack.tag;
    22. if (tag == null) {
    23. tag = new NBTTagCompound();
    24. }
    25. tag.setString("SkullOwner", nick);
    26. itemStack.tag = tag;
    27. return craftStack;
    28. }
    29. public static String getSkin(ItemStack item){
    30. CraftItemStack craftStack = null;
    31. net.minecraft.server.ItemStack itemStack = null;
    32. if (item instanceof CraftItemStack) {
    33. craftStack = (CraftItemStack) item;
    34. itemStack = craftStack.getHandle();
    35. }
    36. else if (item instanceof ItemStack) {
    37. craftStack = new CraftItemStack(item);
    38. itemStack = craftStack.getHandle();
    39. }
    40. NBTTagCompound tag = itemStack.tag;
    41. if (tag == null) {
    42. tag = new NBTTagCompound();
    43. return null;
    44. }
    45. return tag.getString("SkullOwner");
    46. }
    47. }
    48.  
     
  2. Offline

    Infamous Jeezy

    Thanks.
    These classes are pretty cool, do the skull skins save when the specified player disconnects though?
     
  3. Offline

    Shiny Quagsire

    Yes, it just uses DinnerBone's hidden feature of skulls. It saves the data to the NBT Tag of the item, which goes to the tile entity of the block.

    Quick question: How does the wolf collar colors work? I tried to use it, but I don't know what item to apply it to.
     
    Infamous Jeezy likes this.
  4. Offline

    stirante

    Oh God, I'm stupid. Code for collars is very wrong. I'll update it today.

    EDIT:Code updated :D
     
  5. Offline

    hawkfalcon

    Is there a way to just reference colored armor directly, as a material with data?
     
  6. Offline

    gomeow

    Just a question, but this is in the API right? We don't need to include all that code when we go to do it?
     
  7. Offline

    stirante

    You don't have to include all that code. I wrote every method to be self-contained, but some methods needs ColorConverter class.
    Btw. All that code will be in my plugin. This plugin will also have an API.
     
  8. Offline

    Hoolean

    You should put a pull request in, so this can be included in the Bukkit API!
     
  9. Offline

    Malikk

    stirante

    Entities who have had their weapon set to a bow don't/can't fire it. Surely we don't have to give them arrows... lol

    Any ideas?
     
  10. Offline

    Hoolean

    Cancel the event, check if they have a bow in their hands then use LivingEntity.shootProjectile();
     
  11. Offline

    Malikk

    That's easy enough, I guess. Thanks.
     
  12. Offline

    Armadillo

    Very nie. Please request for Bukkit to put it in the API.
     
  13. Offline

    md_5

    In regards to everywhere where you parse an integer, use Integer.decode(...)
    If its starts with 0x or # it will do base 16 (hex) and other neat tricks.
     
  14. Offline

    stirante

    Thanks!

    Code updated :)
     
  15. Offline

    md_5

    There is a java.awt.Color method to elimate line 29. Right now you are taking 3 ints -> string -> 1 int
     
  16. Offline

    stirante

    I updated every class (formatting :/). Also I added support DyeColor and Color to CollarColor and Armor.

    I'm adding pull requests :)

    md_5 How to connect CraftBukkit with Bukkit? I thought that every Bukkit class extends CraftBukkit class

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  17. Offline

    md_5

    Other way round, what you need to do is now create an interface in Bukkit matching the methods you have added. These interfaces must be javadoc'd too.

    So for your CraftItemStack.setName(..)
    You need in Bukkit:
    ItemStack.setName(..)

    Code:
    /**
    * Set the name of this item, visible in the players inventory.
    *
    * @param name the new display name of this item
    */
    Or something similar.
     
  18. Offline

    Malikk

    That doesn't work. The event is never fired.
     
  19. Offline

    Me4502

    Use the entity attack event, or entity target event... or entity move event, bow shoot event won't fire.
     
  20. Offline

    Malikk

    EntityTarget and EntityMove would both fire about a thousand more times than I need and EntityAttack simply doesn't exist...

    I think I'll just be waiting this one out for a fix. *Sigh*
     
  21. Offline

    stirante

    You can create custom mob which has AI of skeleton
     
  22. Offline

    Uniclaw

    Hm, the Skull-Skin changing not works :( I've copied the methods into my main, and then make this:
    ItemStack is = new ItemStack(397, 1);
    ItemStack isc = Sc.setSkin(is, "myname(censored)");
    p.getInventory().addItem(isc);

    But the skin is still the skeleton..
     
  23. Offline

    stirante

    Try this:

    ItemStack is = new ItemStack(397, 3);
    ItemStack isc = Sc.setSkin(is, "myname(censored)");
    p.getInventory().addItem(isc);
     
  24. Offline

    Uniclaw

    Thanks :D !

    PS: It's not new ItemStack(397, 3), but new ItemSTack(397, amount, (byte)3) :D
     
  25. Offline

    stirante

    Yea, I forgot about it :D
     
  26. Offline

    Uniclaw

    One thing: How i can give a default texture (stone, grass, and so on) to the skull? stirante
     
  27. Offline

    stirante

    Uniclaw I don't think it's possible, but I'll check code

    EDIT: It's impossible :(
     
  28. Offline

    Uniclaw

    Ah, ok :(

    Hm.. I have a Idea to mybe changing the texture.. But one Problem: how i can acces to an protected constructor? stirante

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

    stirante

    I don't think it's possible. Look at TileEntitySkullRenderer:
    Code:java
    1.  
    2. if (par7Str != null && par7Str.length() > 0)
    3. {
    4. String var9 = "[URL]http://skins.minecraft.net/MinecraftSkins/[/URL]" + StringUtils.stripControlCodes(par7Str) + ".png";
    5.  
    6. if (!field_82397_a.tileEntityRenderer.renderEngine.func_82773_c(var9))
    7. {
    8. field_82397_a.tileEntityRenderer.renderEngine.obtainImageData(var9, new ImageBufferDownload());
    9. }
    10.  
    11. this.func_82392_a(var9, "/mob/char.png");
    12. }
    13. else
    14. {
    15. this.bindTextureByName("/mob/char.png");
    16. }
    17.  


    I added pull request(https://github.com/Bukkit/Bukkit/pull/710, https://github.com/Bukkit/CraftBukkit/pull/919)
    And I'm planning NBTData class. It would work like that:

    In ItemStack there will be function getNBTData() which will return instance of NBTData class. There will be also classes extending NBTData (for example SkullData, WrittenBookData, ArmorData). So if someone want to change skin of skull he will write something like that:
    SkullData data = (SkullData)itemStack.getNBTData(); data.setSkin("stirante");

    What you think?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  30. Offline

    chaseoes

    How do we use the armor one? I'm not sure what I'm supposed to pass it for the color, since it's supposed to be an int.

    Edit: Nevermind! Didn't see the one that accepts a Color.
     
Thread Status:
Not open for further replies.

Share This Page