Scheduling an event & command help.

Discussion in 'Spigot Plugin Development' started by Gorobetz, May 29, 2017.

Thread Status:
Not open for further replies.
  1. I don't exactly know if it is possible, but I am working on a highly customizable system that includes an inventory GUI and 3 lists, which are changed whenever someone selects something in my inventory. The thing is, that it would run off of InventoryInteractEvent, which can be called whenever, and not only that, but I need a way to make sure this is in my new inventory and not my previous one.

    Example: I type /dna. Once I do, an inventory opens with all these different options. I can select different ones and add them to a list. Wanna know what is hard? I wanna be sure I am using the list I used last time I clicked in this inventory. Basically a while() expression, however, I don't suppose that you can put an event in an event, and not only that, but there is no method (in spigot 1.11) that figures out if the player is looking in an inventory.

    My class Im focusing on:
    Code (Text):

    public class builder extends JavaPlugin implements Listener{
     
    @Override
    public void onEnable(){
        Bukkit.getPluginManager().registerEvents(this, this);
    }

    private List<String> effects;
    private List<Integer> cost;
    private List<Integer> power;
     
    private int speed1 = 1; //Positive
    private int speed2 = 2;
    private int speed3 = 4;

    private int strength1 = 1; //Positive
    private int strength2 = 3;
    private int strength3 = 5;

    private int regen1 = 1; //Positive
    private int regen2 = 2;
    private int regen3 = 4;

    private int resistance1 = 1; //Positive
    private int resistance2 = 2;
    private int resistance3 = 4;

    private int nightvision1 = 1; //Positive

    private int breathing1 = 1; //Positive
    private int breathing2 = 2;
    private int breathing3 = 3;

    private int miningfatigue1 = -1; //Negative
    private int miningfatigue2 = -2;

    private int blind1 = -2; //Negative

    private int slowness1 = -1; //Negative
    private int slowness2 = -2;
    private int slowness3 = -4;

    //                -----Costs-----

    private int speed1cost = 2;
    private int speed2cost = 3;
    private int speed3cost = 4;

    private int strength1cost = 2;
    private int strength2cost = 4;
    private int strength3cost = 7;

    private int regen1cost = 1;
    private int regen2cost = 3;
    private int regen3cost = 5;

    private int resistance1cost = 2;
    private int resistance2cost = 3;
    private int resistance3cost = 5;

    private int nightvision1cost = 3;

    private int breathing1cost = 2;
    private int breathing2cost = 3;
    private int breathing3cost = 4;

    private int miningfatigue1cost = 1;
    private int miningfatigue2cost = 2;

    private int blind1cost = 2;

    private int slowness1cost = 1;
    private int slowness2cost = 2;
    private int slowness3cost = 3;

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        if(label.equals("dna")){
         
            Player p = (Player) sender;
            Inventory inv = Bukkit.createInventory(null, 98, ChatColor.AQUA + "DNA Builder");
         
            ItemStack fill = new ItemStack(Material.QUARTZ_BLOCK, 1);
            ItemStack speed1 = new ItemStack(Material.RECORD_10, 1);
            ItemStack speed2 = new ItemStack(Material.RECORD_10, 1);
            ItemStack speed3 = new ItemStack(Material.RECORD_10, 1);
            ItemStack strength1 = new ItemStack(Material.RECORD_11, 1);
            ItemStack strength2 = new ItemStack(Material.RECORD_11, 1);
            ItemStack strength3 = new ItemStack(Material.RECORD_11, 1);
            ItemStack regen1 = new ItemStack(Material.RECORD_12, 1);
            ItemStack regen2 = new ItemStack(Material.RECORD_12, 1);
            ItemStack regen3 = new ItemStack(Material.RECORD_12, 1);
            ItemStack resis1 = new ItemStack(Material.RECORD_3, 1);
            ItemStack resis2 = new ItemStack(Material.RECORD_3, 1);
            ItemStack resis3 = new ItemStack(Material.RECORD_3, 1);      
            ItemStack nvision = new ItemStack(Material.RECORD_4, 1);
            ItemStack breathing1 = new ItemStack(Material.RECORD_5, 1);
            ItemStack breathing2 = new ItemStack(Material.RECORD_5, 1);
            ItemStack breathing3 = new ItemStack(Material.RECORD_5, 1);
            ItemStack miningfatigue1 = new ItemStack(Material.RECORD_7, 1);
            ItemStack miningfatigue2 = new ItemStack(Material.RECORD_7, 1);
            ItemStack blind = new ItemStack(Material.RECORD_8, 1);
            ItemStack slowness1 = new ItemStack(Material.RECORD_9, 1);
            ItemStack slowness2 = new ItemStack(Material.RECORD_9, 1);
            ItemStack slowness3 = new ItemStack(Material.RECORD_9, 1);
         
            ItemMeta fillmeta = fill.getItemMeta();
            ItemMeta speed1meta = speed1.getItemMeta();
            ItemMeta speed2meta = speed2.getItemMeta();
            ItemMeta speed3meta = speed3.getItemMeta();
            ItemMeta strength1meta = strength1.getItemMeta();
            ItemMeta strength2meta = strength2.getItemMeta();
            ItemMeta strength3meta = strength3.getItemMeta();
            ItemMeta regen1meta = regen1.getItemMeta();
            ItemMeta regen2meta = regen2.getItemMeta();
            ItemMeta regen3meta = regen3.getItemMeta();
            ItemMeta resis1meta = resis1.getItemMeta();
            ItemMeta resis2meta = resis2.getItemMeta();
            ItemMeta resis3meta = resis3.getItemMeta();
            ItemMeta nvisionmeta = nvision.getItemMeta();
            ItemMeta breathing1meta = breathing1.getItemMeta();
            ItemMeta breathing2meta = breathing2.getItemMeta();
            ItemMeta breathing3meta = breathing3.getItemMeta();
            ItemMeta miningfatigue1meta = miningfatigue1.getItemMeta();
            ItemMeta miningfatigue2meta = miningfatigue2.getItemMeta();
            ItemMeta blindmeta = blind.getItemMeta();
            ItemMeta slowness1meta = slowness1.getItemMeta();
            ItemMeta slowness2meta = slowness2.getItemMeta();
            ItemMeta slowness3meta = slowness3.getItemMeta();
         
            fillmeta.setDisplayName("");  
            speed1meta.setDisplayName(ChatColor.GRAY + "Faster Running I " + ChatColor.GREEN + "(+1)" + ChatColor.GOLD + "(2 Coins)");
            speed2meta.setDisplayName(ChatColor.GRAY + "Faster Running II " + ChatColor.GREEN + "(+2)" + ChatColor.GOLD + "(3 Coins)");
            speed3meta.setDisplayName(ChatColor.GRAY + "Faster Running III " + ChatColor.GREEN + "(+4)" + ChatColor.GOLD + "(5 Coins)");
            strength1meta.setDisplayName(ChatColor.RED + "Buffer Arms I " + ChatColor.GREEN + "(+1)" + ChatColor.GOLD + "(2 Coins)");
            strength2meta.setDisplayName(ChatColor.RED + "Buffer Arms II " + ChatColor.GREEN + "(+3)" + ChatColor.GOLD + "(4 Coins)");
            strength3meta.setDisplayName(ChatColor.RED + "Buffer Arms III " + ChatColor.GREEN + "(+5)" + ChatColor.GOLD + "(7 Coins)");
            regen1meta.setDisplayName(ChatColor.RED + "Quick HealingI " + ChatColor.GREEN + "(+1)" + ChatColor.GOLD + "(1 Coins)");
            regen2meta.setDisplayName(ChatColor.RED + "Quick HealingII " + ChatColor.GREEN + "(+2)" + ChatColor.GOLD + "(3 Coins)");
            regen3meta.setDisplayName(ChatColor.RED + "Quick HealingIII " + ChatColor.GREEN + "(+4)" + ChatColor.GOLD + "(5 Coins)");
            resis1meta.setDisplayName(ChatColor.GOLD + "Thicker Skin I " + ChatColor.GREEN + "(+1)" + ChatColor.GOLD + "(2 Coins)");
            resis2meta.setDisplayName(ChatColor.GOLD + "Thicker Skin II " + ChatColor.GREEN + "(+2)" + ChatColor.GOLD + "(3 Coins)");
            resis3meta.setDisplayName(ChatColor.GOLD + "Thicker Skin III " + ChatColor.GREEN + "(+4)" + ChatColor.GOLD + "(5 Coins)");
            nvisionmeta.setDisplayName(ChatColor.AQUA + "Enhanced Vision " + ChatColor.GREEN + "+1" + ChatColor.GOLD + "(3 Coins)");
            breathing1meta.setDisplayName(ChatColor.BLUE + "Strenthened Lungs I " + ChatColor.GREEN + "(+1)" + ChatColor.GOLD + "(2 Coins)");
            breathing2meta.setDisplayName(ChatColor.BLUE + "Strenthened Lungs II " + ChatColor.GREEN + "(+1)" + ChatColor.GOLD + "(3 Coins)");
            breathing3meta.setDisplayName(ChatColor.BLUE + "Strenthened Lungs III " + ChatColor.GREEN + "(+2)" + ChatColor.GOLD + "(4 Coins)");
            miningfatigue1meta.setDisplayName(ChatColor.DARK_GREEN + "Mining Fatigue I " + ChatColor.RED + "(-1)" + ChatColor.GOLD + "(1 Coins)");
            miningfatigue2meta.setDisplayName(ChatColor.DARK_GREEN + "Mining Fatigue II " + ChatColor.RED + "(-2)" + ChatColor.GOLD + "(2 Coins)");
            blindmeta.setDisplayName(ChatColor.BLACK + "Blindness" + ChatColor.RED + "(-2)" + ChatColor.GOLD + "(2 Coins)");
            slowness1meta.setDisplayName(ChatColor.DARK_RED + "Slowed Legs I" + ChatColor.RED + "(-1)" + ChatColor.GOLD + "(1 Coins)");
            slowness2meta.setDisplayName(ChatColor.DARK_RED + "Slowed Legs I" + ChatColor.RED + "(-2)" + ChatColor.GOLD + "(2 Coins)");
            slowness3meta.setDisplayName(ChatColor.DARK_RED + "Slowed Legs I" + ChatColor.RED + "(-4)" + ChatColor.GOLD + "(3 Coins)");
         
            speed1.setItemMeta(speed1meta);
            speed2.setItemMeta(speed2meta);
            speed3.setItemMeta(speed3meta);
            strength1.setItemMeta(strength1meta);
            strength2.setItemMeta(strength2meta);
            strength3.setItemMeta(strength3meta);
            regen1.setItemMeta(regen1meta);
            regen2.setItemMeta(regen2meta);
            regen3.setItemMeta(regen3meta);
            resis1.setItemMeta(resis1meta);
            resis2.setItemMeta(resis2meta);
            resis3.setItemMeta(resis3meta);
            nvision.setItemMeta(nvisionmeta);
            breathing1.setItemMeta(breathing1meta);
            breathing2.setItemMeta(breathing2meta);
            breathing3.setItemMeta(breathing3meta);
            miningfatigue1.setItemMeta(miningfatigue1meta);
            miningfatigue2.setItemMeta(miningfatigue2meta);
            blind.setItemMeta(blindmeta);
            slowness1.setItemMeta(slowness1meta);
            slowness2.setItemMeta(slowness2meta);
            slowness3.setItemMeta(slowness3meta);
         
            inv.setItem(0, fill);
            inv.setItem(1, fill);
            inv.setItem(2, fill);
            inv.setItem(3, fill);
            inv.setItem(4, fill);
            inv.setItem(5, fill);
            inv.setItem(6, fill);
            inv.setItem(7, fill);
            inv.setItem(8, fill);
         
            inv.setItem(9, fill);
            inv.setItem(12, speed1);
            inv.setItem(13, speed2);
            inv.setItem(14, speed3);
            inv.setItem(17, fill);
         
            inv.setItem(18, fill);
            inv.setItem(21, strength1);
            inv.setItem(22, strength2);
            inv.setItem(23, strength3);
            inv.setItem(26, fill);
         
            inv.setItem(27, fill);
            inv.setItem(30, regen1);
            inv.setItem(31, regen2);
            inv.setItem(32, regen3);
            inv.setItem(35, fill);
         
            inv.setItem(36, fill);
            inv.setItem(39, resis1);
            inv.setItem(40, resis2);
            inv.setItem(41, resis3);
            inv.setItem(44, fill);
         
            inv.setItem(45, fill);
            inv.setItem(48, fill);
            inv.setItem(49, nvision);
            inv.setItem(50, fill);
            inv.setItem(53, fill);
         
            inv.setItem(54, fill);
            inv.setItem(57, breathing1);
            inv.setItem(58, breathing2);
            inv.setItem(59, breathing3);
            inv.setItem(62, fill);
         
            inv.setItem(63, fill);
            inv.setItem(66, miningfatigue1);
            inv.setItem(67, fill);
            inv.setItem(68, miningfatigue2);
            inv.setItem(71, fill);
         
            inv.setItem(72, fill);
            inv.setItem(75, fill);
            inv.setItem(76, blind);
            inv.setItem(77, fill);
            inv.setItem(80, fill);
         
            inv.setItem(81, fill);
            inv.setItem(84, slowness1);
            inv.setItem(85, slowness2);
            inv.setItem(86, slowness3);
            inv.setItem(89, fill);
         
            inv.setItem(90, fill);
            inv.setItem(91, fill);
            inv.setItem(92, fill);
            inv.setItem(93, fill);
            inv.setItem(94, fill);
            inv.setItem(95, fill);
            inv.setItem(96, fill);
            inv.setItem(97, fill);
            inv.setItem(98, fill);
         
            p.openInventory(inv);
         
         
         
            }
     
        return false;
        }
    }
     
    Anyways, one more thing I need help with.

    My command doesnt work .-.

    It is registered (It is acknowledged ingame), but no inventory opens, and my debug message does nothing.

    Plugin.YML:
    Code (Text):

    name: GalactiCraft
    main: com.stormcraft.main.main
    version: 0.1
    description: The core plugin for GalacticCraft
    commands:
      dna:
        description: Build genetics!
     
    And before anyone gives me "Learn java" bullshit, I am learning it right now and am doing a project along the way to help learn
     
    #1 Gorobetz, May 29, 2017
    Last edited: May 29, 2017
    • Funny Funny x 1
  2. I'm gonna say it anyway, Learn Java and Follow Conventions.

    Class names are UpperCamelCase.
    Don't create walls of code, use for loops.
    These variables dont need to be static
    Use @Override

    Don't check against the label, check Command#getName()

    Also the class "com.stormcraft.main.main", is not your main class... The name of your main class is "builder" (should be "Builder")
     
    • Like Like x 1
  3. Yes it is.
    Should be*
    That is just tradition for people to name classes like that, but there is no rule against it.
    Makes no sense, I did it for other commands and it did work.
    I would prefer it to be static.

    Override is there.

    What about the scheduler? Do you have anything to say about that or do you just want to nitpick over things that dont have a big effect (statics, conventions, loops that wouldn't be useful)?

    And what about my inventory not opening?
     
    #3 Gorobetz, May 29, 2017
    Last edited: May 29, 2017
    • Funny Funny x 1
  4. Sweetie.... your class is literally named "builder".
    And because commands can have many labels. you're only checking against one of them. Checking the name of the command is the correct way.
    They don't NEED to be static, and having them be static does absolutely nothing...
    You do not have Override on the "onCommand" method.
    And yes, the loop would be useful, you wouldn't need 30 lines to set all the slots, you could instead have 3.

    And yes. I will nitpick. If you want to be helped on these forums, follow convention, no one wants to look through your shitty code...

    Just trying to help, don't be a dick :D
     
    • Like Like x 1
  5. You are being a dick yourself saying this
    Because you are acting like you are smarter than me in java and top shit when you dont even know what you are talking about.

    But yeah, because my plugin has only 1 command my plugin definitely has 1 class and no others...
    Having said I would do for each item, give it the same cost because I dont know if it is strength or speed,
    oh wait I'll just use an if statement
    but nah that will take more lines of code

    And I just love how you are trying to convince me not to have it static just because you are against it, when I have a specific reason for it to be like that

    Now since your only valid point is that I forgot an override that was never mentioned to me, and because it is not in java itself, but rather from the spigot API, I don't know java. Wait, what's that? Override doesn't work? no way!
    [​IMG]

    Well, Im convinced. I'll never try programming again because of this bullshit you're giving me
     

    Attached Files:

    #5 Gorobetz, May 29, 2017
    Last edited: May 29, 2017
    • Optimistic Optimistic x 1
  6. @Gorobetz Just listen to @Sxtanna. Learn the Java naming conventions and stop being such a bitch. I can't believe you are calling @Sxtanna a dick when he/she is actually helping you make your code more clean and efficient.
    You can go ahead and get your ass out of here and lock this thread and stop coding if you are going to talk shit to people like that.
    I think you are the one who should be called a dick.
     
    #6 Username1234, May 29, 2017
    Last edited: May 29, 2017
  7. this is annoying

    A convention is nothing more than a tradition, it has absolutely no effect on the code and how it operates, nor any effect on compilation speeds, or effectiveness. The only thing it is good for is feeding retarded egos.

    Then you told me I have no position to call someone a dick when they called me one because I was asking for help and they metaphorically slapped me with their bullshit

    and this part
    is just autistic on it's own

    And also, how am I in anyway an ass? Because I mentioned how loops wouldn't work and how there is a reason for my variablels to be static?

    EDIT: Forgot to mention how you took my sarcastic statement and turned it into something serious, which is retarded on it's own, and I could use the literal definition of retarded to back this statement up
     
    • Funny Funny x 1
  8. Hey, I learned something new today! If you follow the naming convention, you are retarded egos!
    Uh no you are the retarded ego
     
  9. The retarded ego is the fact that you actually care about the case

    And you know thats what I meant when I said it the first time
     
    #9 Gorobetz, May 29, 2017
    Last edited: May 30, 2017
    • Optimistic Optimistic x 1
  10. Oh god... I'm out...
     
  11. You have SERIOUS issues...
     
  12. electronicboy

    IRC Staff

    1. there is blatantly no reason to go on the attack against each other, especially when somebody is trying to help you.

    2. "conventions" are there for a reason and make it easier when looking at somebodies code who is asking for help. If you're going to ask for help or want others to look at it, you should follow conventions so that people can actually work out what your code is doing. lowerCamelCase and UpperCamelCase to represent fields and classes is a concept that should be highly ingrained in you so that you don't produce unmaintainable code, or have to start using oddball field names because you've not named them sanely. as much as you're going to hate to hear this, if you're not willing to follow any form of convention, you're likely wasting your time as nobody is going to want to hire somebody who writes code without any form of standard, it's good to learn sane practices from the start, especially those which are considered important such as proper casing.

    3. @Override is a sane convention that is part of the Java language and enforced by the compiler in order to deal with cases of inheritance, your onCommand method is screaming because your command isn't in a Class that defines onCommand(e.g. JavaPlugin, or a CommandExecuter class. If you're creating your own class for commands, you should make it implement CommandExecuter). In this case, unless you've made more changes to your code beyond what is in the original post and adding @Override, you've likely hit one of the fun bugs in Eclipse.

    4. using labels, as already noted is bad as that passes whatever somebody ran instead of the actual command name, e.g. if you have an alias /taco for /goodfood, if you ran /taco, label is going to be taco, command name is going to be good food.

    5. your main: in plugin.yml is wrong, your JavaPlugin class is apparently called builder, you're referring to a class as main; which either means that you've defined more than one JavaPlugin class (Main is also considered bad practice if you ever expect people to integrate anything with your plugin), which you can't do as you'd throw an exception upon creating an instance of it, or your plugin.yml is actually in fact wrong.
     
Thread Status:
Not open for further replies.