2015-09-27 15:31:43 +02:00
|
|
|
/*
|
2016-08-02 13:08:22 +02:00
|
|
|
* This file ("PlayerData.java") is part of the Actually Additions mod for Minecraft.
|
2015-09-27 15:31:43 +02:00
|
|
|
* 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
|
2015-09-27 15:31:43 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2015-09-27 15:31:43 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-04 13:51:06 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.data;
|
2015-09-27 15:31:43 +02:00
|
|
|
|
2016-11-12 13:54:49 +01:00
|
|
|
import de.ellpeck.actuallyadditions.api.booklet.IBookletPage;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.booklet.gui.GuiBooklet;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.booklet.misc.BookletUtils;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
|
|
|
import net.minecraft.nbt.ListNBT;
|
2021-02-27 17:35:21 +01:00
|
|
|
import net.minecraft.nbt.StringNBT;
|
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
2021-02-26 22:15:48 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2015-09-27 15:31:43 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public final class PlayerData {
|
2015-09-27 15:31:43 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public static PlayerSave getDataFromPlayer(PlayerEntity player) {
|
2021-08-22 17:09:06 +02:00
|
|
|
WorldData worldData = WorldData.get(player.getCommandSenderWorld());
|
2017-03-08 20:06:55 +01:00
|
|
|
ConcurrentHashMap<UUID, PlayerSave> data = worldData.playerSaveData;
|
2021-08-22 17:09:06 +02:00
|
|
|
UUID id = player.getUUID();
|
2016-12-28 14:55:03 +01:00
|
|
|
|
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);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (save != null && save.id != null && save.id.equals(id)) {
|
|
|
|
return save;
|
|
|
|
}
|
2015-10-03 10:16:18 +02:00
|
|
|
}
|
2015-10-22 22:18:33 +02:00
|
|
|
|
|
|
|
//Add Data if none is existant
|
2016-11-22 18:26:29 +01:00
|
|
|
PlayerSave save = new PlayerSave(id);
|
2017-03-08 20:06:55 +01:00
|
|
|
data.put(id, save);
|
2021-08-22 17:09:06 +02:00
|
|
|
worldData.setDirty();
|
2016-11-22 18:26:29 +01:00
|
|
|
return save;
|
2015-10-03 10:16:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static class PlayerSave {
|
2015-09-27 15:31:43 +02:00
|
|
|
|
2016-11-11 21:41:07 +01:00
|
|
|
public UUID id;
|
2015-09-27 15:31:43 +02:00
|
|
|
|
2016-11-11 21:41:07 +01:00
|
|
|
public boolean bookGottenAlready;
|
2016-11-12 15:09:30 +01:00
|
|
|
public boolean didBookTutorial;
|
2016-11-22 18:26:29 +01:00
|
|
|
public boolean hasBatWings;
|
2016-11-22 19:35:52 +01:00
|
|
|
public boolean shouldDisableBatWings;
|
2016-11-22 18:26:29 +01:00
|
|
|
public int batWingsFlyTime;
|
2015-09-27 15:31:43 +02:00
|
|
|
|
2016-11-12 13:54:49 +01:00
|
|
|
public IBookletPage[] bookmarks = new IBookletPage[12];
|
2019-02-27 19:53:05 +01:00
|
|
|
public List<String> completedTrials = new ArrayList<>();
|
2016-11-12 13:54:49 +01:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2016-11-12 13:54:49 +01:00
|
|
|
public GuiBooklet lastOpenBooklet;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public PlayerSave(UUID id) {
|
2016-11-11 21:41:07 +01:00
|
|
|
this.id = id;
|
2015-10-23 16:54:33 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readFromNBT(CompoundNBT compound, boolean savingToFile) {
|
2016-11-11 21:41:07 +01:00
|
|
|
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
|
|
|
|
2016-11-22 18:26:29 +01:00
|
|
|
this.hasBatWings = compound.getBoolean("HasBatWings");
|
2021-02-27 16:33:00 +01:00
|
|
|
this.batWingsFlyTime = compound.getInt("BatWingsFlyTime");
|
2016-11-12 13:54:49 +01:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
ListNBT bookmarks = compound.getList("Bookmarks", 8);
|
2017-02-13 15:23:28 +01:00
|
|
|
this.loadBookmarks(bookmarks);
|
2016-11-22 19:35:52 +01:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
ListNBT trials = compound.getList("Trials", 8);
|
2017-02-18 00:54:58 +01:00
|
|
|
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");
|
|
|
|
}
|
2016-11-11 21:41:07 +01:00
|
|
|
}
|
2016-07-14 20:25:12 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeToNBT(CompoundNBT compound, boolean savingToFile) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putBoolean("BookGotten", this.bookGottenAlready);
|
|
|
|
compound.putBoolean("DidTutorial", this.didBookTutorial);
|
2016-11-22 19:35:52 +01:00
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putBoolean("HasBatWings", this.hasBatWings);
|
|
|
|
compound.putInt("BatWingsFlyTime", this.batWingsFlyTime);
|
2016-11-12 13:54:49 +01:00
|
|
|
|
2021-02-27 17:35:21 +01:00
|
|
|
compound.put("Bookmarks", this.saveBookmarks());
|
|
|
|
compound.put("Trials", this.saveTrials());
|
2017-02-13 15:23:28 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!savingToFile) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putBoolean("ShouldDisableWings", this.shouldDisableBatWings);
|
2017-02-13 15:23:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public ListNBT saveBookmarks() {
|
|
|
|
ListNBT bookmarks = new ListNBT();
|
2019-05-02 09:10:29 +02:00
|
|
|
for (IBookletPage bookmark : this.bookmarks) {
|
2021-02-27 17:35:21 +01:00
|
|
|
bookmarks.add(StringNBT.valueOf(bookmark == null
|
2021-02-26 22:15:48 +01:00
|
|
|
? ""
|
|
|
|
: bookmark.getIdentifier()));
|
2016-11-12 13:54:49 +01:00
|
|
|
}
|
2017-02-13 15:23:28 +01:00
|
|
|
return bookmarks;
|
|
|
|
}
|
2016-11-22 19:35:52 +01:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public void loadBookmarks(ListNBT bookmarks) {
|
|
|
|
for (int i = 0; i < bookmarks.size(); i++) {
|
2021-02-27 17:35:21 +01:00
|
|
|
String strg = bookmarks.getString(i);
|
|
|
|
if (!strg.isEmpty()) {
|
2017-02-13 15:23:28 +01:00
|
|
|
IBookletPage page = BookletUtils.getBookletPageById(strg);
|
|
|
|
this.bookmarks[i] = page;
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2017-02-13 15:23:28 +01:00
|
|
|
this.bookmarks[i] = null;
|
|
|
|
}
|
2016-11-22 19:35:52 +01:00
|
|
|
}
|
2015-09-27 15:31:43 +02:00
|
|
|
}
|
2017-02-18 00:54:58 +01:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public ListNBT saveTrials() {
|
|
|
|
ListNBT trials = new ListNBT();
|
2019-05-02 09:10:29 +02:00
|
|
|
for (String trial : this.completedTrials) {
|
2021-02-27 17:35:21 +01:00
|
|
|
trials.add(StringNBT.valueOf(trial));
|
2017-02-18 00:54:58 +01:00
|
|
|
}
|
|
|
|
return trials;
|
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public void loadTrials(ListNBT trials) {
|
2017-02-18 00:54:58 +01:00
|
|
|
this.completedTrials.clear();
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
for (int i = 0; i < trials.size(); i++) {
|
2021-02-27 17:35:21 +01:00
|
|
|
String strg = trials.getString(i);
|
|
|
|
if (!strg.isEmpty()) {
|
2017-02-18 00:54:58 +01:00
|
|
|
this.completedTrials.add(strg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-27 15:31:43 +02:00
|
|
|
}
|
2015-10-22 22:18:33 +02:00
|
|
|
|
2015-09-27 15:31:43 +02:00
|
|
|
}
|