Error with HaspMap reading and writing

Discussion in 'Plugin Development' started by BrainyXS, Feb 22, 2018.

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

    BrainyXS

    I have Coded a plugin, you should make /home sethome to set a Home and /home to get teleported to this Location.

    When I use the command or start my server there a lots of Errors.

    Code
    Code:
    import org.bukkit.Material;
    import org.bukkit.plugin.java.JavaPlugin;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Set;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.World;
    import org.bukkit.Location;
    import org.bukkit.plugin.java.JavaPlugin;
    import java.util.UUID;
    
    public class HomeStytemPB extends JavaPlugin {
        private HashMap<UUID,Location> position;
       
        public void onEnable(){
            File datei = new File(this.getDataFolder(), "/Homelist");
            this.getLogger().info("Home-Plugin by BrainyXS erfolgreich aufgesetzt.");
           
           
            if(datei.exists()){
                try {
                    InputStream stream = new FileInputStream(datei);
                    ObjectInputStream objectStream = new ObjectInputStream(stream);
                    position = (HashMap<UUID,Location>) objectStream.readObject();
                    objectStream.close();
                   
                }
                catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            else{
                position = new HashMap<UUID,Location>();
            }
           
        }
       
        public void onDisable(){
            if(position.size() > 0){
                if(!this.getDataFolder().exists()){
                    this.getDataFolder().mkdirs();
                   
                }
               
                File datei = new File(this.getDataFolder(), "/Homelist");
               
                try {
                    OutputStream stream = new FileOutputStream(datei);
                    ObjectOutputStream objectStream = new ObjectOutputStream(stream);
                    objectStream.writeObject(position);
                    objectStream.close();
                   
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
       
        public boolean onCommand(CommandSender sender, Command befehl, String befehlsname, String[] args){
           
            if(sender instanceof Player){
                Player spieler = (Player) sender;
               
                if(args[0].equals("sethome")){
                     //Home adden
                     Location pos = spieler.getLocation();
                     position.put(spieler.getUniqueId(),pos);
                     spieler.sendMessage("Dien Home wurde Erfolgreich gesetzt! Nutze /home für einen Teleport!");
               
                }
                else{
                    spieler.sendMessage("Du wirst sofort nach Hause gebracht.");
                    UUID ID = spieler.getUniqueId();
                    spieler.teleport(position.get(ID));
                }
            }
           
            return true;
        }
    
    }
    Thanks for help!
     
  2. What's the error?

    Sent from my SM-G903F using Tapatalk
     
  3. Offline

    BrainyXS

  4. Offline

    Lorinthio

    The error is that the Location object isn't serializable. Which is because the world object within Location isn't serializable. Might be better to handle Locations as Vectors if worlds don't matter, or handle locations a different way
     
  5. Online

    timtower Moderator Moderator

  6. Offline

    BrainyXS

    what is Serialize?? Could you explain it?
     
  7. Online

    timtower Moderator Moderator

    @BrainyXS Storing it, you are storing it using a stream, do you know how that works?
     
  8. Offline

    BrainyXS

    @timtower No, this is the first Time i crete Hashmap/Lists who stay after a serverrestart... I`m just a hobby-coder and I code just for fun and im not good...
     
Thread Status:
Not open for further replies.

Share This Page