Home-System problem: UUID to int

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

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

    BrainyXS

    Hello everyone

    I`m coding my homesystemplugin, you should make /home sethome to set you current Location as you home.
    With /home you should be teleported to this Location.

    I have an Error in Line 85: UUID can not be converted to Int. How I should do that?
    85 is: spieler.teleport(Homelist.get(ID).getPosition());
    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.List;
    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 List<Homes> Homelist;
     
        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);
                    Homelist = (List<Homes>) objectStream.readObject();
                    objectStream.close();
                 
                }
                catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            else{
                Homelist = new ArrayList<Homes>();
            }
         
        }
     
        public void onDisable(){
            if(Homelist.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(Homelist);
                    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
                     Homes eintrag = new Homes(spieler.getUniqueId(),spieler.getLocation());
                     Homelist.add(eintrag);
                     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(Homelist.get(ID).getPosition());
                }
            }
        }
    }

    And the 2. code for saving:

    Code:
    import java.io.Serializable;
    import java.util.Map;
    import java.util.UUID;
    import org.bukkit.Location;
    
    public class Homes implements Serializable {
        private static final long serialVersionUID = 1L;
        private Map<String,Object> position;
        private UUID besitzer;
    
        public Homes(UUID besitzer, Location position){
    
            this.besitzer = besitzer;  
            this.position = position.serialize();
        }
    
        public Location getPosition(){
            return Location.deserialize(position);
        }
    
        public UUID getBesitzer(){
            return this.besitzer;
        }
    }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @BrainyXS ID is an UUID, but the get method always takes an int with lists.
    You need to find the right one by hand or use a hashmap.
     
  3. Offline

    BrainyXS

    So how I do that in a code
     
  4. Offline

    timtower Administrator Administrator Moderator

    Change the list to a hashmap.
     
  5. Offline

    BrainyXS

    Yes but what I have to change in this Code, I never used a Hashmap beffor, I quite know what It is but i dont know how I use a Hashmap
     
  6. Offline

    timtower Administrator Administrator Moderator

    private Map<String,Object> position;
    Also a hashmap.
    Or just a map, pretty close to being the same.
     
  7. Offline

    BrainyXS

    so the new codes are

    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.Map;
    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 Map<String,Object> 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 = (Map<String,Object>) objectStream.readObject();
                    objectStream.close();
                 
                }
                catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            else{
                position = new Map<String,Object>();
            }
         
        }
     
        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
                     Homes eintrag = new Homes(spieler.getUniqueId(),spieler.getLocation());
                     position.add(eintrag);
                     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).getPosition());
                }
            }
        }
    }
    and



    Code:
    import java.io.Serializable;
    import java.util.Map;
    import java.util.UUID;
    import org.bukkit.Location;
    
    public class Homes implements Serializable {
        private static final long serialVersionUID = 1L;
        private Map<String,Object> position;
        private UUID besitzer;
    
        public Homes(UUID besitzer, Location position){
    
            this.besitzer = besitzer;    
            this.position = position.serialize();
        }
    
        public Location getPosition(){
            return Location.deserialize(position);
        }
    
        public UUID getBesitzer(){
            return this.besitzer;
        }
    }
    but in Line 43, 78, and 85 are still errors[​IMG]https://prntscr.com/ihw2ft

    Can somebody help me pls?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 23, 2018
  8. Offline

    Lorinthio

    The first error is because Map is abstract. You need to use a class that extends map such as HashMap<>.

    position = new HashMap<String,Object>();
     
Thread Status:
Not open for further replies.

Share This Page