Solved Rainbow Armor Help

Discussion in 'Spigot Plugin Development' started by Nate_Holland, Feb 20, 2016.

  1. Hello Once again! Im working on another plugin for a friend called: RainbowArmor
    I am trying to save the players armor and inventory contents when a cmd is run but am having trouble doing so..
    Am i not using the methods correctly?
    (Yes, i know the Main Class is a bit messy.. will fix that once i get the plugin running fully)
    The armor changes color, but it throws console errors
    Any suggestions?
    (PS. Yes, i am still what you call, a "Noob" at Java Coding.. Still learning.. Just looking for help with this :) )

    Main Class:
    Code (Text):

    package com.RainbowArmor;

    import java.util.HashMap;
    import java.util.Random;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.LeatherArmorMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {
     
        String Armor = "§5[§eRA§5] ";
        private static Main plugin;
     
        public HashMap <String, ItemStack[]> inventories = new HashMap <String, ItemStack[]>();
        public HashMap <String, ItemStack[]> armour = new HashMap <String, ItemStack[]>();
     
        public void onEnable() {
            plugin = this;
         
            getServer().getPluginManager().registerEvents(this, this);
            if(!(this.getConfig().contains("Speed"))) {
                this.getConfig().set("Speed", 5);
            }
            this.getConfig().options().copyDefaults(true);
            saveConfig();
        }
     
        public static Plugin getPlugin() {
            return plugin;
        }
     
        public void onDisable() {
            plugin = null;
            saveConfig();
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] arg) {
            Player player = (Player) sender;
         
            ItemStack[] inv = player.getInventory().getContents();
            ItemStack[] arm = player.getInventory().getArmorContents();
            inventories.put(getName(), inv);
            armour.put(getName(), arm);
         
            if(cmd.getName().equalsIgnoreCase("ra")) {
                if(!(sender instanceof Player)) {
                    sender.sendMessage("You must be in-game to use this command!");
                }
                else {
                    sender.sendMessage(ChatColor.RED + "Usage: /ra <on/off>");
                }
                if(arg.length == 1) {
                    if(arg[0].equalsIgnoreCase("on")) {
                       sender.sendMessage(Armor + "Turning on..");
                     
                        Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                            private Random r = new Random();

                            @Override
                            public void run() {
                                Color c = Color.fromRGB(r.nextInt(255), r.nextInt(255), r.nextInt(255));
                             
                                for(Player p : Bukkit.getServer().getOnlinePlayers()) {
                                    if(p.getInventory().getHelmet() !=null || p.getInventory().getHelmet().getType() == Material.LEATHER_HELMET
                                            || p.getInventory().getHelmet().getType() == Material.CHAINMAIL_HELMET
                                            || p.getInventory().getHelmet().getType() == Material.IRON_HELMET
                                            || p.getInventory().getHelmet().getType() == Material.GOLD_HELMET
                                            || p.getInventory().getHelmet().getType() == Material.DIAMOND_HELMET) {
                                        p.getInventory().clear();
                                        p.getInventory().setHelmet(Rainbow(Material.LEATHER_HELMET, c));
                                   }
                                    if(p.getInventory().getChestplate() !=null || p.getInventory().getChestplate().getType() == Material.LEATHER_CHESTPLATE
                                            || p.getInventory().getChestplate().getType() == Material.CHAINMAIL_CHESTPLATE
                                            || p.getInventory().getChestplate().getType() == Material.IRON_CHESTPLATE
                                            || p.getInventory().getChestplate().getType() == Material.GOLD_CHESTPLATE
                                            || p.getInventory().getChestplate().getType() == Material.DIAMOND_CHESTPLATE) {
                                        p.getInventory().setChestplate(Rainbow(Material.LEATHER_CHESTPLATE, c));
                                    }
                                    if(p.getInventory().getLeggings() !=null || p.getInventory().getLeggings().getType() == Material.LEATHER_LEGGINGS
                                            || p.getInventory().getLeggings().getType() == Material.CHAINMAIL_LEGGINGS
                                            || p.getInventory().getLeggings().getType() == Material.IRON_LEGGINGS
                                            || p.getInventory().getLeggings().getType() == Material.GOLD_LEGGINGS
                                            || p.getInventory().getLeggings().getType() == Material.DIAMOND_LEGGINGS) {
                                        p.getInventory().setLeggings(Rainbow(Material.LEATHER_LEGGINGS, c));
                                    }
                                    if(p.getInventory().getBoots() !=null || p.getInventory().getBoots().getType() == Material.LEATHER_BOOTS
                                            || p.getInventory().getBoots().getType() == Material.CHAINMAIL_BOOTS
                                            || p.getInventory().getBoots().getType() == Material.IRON_BOOTS
                                            || p.getInventory().getBoots().getType() == Material.GOLD_BOOTS
                                            || p.getInventory().getBoots().getType() == Material.DIAMOND_BOOTS) {
                                        p.getInventory().setBoots(Rainbow(Material.LEATHER_BOOTS, c));
                                    }
                                }
                             
                            }
                         
                        }, 0, 15);
                    }
                    if(arg[0].equalsIgnoreCase("off")){
                        Bukkit.getServer().getScheduler().cancelTasks(this);
                        sender.sendMessage(Armor + "Turning off..");
                        player.getInventory().setContents(inventories.get(player.getName()));
                        player.getInventory().setArmorContents(armour.get(player.getName()));
                    }
                }
            }
            return false;
        }
     
        private ItemStack Rainbow(Material m, Color c) {
            ItemStack i = new ItemStack(m, 1);
            LeatherArmorMeta meta = (LeatherArmorMeta) i.getItemMeta();
           meta.setColor(c);
           meta.setDisplayName(ChatColor.DARK_PURPLE + "Rainbow Armor");
           i.setItemMeta(meta);
           return i;
        }
    }

     
     
  2. What error is being thrown to the console?
     
  3. dont spoon feed him!
     
    • Funny Funny x 3
    • Winner Winner x 1
  4. How could he possibly help him without knowing what the issue is? That's definitely not spoon feeding.
     
  5. um Stack Trace plz thx
     
  6. Not sure how that is spoon feeding him. Just asking for the error being thrown?
     
    • Agree Agree x 1
  7. dafuq you high on?
     
    • Winner Winner x 2
    • Funny Funny x 1
  8. Im not looking to be "Spoon Fed"... im hear looking for any suggestions as to why my plugin is breaking..
    Im not good with Debugging..

    But heres my Console Error:
    Code (Text):

    [20:43:20] [Server thread/WARN]: [RainbowArmor] Task #675 for RainbowArmor v1.0 generated an exception
    java.lang.NullPointerException
        at com.RainbowArmor.Main$1.run(Main.java:73) ~[?:?]
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot.jar:git-Spigot-db6de12-07c3001]
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723) [spigot.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-07c3001]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
     
     
  9. What is line 73 in your Main class?
     
  10. || p.getInventory().getHelmet().getType() == Material.IRON_HELMET
     
  11. @Nate_Holland

    You should probably check if the item in their armour slot is not null AND it's type is not Air.

    Edit: This would probably condense your code as well:

    Code (Text):

    for(ItemStack i : player.getInventory().getArmorContents()) {
     
         if(i != null && i.getType() != Material.AIR) {

          //Code here no need to manually check all the armor slots whether there is a certain type of item in
         there

    }

    }
     
    #11 liveinacupboard, Feb 20, 2016
    Last edited: Feb 20, 2016
  12. I tried what you said and somehow it broke and set boots as the helmet and gave me an endless supply of armor.. But, i did manage to shorten my current method and make that work. Thanks for the help!
     

Share This Page