Plugin Not Working adn Rendering Server Useless

Discussion in 'Plugin Development' started by Cammy_the_block, Dec 12, 2012.

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

    Cammy_the_block

    I plugin that I made for some reason stops console cmds from working and people logging on.

    NewKits.java
    Code:
    package com.awesomepossumcraft.newkits;
    
    import java.util.List;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class NewKits extends JavaPlugin {
    
        public void onEnable() {
            Plugin plugin = this;
            Config.setPlugin(plugin);
            Config.findConfig();
            List<String> kitItems = Config.getKitItems();
            List<String> specialInfo = Config.getSpecialInfo();
            EventListener.sendKitItems(kitItems);
            EventListener.sendSpecialInfo(specialInfo);    
            getServer().getPluginManager().registerEvents(
                    new EventListener(), this);
        }
        
        public void onDisable() {
        }
        
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args){
    
            return false;
    
        }
    }
    
    EventListener.java
    Code:
    package com.awesomepossumcraft.newkits;
     
    import java.util.List;
    import java.util.Random;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
     
    public class EventListener implements Listener {
     
        public static Random random;
        public static List<String> kitItems;
        public static List<String> specialInfo;
        public static List<String> itemsToGive = null;
     
        @EventHandler
        public void onPlayerRespawn(PlayerRespawnEvent event) {
            String currentItem = null;
            findItemsToGive();
            Player player = event.getPlayer();
            Inventory inventory = player.getInventory();
            for (String x :itemsToGive){
                char[] charArray;
                int[] itemsToGiveInts = null;
                int count = 0;
                int items;
                charArray = x.toCharArray();
                for(char y : charArray){
                    if (y != ':'){
                        currentItem = currentItem + y;
                    }
                    if (y == ':') {
                        itemsToGiveInts[count] = Integer.parseInt(currentItem);
                        count++;
                    }
                }
                short damage = (short)(itemsToGiveInts[2]);
                byte data = (byte) (itemsToGiveInts[3]);
                ItemStack itemsToAdd = new ItemStack(itemsToGiveInts[0], itemsToGiveInts[1], damage, data);
                inventory.addItem(itemsToAdd);
            }
        }
       
        public void findItemsToGive(){
            boolean headerWasLast = false;
            boolean lastWasSpecial = false;
            boolean currentKitSpecial = false;
            boolean foundIt = false;
            boolean seenCurrentKit = false;
            String currentKit = null;
            String kitToGive = null;
            String z;
            int currentChance = 0;
            int currentCooldown;
            int count = 1;
            for (String x :specialInfo){
               
                int r = random.nextInt(100 / currentChance - 1)+1;
                if (headerWasLast){
                    currentKit = x;
                    headerWasLast = false;
                }
                if (lastWasSpecial){
                    if (count == 1){
                        currentChance = Integer.parseInt(x);
                    }
                    if (count == 2){
                        currentCooldown =Integer.parseInt(x);
                    }
                    count++;
                    if (count == 2){
                        lastWasSpecial = false;
                    }
                }
                if (x == "H"){
                    headerWasLast = true;
                    currentKitSpecial = false;
                }
                if (x == "special"){
                    lastWasSpecial = true;
                }
                if (currentKitSpecial){
                    if (r == 1){
                        kitToGive = currentKit;
                    }
                }
            }
            for (int count2 = 0; foundIt==true; count2++){
                z =kitItems.get(count2);
                if (z == kitToGive){
                    for (String x : kitItems ){
                        if (x == currentKit){
                            seenCurrentKit = true;
                        }
                        if (seenCurrentKit){
                            if(x != "H"){
                                itemsToGive.add(x);
                            }
                        }
                    }
                }
            }
        }
     
        @EventHandler
        public void onPlayerLogin(PlayerLoginEvent event) {
     
        }
        public static void sendKitItems(List kitItems2){
            kitItems = kitItems2;
        }
        public static void sendSpecialInfo(List specialInfo2){
            specialInfo = specialInfo2;
        }
    }
     
    
    Config.yml
    Code:
     kit_1:
      items:
        - 1:10:0:0
        Default: true
        Special: false
          - chance: #integer out of 100
          - cooldown: 9 #counts by seconds. ex: 10 - 10 seconds, 60 - 1 minute.
      
    Plugin.yml
    Code:
        name: NewKits
        main: com.awesomepossumcraft.newkits.NewKits
        version: 0.01
    Thanks for reading through my rats nest of code,
    Cammy
     
  2. Offline

    Drew1080

    Cammy_the_block

    Firstly, your config.java and & EventListener.java are the same thing :p
    Secondly, you have not registered your Listener class in the Main class file.
     
  3. Offline

    KeybordPiano459

    Lol the EventListener is the config file :p and yeah, do what Drew1080 said.
     
  4. Offline

    Cammy_the_block

    Copy and paste FAIL
    How do I register events?

    EDIT: Wow today I'ma failing a lot first a copy and paste fail now a double click fail
     
  5. Offline

    Drew1080

    In you main class file the one that extends JavaPlugin.

    Put in your onEnable() method

    getServer().getPluginManager().registerEvents(new EventListener(), this);

    eventlistener, being your eventlistener class & this, being the plugin
     
  6. Offline

    Cammy_the_block

    I tested it and teh same thing happened. I updated the first post with the new code.
     
  7. Offline

    gomeow

    Wrong,

    getServer().getPluginManager().registerEvents(new EventListener(), this);
     
  8. Offline

    Drew1080

    Yeah i realized that after i posted it.
     
  9. Offline

    fireblast709

    Code:java
    1. for (int count2 = 0; foundIt==true; count2++)
    2. {
    3. z =kitItems.get(count2);
    4. if (z == kitToGive)
    5. {
    6. for (String x : kitItems )
    7. {
    8. if (x == currentKit)
    9. {
    10. seenCurrentKit = true;
    11. }
    12. if (seenCurrentKit)
    13. {
    14. if(x != "H")
    15. {
    16. itemsToGive.add(x);
    17. }
    18. }
    19. }
    20. }
    21. }

    • This code never runs.
    • you compare Strings with ==, while you should compare with .equals(anotherString)
    • config.yml is invalid according to http://yaml-online-parser.appspot.com/
    • Use of static where you as well could use the constructor
    Here is your homework for now. Consult google and ask any further questions here :3
     
  10. Offline

    Cammy_the_block

    Do I have to use .equals?It should run. Also It says the first line is incorrect of config.yml please explain why.
    Code:
      kit_1:
        items:
          - 1:10:0:0
        Default: true
        Special: false
          - chance: 2 #integer out of 100
          - cooldown: 9 #counts by seconds. ex: 10 - 10 seconds, 60 - 1 minute.
      
     
  11. Offline

    fireblast709

    for Strings, use .equals() to test equality
     
  12. Offline

    reesylou

    The other thing that concerns me is the for loop.... you are saying run until foundit is true, but do not set foundit..... and what happens if none of the kits in the config match the one you want? (Hint: endless loop and server death)
     
  13. Offline

    Cammy_the_block

    Thank you I fixed it and are about to test it.
     
Thread Status:
Not open for further replies.

Share This Page