Config questions

Discussion in 'Spigot Plugin Development' started by zDoeProgram, May 10, 2017.

  1. Hello,
    I'm wondering about two questions.

    I have a switch statement that checks the Displayname of the clicked item.
    Code (Text):
    switch (e.getCurrentItem().getItemMeta().getDisplayName()){
    However, I want the Displayname to be configurable, like this:
    Code (Text):
    private String TNT1_NAME = ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("TNT1_NAME"));
    Now, I cant use this for my cases:
    Code (Text):
    case TNT1_NAME:
    because of: "case expressions must be constant expressions".

    How would I make this work?


    Currently whenever someone reloads the server, the faction lose their ability because I never stored them in the config. What would be the most efficent way to do this without causing lag?

    Code (Text):
    Main.getPlugin().econ.withdrawPlayer(p, TNT1PRICE);
                    FPlayer fPlayer = FPlayers.getInstance().getByPlayer(p);
                     Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getPlugin(), new Runnable() {
                            public void run() {
                                p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("IMMUNITY_RAN_OUT")));
                     }, 20 * 60 * TNT1);
    How could I fix this, so the ability start up after server restart without making a lot of lag?
  2. 1. Use if/else expressions, switch statements cannot run case-analysis on variables, and must operate on constant expressions
    if displayname == <var>:
    else if displayname == <other_var>:
    2. You have to save the data somehow so it persists through reload. The best way is to serialize the data. I would recommend just saving it all to json (or if its a lot of data, use a DBMS) for easy retrieval. However, I don't think I have enough information about what you are trying to do to help you more than that.
  3. 1. Is the name in your config file expected to change? If not, can you just make it a final variable?
    2. I agree with AoH_Ruthless above. JSON or a database is probably the best option.
  4. Hello, I changed the switch statement, and now the plugin dont work properly, like for instance, it only display one of my items, and I can drag the item. I think this might be because I made my Inventory name configurable.

    My code:

    My config:

    btw, would I be able to create a custom config file which gets created when the ability is activated and deleted whenever an ability runs out?
  5. You could do that but that's an awful lot of I/O and will slow the plugin considerably. I would recommend only writing to a flatfile the state of these abilities in your onDisable() method, because you only need them saved when you shut down the server.

    Try removing the if statement (while keeping e.setCancelled(true)) at line 64 and see if that prevents it from dragging.
  6. Hey, I fixed the drag thing, it was because of the name. I cant have it configurable my way. However, now when I do my command only one of my TNTs show up, and when I click it nothing happends. No errors in consol.

  7. Uh not quite, #getDisplayName returns a string value.

    The problem being that it could return null, which would break because string switch statements use the string's #hashcode, which null strings have none.
  8. Bump
  9. Hello for your question one you can do..

    Code (Text):
         final String TntName1 = "Boom"; < Must be final
        public void CheckName(String ItemName){
            switch (ItemName) {
            case "BigTNT": //Do big tnt stuff
            case "SmallTNT": //Do small tnt stuff
            case TntName1: //.....