Crafting recipe not working

Discussion in 'Plugin Development' started by SuperSaltyGamer, Aug 28, 2015.

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

    SuperSaltyGamer

    Got this code in my onEnable event.
    Shows no errors but when I put the recipe in it doesn't work.

    Code:
            
            ItemStack isBackpack = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
            isBackpack.getItemMeta().setDisplayName("Backpack");
            isBackpack.getItemMeta().addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            isBackpack.getItemMeta().getLore().add("Backpack");
            ShapedRecipe srBackpack = new ShapedRecipe(isBackpack);
            srBackpack.shape("LLL", "LCL", "LLL");
            srBackpack.setIngredient('L', Material.LEATHER);
            srBackpack.setIngredient('C', Material.CHEST);
           
            getServer().addRecipe(srBackpack);
     
  2. How you handle the ItemMeta will not work. That's how you should do it instead:
    Code:
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName("...");
    meta.doSomeOtherStuff(1, 2, 3);
    item.setItemMeta(meta);
    Does your plugin even load? Use the command /plugins and look if it is in red
     
  3. Offline

    SuperSaltyGamer

    Of course it loads -.-
     
  4. Offline

    SuperSaltyGamer

    All the code in Main.java

    Code:
    package realism;
    
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.FoodLevelChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener
    {
    
        String springGrowth;
        String summerGrowth;
        String autumnGrowth;
        String winterGrowth;
       
        String springMessage;
        String summerMessage;
        String autumnMessage;
        String winterMessage;
       
        String clockMessage;
       
        Integer currentDay;
        Integer currentWeek;
        Integer currentMonth;
        Integer currentYear;
       
        public enum DateFormats
        {
           
            SHORT2(1),
            SHORT3(2),
            FULL(3);
    
            private final int value;
    
            private DateFormats(int value)
            {
                this.value = value;
            }
    
            public int getValue()
            {
                return this.value;
            }
           
        }
       
        public String formatDay(int day, DateFormats format)
        {
           
            if (format.getValue() == 1)
            {
               
                if (day == 0)
                {
                    return "Mo.";
                }
                if (day == 1)
                {
                    return "Tu.";
                }
                if (day == 2)
                {
                    return "We.";
                }
                if (day == 3)
                {
                    return "Th.";
                }
                if (day == 4)
                {
                    return "Fr.";
                }
                if (day == 5)
                {
                    return "Sa.";
                }
                if (day == 6)
                {
                    return "Su.";
                }
    
            }
            if (format.getValue() == 2)
            {
               
                if (day == 0)
                {
                    return "Mon.";
                }
                if (day == 1)
                {
                    return "Tue.";
                }
                if (day == 2)
                {
                    return "Wed.";
                }
                if (day == 3)
                {
                    return "Thu.";
                }
                if (day == 4)
                {
                    return "Fri.";
                }
                if (day == 5)
                {
                    return "Sat.";
                }
                if (day == 6)
                {
                    return "Sun.";
                }
               
            }
            if (format.getValue() == 3)
            {
               
                if (day == 0)
                {
                    return "Monday";
                }
                if (day == 1)
                {
                    return "Tuesday";
                }
                if (day == 2)
                {
                    return "Wednesday";
                }
                if (day == 3)
                {
                    return "Thursday";
                }
                if (day == 4)
                {
                    return "Friday";
                }
                if (day == 5)
                {
                    return "Saturday";
                }
                if (day == 6)
                {
                    return "Sunday";
                }
               
            }
           
            return null;
           
        }
       
        public String formatMonth(int month, DateFormats format)
        {
           
            if (format.getValue() == 2)
            {
               
                if (month == 0)
                {
                    return "Jan.";
                }
                if (month == 1)
                {
                    return "Feb.";
                }
                if (month == 2)
                {
                    return "Mar.";
                }
                if (month == 3)
                {
                    return "Apr.";
                }
                if (month == 4)
                {
                    return "May";
                }
                if (month == 5)
                {
                    return "June";
                }
                if (month == 6)
                {
                    return "July";
                }
                if (month == 7)
                {
                    return "Aug.";
                }
                if (month == 8)
                {
                    return "Sept.";
                }
                if (month == 9)
                {
                    return "Oct.";
                }
                if (month == 10)
                {
                    return "Nov.";
                }
                if (month == 11)
                {
                    return "Dec.";
                }
    
            }
            if (format.getValue() == 3)
            {
               
                if (month == 0)
                {
                    return "January";
                }
                if (month == 1)
                {
                    return "February";
                }
                if (month == 2)
                {
                    return "March";
                }
                if (month == 3)
                {
                    return "April";
                }
                if (month == 4)
                {
                    return "May";
                }
                if (month == 5)
                {
                    return "June";
                }
                if (month == 6)
                {
                    return "July";
                }
                if (month == 7)
                {
                    return "August";
                }
                if (month == 8)
                {
                    return "September";
                }
                if (month == 9)
                {
                    return "October";
                }
                if (month == 10)
                {
                    return "November";
                }
                if (month == 11)
                {
                    return "December";
                }
               
            }
           
            return null;
           
        }
       
        public String colorize(String str)
        {
           
            return ChatColor.translateAlternateColorCodes('&', str);
           
        }
       
        public int getWeek()
        {
           
            return currentWeek + 1;
           
        }
       
        public int getMonth()
        {
           
            return currentMonth + 1;
           
        }
       
         public int getMonthDay()
        {
           
            return (currentWeek * 7) + currentDay + 1;
           
        }
       
        public int getYearDay()
        {
           
            return  (currentMonth * 28) + (currentWeek * 7) + currentDay + 1;
           
        }
       
        public String getSeason()
        {
           
            if (currentMonth >= 0 && currentMonth < 3)
            {
                return colorize("&aSpring");
            }
            if (currentMonth >= 3 && currentMonth < 6)
            {
                return colorize("&2Summer");
            }
            if (currentMonth >= 6 && currentMonth < 9)
            {
                return colorize("&eAutumn");
            }
            if (currentMonth >= 9 && currentMonth < 12)
            {
                return colorize("&bWinter");
            }
           
            return null;
           
        }
       
        public String timeToString(long time, boolean h24)
        {
           
            int hours = (int) ((Math.floor(time / 1000.0) + 6) % 24);
            int minutes = (int) Math.floor((time % 1000) / 1000.0 * 60);
           
            if (h24)
            {
           
                return String.format("%02d:%02d", hours, minutes);
           
            }
            else
            {
               
                DateFormat idf = new SimpleDateFormat("HH:mm");
                DateFormat odf = new SimpleDateFormat("hh:mm");
               
                try {return odf.format(idf.parse(hours + ":" + minutes)).toString();} catch (ParseException e) {}
               
                return null;
               
            }
           
        }
       
        public String printDate()
        {
           
            return colorize(clockMessage.replace("%t-12%", timeToString(getServer().getWorlds().get(0).getTime(), false)).replace("%t-24%", timeToString(getServer().getWorlds().get(0).getTime(), true)).replace("%dy-f%", formatDay(currentDay, DateFormats.FULL)).replace("%dy-s%", formatDay(currentDay, DateFormats.SHORT3)).replace("%d%", String.valueOf(getMonthDay())).replace("%m-f%", formatMonth(currentMonth, DateFormats.FULL)).replace("%m-s%", formatMonth(currentMonth, DateFormats.SHORT3)).replace("%y%", String.format("%04d", currentYear)).replace("%s%", getSeason()));
           
        }
       
        public void onEnable()
        {
           
            getConfig().options().copyDefaults(true);
           
            saveDefaultConfig();
           
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            /*
            ItemStack isBackpack = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
            isBackpack.getItemMeta().setDisplayName("Backpack");
            isBackpack.getItemMeta().addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            isBackpack.getItemMeta().getLore().add("Backpack");
            ShapedRecipe srBackpack = new ShapedRecipe(isBackpack);
            srBackpack.shape("LLL", "LCL", "LLL");
            srBackpack.setIngredient('L', Material.LEATHER);
            srBackpack.setIngredient('C', Material.CHEST);
           
            getServer().addRecipe(srBackpack); */
           
            springGrowth = getConfig().getString("seasons-spring-growth");
            summerGrowth = getConfig().getString("seasons-summer-growth");
            autumnGrowth = getConfig().getString("seasons-autumn-growth");
            winterGrowth = getConfig().getString("seasons-winter-growth");
           
            springMessage = getConfig().getString("seasons-spring-message");
            summerMessage = getConfig().getString("seasons-summer-message");
            autumnMessage = getConfig().getString("seasons-autumn-message");
            winterMessage = getConfig().getString("seasons-winter-message");
           
            clockMessage = getConfig().getString("clock-message");
           
            currentDay = getConfig().getInt("date-currentDay");
            currentWeek = getConfig().getInt("date-currentWeek");
            currentMonth = getConfig().getInt("date-currentMonth");
            currentYear = getConfig().getInt("date-currentYear");
           
            getServer().getScheduler().runTaskTimer(this, new Runnable()
            {
               
                public void run()
                {
               
                    if (getServer().getWorlds().get(0).getFullTime() == 18000)
                    {
    
                        if (!(currentDay + 1 == 7))
                        {
                           
                            currentDay += 1;
                            return;
                        }
                        else
                        {
                           
                            currentDay = 0;
                           
                            if (!(currentWeek + 1 == 4))
                            {
                               
                                currentWeek += 1;
                                return;
                            }
                            else
                            {
                               
                                currentWeek = 0;
                               
                                if (!(currentMonth + 1 == 12))
                                {
                                   
                                    currentMonth += 1;
                                   
                                    if (currentMonth == 0)
                                    {
                                        getServer().broadcastMessage(colorize(springMessage));
                                        getServer().getWorlds().get(0).setGameRuleValue("randomTickSpeed", springGrowth);
                                        return;
                                    }
                                    if (currentMonth == 3)
                                    {
                                        getServer().broadcastMessage(colorize(summerMessage));
                                        getServer().getWorlds().get(0).setGameRuleValue("randomTickSpeed", summerGrowth);
                                        return;
                                    }
                                    if (currentMonth == 6)
                                    {
                                        getServer().broadcastMessage(colorize(autumnMessage));
                                        getServer().getWorlds().get(0).setGameRuleValue("randomTickSpeed", autumnGrowth);
                                        return;
                                    }
                                    if (currentMonth == 9)
                                    {
                                        getServer().broadcastMessage(colorize(winterMessage));
                                        getServer().getWorlds().get(0).setGameRuleValue("randomTickSpeed", winterGrowth);
                                        return;
                                    }
                                    return;
                                }
                                else
                                {
                                   
                                    currentMonth = 0;
                                    currentYear += 1;
                                   
                                    getServer().broadcastMessage(colorize(springMessage));
                                    getServer().getWorlds().get(0).setGameRuleValue("randomTickSpeed", springGrowth);
                                    return;
                                }
                               
                            }
                           
                        }
                       
                    }
                   
                }
               
            }, 0L, 1L);
               
        }
       
        public void onDisable()
        {
           
            getConfig().set("date-currentDay", currentDay);
            getConfig().set("date-currentWeek", currentWeek);
            getConfig().set("date-currentMonth", currentMonth);
            getConfig().set("date-currentYear", currentYear);
           
            saveConfig();
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
       
            return false;
           
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e)
        {
           
            if (e.getAction() == Action.RIGHT_CLICK_AIR)
            {
           
                if (e.getItem().getType() == Material.WATCH)
                {
                    e.getPlayer().sendMessage(printDate());
                    return;
                }
                if (e.getItem().getItemMeta().getLore().get(0).equals("backpack"))
                {
                   
                    getServer().broadcastMessage("2");
                   
                }
           
            }
       
        }
       
        @EventHandler
        public void onFoodLevelChange(FoodLevelChangeEvent  e)
        {
       
            if (!(e.getEntity() instanceof Player)) {return;}
            if (e.getFoodLevel() == 0) {e.getEntity().setHealth(0);}
           
        }
       
    }
    plugin.yml

    Code:
    name: Realism
    description: Realism
    author: SuperSaltyGamer
    version: 1.0.0
    main: realism.Main
     
  5. Offline

    SuperSaltyGamer

    I commented it out because i wanted the plugin to run, it was stopping there.

    Just now noticed, that an nullPointerException while enabling Realism

    Thi line seems to be the problem

    imBackpack.getLore().add("Backpack");

    What am I doing wrong?

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Aug 29, 2015
Thread Status:
Not open for further replies.

Share This Page