ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/data/PlayerData.java

141 lines
4.8 KiB
Java
Raw Normal View History

/*
2016-08-02 13:08:22 +02:00
* This file ("PlayerData.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.data;
2019-05-02 09:10:29 +02:00
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import de.ellpeck.actuallyadditions.api.booklet.IBookletPage;
import de.ellpeck.actuallyadditions.mod.booklet.gui.GuiBooklet;
import de.ellpeck.actuallyadditions.mod.booklet.misc.BookletUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2019-05-02 09:10:29 +02:00
public final class PlayerData {
2019-05-02 09:10:29 +02:00
public static PlayerSave getDataFromPlayer(EntityPlayer player) {
2017-03-08 20:06:55 +01:00
WorldData worldData = WorldData.get(player.getEntityWorld());
ConcurrentHashMap<UUID, PlayerSave> data = worldData.playerSaveData;
UUID id = player.getUniqueID();
2019-05-02 09:10:29 +02:00
if (data.containsKey(id)) {
2017-03-08 20:06:55 +01:00
PlayerSave save = data.get(id);
2019-05-02 09:10:29 +02:00
if (save != null && save.id != null && save.id.equals(id)) { return save; }
2015-10-03 10:16:18 +02:00
}
//Add Data if none is existant
PlayerSave save = new PlayerSave(id);
2017-03-08 20:06:55 +01:00
data.put(id, save);
worldData.markDirty();
return save;
2015-10-03 10:16:18 +02:00
}
2019-05-02 09:10:29 +02:00
public static class PlayerSave {
public UUID id;
public boolean bookGottenAlready;
2016-11-12 15:09:30 +01:00
public boolean didBookTutorial;
public boolean hasBatWings;
2016-11-22 19:35:52 +01:00
public boolean shouldDisableBatWings;
public int batWingsFlyTime;
public IBookletPage[] bookmarks = new IBookletPage[12];
2019-02-27 19:53:05 +01:00
public List<String> completedTrials = new ArrayList<>();
@SideOnly(Side.CLIENT)
public GuiBooklet lastOpenBooklet;
2019-05-02 09:10:29 +02:00
public PlayerSave(UUID id) {
this.id = id;
2015-10-23 16:54:33 +02:00
}
2019-05-02 09:10:29 +02:00
public void readFromNBT(NBTTagCompound compound, boolean savingToFile) {
this.bookGottenAlready = compound.getBoolean("BookGotten");
2016-11-12 15:09:30 +01:00
this.didBookTutorial = compound.getBoolean("DidTutorial");
2016-11-22 19:35:52 +01:00
this.hasBatWings = compound.getBoolean("HasBatWings");
this.batWingsFlyTime = compound.getInteger("BatWingsFlyTime");
NBTTagList bookmarks = compound.getTagList("Bookmarks", 8);
this.loadBookmarks(bookmarks);
2016-11-22 19:35:52 +01:00
2017-02-18 00:54:58 +01:00
NBTTagList trials = compound.getTagList("Trials", 8);
this.loadTrials(trials);
2019-05-02 09:10:29 +02:00
if (!savingToFile) {
2016-11-22 19:35:52 +01:00
this.shouldDisableBatWings = compound.getBoolean("ShouldDisableWings");
}
}
2019-05-02 09:10:29 +02:00
public void writeToNBT(NBTTagCompound compound, boolean savingToFile) {
compound.setBoolean("BookGotten", this.bookGottenAlready);
2016-11-12 15:09:30 +01:00
compound.setBoolean("DidTutorial", this.didBookTutorial);
2016-11-22 19:35:52 +01:00
compound.setBoolean("HasBatWings", this.hasBatWings);
compound.setInteger("BatWingsFlyTime", this.batWingsFlyTime);
compound.setTag("Bookmarks", this.saveBookmarks());
2017-02-18 00:54:58 +01:00
compound.setTag("Trials", this.saveTrials());
2019-05-02 09:10:29 +02:00
if (!savingToFile) {
compound.setBoolean("ShouldDisableWings", this.shouldDisableBatWings);
}
}
2019-05-02 09:10:29 +02:00
public NBTTagList saveBookmarks() {
NBTTagList bookmarks = new NBTTagList();
2019-05-02 09:10:29 +02:00
for (IBookletPage bookmark : this.bookmarks) {
bookmarks.appendTag(new NBTTagString(bookmark == null ? "" : bookmark.getIdentifier()));
}
return bookmarks;
}
2016-11-22 19:35:52 +01:00
2019-05-02 09:10:29 +02:00
public void loadBookmarks(NBTTagList bookmarks) {
for (int i = 0; i < bookmarks.tagCount(); i++) {
String strg = bookmarks.getStringTagAt(i);
2019-05-02 09:10:29 +02:00
if (strg != null && !strg.isEmpty()) {
IBookletPage page = BookletUtils.getBookletPageById(strg);
this.bookmarks[i] = page;
2019-05-02 09:10:29 +02:00
} else {
this.bookmarks[i] = null;
}
2016-11-22 19:35:52 +01:00
}
}
2017-02-18 00:54:58 +01:00
2019-05-02 09:10:29 +02:00
public NBTTagList saveTrials() {
2017-02-18 00:54:58 +01:00
NBTTagList trials = new NBTTagList();
2019-05-02 09:10:29 +02:00
for (String trial : this.completedTrials) {
2017-02-18 00:54:58 +01:00
trials.appendTag(new NBTTagString(trial));
}
return trials;
}
2019-05-02 09:10:29 +02:00
public void loadTrials(NBTTagList trials) {
2017-02-18 00:54:58 +01:00
this.completedTrials.clear();
2019-05-02 09:10:29 +02:00
for (int i = 0; i < trials.tagCount(); i++) {
2017-02-18 00:54:58 +01:00
String strg = trials.getStringTagAt(i);
2019-05-02 09:10:29 +02:00
if (strg != null && !strg.isEmpty()) {
2017-02-18 00:54:58 +01:00
this.completedTrials.add(strg);
}
}
}
}
}