Solved help with spigot

Discussion in 'Spigot Plugin Development' started by jorikasgaming, Jul 9, 2018.

  1. Hi, I am making a plugin and I want to save a stringlist to a config.
    and sometimes it saves the list properly, and sometimes it saves it like this:
    OIL:
    '1': []
    '2': []
    '3': []
    '4': []
    '5': []
    '6': []
    '7': []
    '8': []
    '9': []
    '10': []
    '11': []
    '12': []
    '13': []
    '14': []
    '15': []
    '16': []
    '17': []
    '18': []
    '19': []
    '20': []
    '21': []
    '22': []
    '23': []
    instead of those brackets there should be quite a few strings :/
    can someone pls explain? I am developping for spigot but in my eclipse i used paperspigot as source so idk maybe that messed it up... i don't know how to change the source spigot jar so any explanation would be great :D thanks in advance :)
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Those are empty arrays, meaning there isn't anything in them. You probably aren't saving them correctly. Post your code.
     
  3. it is an animating plugin, so there is a lot going on

    Code (Text):
    package me.jorikas.plugins.animator;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;

    public class animwizzard implements CommandExecutor {
        public static ArrayList<UUID> pos1 = new ArrayList<UUID>();
        public static ArrayList<UUID> pos2 = new ArrayList<UUID>();
        public static ArrayList<String> names = new ArrayList<String>();
        public static HashMap<String, Location> canvas1 = new HashMap<String, Location>();
        public static HashMap<String, Location> canvas2 = new HashMap<String, Location>();
        public HashMap<String, Integer> frameid = new HashMap<String, Integer>();
        public Plugin mainas = Main.mainas();
        public FileConfiguration config = mainas.getConfig();
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(!sender.hasPermission("blockanimator.create")) {
                return true;
            }
            getnames();
            if(args[0].equals("frame")) {
                if(!names.contains(args[1])) {
                    sender.sendMessage(ChatColor.RED + "invalid canvas name!");
                    return true;
                }
                if(args.length == 1) {
                    sender.sendMessage(ChatColor.RED+"you must specify the canvas name!");
                }
                if(args.length == 2) {
                    int frameas = frameid.get(args[1]);
                    frameas ++;
                    frameid.remove(args[1]);
                    frameid.put(args[1], frameas);
                    config.set(args[1]+"."+frameas, gotroughblocks(canvas1.get(args[1]), canvas2.get(args[1]), frameas, args[1]));
                    mainas.saveConfig();
                    sender.sendMessage(ChatColor.YELLOW + "Frame "+frameas+" for animation "+args[1]+" created!");
                   
                }
            }
           
            if(args[0].startsWith("canvas")) {
                if(args[1].equals("new")) {
                   
                   
                   
                   
                   
                    // if command /animator canvas new ;; then start canvas wizzard
                    Player player = (Player) sender;
                    if(sender instanceof Player) {
                        sender.sendMessage(ChatColor.YELLOW+"Starting creation of canvas");
                        sender.sendMessage(ChatColor.BLUE+"punch position 1 of your new canvas");
                        pos1.add(player.getUniqueId());
                    }
                    // =========================================
                   
                   
                   
                   
                    // if sender not player spit out an error
                    else {
                        sender.sendMessage(ChatColor.RED+"you must be a player!");
                    }
                    return true;
                    // =======================================
                }
               
                if(args[1].equals("create")) {
                    if(names.contains(args[2])) {
                        sender.sendMessage(ChatColor.RED+"Animation named "+args[2]+", already exists!");
                        return true;
                    }
                    names.add(args[2]);
                    Player player = (Player) sender;
                    canvas1.put(args[2], leftright.canvas1.get(player.getUniqueId()));
                    canvas2.put(args[2], leftright.canvas2.get(player.getUniqueId()));
                    leftright.canvas1.remove(player.getUniqueId());
                    leftright.canvas2.remove(player.getUniqueId());
                    // ideti config section
                    config.createSection(args[2]);
                    sender.sendMessage(ChatColor.YELLOW+"Animation canvas named "+args[2]+", created!");
                    frameid.put(args[2], 0);
                   
                }
            }

            return true;
        }
        public List<String> gotroughblocks (Location l1, Location l2, int frameidnow, String name) {
            // data
            List<String> blocklist = new ArrayList<String>();
            int x1 = (int) l1.getX();
            int x2 = (int) l2.getX();
            int y1 = (int) l1.getY();
            int y2 = (int) l2.getY();
            int z1 = (int) l1.getZ();
            int z2 = (int) l2.getZ();
            // =================
           
            //check if reversed and fix
            if(x1>x2) {
                int tempint1 = x1;
                int tempint2 = x2;
                x1 = tempint2;
                x2 = tempint1;
            }
            if(y1>y2) {
                int tempint1 = y1;
                int tempint2 = y2;
                y1 = tempint2;
                y1 = tempint1;
            }
            if(z1>z2) {
                int tempint1 = z1;
                int tempint2 = z2;
                z1 = tempint2;
                z2 = tempint1;
            }
            //=================
            //foreach block
            for(int iksas = x1; iksas<=x2; iksas++) {
                for(int igrek = y1; igrek<=y2; igrek++) {
                    for(int zed = z1; zed<=z2; zed++) {
                        Location locforsaving = new Location(l1.getWorld(),iksas,igrek,zed);
                        Block blokforsaving = locforsaving.getBlock();
                        String bloc = blockparser(blokforsaving);
                        blocklist.add(bloc);
                        // ideda i block parseri
                    }
                }
            }
            return blocklist;
            // ===================================
        }
       
       
        // block parser to string
        public String blockparser(Block blokas) {
            //string structure : x:123 ;y:123 ;z:123 ;w:world ;b:dirt ;data:byte
            String forsavin = blokas.getX()+" 00759 "+blokas.getY()+" 00759 "+blokas.getZ()+" 00759 "+blokas.getWorld().getName()+" 00759 "+blokas.getType()+" 00759 "+blokas.getData();
            System.out.println(forsavin);
            return forsavin;
        }
        //============================
        // get names
        public void getnames() {
            for(String key: config.getKeys(false)) {
                if(!names.contains(key)) {
                    names.add(key);
                }
            }
        }
        // =============
    }
     
    this is just for a single class and i need those statics
     
  4. Your naming conventions are wrong. Classes should be named with capitalised first letters of every word, e.g. InventoryBuilder or AnimWizzard.
    Functions should be named with camel casing, i.e. the same as classes, except for the first word, e.g. onCommand or getRoughBlocks. Also, your formatting should be such that there is a line between variable names and the onCommand function, yet there are massive spaces later on? Use an IDE like IntelliJ, which when you press ctrl + alt + l auto formats your code :)

    I'm not sure, but the ArrayList pos1 sounds like it's independent for each player, or wizzard or I dunno, but if so it should not be static and there should be a player or wizzard or I dunno class.
     
  5. Thanks for all the information, I didn't know all the naming conventions, but could that be why sometimes for no reason my plugin breaks? I didn't know how to make multi instance plug-ins when I made the wizard, not too sure now but I think I have an idea... Well either way thanks very much, I will check code when I wake up... Pretty sure I figured out my problem.
     
  6. wow, just installed intellij, put my class in there and did autoformat, then put it back in eclipse :/ because i don't knwo how to swtich ides yet, ill have to learn a bit about intellij, but long story short after autoformating it works well :)
     
  7. nvm still doesnt work, it still randomly breaks, but i will remake the animwizzard in intellij so its ok thanks :)
     
  8. Oh god, you are killing Java!
    Why? Java is OOP based so, if you use static keyword everywhere has no sense!
    protip: learn java and after you'll be able to make plugins!
     
  9. I may not be good at Java. This plugin was something I threw together really quickly for my brother to play with. I am a c# programmer and a php back end developper. Stop telling people to go learn Java first if you can't help.
     
    • Agree Agree x 1
  10. DavidDevelops

    Supporter

    Well its true, To use an API you need to know the base language, If you truly are a developer you understand this, Either way, No need to switch IDEs if you don't want to CRLT + F autoformats too (Or it was CRLT + Shift + F, also CRLT + O auto removes unused imports)

    But either way, You don't seem very professional if you are a c# Programming and a PHP developer, Either way, With no java knowledge you can't write plugins, Take a few and learn the basics, java shouldn't be hard to learn the basics if you know c# (I Know its different but not very hard to move over in my option)
     
    • Like Like x 1
  11. Java and C# are very similar. Trust me, learn Java and you’ll be able to make every plugin you want
     
  12. DavidDevelops

    Supporter

    Kind of, He still will need to learn to use the api, and for more advanced things nms and reflection
     
  13. well thanks all. I know c# and java are very simillar, but I do plugins when I am bored or when my brother needs to play with something. But ok it seems like java is good either way.. .