unify lore management

This commit is contained in:
hyperdefined
2022-12-07 14:56:57 -05:00
parent 2e0ee18594
commit 2c971da1b9
10 changed files with 317 additions and 312 deletions

View File

@@ -21,6 +21,7 @@ import lol.hyper.githubreleaseapi.GitHubRelease;
import lol.hyper.githubreleaseapi.GitHubReleaseAPI;
import lol.hyper.toolstats.commands.CommandToolStats;
import lol.hyper.toolstats.events.*;
import lol.hyper.toolstats.tools.ItemLore;
import lol.hyper.toolstats.tools.NumberFormat;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bstats.bukkit.Metrics;
@@ -69,6 +70,7 @@ public final class ToolStats extends JavaPlugin {
public SheepShear sheepShear;
public VillagerTrade villagerTrade;
public CommandToolStats commandToolStats;
public ItemLore itemLore;
public final Logger logger = this.getLogger();
public final File configFile = new File(this.getDataFolder(), "config.yml");
@@ -99,6 +101,7 @@ public final class ToolStats extends JavaPlugin {
sheepShear = new SheepShear(this);
villagerTrade = new VillagerTrade(this);
commandToolStats = new CommandToolStats(this);
itemLore = new ItemLore(this);
Bukkit.getServer().getPluginManager().registerEvents(blocksMined, this);
Bukkit.getServer().getPluginManager().registerEvents(chunkPopulate, this);

View File

@@ -19,9 +19,7 @@ package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.ItemChecker;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@@ -33,7 +31,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.List;
public class BlocksMined implements Listener {
@@ -60,8 +57,8 @@ public class BlocksMined implements Listener {
if (!ItemChecker.isMineTool(heldItem.getType())) {
return;
}
// if it's an item we want, update the stats
Bukkit.getScheduler().runTaskLater(toolStats, () -> updateBlocksMined(heldItem), 1);
// update the blocks mined
updateBlocksMined(heldItem);
}
private void updateBlocksMined(ItemStack playerTool) {
@@ -86,40 +83,17 @@ public class BlocksMined implements Listener {
blocksMined++;
container.set(toolStats.genericMined, PersistentDataType.INTEGER, blocksMined);
String configLore = toolStats.getLoreFromConfig("blocks-mined", false);
String configLoreRaw = toolStats.getLoreFromConfig("blocks-mined", true);
String blocksMinedFormatted = toolStats.numberFormat.formatInt(blocksMined);
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{blocks}", blocksMinedFormatted, "blocks-mined");
if (configLore == null || configLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages.blocks-mined!");
// if the list returned null, don't add it
if (newLore == null) {
return;
}
List<String> lore;
String newLine = configLoreRaw.replace("{blocks}", toolStats.numberFormat.formatInt(blocksMined));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;
// we do a for loop like this, we can keep track of index
// this doesn't mess the lore up of existing items
for (int x = 0; x < lore.size(); x++) {
if (lore.get(x).contains(configLore)) {
hasLore = true;
lore.set(x, newLine);
break;
}
}
// if the item has lore but doesn't have the tag, add it
if (!hasLore) {
lore.add(newLine);
}
} else {
// if the item has no lore, create a new list and add the string
lore = new ArrayList<>();
lore.add(newLine);
}
// do we add the lore based on the config?
if (toolStats.checkConfig(playerTool, "blocks-mined")) {
meta.setLore(lore);
meta.setLore(newLore);
}
playerTool.setItemMeta(meta);
}

View File

@@ -73,39 +73,34 @@ public class EntityDamage implements Listener {
return;
}
PlayerInventory attackingPlayerInventory = attackingPlayer.getInventory();
int heldItemSlot = attackingPlayerInventory.getHeldItemSlot();
ItemStack heldItem = attackingPlayerInventory.getItem(attackingPlayer.getInventory().getHeldItemSlot());
// a player killed something with their fist
if (heldItem == null || heldItem.getType() == Material.AIR) {
return;
}
ItemStack heldItem = attackingPlayerInventory.getItemInMainHand();
// only check certain items
if (!ItemChecker.isMeleeWeapon(heldItem.getType())) {
return;
}
// a player is killing another player
if (mobBeingAttacked instanceof Player) {
Bukkit.getScheduler().runTaskLater(toolStats, () -> attackingPlayerInventory.setItem(heldItemSlot, updatePlayerKills(heldItem)), 1);
updatePlayerKills(heldItem);
return;
}
// player is killing regular mob
Bukkit.getScheduler().runTaskLater(toolStats, () -> attackingPlayerInventory.setItem(heldItemSlot, updateMobKills(heldItem)), 1);
updateMobKills(heldItem);
trackedMobs.add(mobBeingAttacked.getUniqueId());
}
// trident is being thrown at something
if (event.getDamager() instanceof Trident) {
Trident trident = (Trident) event.getDamager();
ItemStack clone;
ItemStack newTrident;
// trident is killing player
if (mobBeingAttacked instanceof Player) {
clone = updatePlayerKills(trident.getItem());
newTrident = tridentPlayerKills(trident.getItem());
} else {
clone = updateMobKills(trident.getItem());
// trident is killing a mob
newTrident = tridentMobKills(trident.getItem());
}
if (clone == null) {
return;
if (newTrident != null) {
trident.setItem(newTrident);
}
Bukkit.getScheduler().runTaskLater(toolStats, () -> trident.setItem(clone), 1);
}
// arrow is being shot
if (event.getDamager() instanceof Arrow) {
@@ -116,20 +111,35 @@ public class EntityDamage implements Listener {
if (shootingPlayer.getGameMode() == GameMode.CREATIVE || shootingPlayer.getGameMode() == GameMode.SPECTATOR) {
return;
}
PlayerInventory shootingPlayerInventory = shootingPlayer.getInventory();
int heldItemSlot = shootingPlayerInventory.getHeldItemSlot();
ItemStack heldItem = shootingPlayerInventory.getItem(heldItemSlot);
if (heldItem == null) {
PlayerInventory inventory = shootingPlayer.getInventory();
boolean isMainHand = inventory.getItemInMainHand().getType() == Material.BOW || inventory.getItemInMainHand().getType() == Material.CROSSBOW;
boolean isOffHand = inventory.getItemInOffHand().getType() == Material.BOW || inventory.getItemInMainHand().getType() == Material.CROSSBOW;
ItemStack heldBow = null;
if (isMainHand) {
heldBow = inventory.getItemInMainHand();
toolStats.logger.info("main");
}
if (isOffHand) {
heldBow = inventory.getItemInOffHand();
toolStats.logger.info("offhand");
}
// if the player is hold a bow in both hands
// default to main hand since that takes priority
if (isMainHand && isOffHand) {
heldBow = inventory.getItemInMainHand();
toolStats.logger.info("both");
}
// player swapped
if (heldBow == null) {
return;
}
// if the player is holding the bow/crossbow
// if they switch then oh well
if (heldItem.getType() == Material.BOW || heldItem.getType() == Material.CROSSBOW) {
if (mobBeingAttacked instanceof Player) {
Bukkit.getScheduler().runTaskLater(toolStats, () -> shootingPlayerInventory.setItem(heldItemSlot, updatePlayerKills(heldItem)), 1);
} else {
Bukkit.getScheduler().runTaskLater(toolStats, () -> shootingPlayerInventory.setItem(heldItemSlot, updateMobKills(heldItem)), 1);
}
if (mobBeingAttacked instanceof Player) {
updatePlayerKills(heldBow);
} else {
updateMobKills(heldBow);
}
}
}
@@ -215,14 +225,12 @@ public class EntityDamage implements Listener {
* Updates a weapon's player kills.
*
* @param itemStack The item to update.
* @return A copy of the item.
*/
private ItemStack updatePlayerKills(ItemStack itemStack) {
ItemStack finalItem = itemStack.clone();
ItemMeta meta = finalItem.getItemMeta();
private void updatePlayerKills(ItemStack itemStack) {
ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
toolStats.logger.warning(itemStack + " does NOT have any meta! Unable to update stats.");
return null;
return;
}
Integer playerKills = 0;
PersistentDataContainer container = meta.getPersistentDataContainer();
@@ -238,57 +246,31 @@ public class EntityDamage implements Listener {
playerKills++;
container.set(toolStats.swordPlayerKills, PersistentDataType.INTEGER, playerKills);
String playerKillsLore = toolStats.getLoreFromConfig("kills.player", false);
String playerKillsLoreRaw = toolStats.getLoreFromConfig("kills.player", true);
String playerKillsFormatted = toolStats.numberFormat.formatInt(playerKills);
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{kills}", playerKillsFormatted, "kills.player");
if (playerKillsLore == null || playerKillsLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages.kills.player!");
return null;
// if the list returned null, don't add it
if (newLore == null) {
return;
}
List<String> lore;
String newLine = playerKillsLoreRaw.replace("{kills}", toolStats.numberFormat.formatInt(playerKills));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;
// we do a for loop like this, we can keep track of index
// this doesn't mess the lore up of existing items
for (int x = 0; x < lore.size(); x++) {
if (lore.get(x).contains(playerKillsLore)) {
hasLore = true;
lore.set(x, newLine);
break;
}
}
// if the item has lore but doesn't have the tag, add it
if (!hasLore) {
lore.add(newLine);
}
} else {
// if the item has no lore, create a new list and add the string
lore = new ArrayList<>();
lore.add(newLine);
}
// do we add the lore based on the config?
if (toolStats.checkConfig(itemStack, "player-kills")) {
meta.setLore(lore);
meta.setLore(newLore);
}
finalItem.setItemMeta(meta);
return finalItem;
itemStack.setItemMeta(meta);
}
/**
* Updates a weapon's mob kills.
*
* @param itemStack The item to update.
* @return A copy of the item.
*/
private ItemStack updateMobKills(ItemStack itemStack) {
ItemStack finalItem = itemStack.clone();
ItemMeta meta = finalItem.getItemMeta();
private void updateMobKills(ItemStack itemStack) {
ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
toolStats.logger.warning(itemStack + " does NOT have any meta! Unable to update stats.");
return null;
return;
}
Integer mobKills = 0;
PersistentDataContainer container = meta.getPersistentDataContainer();
@@ -304,43 +286,19 @@ public class EntityDamage implements Listener {
mobKills++;
container.set(toolStats.swordMobKills, PersistentDataType.INTEGER, mobKills);
String mobKillsLore = toolStats.getLoreFromConfig("kills.mob", false);
String mobKillsLoreRaw = toolStats.getLoreFromConfig("kills.mob", true);
String mobKillsFormatted = toolStats.numberFormat.formatInt(mobKills);
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{kills}", mobKillsFormatted, "kills.mob");
if (mobKillsLore == null || mobKillsLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages.kills.mob!");
return null;
// if the list returned null, don't add it
if (newLore == null) {
return;
}
List<String> lore;
String newLine = mobKillsLoreRaw.replace("{kills}", toolStats.numberFormat.formatInt(mobKills));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;
// we do a for loop like this, we can keep track of index
// this doesn't mess the lore up of existing items
for (int x = 0; x < lore.size(); x++) {
if (lore.get(x).contains(mobKillsLore)) {
hasLore = true;
lore.set(x, newLine);
break;
}
}
// if the item has lore but doesn't have the tag, add it
if (!hasLore) {
lore.add(newLine);
}
} else {
// if the item has no lore, create a new list and add the string
lore = new ArrayList<>();
lore.add(newLine);
}
// do we add the lore based on the config?
if (toolStats.checkConfig(itemStack, "mob-kills")) {
meta.setLore(lore);
meta.setLore(newLore);
}
finalItem.setItemMeta(meta);
return finalItem;
itemStack.setItemMeta(meta);
}
/**
@@ -369,40 +327,101 @@ public class EntityDamage implements Listener {
damageTaken = damageTaken + damage;
container.set(toolStats.armorDamage, PersistentDataType.DOUBLE, damageTaken);
String damageTakenLore = toolStats.getLoreFromConfig("damage-taken", false);
String damageTakenLoreRaw = toolStats.getLoreFromConfig("damage-taken", true);
String damageTakenFormatted = toolStats.numberFormat.formatDouble(damageTaken);
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{damage}", damageTakenFormatted, "damage-taken");
if (damageTakenLore == null || damageTakenLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages.damage-taken!");
// if the list returned null, don't add it
if (newLore == null) {
return;
}
List<String> lore;
String newLine = damageTakenLoreRaw.replace("{damage}", toolStats.numberFormat.formatDouble(damageTaken));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;
// we do a for loop like this, we can keep track of index
// this doesn't mess the lore up of existing items
for (int x = 0; x < lore.size(); x++) {
if (lore.get(x).contains(damageTakenLore)) {
hasLore = true;
lore.set(x, newLine);
break;
}
}
// if the item has lore but doesn't have the tag, add it
if (!hasLore) {
lore.add(newLine);
}
} else {
// if the item has no lore, create a new list and add the string
lore = new ArrayList<>();
lore.add(newLine);
}
if (toolStats.config.getBoolean("enabled.armor-damage")) {
meta.setLore(lore);
meta.setLore(newLore);
}
itemStack.setItemMeta(meta);
}
/**
* Updates a trident's mob kills.
*
* @param trident The item to update.
*/
private ItemStack tridentMobKills(ItemStack trident) {
ItemStack newTrident = trident.clone();
ItemMeta meta = newTrident.getItemMeta();
if (meta == null) {
toolStats.logger.warning(newTrident + " does NOT have any meta! Unable to update stats.");
return null;
}
Integer mobKills = 0;
PersistentDataContainer container = meta.getPersistentDataContainer();
if (container.has(toolStats.swordMobKills, PersistentDataType.INTEGER)) {
mobKills = container.get(toolStats.swordMobKills, PersistentDataType.INTEGER);
}
if (mobKills == null) {
mobKills = 0;
toolStats.logger.warning(newTrident + " does not have valid mob-kills set! Resting to zero. This should NEVER happen.");
}
mobKills++;
container.set(toolStats.swordMobKills, PersistentDataType.INTEGER, mobKills);
String mobKillsFormatted = toolStats.numberFormat.formatInt(mobKills);
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{kills}", mobKillsFormatted, "kills.mob");
// if the list returned null, don't add it
if (newLore == null) {
return null;
}
// do we add the lore based on the config?
if (toolStats.checkConfig(newTrident, "mob-kills")) {
meta.setLore(newLore);
}
newTrident.setItemMeta(meta);
return newTrident;
}
/**
* Updates a trident's player kills.
*
* @param trident The item to update.
*/
private ItemStack tridentPlayerKills(ItemStack trident) {
ItemStack newTrident = trident.clone();
ItemMeta meta = newTrident.getItemMeta();
if (meta == null) {
toolStats.logger.warning(newTrident + " does NOT have any meta! Unable to update stats.");
return null;
}
Integer playerKills = 0;
PersistentDataContainer container = meta.getPersistentDataContainer();
if (container.has(toolStats.swordPlayerKills, PersistentDataType.INTEGER)) {
playerKills = container.get(toolStats.swordPlayerKills, PersistentDataType.INTEGER);
}
if (playerKills == null) {
playerKills = 0;
toolStats.logger.warning(newTrident + " does not have valid player-kills set! Resting to zero. This should NEVER happen.");
}
playerKills++;
container.set(toolStats.swordPlayerKills, PersistentDataType.INTEGER, playerKills);
String playerKillsFormatted = toolStats.numberFormat.formatInt(playerKills);
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{kills}", playerKillsFormatted, "kills.player");
// if the list returned null, don't add it
if (newLore == null) {
return null;
}
// do we add the lore based on the config?
if (toolStats.checkConfig(newTrident, "player-kills")) {
meta.setLore(newLore);
}
newTrident.setItemMeta(meta);
return newTrident;
}
}

View File

@@ -74,7 +74,7 @@ public class EntityDeath implements Listener {
* Adds "drop by" tag to item.
*
* @param oldItem The item to add lore to.
* @param mob The mob or player name.
* @param mob The mob or player name.
*/
private ItemStack addLore(ItemStack oldItem, String mob) {
ItemStack newItem = oldItem.clone();
@@ -82,35 +82,16 @@ public class EntityDeath implements Listener {
if (meta == null) {
return null;
}
boolean hasTag = false;
String droppedByLore = toolStats.getLoreFromConfig("dropped-by", false);
String droppedByLoreRaw = toolStats.getLoreFromConfig("dropped-by", true);
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{name}", mob, "dropped-by");
if (droppedByLore == null || droppedByLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages.dropped-by!");
// if the list returned null, don't add it
if (newLore == null) {
return null;
}
List<String> lore;
if (meta.hasLore()) {
lore = meta.getLore();
for (int x = 0; x < lore.size(); x++) {
if (lore.get(x).contains(droppedByLore)) {
// replace existing tag
lore.set(x, droppedByLoreRaw.replace("{name}", mob));
hasTag = true;
}
}
} else {
// if the item has no lore, create a new list and add the string
lore = new ArrayList<>();
}
if (!hasTag) {
lore.add(droppedByLoreRaw.replace("{name}", mob));
}
if (toolStats.config.getBoolean("enabled.dropped-by")) {
meta.setLore(lore);
meta.setLore(newLore);
}
newItem.setItemMeta(meta);
return newItem;

View File

@@ -38,7 +38,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -145,25 +144,12 @@ public class GenerateLoot implements Listener {
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
String foundByLoreRaw = toolStats.getLoreFromConfig("looted.found-by", true);
String foundOnLoreRaw = toolStats.getLoreFromConfig("looted.found-on", true);
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
List<String> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate, "LOOTED");
if (foundByLoreRaw == null || foundOnLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages.looted!");
return null;
}
List<String> lore;
if (meta.hasLore()) {
lore = meta.getLore();
} else {
lore = new ArrayList<>();
}
if (toolStats.checkConfig(newItem, "looted-tag")) {
lore.add(foundOnLoreRaw.replace("{date}", toolStats.numberFormat.formatDate(finalDate)));
lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
meta.setLore(newLore);
}
meta.setLore(lore);
newItem.setItemMeta(meta);
return newItem;
}

View File

@@ -31,7 +31,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -94,25 +93,12 @@ public class PickupItem implements Listener {
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
container.remove(toolStats.newElytra);
String foundByLoreRaw = toolStats.getLoreFromConfig("looted.found-by", true);
String foundOnLoreRaw = toolStats.getLoreFromConfig("looted.found-on", true);
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
List<String> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate, "LOOTED");
if (foundByLoreRaw == null || foundOnLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages.looted!");
return null;
}
List<String> lore;
if (meta.hasLore()) {
lore = meta.getLore();
} else {
lore = new ArrayList<>();
}
if (toolStats.config.getBoolean("enabled.elytra-tag")) {
lore.add(foundOnLoreRaw.replace("{date}", toolStats.numberFormat.formatDate(finalDate)));
lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
meta.setLore(newLore);
}
meta.setLore(lore);
finalItem.setItemMeta(meta);
return finalItem;
}

View File

@@ -20,7 +20,6 @@ package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.ItemChecker;
import lol.hyper.toolstats.tools.UUIDDataType;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Item;
@@ -35,7 +34,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -127,39 +125,16 @@ public class PlayerFish implements Listener {
fishCaught++;
container.set(toolStats.fishingRodCaught, PersistentDataType.INTEGER, fishCaught);
String fishCaughtLore = toolStats.getLoreFromConfig("fished.fish-caught", false);
String fishCaughtLoreRaw = toolStats.getLoreFromConfig("fished.fish-caught", true);
String fishCaughtFormatted = toolStats.numberFormat.formatInt(fishCaught);
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{fish}", fishCaughtFormatted, "fished.fish-caught");
if (fishCaughtLore == null || fishCaughtLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages.fish-caught!");
// if the list returned null, don't add it
if (newLore == null) {
return;
}
List<String> lore;
String newLine = fishCaughtLoreRaw.replace("{fish}", toolStats.numberFormat.formatInt(fishCaught));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;
// we do a for loop like this, we can keep track of index
// this doesn't mess the lore up of existing items
for (int x = 0; x < lore.size(); x++) {
if (lore.get(x).contains(fishCaughtLore)) {
hasLore = true;
lore.set(x, newLine);
break;
}
}
// if the item has lore but doesn't have the tag, add it
if (!hasLore) {
lore.add(newLine);
}
} else {
// if the item has no lore, create a new list and add the string
lore = new ArrayList<>();
lore.add(newLine);
}
if (toolStats.config.getBoolean("enabled.fish-caught")) {
meta.setLore(lore);
meta.setLore(newLore);
}
fishingRod.setItemMeta(meta);
}
@@ -188,25 +163,11 @@ public class PlayerFish implements Listener {
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
String caughtByLoreRaw = toolStats.getLoreFromConfig("fished.caught-by", true);
String caughtOnLoreRaw = toolStats.getLoreFromConfig("fished.caught-on", true);
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
List<String> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate, "FISHED");
if (caughtByLoreRaw == null || caughtOnLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages.fished!");
return null;
}
List<String> lore;
if (meta.hasLore()) {
lore = meta.getLore();
assert lore != null;
} else {
lore = new ArrayList<>();
}
if (toolStats.checkConfig(newItem, "fished-tag")) {
lore.add(caughtOnLoreRaw.replace("{date}", toolStats.numberFormat.formatDate(finalDate)));
lore.add(caughtByLoreRaw.replace("{player}", owner.getName()));
meta.setLore(lore);
meta.setLore(newLore);
}
newItem.setItemMeta(meta);
return newItem;

View File

@@ -18,7 +18,6 @@
package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
@@ -34,7 +33,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.List;
public class SheepShear implements Listener {
@@ -122,39 +120,15 @@ public class SheepShear implements Listener {
sheepSheared++;
container.set(toolStats.shearsSheared, PersistentDataType.INTEGER, sheepSheared);
String sheepShearedLore = toolStats.getLoreFromConfig("sheep-sheared", false);
String sheepShearedLoreRaw = toolStats.getLoreFromConfig("sheep-sheared", true);
String sheepShearedFormatted = toolStats.numberFormat.formatInt(sheepSheared);
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{sheep}", sheepShearedFormatted, "sheep-sheared");
if (sheepShearedLore == null || sheepShearedLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages.sheep-sheared!");
// if the list returned null, don't add it
if (newLore == null) {
return;
}
List<String> lore;
String newLine = sheepShearedLoreRaw.replace("{sheep}", toolStats.numberFormat.formatInt(sheepSheared));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;
// we do a for loop like this, we can keep track of index
// this doesn't mess the lore up of existing items
for (int x = 0; x < lore.size(); x++) {
if (lore.get(x).contains(sheepShearedLore)) {
hasLore = true;
lore.set(x, newLine);
break;
}
}
// if the item has lore but doesn't have the tag, add it
if (!hasLore) {
lore.add(newLine);
}
} else {
// if the item has no lore, create a new list and add the string
lore = new ArrayList<>();
lore.add(newLine);
}
if (toolStats.config.getBoolean("enabled.sheep-sheared")) {
meta.setLore(lore);
meta.setLore(newLore);
}
newShears.setItemMeta(meta);
}

View File

@@ -36,7 +36,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -110,24 +109,11 @@ public class VillagerTrade implements Listener {
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
String tradedByLoreRaw = toolStats.getLoreFromConfig("traded.traded-by", true);
String tradedOnLoreRaw = toolStats.getLoreFromConfig("traded.traded-on", true);
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
List<String> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate, "TRADED");
if (tradedByLoreRaw == null || tradedOnLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages.traded!");
return null;
}
List<String> lore;
if (meta.hasLore()) {
lore = meta.getLore();
} else {
lore = new ArrayList<>();
}
if (toolStats.checkConfig(newItem, "traded-tag")) {
lore.add(tradedOnLoreRaw.replace("{date}", toolStats.numberFormat.formatDate(finalDate)));
lore.add(tradedByLoreRaw.replace("{player}", owner.getName()));
meta.setLore(lore);
meta.setLore(newLore);
}
newItem.setItemMeta(meta);
return newItem;

View File

@@ -0,0 +1,135 @@
/*
* This file is part of ToolStats.
*
* ToolStats is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ToolStats is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ToolStats. If not, see <https://www.gnu.org/licenses/>.
*/
package lol.hyper.toolstats.tools;
import lol.hyper.toolstats.ToolStats;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class ItemLore {
private final ToolStats toolStats;
public ItemLore(ToolStats toolStats) {
this.toolStats = toolStats;
}
/**
* Adds new lore to an item.
*
* @param placeholder The placeholder from the config. ex: {kills}
* @param placeholderValue The value to replace the placeholder.
* @param configLorePath The path to the config message.
* @return The item's new lore.
*/
public List<String> addItemLore(ItemMeta itemMeta, String placeholder, String placeholderValue, String configLorePath) {
String configLore = toolStats.getLoreFromConfig(configLorePath, false);
String configLoreRaw = toolStats.getLoreFromConfig(configLorePath, true);
if (configLore == null || configLoreRaw == null) {
toolStats.logger.warning("There is no lore message for messages." + configLorePath + "!");
toolStats.logger.warning("Unable to update lore for item.");
return null;
}
List<String> newLore;
// replace the placeholder with the value
// ex: {kills} -> a number
String newLine = configLoreRaw.replace(placeholder, placeholderValue);
if (itemMeta.hasLore()) {
newLore = itemMeta.getLore();
boolean hasLore = false;
// keep track of line index
// this doesn't mess the lore of existing items
for (int x = 0; x < newLore.size(); x++) {
// check to see if the line matches the config value
// this means we update this line only!
if (newLore.get(x).contains(configLore)) {
hasLore = true;
newLore.set(x, newLine);
break;
}
}
// if the item has lore but doesn't have our line, add it
if (!hasLore) {
newLore.add(newLine);
}
} else {
// if the item has no lore, create a new list and add the line
newLore = new ArrayList<>();
newLore.add(newLine);
}
return newLore;
}
/**
* Adds new ownership tag to an item.
*
* @param itemMeta The item meta.
* @param playerName The new owner of item.
* @param formattedDate The date of the ownership.
* @param type The type of new ownership.
* @return The item's new lore.
*/
public List<String> addNewOwner(ItemMeta itemMeta, String playerName, String formattedDate, String type) {
String dateCreated = null;
String itemOwner = null;
switch (type) {
case "LOOTED": {
dateCreated = toolStats.getLoreFromConfig("looted.found-on", true);
itemOwner = toolStats.getLoreFromConfig("looted.found-by", true);
break;
}
case "CREATED": {
dateCreated = toolStats.getLoreFromConfig("created.created-on", true);
itemOwner = toolStats.getLoreFromConfig("created.created-by", true);
break;
}
case "FISHED": {
dateCreated = toolStats.getLoreFromConfig("fished.caught-on", true);
itemOwner = toolStats.getLoreFromConfig("fished.caught-by", true);
break;
}
case "TRADED": {
dateCreated = toolStats.getLoreFromConfig("traded.traded-on", true);
itemOwner = toolStats.getLoreFromConfig("traded.traded-by", true);
}
}
if (dateCreated == null || itemOwner == null) {
toolStats.logger.warning("There is no lore message for messages." + type.toLowerCase(Locale.ENGLISH) + "!");
toolStats.logger.warning("Unable to update lore for item.");
return null;
}
List<String> newLore;
if (itemMeta.hasLore()) {
newLore = itemMeta.getLore();
} else {
newLore = new ArrayList<>();
}
newLore.add(dateCreated.replace("{date}", formattedDate));
newLore.add(itemOwner.replace("{player}", playerName));
return newLore;
}
}