updata tp 1
This commit is contained in:
@ -4,8 +4,14 @@ import cn.nukkit.Player;
|
||||
import cn.nukkit.item.Item;
|
||||
import com.alphaae.mcpe.servers.MainPlugin;
|
||||
import com.alphaae.mcpe.servers.model.RePlayer;
|
||||
import com.alphaae.mcpe.servers.model.UserLocation;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerDataUtils {
|
||||
@ -23,12 +29,34 @@ public class PlayerDataUtils {
|
||||
public static RePlayer LoadData(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
try {
|
||||
// long begin = System.currentTimeMillis();
|
||||
File playerDataFile = new File(PLAYER_DATA_FOLDER, uuid.toString() + FILE_TYPE);
|
||||
if (!playerDataFile.exists()) {
|
||||
CreateNewPlayerData(player);
|
||||
if (!CreateNewPlayerData(player))
|
||||
return null;
|
||||
}
|
||||
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(playerDataFile));
|
||||
RePlayer rePlayer = (RePlayer) inputStream.readObject();
|
||||
//NIOs
|
||||
StringBuilder json = new StringBuilder();
|
||||
Charset charset = Charset.forName("UTF-8");
|
||||
CharsetDecoder decoder = charset.newDecoder();
|
||||
FileInputStream inputStream = new FileInputStream(playerDataFile);
|
||||
FileChannel channel = inputStream.getChannel();
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(1024); //初始化缓冲区大小
|
||||
CharBuffer charBuffer = CharBuffer.allocate(1024);
|
||||
channel.read(byteBuffer); //将文件通道里面的字节读到缓冲区中
|
||||
byteBuffer.flip(); //将position置为0,limit放到position位置
|
||||
decoder.decode(byteBuffer, charBuffer, false); //解码
|
||||
charBuffer.flip();
|
||||
while (charBuffer.hasRemaining()) {
|
||||
json.append(charBuffer.get());
|
||||
}
|
||||
channel.close();
|
||||
inputStream.close();
|
||||
|
||||
RePlayer rePlayer = RePlayer.decodeObject(json.toString()); //解码对象
|
||||
MainPlugin.getPlugin().getServer().getLogger().info("" + rePlayer.getJSONData());
|
||||
// long time = System.currentTimeMillis() - begin;
|
||||
// MainPlugin.getPlugin().getServer().getLogger().info("角色数据载入耗时:" + time + " ms");
|
||||
return rePlayer;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@ -36,12 +64,16 @@ public class PlayerDataUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean ChangeData(RePlayer rePlayer) {
|
||||
public static boolean SaveData(RePlayer rePlayer) {
|
||||
|
||||
File playerDataFile = new File(PLAYER_DATA_FOLDER, rePlayer.getUuid().toString() + FILE_TYPE);
|
||||
try {
|
||||
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(playerDataFile, false));
|
||||
outputStream.writeObject(rePlayer);
|
||||
//NIO
|
||||
FileOutputStream outputStream = new FileOutputStream(playerDataFile);
|
||||
FileChannel channel = outputStream.getChannel();
|
||||
ByteBuffer buffer = ByteBuffer.wrap(rePlayer.getJSONData().getBytes("UTF-8"));
|
||||
channel.write(buffer);
|
||||
channel.close();
|
||||
outputStream.close();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
@ -57,12 +89,10 @@ public class PlayerDataUtils {
|
||||
player.getInventory().addItem(new Item(347));
|
||||
|
||||
RePlayer rePlayer = new RePlayer(uuid, "新火", 2000);
|
||||
|
||||
File playerDataFile = new File(PLAYER_DATA_FOLDER, uuid.toString() + FILE_TYPE);
|
||||
playerDataFile.createNewFile();
|
||||
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(playerDataFile));
|
||||
outputStream.writeObject(rePlayer);
|
||||
outputStream.close();
|
||||
return true;
|
||||
return SaveData(rePlayer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
Reference in New Issue
Block a user