Eliminate journal storage mechanism

This commit is contained in:
PikaMug 2020-11-25 01:12:38 -05:00
parent 879f37668c
commit a8afe550fc
7 changed files with 140 additions and 113 deletions

View File

@ -43,7 +43,6 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.Crops;
@ -57,6 +56,7 @@ import me.blackvein.quests.events.quest.QuestTakeEvent;
import me.blackvein.quests.events.quester.QuesterPostStartQuestEvent;
import me.blackvein.quests.events.quester.QuesterPreOpenGUIEvent;
import me.blackvein.quests.events.quester.QuesterPreStartQuestEvent;
import me.blackvein.quests.item.QuestJournal;
import me.blackvein.quests.storage.Storage;
import me.blackvein.quests.tasks.StageTimer;
import me.blackvein.quests.util.ConfigUtil;
@ -71,7 +71,6 @@ import net.citizensnpcs.api.npc.NPC;
public class Quester implements Comparable<Quester> {
private final Quests plugin;
public boolean hasJournal = false;
private UUID id;
protected String questIdToTake;
protected int questPoints = 0;
@ -356,11 +355,39 @@ public class Quester implements Comparable<Quester> {
}
return new QuestData(this);
}
public boolean hasJournal() {
return getJournal() != null;
}
public ItemStack getJournal() {
if (getPlayer() == null || !getPlayer().isOnline()) {
return null;
}
for (final ItemStack is : getPlayer().getInventory().getContents()) {
if (ItemUtil.isJournal(is)) {
return is;
}
}
return null;
}
public int getJournalIndex() {
if (getPlayer() == null || !getPlayer().isOnline()) {
return -1;
}
final ItemStack[] arr = getPlayer().getInventory().getContents();
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
if (ItemUtil.isJournal(arr[i])) {
return i;
}
}
}
return -1;
}
public void updateJournal() {
if (!hasJournal) {
return;
}
if (getPlayer() == null) {
return;
}
@ -368,76 +395,10 @@ public class Quester implements Comparable<Quester> {
plugin.getLogger().info("Could not update Quests Journal for " + getPlayer().getName() + " while offline");
return;
}
final Inventory inv = getPlayer().getInventory();
final ItemStack[] arr = inv.getContents();
int index = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
if (ItemUtil.isJournal(arr[i])) {
index = i;
break;
}
}
}
final int index = getJournalIndex();
if (index != -1) {
final String title = Lang.get(getPlayer(), "journalTitle");
final ItemStack stack = new ItemStack(Material.WRITTEN_BOOK, 1);
final ItemMeta meta = stack.getItemMeta();
meta.setDisplayName(ChatColor.LIGHT_PURPLE + title);
final BookMeta book = (BookMeta) meta;
book.setTitle(ChatColor.LIGHT_PURPLE + title);
book.setAuthor(getPlayer().getName());
if (currentQuests.isEmpty()) {
book.addPage(ChatColor.DARK_RED + Lang.get(getPlayer(), "journalNoQuests").replace("<journal>", title));
} else {
int currentLength = 0;
int currentLines = 0;
String page = "";
final List<Quest> sortedList = currentQuests.keySet().stream()
.sorted(Comparator.comparing(Quest::getName))
.collect(Collectors.toList());
for (final Quest quest : sortedList) {
if ((currentLength + quest.getName().length() > 240) || (currentLines
+ ((quest.getName().length() % 19) == 0 ? (quest.getName().length() / 19)
: ((quest.getName().length() / 19) + 1))) > 13) {
book.addPage(page);
page += ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + quest.getName() + "\n";
currentLength = quest.getName().length();
currentLines = (quest.getName().length() % 19) == 0 ? (quest.getName().length() / 19)
: (quest.getName().length() + 1);
} else {
page += ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + quest.getName() + "\n";
currentLength += quest.getName().length();
currentLines += (quest.getName().length() / 19);
}
if (getCurrentObjectives(quest, false) != null) {
for (final String obj : getCurrentObjectives(quest, false)) {
// Length/Line check
if ((currentLength + obj.length() > 240) || (currentLines + ((obj.length() % 19)
== 0 ? (obj.length() / 19) : ((obj.length() / 19) + 1))) > 13) {
book.addPage(page);
page = obj + "\n";
currentLength = obj.length();
currentLines = (obj.length() % 19) == 0 ? (obj.length() / 19) : (obj.length() + 1);
} else {
page += obj + "\n";
currentLength += obj.length();
currentLines += (obj.length() / 19);
}
}
} else {
plugin.getLogger().severe("Quest Journal: objectives were null for " + quest.getName());
}
if (currentLines < 13)
page += "\n";
book.addPage(page);
page = "";
currentLines = 0;
currentLength = 0;
}
}
stack.setItemMeta(book);
inv.setItem(index, stack);
final QuestJournal journal = new QuestJournal(this);
getPlayer().getInventory().setItem(index, journal.toItemStack());
}
}
@ -3193,7 +3154,6 @@ public class Quester implements Comparable<Quester> {
if (representedPlayer == null) {
representedPlayer = getOfflinePlayer();
}
data.set("hasJournal", hasJournal);
data.set("lastKnownName", representedPlayer.getName());
return data;
}

