How to link a certain recipe with a certain thing in the event listener

Discussion in 'Plugin Development' started by 17nhammond, Jan 10, 2014.

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

    17nhammond

    So I am working on a plugin where you can craft like 5 different furnaces. I want to make it so the first furnace cooks at (120), the next (140), the next (160), the next (180), and the next (200). I want to make it so that one of the recipes is set with the furnace to cook at 120 and the next recipe to cook at 140. I was wondering how you would do this?

    Event Listener:

    Code:java
    1. package me.NateD101.furnacespeeder;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.block.Furnace;
    9. import org.bukkit.entity.Entity;
    10. import org.bukkit.entity.Item;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.BlockBreakEvent;
    14. import org.bukkit.event.block.BlockPlaceEvent;
    15. import org.bukkit.event.inventory.FurnaceBurnEvent;
    16. import org.bukkit.event.inventory.FurnaceSmeltEvent;
    17. import org.bukkit.event.inventory.InventoryClickEvent;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.meta.ItemMeta;
    20.  
    21.  
    22.  
    23. public class EventListener implements Listener {
    24. HashMap<Location, String> data = new HashMap();
    25.  
    26. @EventHandler
    27. public void onPlaced(BlockPlaceEvent event) {
    28. if (event.getItemInHand() != null && event.getItemInHand().hasItemMeta()) {
    29. if (event.getItemInHand().getItemMeta().hasDisplayName()) {
    30. data.put(event.getBlock().getLocation(), event.getItemInHand().getItemMeta().getDisplayName());
    31. }
    32. }
    33. }
    34.  
    35. @EventHandler
    36. public void onBlockBroken(BlockBreakEvent event) {
    37. for (Location loc : data.keySet()) {
    38. if (event.getBlock().getLocation().equals(loc)) {
    39. try {
    40. for (Entity ent : event.getBlock().getLocation().getWorld().getEntities()) {
    41. if (ent instanceof Item) {
    42. Item item = (Item) ent;
    43. ItemStack j = item.getItemStack();
    44. if (item.getLocation().equals(event.getBlock().getLocation()) && j.getType() == event.getBlock().getType()) {
    45. ItemMeta k = j.getItemMeta();
    46. k.setDisplayName(data.get(loc));
    47. j.setItemMeta(k);
    48. //Sucess
    49. }
    50. //else no items, or couldn't find it
    51. }
    52. }
    53. } catch (Exception ex) {
    54. //Couldn't find the item
    55. }
    56. return;
    57. }
    58. }
    59. }
    60. // vanilla minecraft is 100 you can have a cooktime from 0-200
    61. Short cooktime = (short)200;
    62.  
    63. @EventHandler
    64. public void furnaceBurn(FurnaceBurnEvent event) {
    65. Furnace furnace = (Furnace) event.getBlock().getState();
    66. furnace.setCookTime(cooktime);
    67. }
    68.  
    69. @EventHandler
    70. public void furnaceSmeltEvent(FurnaceSmeltEvent event) {
    71. Furnace furnace = (Furnace) event.getBlock().getState();
    72. furnace.setCookTime(cooktime);
    73. }
    74.  
    75. @EventHandler
    76. public void onInventoryClick(InventoryClickEvent event) {
    77. Block blocktype = event.getWhoClicked().getTargetBlock(null, 10);
    78.  
    79. if (blocktype.getType() == Material.FURNACE || blocktype.getType() == Material.BURNING_FURNACE) {
    80. if ((event.getSlot() == 0 || event.getSlot() == 1) && event.getCursor().getType() != Material.AIR) {
    81. Furnace furnace = (Furnace) blocktype.getState();
    82. furnace.setCookTime(cooktime);
    83. furnace.getMetadata(null);
    84. }
    85. }
    86. }
    87.  
    88.  
    89.  
    90. }


    Main class:

    Code:java
    1. package me.NateD101.furnacespeeder;
    2. import java.util.ArrayList;
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.inventory.ItemStack;
    8. import org.bukkit.inventory.ShapedRecipe;
    9. import org.bukkit.inventory.meta.ItemMeta;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Furnacespeeder extends JavaPlugin
    14.  
    15. {
    16.  
    17. public final Logger logger = Logger.getLogger("Minecraft");
    18. public static Furnacespeeder plugin;
    19.  
    20. public void onDisable()
    21. {
    22. PluginDescriptionFile pdfFile = getDescription();
    23. this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    24. getServer().clearRecipes();
    25. }
    26.  
    27. public void onEnable()
    28. {
    29. getServer().getPluginManager().registerEvents(new EventListener(), this);
    30. PluginDescriptionFile pdfFile = getDescription();
    31. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
    32.  
    33. ItemStack ftwo = new ItemStack(Material.FURNACE, 1);
    34.  
    35. ItemMeta furnacetwo = ftwo.getItemMeta();
    36. furnacetwo.setDisplayName(ChatColor.RESET + "Furnace Two");
    37. ArrayList<String> loreftwo = new ArrayList();
    38. loreftwo.add(ChatColor.GOLD + "Furnace with Faster Cooking Time");
    39. furnacetwo.setLore(loreftwo);
    40. ftwo.setItemMeta(furnacetwo);
    41.  
    42. ShapedRecipe furnacetwo1 = new ShapedRecipe(new ItemStack(ftwo));
    43. furnacetwo1.shape(new String[]{"SSS", "S S", "SSS"});
    44. furnacetwo1.setIngredient('S', Material.STONE);
    45.  
    46.  
    47.  
    48. ItemStack fthree = new ItemStack(Material.FURNACE, 1);
    49.  
    50. ItemMeta furnacethree = fthree.getItemMeta();
    51. furnacethree.setDisplayName(ChatColor.RESET + "Furnace Three");
    52. ArrayList<String> lorefthree = new ArrayList();
    53. lorefthree.add(ChatColor.GOLD + "Furnace with Faster Cooking Time");
    54. furnacethree.setLore(lorefthree);
    55. fthree.setItemMeta(furnacethree);
    56.  
    57. ShapedRecipe furnacethree1 = new ShapedRecipe(new ItemStack(fthree));
    58. furnacethree1.shape(new String[]{"CCC", "C C", "CCC"});
    59. furnacethree1.setIngredient('C', Material.COAL);
    60.  
    61.  
    62.  
    63.  
    64.  
    65.  
    66. getServer().addRecipe(furnacetwo1);
    67. getServer().addRecipe(furnacethree1);
    68.  
    69.  
    70.  
    71. }
    72. }



    If you know how to do this I would appreciate it!
    Thanks!
    NateD101
     
  2. Offline

    DrJava

    You could store the furnaces in a hashmap (FurnaceLoc, CookTime), or use MeteData; but that's removed on block place.
     
  3. Offline

    JRL1004

    DrJava and Jake6177 like this.
  4. Offline

    17nhammond

    DrJava JRL1004
    How would I do that, Could you give me an example?
     
Thread Status:
Not open for further replies.

Share This Page