Make a recipe with a custom recipe display name! Help!

Discussion in 'Plugin Development' started by Jetsinsu, Sep 21, 2014.

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

    Jetsinsu

    How do I make a recipe that would use a custom item name than a Material name? Since I made a custom named item, idk how to only accept the Displayname, it always only works with regular item.

    If you don't understand heres the code

    Code:java
    1. ItemStack mlb = new ItemStack(Material.FIREBALL, 1);
    2. ItemMeta meta23 = mlb.getItemMeta();
    3. meta23.setDisplayName(ChatColor.GRAY + "Magic Lava Ball");
    4. meta23.setLore(Arrays.asList("Hmm, seems like it needs to be mixed with lava."));
    5. mlb.setItemMeta(meta23);
    6.  
    7. ShapedRecipe mlbrecipe = new ShapedRecipe(mlb);
    8. mlbrecipe.shape(
    9. " @ ",
    10. "@@@",
    11. " @ ");
    12. mlbrecipe.setIngredient('@', Material.BLAZE_POWDER);
    13. //if material display name name is " "
    14. Bukkit.getServer().addRecipe(mlbrecipe);
     
  2. Offline

    SyTeck

    Maybe you can use the PrepareItemCraftEvent event?
     
  3. Offline

    Jetsinsu

    SyTeck
    I did but it wouldn't work :|
     
  4. Offline

    ChipDev

    Use setIngredient('@', YourItemStack);
     
  5. Offline

    SyTeck

  6. Offline

    Jetsinsu

    ChipDev
    Seems like the method doesn't take ItemStacks, it says it takes MaterialData.......
     
  7. Offline

    FabeGabeMC

  8. Offline

    fireblast709

    Jetsinsu Give your named ItemStack a datavalue which vanilla doesn't use, and use that in the recipe. This works for every item that doesn't use the datavalue (for example, this method doesn't work with tools and dyes)
     
  9. Offline

    Jetsinsu

    fireblast709
    I don't know much about datavaules yet, can you give me an example?
     
  10. Offline

    fireblast709

    Jetsinsu for example, wool. White wool has a datavalue of 0, orange wool has a datavalue of 1, etc. By using this value when it is absent (so it won't work that well with wool since wool actually uses the datavalue to differentiate between different colours), we can define 'special' items without too much trickery.

    Say I have a special stick, I could give the player a stick with datavalue 1. Since sticks don't use the datavalue, MC will treat it like any stick. But as soon as we use it in a recipe where an ingredient is a stick with datavalue 1, only that stick can be used in the recipe (and normal sticks, with datavalue 0, won't work).
     
  11. Offline

    Jetsinsu

    fireblast709

    Would this work?
    Code:java
    1. ItemStack is = new ItemStack(Material.BLAZE_POWDER, 1, (short) 1);
    2. ItemMeta m = is.getItemMeta();
    3. m.setDisplayName(ChatColor.GRAY + "Magic Lava Particles");
    4. m.setLore(Arrays.asList("Perhaps this can be made into a ball, but how?"));
    5. is.setItemMeta(m);
     
  12. Offline

    fireblast709

  13. Offline

    Jetsinsu

    fireblast709
    Hmm, seems like it doesn't work when I use this code.

    Code:java
    1. mlbrecipe.setIngredient('@', is.getData());
     
  14. Offline

    fireblast709

    Jetsinsu doesn't work as in "nothing shows up"?
     
  15. Offline

    Jetsinsu

    @fireblast709
    Nope. When I craft withe the custom item, it doesn't show up :\
     
  16. Offline

    fireblast709

    Jetsinsu how did you get the custom items in your inventory?
     
  17. Offline

    Jetsinsu

    @fireblast709
    After killing a monster.
     
  18. Offline

    fireblast709

    Jetsinsu I meant more code-wise. Do you drop an ItemStack with datavalue 1 as well?
     
  19. Offline

    Jetsinsu

    fireblast709

    Welp. Idk what's wrong but maybe you can find out.

    Code:java
    1. ItemStack is = new ItemStack(Material.BLAZE_POWDER, 1, (short) 1);


    Code:java
    1. @EventHandler
    2. public void EntityDeath(EntityDeathEvent e){
    3. PigZombie pz = (PigZombie) e.getEntity();
    4. if (et == EntityType.PIG_ZOMBIE){
    5. if (pz.getCustomName() == "Master Zompig"){
    6. if (r.nextInt(100) < 99){
    7. if (dc == DamageCause.ENTITY_ATTACK && e.getEntity().getKiller() != null){
    8. ItemMeta m = is.getItemMeta();
    9. m.setDisplayName(ChatColor.GRAY + "Magic Lava Particles");
    10. m.setLore(Arrays.asList("Perhaps this can be crafted into a ball."));
    11. is.setItemMeta(m);
    12. loc.getWorld().dropItemNaturally(loc, is);
    13. }
    14. }
    15. }
    16. }
     
  20. Offline

    Jetsinsu

    fireblast709
    Can you check if the coding is correct on the top of this post? Thanks!
     
  21. Offline

    fireblast709

    Jetsinsu That looks correct as well. Give me a second to write some test code.

    [EDIT] Just tested it and it worked fine for me
     
  22. Offline

    Jetsinsu

    fireblast709
    Are you able to craft the items with the other code because mine doesn't work :\
     
  23. Offline

    fireblast709

    Jetsinsu can you post all the code regarding the recipe registering, dropping, and even events that manipulate crafting (Note: I don't use any crafting events in my version - it could very well be the cause of your issue)
     
  24. Offline

    Jetsinsu

    fireblast709

    Here you go.

    Code:java
    1. package me.jetsinsu.powerup;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5. import java.util.HashMap;
    6. import java.util.List;
    7. import java.util.Random;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.GameMode;
    12. import org.bukkit.Location;
    13. import org.bukkit.Material;
    14. import org.bukkit.SkullType;
    15. import org.bukkit.Sound;
    16. import org.bukkit.block.Block;
    17. import org.bukkit.block.BlockFace;
    18. import org.bukkit.block.Skull;
    19. import org.bukkit.entity.Arrow;
    20. import org.bukkit.entity.Entity;
    21. import org.bukkit.entity.EntityType;
    22. import org.bukkit.entity.LivingEntity;
    23. import org.bukkit.entity.PigZombie;
    24. import org.bukkit.entity.Player;
    25. import org.bukkit.event.EventHandler;
    26. import org.bukkit.event.Listener;
    27. import org.bukkit.event.block.Action;
    28. import org.bukkit.event.block.BlockPlaceEvent;
    29. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    30. import org.bukkit.event.entity.EntityDamageEvent;
    31. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    32. import org.bukkit.event.entity.EntityDeathEvent;
    33. import org.bukkit.event.inventory.InventoryCloseEvent;
    34. import org.bukkit.event.player.PlayerInteractEntityEvent;
    35. import org.bukkit.event.player.PlayerInteractEvent;
    36. import org.bukkit.event.player.PlayerMoveEvent;
    37. import org.bukkit.event.player.PlayerRespawnEvent;
    38. import org.bukkit.event.player.PlayerToggleFlightEvent;
    39. import org.bukkit.inventory.ItemStack;
    40. import org.bukkit.inventory.ShapedRecipe;
    41. import org.bukkit.inventory.ShapelessRecipe;
    42. import org.bukkit.inventory.meta.ItemMeta;
    43. import org.bukkit.plugin.java.JavaPlugin;
    44. import org.bukkit.potion.PotionEffect;
    45. import org.bukkit.potion.PotionEffectType;
    46. import org.bukkit.scheduler.BukkitRunnable;
    47. import org.bukkit.util.Vector;
    48.  
    49. public class PowerUp extends JavaPlugin implements Listener{
    50.  
    51. String powerup = (ChatColor.GREEN + "[" + ChatColor.AQUA + "PowerUp" + ChatColor.GREEN + "]" + ChatColor.AQUA);
    52.  
    53. String world = this.getConfig().getString("PowerUp.worlds");
    54. List<String> enabledWorlds = Arrays.asList(world);
    55. ArrayList<String> cooldown = new ArrayList<String>();
    56. ArrayList<String> cooldown2 = new ArrayList<String>();
    57.  
    58. HashMap<String, Double> playerHealth = new HashMap<String, Double>();
    59.  
    60. PowerUp plugin = this;
    61.  
    62. int ID = 0;
    63.  
    64. private Random r = new Random();
    65.  
    66. ItemStack is = new ItemStack(Material.BLAZE_POWDER, 1, (short) 1);
    67.  
    68. public void onEnable(){
    69. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    70.  
    71. recipe();
    72. }
    73.  
    74. @EventHandler
    75. public void EntityDeath(EntityDeathEvent e){
    76. EntityType et = e.getEntity().getType();
    77. DamageCause dc = e.getEntity().getLastDamageCause().getCause();
    78. Location loc = e.getEntity().getLocation();
    79. if (et == EntityType.PIG_ZOMBIE){
    80. if (r.nextInt(100) < 1){
    81. if (dc == DamageCause.ENTITY_ATTACK && e.getEntity().getKiller() != null){
    82. dropSkull(loc, 2);
    83. }
    84. }
    85. }
    86.  
    87. PigZombie pz = (PigZombie) e.getEntity();
    88. if (et == EntityType.PIG_ZOMBIE){
    89. if (pz.getCustomName() == "Master Zompig"){
    90. if (r.nextInt(100) < 99){
    91. if (dc == DamageCause.ENTITY_ATTACK && e.getEntity().getKiller() != null){
    92. ItemMeta m = is.getItemMeta();
    93. m.setDisplayName(ChatColor.GRAY + "Magic Lava Particles");
    94. m.setLore(Arrays.asList("Perhaps this can be crafted into a ball."));
    95. is.setItemMeta(m);
    96. loc.getWorld().dropItemNaturally(loc, is);
    97. }
    98. }
    99. }
    100. }
    101. }
    102.  
    103. private void dropSkull(Location loc, int type){
    104. loc.getWorld().dropItemNaturally(loc, new ItemStack(Material.SKULL_ITEM, 1, (byte) type));
    105. }
    106.  
    107. @SuppressWarnings("deprecation")
    108. @EventHandler
    109. public void onBlockPlace(BlockPlaceEvent e){
    110. int x = e.getBlock().getX();
    111. int y = e.getBlock().getY();
    112. int z = e.getBlock().getZ();
    113. Location l = new Location(e.getBlock().getWorld(), x, y-1, z);
    114. Location l2 = new Location(e.getBlock().getWorld(), x, y-2, z);
    115. int b = l.getBlock().getTypeId();
    116. int b2 = l2.getBlock().getTypeId();
    117. Block b3 = l.getBlock();
    118. Block b4 = l2.getBlock();
    119.  
    120. if (e.getPlayer() instanceof Player){
    121. @SuppressWarnings("unused")
    122. Player p = (Player) e.getPlayer();
    123. if (e.getBlock().getState() instanceof Skull){
    124. Skull skull = (Skull) e.getBlock().getState();
    125. SkullType skullType = skull.getSkullType();
    126. if (skullType == SkullType.ZOMBIE){
    127. if (b == 88 && b2 == 88){
    128. e.getBlock().setType(Material.AIR);
    129. b3.setType(Material.AIR);
    130. b4.setType(Material.AIR);
    131. final PigZombie pz = (PigZombie) e.getBlock().getWorld().spawnCreature(e.getBlock().getLocation(), EntityType.PIG_ZOMBIE);
    132. pz.setCustomName("Master Zompig");
    133. pz.setCustomNameVisible(true);
    134. new BukkitRunnable(){
    135.  
    136. @Override
    137. public void run() {
    138. int x = pz.getLocation().getBlock().getX();
    139. int y = pz.getLocation().getBlock().getY();
    140. int z = pz.getLocation().getBlock().getZ();
    141. Location l = new Location(pz.getWorld(), x, y+2, z);
    142. Location l2 = new Location(pz.getWorld(), x+1, y, z);
    143. Location l3 = new Location(pz.getWorld(), x+1, y+1, z);
    144. Location l4 = new Location(pz.getWorld(), x-1, y, z);
    145. Location l5 = new Location(pz.getWorld(), x-1, y+1, z);
    146. Location l6 = new Location(pz.getWorld(), x, y, z+1);
    147. Location l7 = new Location(pz.getWorld(), x, y+1, z+1);
    148. Location l8 = new Location(pz.getWorld(), x, y, z-1);
    149. Location l9 = new Location(pz.getWorld(), x, y+1, z-1);
    150. int b = l.getBlock().getTypeId();
    151. int b2 = l2.getBlock().getTypeId();
    152. int b3 = l3.getBlock().getTypeId();
    153. int b4 = l4.getBlock().getTypeId();
    154. int b5 = l5.getBlock().getTypeId();
    155. int b6 = l6.getBlock().getTypeId();
    156. int b7 = l7.getBlock().getTypeId();
    157. int b8 = l8.getBlock().getTypeId();
    158. int b9 = l9.getBlock().getTypeId();
    159. Block b10 = l.getBlock();
    160. Block b11 = l2.getBlock();
    161. Block b12 = l3.getBlock();
    162. Block b13 = l4.getBlock();
    163. Block b14 = l5.getBlock();
    164. Block b15 = l6.getBlock();
    165. Block b16 = l7.getBlock();
    166. Block b17 = l8.getBlock();
    167. Block b18 = l9.getBlock();
    168. pz.setVelocity(pz.getLocation().getDirection().multiply(2));
    169.  
    170. if (b != 0){
    171. b10.breakNaturally();
    172. }
    173.  
    174. if (b2 != 0){
    175. b11.breakNaturally();
    176. }
    177.  
    178. if (b3 != 0){
    179. b12.breakNaturally();
    180. }
    181.  
    182. if (b4 != 0){
    183. b13.breakNaturally();
    184. }
    185.  
    186. if (b5 != 0){
    187. b14.breakNaturally();
    188. }
    189.  
    190. if (b6 != 0){
    191. b15.breakNaturally();
    192. }
    193.  
    194. if (b7 != 0){
    195. b16.breakNaturally();
    196. }
    197.  
    198. if (b8 != 0){
    199. b17.breakNaturally();
    200. }
    201.  
    202. if (b9 != 0){
    203. b18.breakNaturally();
    204. }
    205. }
    206. }.runTaskTimer(this, 20L, 60);
    207. }
    208. }
    209. }
    210. }
    211. }
    212.  
    213. private void recipe(){
    214.  
    215. //Magic Lava Ball
    216. ItemStack mlb = new ItemStack(Material.FIREBALL, 1);
    217. ItemMeta meta23 = mlb.getItemMeta();
    218. meta23.setDisplayName(ChatColor.GRAY + "Magic Lava Ball");
    219. meta23.setLore(Arrays.asList("Hmm, seems like it needs to be mixed with lava."));
    220. mlb.setItemMeta(meta23);
    221.  
    222. ShapedRecipe mlbrecipe = new ShapedRecipe(mlb);
    223. mlbrecipe.shape(
    224. " @ ",
    225. "@@@",
    226. " @ ");
    227. mlbrecipe.setIngredient('@', is.getData());
    228. Bukkit.getServer().addRecipe(mlbrecipe);
    229. }
    230. }
    231.  
     
Thread Status:
Not open for further replies.

Share This Page