HashMap的节能与ObjectOutputStream中(HashMap saving with

2019-09-21 02:35发布

为了节省命令隐形传输点,我有一个HashMap

public HashMap<Player, Location> mapHomes = new HashMap<>();

这是访问是这样的:

if(cmd.getName().equalsIgnoreCase("sethome")){
    Location loc = player.getLocation();
    mapHomes.put(player, loc);
    sender.sendMessage("Home set !");
    return true;
}
if(cmd.getName().equalsIgnoreCase("home")){
    Location loc1 = mapHomes.get(player);
    player.teleport(loc1);
    sender.sendMessage("Teleported to home");
    return true;
}
return false;

由于这些设置应保持在重新启动,我已经实现了一个保存方法:

public void save(HashMap<Player,Location> mapHome, String path) throws NotSerializableException{
    try{
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
        oos.writeObject(mapHome);
        oos.flush();
        oos.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

但它不工作。 它抛出NotSerializableException

我认为主要的问题是PlayerLocation不是序列类型,所以我应该怎么办写这个HashMap

Answer 1:

HashMap已经是Serializable

问题是,在地图内的对象都没有,所以你必须让他们序列化了。

public class SerializedPlayer extends Player implements Serializable {
    public SerializedPlayer() {}
    public SerializedPlayer(Player playerToClone) {
        this.setField1(playerToClone.getField1());
        // Set all the fields
    }
}

当添加到地图:

map.put(new SerializedPlayer(player), new SerializedLocation(location));


Answer 2:

NotSerializableException当实例需要有一个被抛出Serializable接口。

class YourClass implements Serializable {
    // ...
}


Answer 3:

class Player implements Serializable {}

class Location implements Serializable {}

记住,你只能序列化实施的对象Serializable接口。 所以,你的PlayerLocation类必须实现的接口了。



Answer 4:

我觉得你写HashMap<Player, Location>到文件。

什么,你需要做的就是让你的PlayerLocation类的序列化。

public class Player implements java.io.Serializable {
    // ...
}

public class Location implements java.io.Serializable {
    // ...
}

HashMap是序列化的。



Answer 5:

为了让Location可序列化,我建议使用这个类 。 我自己写的,只是请你给我的信用代码。

package com.github.JamesNorris.Class;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;

/**
 * The class that allows location to be serialized, and be used
 * in a file. This class should not be changed, otherwise it
 * will not work for older files.
 * 
 * @author Jnorr44
 */

public final class SerializableLocation implements Serializable {
    private static final long serialVersionUID = 8650311534439769069L;

    private final String world;
    private final String uuid;
    private final double x, y, z;
    private final float yaw, pitch;
    private transient Location loc;

    /**
     * Creates a new SerializableLocation instance of any org.bukkit.Location.
     * 
     * @param l
     */

    public SerializableLocation(Location l) {
        this.world = l.getWorld().getName();
        this.uuid = l.getWorld().getUID().toString();
        this.x = l.getX();
        this.y = l.getY();
        this.z = l.getZ();
        this.yaw = l.getYaw();
        this.pitch = l.getPitch();
    }

    /**
     * Gets the org.bukkit.Location back from any SerializableLocation.
     * 
     * @param l
     * @return
     */

    public static Location returnLocation(SerializableLocation l) {
        float pitch = l.pitch;
        float yaw = l.yaw;
        double x = l.x;
        double y = l.y;
        double z = l.z;
        World world = Bukkit.getWorld(l.world);
        Location location = new Location(world, x, y, z, yaw, pitch);
        return location;
    }

    // FROM HERE ON NEEDS DOC NOTES

    public SerializableLocation(Map<String, Object> map) {
        this.world = (String) map.get("world");
        this.uuid = (String) map.get("uuid");
        this.x = (Double) map.get("x");
        this.y = (Double) map.get("y");
        this.z = (Double) map.get("z");
        this.yaw = ((Float) map.get("yaw")).floatValue();
        this.pitch = ((Float) map.get("pitch")).floatValue();
    }

    public final Map<String, Object> serialize() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("world", this.world);
        map.put("uuid", this.uuid);
        map.put("x", this.x);
        map.put("y", this.y);
        map.put("z", this.z);
        map.put("yaw", this.yaw);
        map.put("pitch", this.pitch);
        return map;
    }

    public final Location getLocation(Server server) {
        if (loc == null) {
            World world = server.getWorld(this.uuid);
            if (world == null) {
                world = server.getWorld(this.world);
            }
            loc = new Location(world, x, y, z, yaw, pitch);
        }
        return loc;
    }
}

现在简单地做:

SerializableLocation loc = new SerializableLocation(LOCATION);

这是必需的原因是因为Location包含worldxyzyawpitch ,其中world是不可序列。



Answer 6:

事实上,你正在试图序列PlayerLocation ,努力实现新的课程等,但你为什么要这么做?

如果你不保存这将是更好的PlayerLocation的对象,但他们的字符串表示来代替。 例如,你可以使用Player.getName()和类似"world:100:65:100"的位置(所以我们可以很容易地得到所有数据回来String.split(":")我认为这是一个更好的方法。



文章来源: HashMap saving with ObjectOutputStream