View File

@ -0,0 +1,87 @@
package me.blackvein.quests.item;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import me.blackvein.quests.Quest;
import me.blackvein.quests.Quester;
import me.blackvein.quests.util.Lang;
public class QuestJournal {
final Quester owner;
ItemStack journal = new ItemStack(Material.WRITTEN_BOOK);
public QuestJournal(final Quester owner) {
this.owner = owner;
final BookMeta book = (BookMeta) journal.getItemMeta();
final Player player = owner.getPlayer();
final String title = Lang.get(player, "journalTitle");
book.setDisplayName(ChatColor.LIGHT_PURPLE + title);
book.setTitle(ChatColor.LIGHT_PURPLE + title);
book.setAuthor(player.getName());
if (owner.getCurrentQuests().isEmpty()) {
book.addPage(ChatColor.DARK_RED + Lang.get(player, "journalNoQuests").replace("<journal>", title));
} else {
int currentLength = 0;
int currentLines = 0;
String page = "";
final List<Quest> sortedList = owner.getCurrentQuests().keySet().stream()
.sorted(Comparator.comparing(Quest::getName))
.collect(Collectors.toList());
for (final Quest quest : sortedList) {
if ((currentLength + quest.getName().length() > 240) || (currentLines
+ ((quest.getName().length() % 19) == 0 ? (quest.getName().length() / 19)
: ((quest.getName().length() / 19) + 1))) > 13) {
book.addPage(page);
page += ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + quest.getName() + "\n";
currentLength = quest.getName().length();
currentLines = (quest.getName().length() % 19) == 0 ? (quest.getName().length() / 19)
: (quest.getName().length() + 1);
} else {
page += ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + quest.getName() + "\n";
currentLength += quest.getName().length();
currentLines += (quest.getName().length() / 19);
}
if (owner.getCurrentObjectives(quest, false) != null) {
for (final String obj : owner.getCurrentObjectives(quest, false)) {
// Length/Line check
if ((currentLength + obj.length() > 240) || (currentLines + ((obj.length() % 19)
== 0 ? (obj.length() / 19) : ((obj.length() / 19) + 1))) > 13) {
book.addPage(page);
page = obj + "\n";
currentLength = obj.length();
currentLines = (obj.length() % 19) == 0 ? (obj.length() / 19) : (obj.length() + 1);
} else {
page += obj + "\n";
currentLength += obj.length();
currentLines += (obj.length() / 19);
}
}
}
if (currentLines < 13)
page += "\n";
book.addPage(page);
page = "";
currentLines = 0;
currentLength = 0;
}
}
journal.setItemMeta(book);
}
public Quester getOwner() {
return owner;
}
public ItemStack toItemStack() {
return journal;
}
}

View File

@ -41,7 +41,6 @@ import org.bukkit.conversations.Conversation;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import me.blackvein.quests.Quest;
import me.blackvein.quests.Quester;
@ -53,6 +52,7 @@ import me.blackvein.quests.events.command.QuestsCommandPreQuestsJournalEvent;
import me.blackvein.quests.events.command.QuestsCommandPreQuestsListEvent;
import me.blackvein.quests.events.quest.QuestQuitEvent;
import me.blackvein.quests.interfaces.ReloadCallback;
import me.blackvein.quests.item.QuestJournal;
import me.blackvein.quests.storage.Storage;
import me.blackvein.quests.util.ItemUtil;
import me.blackvein.quests.util.Lang;
@ -691,40 +691,26 @@ public class CmdExecutor implements CommandExecutor {
}
final Inventory inv = player.getInventory();
if (quester.hasJournal) {
final ItemStack[] arr = inv.getContents();
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
if (ItemUtil.isJournal(arr[i])) {
inv.setItem(i, null);
}
}
}
final int index = quester.getJournalIndex();
if (index != -1) {
inv.setItem(index, null);
player.sendMessage(ChatColor.YELLOW + Lang.get(player, "journalPutAway")
.replace("<journal>", Lang.get(player, "journalTitle")));
quester.hasJournal = false;
} else if (player.getItemInHand() == null || player.getItemInHand().getType().equals(Material.AIR)) {
final ItemStack stack = new ItemStack(Material.WRITTEN_BOOK, 1);
final ItemMeta meta = stack.getItemMeta();
meta.setDisplayName(ChatColor.LIGHT_PURPLE + Lang.get("journalTitle"));
stack.setItemMeta(meta);
player.setItemInHand(stack);
final QuestJournal journal = new QuestJournal(quester);
player.setItemInHand(journal.toItemStack());
player.sendMessage(ChatColor.YELLOW + Lang.get(player, "journalTaken")
.replace("<journal>", Lang.get(player, "journalTitle")));
quester.hasJournal = true;
quester.updateJournal();
//quester.updateJournal();
} else if (inv.firstEmpty() != -1) {
final ItemStack[] arr = inv.getContents();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == null) {
final ItemStack stack = new ItemStack(Material.WRITTEN_BOOK, 1);
final ItemMeta meta = stack.getItemMeta();
meta.setDisplayName(ChatColor.LIGHT_PURPLE + Lang.get("journalTitle"));
stack.setItemMeta(meta);
inv.setItem(i, stack);
player.sendMessage(ChatColor.YELLOW + Lang.get(player, "journalTaken"));
quester.hasJournal = true;
quester.updateJournal();
final QuestJournal journal = new QuestJournal(quester);
inv.setItem(i, journal.toItemStack());
player.sendMessage(ChatColor.YELLOW + Lang.get(player, "journalTaken")
.replace("<journal>", Lang.get(player, "journalTitle")));
//quester.updateJournal();
break;
}
}

