Command setup not right?

Discussion in 'Plugin Development' started by Lightcaster5, Oct 15, 2019.

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

    Lightcaster5

    Im working on a plugin to create a "builder mode" where when a user does /buildermode (or any of its aliases) it will store their inventory in a hashmap and give them a few items that will help them build. I have only started and I got to a point where I felt like I should do a test and I loaded the plugin up and I got this error in my console.
    Console Error (open)

    Code:
    15.10 08:45:18 [Server] INFO [BuilderMode] Enabling BuilderMode v1.0.0
    15.10 08:45:18 [Server] ERROR Error occurred while enabling BuilderMode v1.0.0 (Is it up to date?)
    15.10 08:45:18 [Server] INFO java.lang.NullPointerException: null
    15.10 08:45:18 [Server] INFO at me.lightcaster5.buildermode.PlayerInventory.<init>(PlayerInventory.java:23) ~[?:?]
    15.10 08:45:18 [Server] INFO at me.lightcaster5.buildermode.Main.onEnable(Main.java:29) ~[?:?]
    15.10 08:45:18 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    15.10 08:45:18 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    15.10 08:45:18 [Server] INFO at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    15.10 08:45:18 [Server] INFO at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    15.10 08:45:18 [Server] INFO at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    15.10 08:45:18 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    15.10 08:45:18 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    15.10 08:45:18 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    15.10 08:45:18 [Server] INFO at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    15.10 08:45:18 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    15.10 08:45:18 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]


    Here are all the plugin files
    Files (open)
    Main.java (open)

    Code:
    package me.lightcaster5.buildermode;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.lightcaster5.buildermode.listeners.ClearInvinListener;
    
    public class Main extends JavaPlugin {
      
    
        public HashMap<String, Boolean> builderMap = new HashMap<String, Boolean>();
        public HashMap<String, ItemStack[]> invMap = new HashMap<String, ItemStack[]>();
        public HashMap<String, ItemStack[]> armorMap = new HashMap<String, ItemStack[]>();
      
      
      
        public ItemStack wand = new ItemStack(Material.WOOD_AXE);
        public ItemStack clear = new ItemStack(Material.HOPPER);
        public ItemStack off = new ItemStack(Material.BARRIER);
      
        @Override
        public void onEnable() {
          
            getCommand("buildermode").setExecutor(new PlayerInventory(this));
            getServer().getPluginManager().registerEvents(new ClearInvinListener(this), this);
    
            ItemMeta wandMeta = wand.getItemMeta();
            ArrayList<String> wandLore = new ArrayList<String>();
            wandMeta.setDisplayName("§fWand");
            wandLore.add("§7WorldEdit wand");
            wandLore.add("§7Usage:");
            wandLore.add("§7Left click to set position 1");
            wandLore.add("§7Right click to set position 2");
            wandLore.add("§7(Use //pos1 and //pos2");
            wandLore.add("§7to use your position)");
            wandMeta.setLore(wandLore);
            wand.setItemMeta(wandMeta);
    
            ItemMeta clearMeta = clear.getItemMeta();
            ArrayList<String> clearLore = new ArrayList<String>();
            clearMeta.setDisplayName("§fReset Inventory");
            clearLore.add("§7Click to reset inventory");
            clearMeta.setLore(clearLore);
            clear.setItemMeta(clearMeta);
          
            ItemMeta offMeta = off.getItemMeta();
            ArrayList<String> offLore = new ArrayList<String>();
            offMeta.setDisplayName("§fExit Builder Mode");
            offLore.add("§7Click to turn off");
            offLore.add("§7builder mode.");
            offMeta.setLore(offLore);
            off.setItemMeta(offMeta);
        }
      
        @Override
        public void onDisable() {
          
          
          
        }
    
    }
    
    PlayerInventory.java (open)

    Code:
    package me.lightcaster5.buildermode;
    
    import java.util.HashMap;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class PlayerInventory implements CommandExecutor, Listener {
    
        Main plugin;
        public PlayerInventory(Main pl) {
            plugin = pl;
        }
      
        HashMap<String, Boolean> builderMap = plugin.builderMap;
        HashMap<String, ItemStack[]> invMap = plugin.invMap;
        HashMap<String, ItemStack[]> armorMap = plugin.armorMap;
        ItemStack wand = plugin.wand;
        ItemStack clear = plugin.clear;
        ItemStack off = plugin.off;
      
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = (Player) e.getPlayer();
            if (builderMap.containsKey(p.getName())) {
                builderMap.put(p.getName(), false);
            } else {
                builderMap.put(p.getName(), false);
            }
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            if (command.getName().equalsIgnoreCase("buildermode")) {
                if (sender instanceof Player) {
                    if (sender.hasPermission("buildermode.use")) {
                        int length = args.length;
                        Player p = (Player) sender;
                        if (length == 0) {
                            if (builderMap.get(p.getName()) == false) {
                                builderMap.put(p.getName(), true);
                                invMap.put(p.getName(), p.getInventory().getContents());
                                armorMap.put(p.getName(), p.getInventory().getArmorContents());
                                p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 1, false));
                                p.getInventory().clear();
    
                              
                                p.getInventory().setItem(0, wand);
                                p.getInventory().setItem(8, clear);
                                p.getInventory().setItem(17, off);
                              
                                p.sendMessage("§8(§f§lEditor§8) §7You are now in §abuilder §7mode.");
                                return true;
                            } else {
                                builderMap.put(p.getName(), false);
                                p.getInventory().clear();
                                p.getInventory().setContents(invMap.get(p.getName()));
                                p.getInventory().setArmorContents(armorMap.get(p.getName()));
                              
                              
                                p.sendMessage("§8(§f§lEditor§8) §7You are now in §adefault §7mode.");
                                return true;
                            }
                        }
                    } else {
                        sender.sendMessage("§fUnknown command. Type \"/help\" for help.");
                        return true;
                    }
                } else {
                    sender.sendMessage("You must be a player to use this command!");
                    return true;
                }
            }
            return true;
        }
    
    }
    
    ClearInvinListener.java (open)

    Code:
    package me.lightcaster5.buildermode.listeners;
    
    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.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import me.lightcaster5.buildermode.Main;
    
    public class ClearInvinListener implements Listener {
      
        Main plugin;
        public ClearInvinListener(Main pl) {
            plugin = pl;
        }
      
        ItemStack wand = plugin.wand;
        ItemStack clear = plugin.clear;
        ItemStack off = plugin.off;
      
        @EventHandler
        public void onPlayerUse(PlayerInteractEvent event){
            Player p = event.getPlayer();
    
            Action action = event.getAction();
          
            if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK || action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK)
                if(p.getInventory().getItemInMainHand().equals(clear)){
                    event.setCancelled(true);
                    p.getInventory().clear();
                  
                    p.getInventory().setItem(0, wand);
                    p.getInventory().setItem(8, clear);
                    p.getInventory().setItem(17, off);
                    p.sendMessage("§8(§f§lEditor§8) §7Your builder inventory has been reset.");
                }
        }
    }
    
    TurnOffListener.java (open)

    Code:
    package me.lightcaster5.buildermode.listeners;
    
    import java.util.HashMap;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    
    import me.lightcaster5.buildermode.Main;
    
    public class TurnOffListener implements Listener {
      
        Main plugin;
        public TurnOffListener(Main pl) {
            plugin = pl;
        }
      
        ItemStack off = plugin.off;
        HashMap<String, Boolean> builderMap = plugin.builderMap;
        HashMap<String, ItemStack[]> invMap = plugin.invMap;
        HashMap<String, ItemStack[]> armorMap = plugin.armorMap;
      
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player p = (Player) event.getWhoClicked();
            if (builderMap.get(p.getName()) == true) {
                if (event.getCursor() == off) {
                    event.setCancelled(true);
                    builderMap.put(p.getName(), false);
                    p.closeInventory();
                    p.getInventory().clear();
                    p.getInventory().setContents(invMap.get(p.getName()));
                    p.getInventory().setArmorContents(armorMap.get(p.getName()));
                  
                  
                    p.sendMessage("§8(§f§lEditor§8) §7You are now in §adefault §7mode.");
                }
            }
        }
    }
    
    plugin.yml (open)

    Code:
    name: BuilderMode
    author: Lightcaster5
    description: An easy way for players to get building
    main: me.lightcaster5.buildermode.Main
    version: 1.0.0
    commands:
       buildermode:
          description: Toggles builder mode
          aliases: [build, builder, builderm, bmode, bm]


    This plugin is nowhere near done for those who might point out a few things that would let the player get around things. Thanks for the help in advanced

    EDIT: For those who need it, the plugin is 1.12.2 along with my server.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Lightcaster5 You have stuff like this:
    Code:
    HashMap<String, Boolean> builderMap = plugin.builderMap;
    HashMap<String, ItemStack[]> invMap = plugin.invMap;
    HashMap<String, ItemStack[]> armorMap = plugin.armorMap;
    Do that in the constructor instead.
     
  3. Assign the variables in all your listeners inside the constructor (plugin is null at this point)

    EDIT: sry tim didn't see that
     
    timtower likes this.
  4. Offline

    Lightcaster5

    I put them out of any constructor so I could call them from any constructor. Should I add public before all of them?

    What do you mean the plugin is null?
     
  5. @Lightcaster5
    You declare the hashmaps before calling the constructor, which assigns 'plugin'. You try to use this null pointer 'plugin'before it gets any value (by the constructor)

    Adding public also doesn't help
     
  6. Offline

    Lightcaster5

    So if, in my main class, I declare the hashmaps in the onEnable() constructor it will work correctly?
     
  7. In the main class it doesn't matter because you do not refer to any object, you just create a new HashMap. But in the other classes you take existing objects from your main

    Edit: theoretically, you don't even need these variables in each class since you store the plugin reference. With that you can access the hashmaps at any point in your code
     
  8. Offline

    Lightcaster5

    So how should I go about using it because I need the three items along with their metadata and all of the hashmaps to be accessible from different classes.
     
  9. In every class you want to access the items and hashmaps you create a constructor and pass the main class to it (like you already have). You save the Main instance in a variable (like you already have) and if you need the item/hashmap, you access it through this main instance. The only difference to your code is that you get the variable values later, not before the constructor got called
     
  10. Online

    KarimAKL

    @Lightcaster5 I'd probably create a class containing the maps, getters for them, and a method to put players into "builder mode".
    You can either make it static, or initialize it in your main class, though i'd recommend initializing it in your main class.
    If it's not static: Make a getter to that instance in your main class, and then get that from your passed main class instance, or pass the instance to your class.
     
  11. Offline

    Lightcaster5

    Thank you both for you help, I think I got it working now.
     
  12. Offline

    Strahan

    Also you should initializing things with the interface rather than the implementation; do Map<> = new HashMap<> instead of HashMap<> = new HashMap<>
     
Thread Status:
Not open for further replies.

Share This Page