Event Struggles

Discussion in 'Plugin Development' started by AtlantiaKing11NL, Mar 17, 2016.

Thread Status:
Not open for further replies.
  1. Hello! Im creating a minigame and im a little stuck...

    have you ever played GMod? Well if you played it, there is a gamemode in it called PedoBear.
    Im recreating this minigame for a friend. im stuck at opening the MyInventory to choose a kit with the event stuff...

    So i hope anyone can help me, this is my script!

    Main:

    Code:
    package me.AtlantiaProduction;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        public Files Maps;
        public Files Players;
        public Files Config;
    
        @Override
        public void onEnable() { 
            Maps = new Files(getDataFolder(), "Maps");
            Players = new Files(getDataFolder(), "Players");
            Config = new Files(getDataFolder(), "Config");
      
            saveConfig();
      
            if(!Maps.fileExists()) {
                Maps.createFile();
                Maps.loadFile();
                Maps.saveFile();
            }
            if(!Players.fileExists()) {
                Players.createFile();
                Players.loadFile();
                Players.saveFile();
            }
            if(!Config.fileExists()) {
                Config.createFile();
                Config.loadFile();
                Config.saveFile();
            }
      
      
      
        }
    
        @Override
        public void onDisable() { 
            saveConfig();
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (!(sender instanceof Player)) {
               sender.sendMessage(ChatColor.RED + "Only players can use this command");
            return false;
            }
            Player p = (Player) sender;
    
            if(cmd.getName().equalsIgnoreCase("pb")) {
               if (args.length == 0) {
                  p.sendMessage(ChatColor.RED + this.getConfig().getString("TOFEWARGUMENTS"));
                 return false;
               }
               if(args[0].equalsIgnoreCase("create")) {
                   if(p.hasPermission("Deathmatch.setup")) {
                   if(args.length == 2) {
                   this.Maps.loadFile();
                   if(this.Maps.getConfig().getString(args[1].toLowerCase() + ".owner") == null) {
                   p.sendMessage(ChatColor.GOLD + getConfig().getString("CREATE1") + args[1]);
                   new Maps(this).CreateMap(p.getName(), args[1]);
                   } else {
                   p.sendMessage(ChatColor.RED + getConfig().getString("FOUT-CREATE2"));
                   }
                   } else if(args.length <= 1) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOFEWARGUMENTS"));
                   } else if(args.length >= 3) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOMANYARGUMENTS"));
                   }
                   }
                   } else if(args[0].equalsIgnoreCase("delete")) {
                   if(p.hasPermission("Deathmatch.setup")) {
                   if(args.length == 2) {
                   this.Maps.loadFile();
                   p.sendMessage(ChatColor.GOLD + getConfig().getString("DELETE1") + args[1]);
                   new Maps(this).DeleteMap(p.getName(), args[1]);
                   }
                   }
                   } else if(args[0].equalsIgnoreCase("setspawn")) {
                   if(p.hasPermission("Deathmatch.setup")) {
                   if(args.length == 3) {
                   this.Maps.loadFile();
                   new Maps(this).SetSpawn(p.getPlayer(), args[1], args[2]);
                   } else if(args.length <= 2) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOFEWARGUMENTS"));
                   } else if(args.length >= 4) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOMANYARGUMENTS"));
                   }
                   }
                   } else if(args[0].equalsIgnoreCase("sethub")) {
                   if(p.hasPermission("Deathmatch.setup")) {
                   if(args.length == 2) {
                   this.Maps.loadFile();
                   new Maps(this).SetHub(p.getPlayer(), args[1]);
                   } else if(args.length <= 1) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOFEWARGUMENTS"));
                   } else if(args.length >= 3) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOMANYARGUMENTS"));
                   }
                   }
                   } else if(args[0].equalsIgnoreCase("setlobby")) {
                   if(p.hasPermission("Deathmatch.setup")) {
                   if(args.length == 2) {
                   this.Maps.loadFile();
                   new Maps(this).SetLobby(p.getPlayer(), args[1]);
                   } else if(args.length <= 1) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOFEWARGUMENTS"));
                   } else if(args.length >= 3) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOMANYARGUMENTS"));
                   }
                   }
                   } else if(args[0].equalsIgnoreCase("leave")) {
                   if(args.length == 2) {
                   this.Maps.loadFile();
                   new Maps(this).LeaveGame(p.getPlayer(), args[1]);
                   } else if(args.length <= 1) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOFEWARGUMENTS"));
                   } else if(args.length >= 3) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOMANYARGUMENTS"));
                   }
                   } else if(args[0].equalsIgnoreCase("join")) {
                       if(args.length == 2) {
                       this.Maps.loadFile();
                       new Maps(this).JoinGame(p.getPlayer(), args[1]);
                       new Maps(this).KitLobby(p.getPlayer());
                       new PickClassEvents(this, args[1]);
                   } else if(args.length <= 1) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOFEWARGUMENTS"));
                   } else if(args.length >= 3) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOMANYARGUMENTS"));
                   }
                   } else if(args.length == 0) {
                      p.sendMessage(ChatColor.RED + this.getConfig().getString("TOFEWARGUMENTS"));
                   }
               }
    
            return false;
    
            }
    }
    
    
    PickClassEvents

    Code:
    package me.AtlantiaProduction;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.AtlantiaProduction.Main;
    
    public class PickClassEvents implements Listener {
    
        public static Inventory myInventory = Bukkit.createInventory(null, 9, ChatColor.DARK_PURPLE + "Pick your Class");
        public Main plugin;
        public String arenaname;
        public static Inventory myTeam = Bukkit.createInventory(null, 9, ChatColor.DARK_PURPLE + "Pick your Team");
        int Countdown1 = 1;
    
        public PickClassEvents(Main plugin, String arenaname) {
            this.plugin = plugin;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            this.arenaname = arenaname;
        
            ItemStack Bear = new ItemStack(Material.REDSTONE_TORCH_OFF);
            ItemStack Victim = new ItemStack(Material.REDSTONE);
        
            ItemMeta Bearm = Bear.getItemMeta();
            ItemMeta Victimm = Victim.getItemMeta();
        
            Bearm.setDisplayName(ChatColor.BLACK + "PedoBear");
            Victimm.setDisplayName(ChatColor.WHITE + "Victim");
        
            Bear.setItemMeta(Bearm);
            Victim.setItemMeta(Victimm);
        
            myInventory.setItem(0, Bear);
            myInventory.setItem(1, Victim);
        
        }
    
        public void NETHERSTAR(PlayerInteractEvent e) {
            if(plugin.Maps.getConfig().getStringList(arenaname.toLowerCase() + ".Players").contains(e.getPlayer().getName())) {
                if(e.getItem().getItemMeta().getDisplayName() == "Classes") {
                    e.getPlayer().openInventory(myInventory);
                }
            }
        }
    
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
        
            Player player = (Player) event.getWhoClicked();
            ItemStack clicked = event.getCurrentItem();
            Inventory inventory = event.getInventory();
        
            if (inventory.getName().equals(myInventory.getName())) {
            
                if (clicked.getType() == Material.REDSTONE_TORCH_OFF) {
                
                    event.setCancelled(true); // Make it so the dirt is back in its original spot.
                    if(plugin.Maps.getConfig().getString(arenaname.toLowerCase() + ".Bear") == player.getName()) {
                    
                        player.sendMessage(ChatColor.RED + "You are already the PedoBear");
                    
                    } else {
                
                    plugin.Maps.getConfig().set(arenaname.toLowerCase() + ".Bear", player.getName());
                    player.sendMessage(ChatColor.AQUA + "When game start, You are gonna be...");
                    player.sendMessage(ChatColor.GOLD + "The PedoBear!");
                
                    }
                
                } else if(clicked.getType() == Material.REDSTONE) {
                
                    event.setCancelled(true);
                
                    if(plugin.Maps.getConfig().getString(arenaname.toLowerCase() + ".Bear") == player.getName()) {
                    
                        player.sendMessage(ChatColor.RED + "You are already a Victim");
                    
                    } else {
                    
                        player.sendMessage(ChatColor.AQUA + "When game start, You are gonna be...");
                        player.sendMessage(ChatColor.GOLD + "A Victim!");
                
                    }
                
                }
            
                plugin.Maps.saveFile();
            }
        }
    }
    
    
    I hope you can help me aggain!

    Greetings
     
  2. Offline

    mcdorli

    1.: This is not a script, this is code, scripts are usually smaller
    2.: Follow java naming conventions
    3.: Don't abude static

    You nsver actually register a command

    And please, for the love of god, format your code. This was only acceptable with FORTRAN 30 years ago
     
  3. Offline

    Gonmarte

    Why shouldnt we abuse of static? Just curious ;)
     
  4. Offline

    Swakiny


    Static's doesn't delete their content when creating a new instance I think
     
  5. Offline

    Zombie_Striker

    @Gonmarte
    Static objects are not cleared from memory, so you have to understand that you have to set all static variables to null before you stop your program (this includes if your server shutdown unexpectedly.) Referencing static objects also mean that all instances of the same classe share the same static instance. Finally, it gets created before even the class gets created, so you have the chance of getting the instance before it is set to anything.
     
    Gonmarte likes this.
  6. Offline

    MajorSkillage

    @AtlantiaKing11NL If you actually debugged this yourself, you could have identified all issues that have currently been mentioned. The other thing is if you're having trouble getting a user to open an inventory do some research before coming here.
     
  7. Offline

    Irantwomiles

    Just out of curiosity when is abusing anything ok? It's a has a fairly negative connotation. To answer your question, the static modifier is not purely meant to access items from other classes, it's not the purpose of making objects static.
     
    Gonmarte likes this.
  8. Offline

    mcdorli

    It's simply a bad habit. It makes java look like a functional programming language. It's only useful, if you use singletons.
     
Thread Status:
Not open for further replies.

Share This Page