View File

@ -685,9 +685,7 @@ public class PlayerListener implements Listener {
}
}
if (found != null) {
final Quester quester = plugin.getQuester(target.getUniqueId());
evt.getDrops().remove(found);
quester.hasJournal = false;
}
}
@ -815,7 +813,7 @@ public class PlayerListener implements Listener {
@Override
public void run() {
if (quester.hasJournal) {
if (quester.hasJournal()) {
quester.updateJournal();
}
if (quester.canUseCompass()) {

View File

@ -112,7 +112,6 @@ public class SeparatedYamlStorage implements StorageImplementation {
int questPoints = quester.getQuestPoints();
questPoints = data.getInt("quest-points");
quester.setQuestPoints(questPoints);
quester.hasJournal = data.getBoolean("hasJournal");
if (data.isList("completed-Quests")) {
for (final String s : data.getStringList("completed-Quests")) {
for (final Quest q : plugin.getQuests()) {

View File

@ -32,11 +32,11 @@ import me.blackvein.quests.storage.implementation.StorageImplementation;
import me.blackvein.quests.storage.implementation.sql.connection.ConnectionFactory;
public class SqlStorage implements StorageImplementation {
private static final String PLAYER_SELECT = "SELECT lastknownname, hasjournal, questpoints FROM '{prefix}players' WHERE uuid=?";
private static final String PLAYER_SELECT = "SELECT lastknownname, questpoints FROM '{prefix}players' WHERE uuid=?";
private static final String PLAYER_SELECT_USERNAME = "SELECT lastknownname FROM '{prefix}players' WHERE uuid=? LIMIT 1";
private static final String PLAYER_UPDATE_USERNAME = "UPDATE '{prefix}players' SET lastknownname=? WHERE uuid=?";
private static final String PLAYER_INSERT = "INSERT INTO '{prefix}players' (uuid, lastknownname, hasjournal, questpoints) "
+ "VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE uuid=uuid, lastknownname=VALUES(lastknownname), hasjournal=VALUES(hasjournal), questpoints=VALUES(questpoints)";
private static final String PLAYER_INSERT = "INSERT INTO '{prefix}players' (uuid, lastknownname, questpoints) "
+ "VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE uuid=uuid, lastknownname=VALUES(lastknownname), questpoints=VALUES(questpoints)";
private static final String PLAYER_DELETE = "DELETE FROM '{prefix}players' WHERE uuid=?";
private static final String PLAYER_CURRENT_QUESTS_SELECT_BY_UUID = "SELECT questid, stageNum FROM '{prefix}player_currentquests' WHERE uuid=?";
@ -94,7 +94,6 @@ public class SqlStorage implements StorageImplementation {
queries[0] = "CREATE TABLE IF NOT EXISTS `" + statementProcessor.apply("{prefix}players")
+ "` (`uuid` VARCHAR(36) NOT NULL, "
+ "`lastknownname` VARCHAR(16) NOT NULL, "
+ "`hasjournal` BOOL NOT NULL, "
+ "`questpoints` BIGINT NOT NULL, "
+ "PRIMARY KEY (`uuid`)"
+ ") DEFAULT CHARSET = utf8mb4";
@ -160,7 +159,6 @@ public class SqlStorage implements StorageImplementation {
ps.setString(1, uniqueId.toString());
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
quester.hasJournal = rs.getBoolean("hasjournal");
quester.setQuestPoints(rs.getInt("questpoints"));
}
}
@ -200,8 +198,7 @@ public class SqlStorage implements StorageImplementation {
try (PreparedStatement ps = c.prepareStatement(statementProcessor.apply(PLAYER_INSERT))) {
ps.setString(1, uniqueId.toString());
ps.setString(2, lastknownname);
ps.setBoolean(3, quester.hasJournal);
ps.setInt(4, quester.getQuestPoints());
ps.setInt(3, quester.getQuestPoints());
ps.execute();
}
}

View File

@ -590,7 +590,7 @@ public class ItemUtil {
/**
* Checks whether an ItemStack is a Quest Journal based on book title
*
* @param is IemsStack to check
* @param is ItemStack to check
* @return true if display name equals colored journal title
*/
public static boolean isJournal(final ItemStack is) {