mirror of
https://github.com/hyperdefined/ToolStats.git
synced 2025-12-06 06:41:44 +00:00
Compare commits
72 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fe5e55d746 | ||
|
|
0574fb61a6 | ||
|
|
433ab547cb | ||
|
|
9746789f2b | ||
|
|
fd3c3ca8ed | ||
|
|
c6dc2c3368 | ||
|
|
d11211b1c7 | ||
|
|
e8c2296bd7 | ||
|
|
d8f296a085 | ||
|
|
58dee809f3 | ||
|
|
590cf6e8b0 | ||
|
|
a7cc8671bd | ||
|
|
37a7c642d3 | ||
|
|
31acdd9527 | ||
|
|
9b0ac2d912 | ||
|
|
4ea9e1600b | ||
|
|
72b8b31779 | ||
|
|
fce135f2f8 | ||
|
|
425b315104 | ||
|
|
50a0bc800c | ||
|
|
583cd824f4 | ||
|
|
d63f00166d | ||
|
|
0bcb0c7370 | ||
|
|
e4859de614 | ||
|
|
f7c1af153f | ||
|
|
946dc591f5 | ||
|
|
d279569199 | ||
|
|
f2739cf433 | ||
|
|
bef854be14 | ||
|
|
2c971da1b9 | ||
|
|
2e0ee18594 | ||
|
|
98a5f20a86 | ||
|
|
dd8a0db3b6 | ||
|
|
fee7d5cd99 | ||
|
|
70ebf49958 | ||
|
|
50d2ec0507 | ||
|
|
6428925e40 | ||
|
|
1d4faa89a1 | ||
|
|
1ad650f42b | ||
|
|
fdb6626c42 | ||
|
|
cd80e6a675 | ||
|
|
03d45ea8f7 | ||
|
|
91a98cf664 | ||
|
|
72b00e96f6 | ||
|
|
07458bfc11 | ||
|
|
24d30f6059 | ||
|
|
bea7e52347 | ||
|
|
1b1b45ef6d | ||
|
|
2de2ae82a7 | ||
|
|
7639943fea | ||
|
|
2b75ea094d | ||
|
|
bc8496fad4 | ||
|
|
1cef74311e | ||
|
|
cbbdb4e9c4 | ||
|
|
f6d35c459c | ||
|
|
4624d7a847 | ||
|
|
3766db3454 | ||
|
|
5a5e29c5e2 | ||
|
|
d188877f18 | ||
|
|
05200f075b | ||
|
|
9973d743a5 | ||
|
|
a89b9d6f6a | ||
|
|
082f529c32 | ||
|
|
05f940026f | ||
|
|
59d0295b18 | ||
|
|
af908f2eb6 | ||
|
|
c86e96e200 | ||
|
|
8061293810 | ||
|
|
656e265006 | ||
|
|
507813f106 | ||
|
|
e3e1c6a690 | ||
|
|
5eebcb9ff4 |
@@ -1,11 +1,9 @@
|
||||
<h1 align="center">ToolStats</h1>
|
||||
|
||||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/Minecraft-1.15--1.19-orange" alt="Minecraft versions">
|
||||
<img src="https://img.shields.io/badge/Minecraft-1.15--1.19.2-orange" alt="Minecraft versions">
|
||||
<img src="https://img.shields.io/github/v/release/hyperdefined/ToolStats" alt="GitHub release (latest by date)">
|
||||
<a href="https://github.com/hyperdefined/ToolStats/releases"><img src="https://img.shields.io/github/downloads/hyperdefined/ToolStats/total?logo=github" alt="Downloads"></a>
|
||||
<a href="https://en.cryptobadges.io/donate/1F29aNKQzci3ga5LDcHHawYzFPXvELTFoL"><img src="https://en.cryptobadges.io/badge/micro/1F29aNKQzci3ga5LDcHHawYzFPXvELTFoL" alt="Donate with Bitcoin"></a>
|
||||
<a href="https://en.cryptobadges.io/donate/0xF3b4e87E4c11f586949ca8740eD33A1e473F924c"><img src="https://en.cryptobadges.io/badge/micro/0xF3b4e87E4c11f586949ca8740eD33A1e473F924c" alt="Donate with Ethereum"></a>
|
||||
<a href="https://ko-fi.com/hyperdefined"><img src="https://img.shields.io/badge/Donate-Ko--fi-red" alt="Donate via Ko-fi"></a>
|
||||
<a href="https://www.gnu.org/licenses/gpl-3.0"><img src="https://img.shields.io/badge/License-GPLv3-blue.svg" alt="License: GPL v3"></a>
|
||||
<a href="https://wakatime.com/badge/user/992a7647-176a-477c-8086-e1abfba87ff4/project/0200f07a-f303-4103-a5f2-34b38c9c1fa4"><img src="https://wakatime.com/badge/user/992a7647-176a-477c-8086-e1abfba87ff4/project/0200f07a-f303-4103-a5f2-34b38c9c1fa4.svg" alt="wakatime"></a>
|
||||
|
||||
20
pom.xml
20
pom.xml
@@ -23,7 +23,7 @@
|
||||
|
||||
<groupId>lol.hyper</groupId>
|
||||
<artifactId>toolstats</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.6.2</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>ToolStats</name>
|
||||
@@ -51,7 +51,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.10.1</version>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
@@ -60,7 +60,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<version>3.4.1</version>
|
||||
<configuration>
|
||||
<relocations>
|
||||
<relocation>
|
||||
@@ -115,31 +115,25 @@
|
||||
<dependency>
|
||||
<groupId>org.bstats</groupId>
|
||||
<artifactId>bstats-bukkit</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<version>3.0.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>lol.hyper</groupId>
|
||||
<artifactId>github-release-api</artifactId>
|
||||
<version>1.0.2</version>
|
||||
<version>1.0.4</version>
|
||||
<scope>compile</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-text-minimessage</artifactId>
|
||||
<version>4.11.0</version>
|
||||
<version>4.13.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-platform-bukkit</artifactId>
|
||||
<version>4.1.1</version>
|
||||
<version>4.3.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -21,6 +21,8 @@ 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;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -33,9 +35,6 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public final class ToolStats extends JavaPlugin {
|
||||
@@ -56,13 +55,11 @@ public final class ToolStats extends JavaPlugin {
|
||||
public final NamespacedKey shearsSheared = new NamespacedKey(this, "sheared");
|
||||
// stores how much damage armor has taken
|
||||
public final NamespacedKey armorDamage = new NamespacedKey(this, "damage-taken");
|
||||
// stores how much damage armor has taken (as int)
|
||||
public final NamespacedKey armorDamageInt = new NamespacedKey(this, "damage-taken-int");
|
||||
// used for tracking new elytras
|
||||
public final NamespacedKey newElytra = new NamespacedKey(this, "new");
|
||||
|
||||
public final SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
public final DecimalFormat decimalFormat = new DecimalFormat("#,###.00");
|
||||
public final DecimalFormat commaFormat = new DecimalFormat("#,###");
|
||||
|
||||
public BlocksMined blocksMined;
|
||||
public ChunkPopulate chunkPopulate;
|
||||
public CraftItem craftItem;
|
||||
@@ -75,14 +72,17 @@ 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");
|
||||
public FileConfiguration config;
|
||||
public final int CONFIG_VERSION = 3;
|
||||
public final int CONFIG_VERSION = 4;
|
||||
|
||||
private BukkitAudiences adventure;
|
||||
|
||||
public NumberFormat numberFormat;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
this.adventure = BukkitAudiences.create(this);
|
||||
@@ -103,6 +103,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);
|
||||
@@ -128,6 +129,8 @@ public final class ToolStats extends JavaPlugin {
|
||||
if (config.getInt("config-version") != CONFIG_VERSION) {
|
||||
logger.warning("Your config file is outdated! Please regenerate the config.");
|
||||
}
|
||||
|
||||
numberFormat = new NumberFormat(this);
|
||||
}
|
||||
|
||||
public void checkForUpdates() {
|
||||
@@ -174,7 +177,7 @@ public final class ToolStats extends JavaPlugin {
|
||||
itemType = "trident";
|
||||
}
|
||||
} else {
|
||||
itemType = itemName.substring(itemName.indexOf("_") + 1);
|
||||
itemType = itemName.substring(itemName.indexOf('_') + 1);
|
||||
}
|
||||
|
||||
if (itemType == null) {
|
||||
@@ -264,7 +267,7 @@ public final class ToolStats extends JavaPlugin {
|
||||
}
|
||||
|
||||
public BukkitAudiences getAdventure() {
|
||||
if(this.adventure == null) {
|
||||
if (this.adventure == null) {
|
||||
throw new IllegalStateException("Tried to access Adventure when the plugin was disabled!");
|
||||
}
|
||||
return this.adventure;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package lol.hyper.toolstats.commands;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import lol.hyper.toolstats.tools.UUIDDataType;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@@ -25,6 +26,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -48,7 +50,7 @@ public class CommandToolStats implements TabExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (!sender.hasPermission("toolstats.use")) {
|
||||
if (!sender.hasPermission("toolstats.command")) {
|
||||
audiences.sender(sender).sendMessage(Component.text("You do not have permission for this command.").color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
@@ -67,11 +69,23 @@ public class CommandToolStats implements TabExecutor {
|
||||
return true;
|
||||
}
|
||||
case "reset": {
|
||||
if (!sender.hasPermission("toolstats.reset")) {
|
||||
audiences.sender(sender).sendMessage(Component.text("You do not have permission for this command.").color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
if (sender instanceof ConsoleCommandSender) {
|
||||
audiences.sender(sender).sendMessage(Component.text("You must be a player for this command.").color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
if (args.length == 2 && args[1].equalsIgnoreCase("confirm")) {
|
||||
if (!sender.hasPermission("toolstats.reset.confirm")) {
|
||||
audiences.sender(sender).sendMessage(Component.text("You do not have permission for this command.").color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
ItemStack heldItem = player.getInventory().getItemInMainHand();
|
||||
if (heldItem.getType() == Material.AIR) {
|
||||
audiences.sender(sender).sendMessage(Component.text("You must hold an item!").color(NamedTextColor.RED));
|
||||
if (ItemChecker.isValidItem(heldItem.getType())) {
|
||||
audiences.sender(sender).sendMessage(Component.text("You must hold a valid item.").color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
fixItemLore(heldItem, player);
|
||||
@@ -142,7 +156,7 @@ public class CommandToolStats implements TabExecutor {
|
||||
if (container.has(toolStats.timeCreated, PersistentDataType.LONG)) {
|
||||
Long time = container.get(toolStats.timeCreated, PersistentDataType.LONG);
|
||||
if (time != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("looted.found-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
|
||||
lore.add(toolStats.getLoreFromConfig("looted.found-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(time))));
|
||||
}
|
||||
}
|
||||
finalMeta.setLore(lore);
|
||||
@@ -184,19 +198,19 @@ public class CommandToolStats implements TabExecutor {
|
||||
// show how when the item was created based on the previous lore
|
||||
switch (type) {
|
||||
case "DEFAULT": {
|
||||
lore.add(toolStats.getLoreFromConfig("created.created-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
|
||||
lore.add(toolStats.getLoreFromConfig("created.created-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(time))));
|
||||
break;
|
||||
}
|
||||
case "CAUGHT": {
|
||||
lore.add(toolStats.getLoreFromConfig("fished.caught-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
|
||||
lore.add(toolStats.getLoreFromConfig("fished.caught-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(time))));
|
||||
break;
|
||||
}
|
||||
case "LOOTED": {
|
||||
lore.add(toolStats.getLoreFromConfig("looted.found-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
|
||||
lore.add(toolStats.getLoreFromConfig("looted.found-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(time))));
|
||||
break;
|
||||
}
|
||||
case "TRADED": {
|
||||
lore.add(toolStats.getLoreFromConfig("traded.traded-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
|
||||
lore.add(toolStats.getLoreFromConfig("traded.traded-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(time))));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -207,7 +221,7 @@ public class CommandToolStats implements TabExecutor {
|
||||
if (container.has(toolStats.swordPlayerKills, PersistentDataType.INTEGER)) {
|
||||
Integer kills = container.get(toolStats.swordPlayerKills, PersistentDataType.INTEGER);
|
||||
if (kills != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", toolStats.commaFormat.format(kills)));
|
||||
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", toolStats.numberFormat.formatInt(kills)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -215,7 +229,7 @@ public class CommandToolStats implements TabExecutor {
|
||||
if (container.has(toolStats.swordMobKills, PersistentDataType.INTEGER)) {
|
||||
Integer kills = container.get(toolStats.swordMobKills, PersistentDataType.INTEGER);
|
||||
if (kills != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", toolStats.commaFormat.format(kills)));
|
||||
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", toolStats.numberFormat.formatInt(kills)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,7 +237,7 @@ public class CommandToolStats implements TabExecutor {
|
||||
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
|
||||
Integer blocksMined = container.get(toolStats.genericMined, PersistentDataType.INTEGER);
|
||||
if (blocksMined != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
|
||||
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", toolStats.numberFormat.formatInt(blocksMined)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -231,7 +245,7 @@ public class CommandToolStats implements TabExecutor {
|
||||
if (container.has(toolStats.fishingRodCaught, PersistentDataType.INTEGER)) {
|
||||
Integer fish = container.get(toolStats.fishingRodCaught, PersistentDataType.INTEGER);
|
||||
if (fish != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", toolStats.commaFormat.format(fish)));
|
||||
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", toolStats.numberFormat.formatInt(fish)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -239,7 +253,7 @@ public class CommandToolStats implements TabExecutor {
|
||||
if (container.has(toolStats.shearsSheared, PersistentDataType.INTEGER)) {
|
||||
Integer sheep = container.get(toolStats.shearsSheared, PersistentDataType.INTEGER);
|
||||
if (sheep != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", toolStats.commaFormat.format(sheep)));
|
||||
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", toolStats.numberFormat.formatInt(sheep)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -247,7 +261,7 @@ public class CommandToolStats implements TabExecutor {
|
||||
if (container.has(toolStats.armorDamage, PersistentDataType.DOUBLE)) {
|
||||
Double damage = container.get(toolStats.armorDamage, PersistentDataType.DOUBLE);
|
||||
if (damage != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", toolStats.commaFormat.format(damage)));
|
||||
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", toolStats.numberFormat.formatDouble(damage)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -263,13 +277,16 @@ public class CommandToolStats implements TabExecutor {
|
||||
if (args.length == 1) {
|
||||
if (sender.hasPermission("toolstats.reload")) {
|
||||
return Arrays.asList("reset", "reload");
|
||||
} else {
|
||||
}
|
||||
if (sender.hasPermission("toolstats.reset")) {
|
||||
return Collections.singletonList("reset");
|
||||
}
|
||||
}
|
||||
if (args.length == 2) {
|
||||
if (args[0].equalsIgnoreCase("reset")) {
|
||||
return Collections.singletonList("confirm");
|
||||
if (sender.hasPermission("toolstats.reset.confirm")) {
|
||||
return Collections.singletonList("confirm");
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -20,17 +20,17 @@ package lol.hyper.toolstats.events;
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
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 {
|
||||
@@ -41,7 +41,7 @@ public class BlocksMined implements Listener {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onBreak(BlockBreakEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
@@ -51,21 +51,20 @@ public class BlocksMined implements Listener {
|
||||
return;
|
||||
}
|
||||
// if the player mines something with their fist
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR) {
|
||||
return;
|
||||
}
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
ItemStack heldItem = inventory.getItemInMainHand();
|
||||
// only check certain items
|
||||
if (!ItemChecker.isMineTool(heldItem.getType())) {
|
||||
return;
|
||||
}
|
||||
// if it's an item we want, update the stats
|
||||
// update the blocks mined
|
||||
updateBlocksMined(heldItem);
|
||||
}
|
||||
|
||||
private void updateBlocksMined(ItemStack itemStack) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
private void updateBlocksMined(ItemStack playerTool) {
|
||||
ItemMeta meta = playerTool.getItemMeta();
|
||||
if (meta == null) {
|
||||
toolStats.logger.warning(playerTool + " does NOT have any meta! Unable to update stats.");
|
||||
return;
|
||||
}
|
||||
// read the current stats from the item
|
||||
@@ -75,47 +74,22 @@ public class BlocksMined implements Listener {
|
||||
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
|
||||
blocksMined = container.get(toolStats.genericMined, PersistentDataType.INTEGER);
|
||||
}
|
||||
|
||||
if (blocksMined == null) {
|
||||
return;
|
||||
} else {
|
||||
blocksMined++;
|
||||
blocksMined = 0;
|
||||
toolStats.logger.warning(playerTool + " does not have valid generic-mined set! Resting to zero. This should NEVER happen.");
|
||||
}
|
||||
|
||||
blocksMined++;
|
||||
container.set(toolStats.genericMined, PersistentDataType.INTEGER, blocksMined);
|
||||
String configLore = toolStats.getLoreFromConfig("blocks-mined", false);
|
||||
String configLoreRaw = toolStats.getLoreFromConfig("blocks-mined", true);
|
||||
|
||||
if (configLore == null || configLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.blocks-mined!");
|
||||
return;
|
||||
}
|
||||
String blocksMinedFormatted = toolStats.numberFormat.formatInt(blocksMined);
|
||||
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{blocks}", blocksMinedFormatted, "blocks-mined");
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
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, configLoreRaw.replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(configLoreRaw.replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(configLoreRaw.replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
|
||||
}
|
||||
// do we add the lore based on the config?
|
||||
if (toolStats.checkConfig(itemStack, "blocks-mined")) {
|
||||
meta.setLore(lore);
|
||||
if (toolStats.checkConfig(playerTool, "blocks-mined")) {
|
||||
meta.setLore(newLore);
|
||||
}
|
||||
itemStack.setItemMeta(meta);
|
||||
playerTool.setItemMeta(meta);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.bukkit.World;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.ItemFrame;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.world.ChunkPopulateEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -35,7 +36,7 @@ import org.bukkit.persistence.PersistentDataType;
|
||||
public class ChunkPopulate implements Listener {
|
||||
|
||||
// this tags all elytras with a "new" tag
|
||||
// this let's us track any new elytras player loot
|
||||
// this lets us track any new elytras player loot
|
||||
|
||||
private final ToolStats toolStats;
|
||||
|
||||
@@ -43,7 +44,7 @@ public class ChunkPopulate implements Listener {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onPopulate(ChunkPopulateEvent event) {
|
||||
if (event.getChunk().getWorld().getEnvironment() != World.Environment.THE_END) {
|
||||
return;
|
||||
@@ -54,21 +55,22 @@ public class ChunkPopulate implements Listener {
|
||||
Chunk chunk = event.getChunk();
|
||||
for (Entity entity : chunk.getEntities()) {
|
||||
// if there is a new item frame
|
||||
if (entity instanceof ItemFrame) {
|
||||
ItemFrame itemFrame = (ItemFrame) entity;
|
||||
// if the item frame has an elytra
|
||||
if (itemFrame.getItem().getType() == Material.ELYTRA) {
|
||||
ItemStack elytraCopy = itemFrame.getItem();
|
||||
ItemMeta meta = elytraCopy.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
}
|
||||
// add the new tag so we know it's new
|
||||
PersistentDataContainer container = meta.getPersistentDataContainer();
|
||||
container.set(toolStats.newElytra, PersistentDataType.INTEGER, 1);
|
||||
elytraCopy.setItemMeta(meta);
|
||||
itemFrame.setItem(elytraCopy);
|
||||
if (!(entity instanceof ItemFrame)) {
|
||||
return;
|
||||
}
|
||||
ItemFrame itemFrame = (ItemFrame) entity;
|
||||
// if the item frame has an elytra
|
||||
if (itemFrame.getItem().getType() == Material.ELYTRA) {
|
||||
ItemStack elytraCopy = itemFrame.getItem();
|
||||
ItemMeta meta = elytraCopy.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
}
|
||||
// add the new tag so we know it's new
|
||||
PersistentDataContainer container = meta.getPersistentDataContainer();
|
||||
container.set(toolStats.newElytra, PersistentDataType.INTEGER, 1);
|
||||
elytraCopy.setItemMeta(meta);
|
||||
itemFrame.setItem(elytraCopy);
|
||||
}
|
||||
}
|
||||
}, 20);
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.CraftItemEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -42,7 +43,7 @@ public class CraftItem implements Listener {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onCraft(CraftItemEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
@@ -89,6 +90,7 @@ public class CraftItem implements Listener {
|
||||
ItemStack newItem = itemStack.clone();
|
||||
ItemMeta meta = newItem.getItemMeta();
|
||||
if (meta == null) {
|
||||
toolStats.logger.warning(itemStack + " does NOT have any meta! Unable to update stats.");
|
||||
return null;
|
||||
}
|
||||
// get the current time
|
||||
@@ -121,13 +123,12 @@ public class CraftItem implements Listener {
|
||||
// get the current lore the item
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
// do we add the lore based on the config?
|
||||
if (toolStats.checkConfig(itemStack, "created-date")) {
|
||||
lore.add(createdOnRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(createdOnRaw.replace("{date}", toolStats.numberFormat.formatDate(finalDate)));
|
||||
}
|
||||
if (toolStats.checkConfig(itemStack, "created-by")) {
|
||||
lore.add(createdByRaw.replace("{player}", owner.getName()));
|
||||
|
||||
@@ -19,10 +19,12 @@ 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.*;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDamageByBlockEvent;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
@@ -39,143 +41,180 @@ public class EntityDamage implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
public final Set<UUID> trackedMobs = new HashSet<>();
|
||||
private final List<EntityDamageEvent.DamageCause> ignoredCauses = Arrays.asList(EntityDamageEvent.DamageCause.SUICIDE, EntityDamageEvent.DamageCause.VOID, EntityDamageEvent.DamageCause.CUSTOM);
|
||||
|
||||
public EntityDamage(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onDamage(EntityDamageByEntityEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
return;
|
||||
}
|
||||
LivingEntity livingEntity = (LivingEntity) event.getEntity();
|
||||
LivingEntity mobBeingAttacked = (LivingEntity) event.getEntity();
|
||||
|
||||
// ignore void and /kill damage
|
||||
EntityDamageEvent.DamageCause cause = event.getCause();
|
||||
if (ignoredCauses.contains(cause)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// mob is going to die
|
||||
if (livingEntity.getHealth() - event.getFinalDamage() <= 0) {
|
||||
if (mobBeingAttacked.getHealth() - event.getFinalDamage() <= 0) {
|
||||
// a player is killing something
|
||||
if (event.getDamager() instanceof Player) {
|
||||
Player player = (Player) event.getDamager();
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
// a player killed something with their fist
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR) {
|
||||
Player attackingPlayer = (Player) event.getDamager();
|
||||
if (attackingPlayer.getGameMode() == GameMode.CREATIVE || attackingPlayer.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
PlayerInventory attackingPlayerInventory = attackingPlayer.getInventory();
|
||||
ItemStack heldItem = attackingPlayerInventory.getItemInMainHand();
|
||||
// only check certain items
|
||||
if (!ItemChecker.isMeleeWeapon(heldItem.getType())) {
|
||||
return;
|
||||
}
|
||||
// a player is killing another player
|
||||
if (livingEntity instanceof Player) {
|
||||
player.getInventory().setItem(player.getInventory().getHeldItemSlot(), updatePlayerKills(heldItem));
|
||||
if (mobBeingAttacked instanceof Player) {
|
||||
updatePlayerKills(heldItem);
|
||||
return;
|
||||
}
|
||||
// player is killing regular mob
|
||||
player.getInventory().setItem(player.getInventory().getHeldItemSlot(), updateMobKills(heldItem));
|
||||
trackedMobs.add(livingEntity.getUniqueId());
|
||||
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 (livingEntity instanceof Player) {
|
||||
clone = updatePlayerKills(trident.getItem());
|
||||
if (mobBeingAttacked instanceof Player) {
|
||||
newTrident = tridentPlayerKills(trident.getItem());
|
||||
} else {
|
||||
clone = updateMobKills(trident.getItem());
|
||||
// trident is killing a mob
|
||||
newTrident = tridentMobKills(trident.getItem());
|
||||
trackedMobs.add(mobBeingAttacked.getUniqueId());
|
||||
}
|
||||
if (clone == null) {
|
||||
return;
|
||||
if (newTrident != null) {
|
||||
trident.setItem(newTrident);
|
||||
}
|
||||
trident.setItem(clone);
|
||||
}
|
||||
// arrow is being shot
|
||||
if (event.getDamager() instanceof Arrow) {
|
||||
Arrow arrow = (Arrow) event.getDamager();
|
||||
// if the shooter is a player
|
||||
if (arrow.getShooter() instanceof Player) {
|
||||
Player player = (Player) arrow.getShooter();
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
Player shootingPlayer = (Player) arrow.getShooter();
|
||||
if (shootingPlayer.getGameMode() == GameMode.CREATIVE || shootingPlayer.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
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();
|
||||
}
|
||||
if (isOffHand) {
|
||||
heldBow = inventory.getItemInOffHand();
|
||||
}
|
||||
|
||||
// if the player is hold a bow in both hands
|
||||
// default to main hand since that takes priority
|
||||
if (isMainHand && isOffHand) {
|
||||
heldBow = inventory.getItemInMainHand();
|
||||
}
|
||||
|
||||
// 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 (livingEntity instanceof Player) {
|
||||
player.getInventory().setItem(player.getInventory().getHeldItemSlot(), updatePlayerKills(heldItem));
|
||||
} else {
|
||||
player.getInventory().setItem(player.getInventory().getHeldItemSlot(), updateMobKills(heldItem));
|
||||
}
|
||||
|
||||
// player is shooting another player
|
||||
if (mobBeingAttacked instanceof Player) {
|
||||
updatePlayerKills(heldBow);
|
||||
} else {
|
||||
updateMobKills(heldBow);
|
||||
trackedMobs.add(mobBeingAttacked.getUniqueId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// player is taken damage but not being killed
|
||||
if (livingEntity instanceof Player) {
|
||||
Player player = (Player) livingEntity;
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
if (mobBeingAttacked instanceof Player) {
|
||||
Player playerTakingDamage = (Player) mobBeingAttacked;
|
||||
if (playerTakingDamage.getGameMode() == GameMode.CREATIVE || playerTakingDamage.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
for (ItemStack armor : inventory.getArmorContents()) {
|
||||
if (armor != null) {
|
||||
if (ItemChecker.isArmor(armor.getType())) {
|
||||
updateArmorDamage(armor, event.getFinalDamage());
|
||||
PlayerInventory playerInventory = playerTakingDamage.getInventory();
|
||||
for (ItemStack armorPiece : playerInventory.getArmorContents()) {
|
||||
if (armorPiece != null) {
|
||||
if (ItemChecker.isArmor(armorPiece.getType())) {
|
||||
updateDamage(armorPiece, event.getFinalDamage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onDamage(EntityDamageEvent event) {
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
return;
|
||||
}
|
||||
LivingEntity livingEntity = (LivingEntity) event.getEntity();
|
||||
|
||||
// ignore void and /kill damage
|
||||
EntityDamageEvent.DamageCause cause = event.getCause();
|
||||
if (ignoredCauses.contains(cause)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LivingEntity mobBeingAttacked = (LivingEntity) event.getEntity();
|
||||
// player is taken damage but not being killed
|
||||
if (livingEntity instanceof Player) {
|
||||
Player player = (Player) livingEntity;
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
if (mobBeingAttacked instanceof Player) {
|
||||
Player playerTakingDamage = (Player) mobBeingAttacked;
|
||||
if (playerTakingDamage.getGameMode() == GameMode.CREATIVE || playerTakingDamage.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
for (ItemStack armor : inventory.getArmorContents()) {
|
||||
if (armor != null) {
|
||||
if (ItemChecker.isArmor(armor.getType())) {
|
||||
updateArmorDamage(armor, event.getFinalDamage());
|
||||
PlayerInventory playerInventory = playerTakingDamage.getInventory();
|
||||
for (ItemStack armorPiece : playerInventory.getArmorContents()) {
|
||||
if (armorPiece != null) {
|
||||
if (ItemChecker.isArmor(armorPiece.getType())) {
|
||||
updateDamage(armorPiece, event.getFinalDamage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onDamage(EntityDamageByBlockEvent event) {
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
return;
|
||||
}
|
||||
LivingEntity livingEntity = (LivingEntity) event.getEntity();
|
||||
|
||||
// ignore void and /kill damage
|
||||
EntityDamageEvent.DamageCause cause = event.getCause();
|
||||
if (ignoredCauses.contains(cause)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LivingEntity mobBeingAttacked = (LivingEntity) event.getEntity();
|
||||
// player is taken damage but not being killed
|
||||
if (livingEntity instanceof Player) {
|
||||
Player player = (Player) livingEntity;
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
if (mobBeingAttacked instanceof Player) {
|
||||
Player playerTakingDamage = (Player) mobBeingAttacked;
|
||||
if (playerTakingDamage.getGameMode() == GameMode.CREATIVE || playerTakingDamage.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
for (ItemStack armor : inventory.getArmorContents()) {
|
||||
if (armor != null) {
|
||||
if (ItemChecker.isArmor(armor.getType())) {
|
||||
updateArmorDamage(armor, event.getFinalDamage());
|
||||
PlayerInventory playerInventory = playerTakingDamage.getInventory();
|
||||
for (ItemStack armorPiece : playerInventory.getArmorContents()) {
|
||||
if (armorPiece != null) {
|
||||
if (ItemChecker.isArmor(armorPiece.getType())) {
|
||||
updateDamage(armorPiece, event.getFinalDamage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -186,126 +225,70 @@ 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) {
|
||||
return null;
|
||||
toolStats.logger.warning(itemStack + " does NOT have any meta! Unable to update stats.");
|
||||
return;
|
||||
}
|
||||
Integer playerKills = 0;
|
||||
PersistentDataContainer container = meta.getPersistentDataContainer();
|
||||
if (container.has(toolStats.swordPlayerKills, PersistentDataType.INTEGER)) {
|
||||
playerKills = container.get(toolStats.swordPlayerKills, PersistentDataType.INTEGER);
|
||||
}
|
||||
|
||||
if (playerKills == null) {
|
||||
return null;
|
||||
} else {
|
||||
playerKills++;
|
||||
playerKills = 0;
|
||||
toolStats.logger.warning(itemStack + " does not have valid player-kills set! Resting to zero. This should NEVER happen.");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
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, playerKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(playerKills)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(playerKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(playerKills)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(playerKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(playerKills)));
|
||||
}
|
||||
// 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) {
|
||||
return null;
|
||||
toolStats.logger.warning(itemStack + " does NOT have any meta! Unable to update stats.");
|
||||
return;
|
||||
}
|
||||
Integer mobKills = 0;
|
||||
PersistentDataContainer container = meta.getPersistentDataContainer();
|
||||
if (container.has(toolStats.swordMobKills, PersistentDataType.INTEGER)) {
|
||||
mobKills = container.get(toolStats.swordMobKills, PersistentDataType.INTEGER);
|
||||
}
|
||||
|
||||
if (mobKills == null) {
|
||||
return null;
|
||||
} else {
|
||||
mobKills++;
|
||||
mobKills = 0;
|
||||
toolStats.logger.warning(itemStack + " does not have valid mob-kills set! Resting to zero. This should NEVER happen.");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
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, mobKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(mobKills)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(mobKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(mobKills)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(mobKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(mobKills)));
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -314,9 +297,10 @@ public class EntityDamage implements Listener {
|
||||
* @param itemStack The armor piece.
|
||||
* @param damage How much damage is being added.
|
||||
*/
|
||||
private void updateArmorDamage(ItemStack itemStack, double damage) {
|
||||
private void updateDamage(ItemStack itemStack, double damage) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta == null) {
|
||||
toolStats.logger.warning(itemStack + " does NOT have any meta! Unable to update stats.");
|
||||
return;
|
||||
}
|
||||
Double damageTaken = 0.0;
|
||||
@@ -324,47 +308,96 @@ public class EntityDamage implements Listener {
|
||||
if (container.has(toolStats.armorDamage, PersistentDataType.DOUBLE)) {
|
||||
damageTaken = container.get(toolStats.armorDamage, PersistentDataType.DOUBLE);
|
||||
}
|
||||
|
||||
if (damageTaken == null) {
|
||||
return;
|
||||
} else {
|
||||
damageTaken = damageTaken + damage;
|
||||
damageTaken = 0.0;
|
||||
toolStats.logger.warning(itemStack + " does not have valid damage-taken set! Resting to zero. This should NEVER happen.");
|
||||
}
|
||||
|
||||
damageTaken = damageTaken + damage;
|
||||
container.set(toolStats.armorDamage, PersistentDataType.DOUBLE, damageTaken);
|
||||
container.set(toolStats.armorDamageInt, PersistentDataType.INTEGER, damageTaken.intValue());
|
||||
|
||||
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!");
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
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, damageTakenLoreRaw.replace("{damage}", toolStats.decimalFormat.format(damageTaken)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(damageTakenLoreRaw.replace("{damage}", toolStats.decimalFormat.format(damageTaken)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(damageTakenLoreRaw.replace("{damage}", toolStats.decimalFormat.format(damageTaken)));
|
||||
}
|
||||
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");
|
||||
|
||||
// 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");
|
||||
|
||||
// do we add the lore based on the config?
|
||||
if (toolStats.checkConfig(newTrident, "player-kills")) {
|
||||
meta.setLore(newLore);
|
||||
}
|
||||
newTrident.setItemMeta(meta);
|
||||
return newTrident;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,10 +22,13 @@ import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -37,7 +40,7 @@ public class EntityDeath implements Listener {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onDeath(EntityDeathEvent event) {
|
||||
LivingEntity livingEntity = event.getEntity();
|
||||
if (livingEntity instanceof Player) {
|
||||
@@ -46,9 +49,21 @@ public class EntityDeath implements Listener {
|
||||
UUID livingEntityUUID = event.getEntity().getUniqueId();
|
||||
// if it's a mob we are tracking that matters
|
||||
if (toolStats.mobKill.trackedMobs.contains(livingEntityUUID)) {
|
||||
for (ItemStack current : event.getDrops()) {
|
||||
if (ItemChecker.isValidItem(current.getType())) {
|
||||
addLore(current, livingEntity.getName());
|
||||
for (int i = 0; i < event.getDrops().size(); i++) {
|
||||
ItemStack droppedItem = event.getDrops().get(i);
|
||||
ItemMeta droppedItemMeta = droppedItem.getItemMeta();
|
||||
if (droppedItemMeta != null) {
|
||||
PersistentDataContainer container = droppedItemMeta.getPersistentDataContainer();
|
||||
if (container.has(toolStats.timeCreated, PersistentDataType.LONG)) {
|
||||
continue; // ignore any items that have our tags
|
||||
}
|
||||
|
||||
}
|
||||
if (ItemChecker.isValidItem(droppedItem.getType())) {
|
||||
ItemStack newItem = addLore(droppedItem, livingEntity.getName());
|
||||
if (newItem != null) {
|
||||
event.getDrops().set(i, newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
toolStats.mobKill.trackedMobs.remove(livingEntityUUID);
|
||||
@@ -58,45 +73,22 @@ public class EntityDeath implements Listener {
|
||||
/**
|
||||
* Adds "drop by" tag to item.
|
||||
*
|
||||
* @param itemStack The item to add lore to.
|
||||
* @param mob The mob or player name.
|
||||
* @param oldItem The item to add lore to.
|
||||
* @param mob The mob or player name.
|
||||
*/
|
||||
private void addLore(ItemStack itemStack, String mob) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
private ItemStack addLore(ItemStack oldItem, String mob) {
|
||||
ItemStack newItem = oldItem.clone();
|
||||
ItemMeta meta = newItem.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
}
|
||||
boolean hasTag = false;
|
||||
|
||||
String droppedByLore = toolStats.getLoreFromConfig("dropped-by", false);
|
||||
String droppedByLoreRaw = toolStats.getLoreFromConfig("dropped-by", true);
|
||||
|
||||
if (droppedByLore == null || droppedByLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.dropped-by!");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
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));
|
||||
}
|
||||
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{name}", mob, "dropped-by");
|
||||
|
||||
if (toolStats.config.getBoolean("enabled.dropped-by")) {
|
||||
meta.setLore(lore);
|
||||
meta.setLore(newLore);
|
||||
}
|
||||
itemStack.setItemMeta(meta);
|
||||
newItem.setItemMeta(meta);
|
||||
return newItem;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.bukkit.block.Chest;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.minecart.StorageMinecart;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.world.LootGenerateEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
@@ -37,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;
|
||||
|
||||
@@ -49,7 +49,7 @@ public class GenerateLoot implements Listener {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onGenerateLoot(LootGenerateEvent event) {
|
||||
InventoryHolder inventoryHolder = event.getInventoryHolder();
|
||||
if (inventoryHolder == null) {
|
||||
@@ -65,9 +65,11 @@ public class GenerateLoot implements Listener {
|
||||
// if the distance is less than 1, it's the same chest
|
||||
for (Block chest : toolStats.playerInteract.openedChests.keySet()) {
|
||||
Location chestLocation = chest.getLocation();
|
||||
double distance = lootLocation.distance(chestLocation);
|
||||
if (distance <= 1.0) {
|
||||
openedChest = chest;
|
||||
if (chest.getWorld() == lootLocation.getWorld()) {
|
||||
double distance = lootLocation.distance(chestLocation);
|
||||
if (distance <= 1.0) {
|
||||
openedChest = chest;
|
||||
}
|
||||
}
|
||||
}
|
||||
// ignore if the chest is not in the same location
|
||||
@@ -142,26 +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();
|
||||
assert lore != null;
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
if (toolStats.checkConfig(newItem, "looted-tag")) {
|
||||
lore.add(foundOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
|
||||
meta.setLore(newLore);
|
||||
}
|
||||
meta.setLore(lore);
|
||||
newItem.setItemMeta(meta);
|
||||
return newItem;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.*;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityPickupItemEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -30,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;
|
||||
|
||||
@@ -42,7 +42,7 @@ public class PickupItem implements Listener {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onPickup(EntityPickupItemEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
@@ -93,26 +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();
|
||||
assert lore != null;
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.elytra-tag")) {
|
||||
lore.add(foundOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
|
||||
meta.setLore(newLore);
|
||||
}
|
||||
meta.setLore(lore);
|
||||
finalItem.setItemMeta(meta);
|
||||
return finalItem;
|
||||
}
|
||||
|
||||
@@ -25,14 +25,15 @@ import org.bukkit.Material;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerFishEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
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;
|
||||
|
||||
@@ -44,7 +45,7 @@ public class PlayerFish implements Listener {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onFish(PlayerFishEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
@@ -58,28 +59,56 @@ public class PlayerFish implements Listener {
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR || heldItem.getType() != Material.FISHING_ROD) {
|
||||
// make sure the player is holding a fishing rod
|
||||
// player can fish with their offhand
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
boolean isMainHand = inventory.getItemInMainHand().getType() == Material.FISHING_ROD;
|
||||
boolean isOffHand = inventory.getItemInOffHand().getType() == Material.FISHING_ROD;
|
||||
ItemStack fishingRod = null;
|
||||
if (isMainHand) {
|
||||
fishingRod = inventory.getItemInMainHand();
|
||||
}
|
||||
if (isOffHand) {
|
||||
fishingRod = inventory.getItemInOffHand();
|
||||
}
|
||||
|
||||
// if the player is hold fishing rods in both hands
|
||||
// default to main hand since that takes priority
|
||||
if (isMainHand && isOffHand) {
|
||||
fishingRod = inventory.getItemInMainHand();
|
||||
}
|
||||
|
||||
// player swapped items?
|
||||
if (fishingRod == null) {
|
||||
return;
|
||||
}
|
||||
updateFishCount(heldItem);
|
||||
|
||||
// update the fishing rod!
|
||||
updateFishCount(fishingRod);
|
||||
|
||||
// check if the player caught an item
|
||||
if (event.getCaught() == null) {
|
||||
return;
|
||||
}
|
||||
ItemStack caughtItem = ((Item) event.getCaught()).getItemStack();
|
||||
Item caughtItemEntity = (Item) event.getCaught();
|
||||
if (ItemChecker.isValidItem(caughtItem.getType())) {
|
||||
addNewLore(caughtItem, player);
|
||||
ItemStack newItem = addNewLore(caughtItem, player);
|
||||
if (newItem != null) {
|
||||
caughtItemEntity.setItemStack(newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a fishing rod's count.
|
||||
* Update a fishing rod's fish count.
|
||||
*
|
||||
* @param itemStack The fishing rod to update.
|
||||
* @param fishingRod The fishing rod to update.
|
||||
*/
|
||||
private void updateFishCount(ItemStack itemStack) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
private void updateFishCount(ItemStack fishingRod) {
|
||||
ItemMeta meta = fishingRod.getItemMeta();
|
||||
if (meta == null) {
|
||||
toolStats.logger.warning(fishingRod + " does NOT have any meta! Unable to update stats.");
|
||||
return;
|
||||
}
|
||||
Integer fishCaught = 0;
|
||||
@@ -87,92 +116,55 @@ public class PlayerFish implements Listener {
|
||||
if (container.has(toolStats.fishingRodCaught, PersistentDataType.INTEGER)) {
|
||||
fishCaught = container.get(toolStats.fishingRodCaught, PersistentDataType.INTEGER);
|
||||
}
|
||||
|
||||
if (fishCaught == null) {
|
||||
return;
|
||||
} else {
|
||||
fishCaught++;
|
||||
fishCaught = 0;
|
||||
toolStats.logger.warning(fishingRod + " does not have valid fish-caught set! Resting to zero. This should NEVER happen.");
|
||||
}
|
||||
|
||||
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!");
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
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, fishCaughtLoreRaw.replace("{fish}", toolStats.commaFormat.format(fishCaught)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(fishCaughtLoreRaw.replace("{fish}", toolStats.commaFormat.format(fishCaught)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(fishCaughtLoreRaw.replace("{fish}", toolStats.commaFormat.format(fishCaught)));
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.fish-caught")) {
|
||||
meta.setLore(lore);
|
||||
meta.setLore(newLore);
|
||||
}
|
||||
itemStack.setItemMeta(meta);
|
||||
fishingRod.setItemMeta(meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds "caught by" tags to newly fished items.
|
||||
* Add lore to newly caught item.
|
||||
*
|
||||
* @param itemStack The item to add lore to.
|
||||
* @param owner The player who caught the item.
|
||||
* @param originalItem The original item to add lore.
|
||||
* @param owner The player who caught it.
|
||||
* @return A copy of the new item with lore.
|
||||
*/
|
||||
private void addNewLore(ItemStack itemStack, Player owner) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
private ItemStack addNewLore(ItemStack originalItem, Player owner) {
|
||||
ItemStack newItem = originalItem.clone();
|
||||
ItemMeta meta = originalItem.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
long timeCreated = System.currentTimeMillis();
|
||||
Date finalDate = new Date(timeCreated);
|
||||
PersistentDataContainer container = meta.getPersistentDataContainer();
|
||||
|
||||
if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.genericOwner, PersistentDataType.LONG)) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
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;
|
||||
if (toolStats.checkConfig(newItem, "fished-tag")) {
|
||||
meta.setLore(newLore);
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
if (toolStats.checkConfig(itemStack, "fished-tag")) {
|
||||
lore.add(caughtOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(caughtByLoreRaw.replace("{player}", owner.getName()));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
itemStack.setItemMeta(meta);
|
||||
newItem.setItemMeta(meta);
|
||||
return newItem;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,14 +24,15 @@ import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Sheep;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
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 {
|
||||
@@ -42,7 +43,7 @@ public class SheepShear implements Listener {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onShear(PlayerInteractEntityEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
@@ -55,27 +56,51 @@ public class SheepShear implements Listener {
|
||||
if (!(entity instanceof Sheep)) {
|
||||
return;
|
||||
}
|
||||
// check if the player is right-clicking with shears only
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR || heldItem.getType() != Material.SHEARS) {
|
||||
|
||||
// make sure the player is holding shears
|
||||
// player can shear with their offhand
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
boolean isMainHand = inventory.getItemInMainHand().getType() == Material.SHEARS;
|
||||
boolean isOffHand = inventory.getItemInOffHand().getType() == Material.SHEARS;
|
||||
ItemStack shears = null;
|
||||
if (isMainHand) {
|
||||
shears = inventory.getItemInMainHand();
|
||||
}
|
||||
if (isOffHand) {
|
||||
shears = inventory.getItemInOffHand();
|
||||
}
|
||||
|
||||
// if the player is hold fishing rods in both hands
|
||||
// default to main hand since that takes priority
|
||||
if (isMainHand && isOffHand) {
|
||||
shears = inventory.getItemInMainHand();
|
||||
}
|
||||
|
||||
// player swapped items?
|
||||
if (shears == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Sheep sheep = (Sheep) entity;
|
||||
// make sure the sheep is not sheared
|
||||
if (!sheep.isSheared()) {
|
||||
addLore(heldItem);
|
||||
if (sheep.isSheared()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// update the stats
|
||||
ItemStack finalShears = shears;
|
||||
addLore(finalShears);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds tags to shears.
|
||||
*
|
||||
* @param itemStack The shears.
|
||||
* @param newShears The shears.
|
||||
*/
|
||||
private void addLore(ItemStack itemStack) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
private void addLore(ItemStack newShears) {
|
||||
ItemMeta meta = newShears.getItemMeta();
|
||||
if (meta == null) {
|
||||
toolStats.logger.warning(newShears + " does NOT have any meta! Unable to update stats.");
|
||||
return;
|
||||
}
|
||||
Integer sheepSheared = 0;
|
||||
@@ -83,47 +108,21 @@ public class SheepShear implements Listener {
|
||||
if (container.has(toolStats.shearsSheared, PersistentDataType.INTEGER)) {
|
||||
sheepSheared = container.get(toolStats.shearsSheared, PersistentDataType.INTEGER);
|
||||
}
|
||||
|
||||
if (sheepSheared == null) {
|
||||
return;
|
||||
} else {
|
||||
sheepSheared++;
|
||||
sheepSheared = 0;
|
||||
toolStats.logger.warning(newShears + " does not have valid sheared set! Resting to zero. This should NEVER happen.");
|
||||
}
|
||||
|
||||
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!");
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
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, sheepShearedLoreRaw.replace("{sheep}", toolStats.commaFormat.format(sheepSheared)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(sheepShearedLoreRaw.replace("{sheep}", toolStats.commaFormat.format(sheepSheared)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(sheepShearedLoreRaw.replace("{sheep}", toolStats.commaFormat.format(sheepSheared)));
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.sheep-sheared")) {
|
||||
meta.setLore(lore);
|
||||
meta.setLore(newLore);
|
||||
}
|
||||
itemStack.setItemMeta(meta);
|
||||
newShears.setItemMeta(meta);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
@@ -35,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;
|
||||
|
||||
@@ -47,7 +47,7 @@ public class VillagerTrade implements Listener {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onTrade(InventoryClickEvent event) {
|
||||
if (event.isCancelled() || event.getCurrentItem() == null) {
|
||||
return;
|
||||
@@ -66,21 +66,21 @@ public class VillagerTrade implements Listener {
|
||||
if (event.getSlotType() == InventoryType.SlotType.RESULT) {
|
||||
ItemStack item = event.getCurrentItem();
|
||||
// only check items we want
|
||||
if (ItemChecker.isValidItem(item.getType())) {
|
||||
// if the player shift clicks, show the warning
|
||||
if (event.isShiftClick()) {
|
||||
String configMessage = toolStats.config.getString("messages.shift-click-warning.trading");
|
||||
if (configMessage != null) {
|
||||
event.getWhoClicked().sendMessage(ChatColor.translateAlternateColorCodes('&', configMessage));
|
||||
}
|
||||
}
|
||||
ItemStack newItem = addLore(item, player);
|
||||
if (newItem != null) {
|
||||
// this gets delayed since villager inventories suck for no reason
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> event.setCurrentItem(newItem), 5);
|
||||
return;
|
||||
if (!ItemChecker.isValidItem(item.getType())) {
|
||||
return;
|
||||
}
|
||||
// if the player shift clicks, show the warning
|
||||
if (event.isShiftClick()) {
|
||||
String configMessage = toolStats.config.getString("messages.shift-click-warning.trading");
|
||||
if (configMessage != null) {
|
||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&', configMessage));
|
||||
}
|
||||
}
|
||||
ItemStack newItem = addLore(item, player);
|
||||
if (newItem != null) {
|
||||
// set the new item
|
||||
inventory.setItem(event.getSlot(), newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,13 +88,15 @@ public class VillagerTrade implements Listener {
|
||||
/**
|
||||
* Adds "traded by" tags to item.
|
||||
*
|
||||
* @param itemStack The item to add lore.
|
||||
* @param owner The player who traded.
|
||||
* @param oldItem The item to add lore.
|
||||
* @param owner The player who traded.
|
||||
* @return The item with lore.
|
||||
*/
|
||||
private ItemStack addLore(ItemStack itemStack, Player owner) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
private ItemStack addLore(ItemStack oldItem, Player owner) {
|
||||
ItemStack newItem = oldItem.clone();
|
||||
ItemMeta meta = newItem.getItemMeta();
|
||||
if (meta == null) {
|
||||
toolStats.logger.warning(newItem + " does NOT have any meta! Unable to update stats.");
|
||||
return null;
|
||||
}
|
||||
long timeCreated = System.currentTimeMillis();
|
||||
@@ -108,27 +110,13 @@ 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;
|
||||
if (toolStats.checkConfig(newItem, "traded-tag")) {
|
||||
meta.setLore(newLore);
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
if (toolStats.checkConfig(itemStack, "traded-tag")) {
|
||||
lore.add(tradedOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(tradedByLoreRaw.replace("{player}", owner.getName()));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
itemStack.setItemMeta(meta);
|
||||
return itemStack;
|
||||
newItem.setItemMeta(meta);
|
||||
return newItem;
|
||||
}
|
||||
}
|
||||
|
||||
137
src/main/java/lol/hyper/toolstats/tools/ItemLore.java
Normal file
137
src/main/java/lol/hyper/toolstats/tools/ItemLore.java
Normal file
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* 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.Collections;
|
||||
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 itemMeta.getLore();
|
||||
}
|
||||
|
||||
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);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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 itemMeta.getLore();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
139
src/main/java/lol/hyper/toolstats/tools/NumberFormat.java
Normal file
139
src/main/java/lol/hyper/toolstats/tools/NumberFormat.java
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* 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 java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class NumberFormat {
|
||||
|
||||
private DecimalFormat DECIMAL_FORMAT;
|
||||
private DecimalFormat COMMA_FORMAT;
|
||||
private SimpleDateFormat DATE_FORMAT;
|
||||
|
||||
/**
|
||||
* Utility class to format different numbers
|
||||
* @param toolStats Plugin instance.
|
||||
*/
|
||||
public NumberFormat(ToolStats toolStats) {
|
||||
|
||||
String dateFormat = toolStats.config.getString("date-format");
|
||||
String decimalSeparator = toolStats.config.getString("number-formats.decimal-separator");
|
||||
String commaSeparator = toolStats.config.getString("number-formats.comma-separator");
|
||||
String commaFormat = toolStats.config.getString("number-formats.comma-format");
|
||||
String decimalFormat = toolStats.config.getString("number-formats.decimal-format");
|
||||
|
||||
// if these config values are missing, use the default ones
|
||||
if (dateFormat == null) {
|
||||
dateFormat = "M/dd/yyyy";
|
||||
toolStats.logger.warning("date-format is missing! Using default American English format.");
|
||||
}
|
||||
|
||||
if (decimalSeparator == null) {
|
||||
decimalSeparator = ".";
|
||||
toolStats.logger.warning("number-formats.decimal-separator is missing! Using default \".\" instead.");
|
||||
}
|
||||
|
||||
if (commaSeparator == null) {
|
||||
commaSeparator = ",";
|
||||
toolStats.logger.warning("number-formats.comma-separator is missing! Using default \",\" instead.");
|
||||
}
|
||||
|
||||
if (commaFormat == null) {
|
||||
commaFormat = "#,###";
|
||||
toolStats.logger.warning("number-formats.comma-format is missing! Using default #,### instead.");
|
||||
}
|
||||
|
||||
if (decimalFormat == null) {
|
||||
decimalFormat = "#,###.00";
|
||||
toolStats.logger.warning("number-formats.comma-separator is missing! Using default #,###.00 instead.");
|
||||
}
|
||||
|
||||
// test the date format
|
||||
try {
|
||||
DATE_FORMAT = new SimpleDateFormat(dateFormat, Locale.getDefault());
|
||||
} catch (NullPointerException | IllegalArgumentException exception) {
|
||||
toolStats.logger.warning("date-format is NOT a valid format! Using default American English format.");
|
||||
exception.printStackTrace();
|
||||
DATE_FORMAT = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
}
|
||||
|
||||
// set the separators
|
||||
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols(Locale.getDefault());
|
||||
formatSymbols.setDecimalSeparator(decimalSeparator.charAt(0));
|
||||
formatSymbols.setGroupingSeparator(commaSeparator.charAt(0));
|
||||
|
||||
|
||||
// test the comma format
|
||||
try {
|
||||
COMMA_FORMAT = new DecimalFormat(commaFormat, formatSymbols);
|
||||
} catch (NullPointerException | IllegalArgumentException exception) {
|
||||
toolStats.logger.warning("number-formats.comma-format is NOT a valid format! Using default #,### instead.");
|
||||
exception.printStackTrace();
|
||||
COMMA_FORMAT = new DecimalFormat("#,###", formatSymbols);
|
||||
}
|
||||
|
||||
// test the decimal format
|
||||
try {
|
||||
DECIMAL_FORMAT = new DecimalFormat(decimalFormat, formatSymbols);
|
||||
} catch (NullPointerException | IllegalArgumentException exception) {
|
||||
toolStats.logger.warning("number-formats.decimal-format is NOT a valid format! Using default #,###.00 instead.");
|
||||
exception.printStackTrace();
|
||||
DECIMAL_FORMAT = new DecimalFormat("#,###.00", formatSymbols);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a number to make it pretty. Example: 4322 to 4,322
|
||||
*
|
||||
* @param number The number to format.
|
||||
* @return The formatted number.
|
||||
*/
|
||||
public String formatInt(int number) {
|
||||
String finalNumber = COMMA_FORMAT.format(number);
|
||||
finalNumber = finalNumber.replaceAll("[\\x{202f}\\x{00A0}]", " ");
|
||||
return finalNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a number to make it pretty. Example: 4322.33 to 4,322.33
|
||||
*
|
||||
* @param number The number to format.
|
||||
* @return The formatted number.
|
||||
*/
|
||||
public String formatDouble(double number) {
|
||||
String finalNumber = DECIMAL_FORMAT.format(number);
|
||||
finalNumber = finalNumber.replaceAll("[\\x{202f}\\x{00A0}]", " ");
|
||||
return finalNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a date into the readable format.
|
||||
*
|
||||
* @param date The date to format.
|
||||
* @return The date into a readable format.
|
||||
*/
|
||||
public String formatDate(Date date) {
|
||||
return DATE_FORMAT.format(date);
|
||||
}
|
||||
}
|
||||
@@ -98,4 +98,17 @@ messages:
|
||||
crafting: "&cCrafting items via shift clicking does not fully apply tags to each item. This is a limitation with the Bukkit API."
|
||||
trading: "&cTrading items via shift clicking does not fully apply tags to each item. This is a limitation with the Bukkit API."
|
||||
|
||||
config-version: 3
|
||||
# Change the default formatting for dates.
|
||||
# See: https://www.digitalocean.com/community/tutorials/java-simpledateformat-java-date-format
|
||||
# Example: "dd/MM/yyyy"
|
||||
date-format: "M/dd/yyyy"
|
||||
|
||||
# Change number formatting.
|
||||
# You probably do not need to touch this.
|
||||
number-formats:
|
||||
comma-separator: ","
|
||||
decimal-separator: "."
|
||||
comma-format: "#,###"
|
||||
decimal-format: "#,###.00"
|
||||
|
||||
config-version: 4
|
||||
@@ -7,12 +7,17 @@ description: Track various tool stats!
|
||||
commands:
|
||||
toolstats:
|
||||
usage: /toolstats
|
||||
description: Main command.
|
||||
permission: toolstats.main
|
||||
permission: toolstats.command
|
||||
permissions:
|
||||
toolstats.main:
|
||||
description: Allows the usage of /toolstats
|
||||
toolstats.command:
|
||||
description: Allows the usage of /toolstats.
|
||||
default: true
|
||||
toolstats.reload:
|
||||
description: Allows the usage of /toolstats reload
|
||||
default: op
|
||||
description: Allows the usage of /toolstats reload.
|
||||
default: op
|
||||
toolstats.reset:
|
||||
description: Allows the usage of /toolstats reset.
|
||||
default: true
|
||||
toolstats.reset.confirm:
|
||||
description: Allows the usage of /toolstats reset confirm.
|
||||
default: true
|
||||
Reference in New Issue
Block a user