1.14.4 serialization

Discussion in 'Spigot Plugin Development' started by eralysium, Jan 26, 2020.

  1. I want to create some kind of kinematics system. So I created a system to add the player's position in the arraylist (with a loop like here). I saved this list with the serialization (do I have to use another method ?).
    And now I want to retrieve the contents of my list to teleport the player at each coordinate (to give the effect of kinematics).


    I would like to retrieve the contents of an arraylist that I serialized (the file will create itself well).
    The problem is that I have errors.


    Code (Text):
    if(cmd.getName().equalsIgnoreCase("test")){
                   
                ArrayList<Location> list= new ArrayList<Location>();
                try {
                  FileInputStream fileIn = new FileInputStream("test");
                  ObjectInputStream ois = new ObjectInputStream(fileIn);
                  list = (ArrayList) ois.readObject();
                  ois.close();
                  fileIn.close();
                } catch (FileNotFoundException e) {
                  e.printStackTrace();
                } catch (IOException e) {
                  e.printStackTrace();
                } catch (ClassNotFoundException e) {
                   e.printStackTrace();
                }
                System.out.println("Lire les donnĂ©es: \n");

               
                player.sendMessage("" + list.size());
                new BukkitRunnable() {
                    private int i = 0;
                    public void run() {
                        i++;
                        if(i >= list.size()) {
                            cancel();
                        }
                        else {
                            player.teleport(list.get(i-1));
                        }
                    }
                }.runTaskTimer(main, 0, 1);
               
            }
    Code (Text):
    erreur line: if(i >= list.size()) {
    local variable list defined in a enclosing scope must be final or effectivly final

    warning line: list = (ArrayList) ois.readObject();
     
  2. You need to create a second variable that represents the final output of the arraylist. Then use that inside the Runnable.
    Code (Text):
    final List<Location> li = list;