mirror of
https://github.com/hyperdefined/ToolStats.git
synced 2025-12-06 06:41:44 +00:00
Compare commits
76 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
63d508fded | ||
|
|
59a8079ba2 | ||
|
|
3767ee2f63 | ||
|
|
7d5f73ae96 | ||
|
|
b581d9d178 | ||
|
|
d355b7a429 | ||
|
|
d06a1c4a40 | ||
|
|
e22fc7b530 | ||
|
|
8478411b8f | ||
|
|
85d072667a | ||
|
|
010ca54144 | ||
|
|
6af49d6c30 | ||
|
|
c4e4ce01fd | ||
|
|
983a35bd0b | ||
|
|
d9ad7ebcad | ||
|
|
995f80e159 | ||
|
|
56850c40d1 | ||
|
|
b9e4bbd1e8 | ||
|
|
6a23c7a724 | ||
|
|
cd6cb999c0 | ||
|
|
f9cff275b5 | ||
|
|
ba1ae01801 | ||
|
|
fd35cf6e9c | ||
|
|
5971ecc919 | ||
|
|
4c4ca0c31e | ||
|
|
294b753430 | ||
|
|
adf6a3149b | ||
|
|
ea68650038 | ||
|
|
114a27e9b5 | ||
|
|
7cdb71df50 | ||
|
|
bd80d6718c | ||
|
|
b9907eedec | ||
|
|
61cf44a407 | ||
|
|
337f88ce36 | ||
|
|
42f100f0b0 | ||
|
|
63b3cb1d03 | ||
|
|
7717b6b5a5 | ||
|
|
a421430fa8 | ||
|
|
7a89b31766 | ||
|
|
0334681971 | ||
|
|
df23f40d6b | ||
|
|
0221ee40cf | ||
|
|
a01d07af4a | ||
|
|
e402d3078a | ||
|
|
4f44ca0777 |
@@ -1,11 +1,9 @@
|
||||
<h1 align="center">ToolStats</h1>
|
||||
|
||||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/Minecraft-1.15+-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>
|
||||
@@ -36,5 +34,8 @@ If item lore is ever incorrect, you can run `/toolstats reset` to reset the item
|
||||

|
||||

|
||||
|
||||
## Documentation
|
||||
Visit the [wiki](https://github.com/hyperdefined/ToolStats/wiki) for help.
|
||||
|
||||
## License
|
||||
This plugin is released under GNU General Public License v3. See [LICENSE](https://github.com/hyperdefined/ToolStats/blob/master/LICENSE).
|
||||
|
||||
39
pom.xml
39
pom.xml
@@ -23,7 +23,7 @@
|
||||
|
||||
<groupId>lol.hyper</groupId>
|
||||
<artifactId>toolstats</artifactId>
|
||||
<version>1.3.1</version>
|
||||
<version>1.5.1</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>ToolStats</name>
|
||||
@@ -37,7 +37,7 @@
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<version>3.2.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>auto-clean</id>
|
||||
@@ -51,7 +51,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.9.0</version>
|
||||
<version>3.10.1</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
@@ -60,13 +60,21 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<version>3.4.0</version>
|
||||
<configuration>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>org.bstats</pattern>
|
||||
<shadedPattern>lol.hyper.toolstats.bstats</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>net.kyori.adventure</pattern>
|
||||
<shadedPattern>lol.hyper.toolstats.adventure</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>lol.hyper.githubreleaseapi</pattern>
|
||||
<shadedPattern>lol.hyper.toolstats.updater</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
</configuration>
|
||||
<executions>
|
||||
@@ -101,15 +109,9 @@
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.18.1-R0.1-SNAPSHOT</version>
|
||||
<version>1.19-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>23.0.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bstats</groupId>
|
||||
<artifactId>bstats-bukkit</artifactId>
|
||||
@@ -119,7 +121,20 @@
|
||||
<dependency>
|
||||
<groupId>lol.hyper</groupId>
|
||||
<artifactId>github-release-api</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.4</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-text-minimessage</artifactId>
|
||||
<version>4.11.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-platform-bukkit</artifactId>
|
||||
<version>4.1.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -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 net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
@@ -32,8 +33,10 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public final class ToolStats extends JavaPlugin {
|
||||
@@ -57,8 +60,7 @@ public final class ToolStats extends JavaPlugin {
|
||||
// used for tracking new elytras
|
||||
public final NamespacedKey newElytra = new NamespacedKey(this, "new");
|
||||
|
||||
public final Set<NamespacedKey> keys = new HashSet<>();
|
||||
|
||||
public SimpleDateFormat dateFormat;
|
||||
public BlocksMined blocksMined;
|
||||
public ChunkPopulate chunkPopulate;
|
||||
public CraftItem craftItem;
|
||||
@@ -67,6 +69,7 @@ public final class ToolStats extends JavaPlugin {
|
||||
public PickupItem pickupItem;
|
||||
public EntityDamage mobKill;
|
||||
public PlayerFish playerFish;
|
||||
public PlayerInteract playerInteract;
|
||||
public SheepShear sheepShear;
|
||||
public VillagerTrade villagerTrade;
|
||||
public CommandToolStats commandToolStats;
|
||||
@@ -76,8 +79,11 @@ public final class ToolStats extends JavaPlugin {
|
||||
public FileConfiguration config;
|
||||
public final int CONFIG_VERSION = 3;
|
||||
|
||||
private BukkitAudiences adventure;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
this.adventure = BukkitAudiences.create(this);
|
||||
if (!configFile.exists()) {
|
||||
this.saveResource("config.yml", true);
|
||||
logger.info("Copying default config!");
|
||||
@@ -91,6 +97,7 @@ public final class ToolStats extends JavaPlugin {
|
||||
pickupItem = new PickupItem(this);
|
||||
mobKill = new EntityDamage(this);
|
||||
playerFish = new PlayerFish(this);
|
||||
playerInteract = new PlayerInteract(this);
|
||||
sheepShear = new SheepShear(this);
|
||||
villagerTrade = new VillagerTrade(this);
|
||||
commandToolStats = new CommandToolStats(this);
|
||||
@@ -103,6 +110,7 @@ public final class ToolStats extends JavaPlugin {
|
||||
Bukkit.getServer().getPluginManager().registerEvents(pickupItem, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(mobKill, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(playerFish, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(playerInteract, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(sheepShear, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(villagerTrade, this);
|
||||
|
||||
@@ -111,15 +119,6 @@ public final class ToolStats extends JavaPlugin {
|
||||
new Metrics(this, 14110);
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(this, this::checkForUpdates);
|
||||
|
||||
keys.add(genericOwner);
|
||||
keys.add(timeCreated);
|
||||
keys.add(swordPlayerKills);
|
||||
keys.add(swordMobKills);
|
||||
keys.add(genericMined);
|
||||
keys.add(fishingRodCaught);
|
||||
keys.add(shearsSheared);
|
||||
keys.add(armorDamage);
|
||||
}
|
||||
|
||||
public void loadConfig() {
|
||||
@@ -127,6 +126,20 @@ public final class ToolStats extends JavaPlugin {
|
||||
if (config.getInt("config-version") != CONFIG_VERSION) {
|
||||
logger.warning("Your config file is outdated! Please regenerate the config.");
|
||||
}
|
||||
|
||||
String dateFormatConfig = config.getString("date-format");
|
||||
if (dateFormatConfig != null) {
|
||||
try {
|
||||
dateFormat = new SimpleDateFormat(dateFormatConfig, Locale.getDefault());
|
||||
} catch (IllegalArgumentException exception) {
|
||||
logger.severe("date-format is NOT a valid format! Using default American English format.");
|
||||
exception.printStackTrace();
|
||||
dateFormat = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
}
|
||||
} else {
|
||||
logger.warning("date-format is missing from your config! Using default American English format.");
|
||||
dateFormat = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
}
|
||||
}
|
||||
|
||||
public void checkForUpdates() {
|
||||
@@ -154,22 +167,26 @@ public final class ToolStats extends JavaPlugin {
|
||||
|
||||
/**
|
||||
* Checks the config to see if we want to show lore on certain items.
|
||||
* @param itemStack The item to check.
|
||||
*
|
||||
* @param itemStack The item to check.
|
||||
* @param configName The config we are checking under.
|
||||
* @return If we want to allow lore or not.
|
||||
*/
|
||||
public boolean checkConfig(ItemStack itemStack, String configName) {
|
||||
String itemName = itemStack.getType().toString().toLowerCase();
|
||||
String itemType = null;
|
||||
if (itemName.contains("bow") || itemName.contains("shears")) {
|
||||
if (itemName.contains("bow") || itemName.contains("shears") || itemName.contains("trident")) {
|
||||
if (itemName.contains("bow")) {
|
||||
itemType = "bow";
|
||||
}
|
||||
if (itemName.contains("shears")) {
|
||||
itemType = "shears";
|
||||
}
|
||||
if (itemName.contains("trident")) {
|
||||
itemType = "trident";
|
||||
}
|
||||
} else {
|
||||
itemType = itemName.substring(itemName.indexOf("_") + 1);
|
||||
itemType = itemName.substring(itemName.indexOf('_') + 1);
|
||||
}
|
||||
|
||||
if (itemType == null) {
|
||||
@@ -198,6 +215,9 @@ public final class ToolStats extends JavaPlugin {
|
||||
case "bow": {
|
||||
return config.getBoolean("enabled." + configName + ".bow");
|
||||
}
|
||||
case "trident": {
|
||||
return config.getBoolean("enabled." + configName + ".trident");
|
||||
}
|
||||
case "helmet":
|
||||
case "chestplate":
|
||||
case "leggings":
|
||||
@@ -210,8 +230,9 @@ public final class ToolStats extends JavaPlugin {
|
||||
|
||||
/**
|
||||
* Gets the lore message from the config.
|
||||
*
|
||||
* @param configName The config name, "messages." is already in front.
|
||||
* @param raw If you want the raw message with the formatting codes and placeholders.
|
||||
* @param raw If you want the raw message with the formatting codes and placeholders.
|
||||
* @return The lore message.
|
||||
*/
|
||||
public String getLoreFromConfig(String configName, boolean raw) {
|
||||
@@ -253,4 +274,11 @@ public final class ToolStats extends JavaPlugin {
|
||||
}
|
||||
return lore;
|
||||
}
|
||||
|
||||
public BukkitAudiences getAdventure() {
|
||||
if (this.adventure == null) {
|
||||
throw new IllegalStateException("Tried to access Adventure when the plugin was disabled!");
|
||||
}
|
||||
return this.adventure;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,12 @@
|
||||
package lol.hyper.toolstats.commands;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.UUIDDataType;
|
||||
import org.bukkit.*;
|
||||
import lol.hyper.toolstats.tools.NumberFormat;
|
||||
import lol.hyper.toolstats.tools.UUIDDataType;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.kyori.adventure.text.Component;
|
||||
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.TabExecutor;
|
||||
@@ -31,57 +35,66 @@ import org.bukkit.persistence.PersistentDataType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class CommandToolStats implements TabExecutor {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
private final BukkitAudiences audiences;
|
||||
|
||||
public CommandToolStats(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
this.audiences = toolStats.getAdventure();
|
||||
}
|
||||
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (!sender.hasPermission("toolstats.use")) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission for this command.");
|
||||
if (!sender.hasPermission("toolstats.command")) {
|
||||
audiences.sender(sender).sendMessage(Component.text("You do not have permission for this command.").color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
if (args.length == 0) {
|
||||
sender.sendMessage(ChatColor.GREEN + "ToolStats version " + toolStats.getDescription().getVersion() + ". Created by hyperdefined.");
|
||||
audiences.sender(sender).sendMessage(Component.text("ToolStats version " + toolStats.getDescription().getVersion() + ". Created by hyperdefined.").color(NamedTextColor.GREEN));
|
||||
return true;
|
||||
}
|
||||
switch (args[0]) {
|
||||
case "reload": {
|
||||
if (sender.isOp() || sender.hasPermission("toolstats.reload")) {
|
||||
if (sender.hasPermission("toolstats.reload")) {
|
||||
toolStats.loadConfig();
|
||||
sender.sendMessage(ChatColor.GREEN + "Configuration reloaded!");
|
||||
audiences.sender(sender).sendMessage(Component.text("Configuration reloaded!").color(NamedTextColor.GREEN));
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission for this command.");
|
||||
audiences.sender(sender).sendMessage(Component.text("You do not have permission for this command.").color(NamedTextColor.RED));
|
||||
}
|
||||
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 (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) {
|
||||
sender.sendMessage(ChatColor.RED + "You must hold an item!");
|
||||
audiences.sender(sender).sendMessage(Component.text("You must hold an item!").color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
fixItemLore(heldItem, player);
|
||||
sender.sendMessage(ChatColor.GREEN + "The lore was reset!");
|
||||
audiences.sender(sender).sendMessage(Component.text("The lore was reset!").color(NamedTextColor.GREEN));
|
||||
return true;
|
||||
}
|
||||
sender.sendMessage(ChatColor.GREEN + "This will remove ALL current lore from the held item and replace it with the correct lore.");
|
||||
sender.sendMessage(ChatColor.GREEN + "The item owner will be who ever is currently running this command.");
|
||||
sender.sendMessage(ChatColor.GREEN + "Only use this if the tags on the tool are incorrect.");
|
||||
sender.sendMessage(ChatColor.GREEN + "Type /toolstats reset confirm to confirm this.");
|
||||
audiences.sender(sender).sendMessage(Component.text("This will remove ALL current lore from the held item and replace it with the correct lore.").color(NamedTextColor.GREEN));
|
||||
audiences.sender(sender).sendMessage(Component.text("The item owner will be who ever is currently running this command.").color(NamedTextColor.GREEN));
|
||||
audiences.sender(sender).sendMessage(Component.text("Only use this if the tags on the tool are incorrect.").color(NamedTextColor.GREEN));
|
||||
audiences.sender(sender).sendMessage(Component.text("Type /toolstats reset confirm to confirm this.").color(NamedTextColor.GREEN));
|
||||
return true;
|
||||
}
|
||||
default: {
|
||||
sender.sendMessage(ChatColor.RED + "Invalid sub-command.");
|
||||
audiences.sender(sender).sendMessage(Component.text("Invalid sub-command.").color(NamedTextColor.RED));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -89,8 +102,9 @@ public class CommandToolStats implements TabExecutor {
|
||||
|
||||
/**
|
||||
* Fixes lore on a given item. This will wipe all lore and reapply our custom ones.
|
||||
*
|
||||
* @param original The item we are fixing.
|
||||
* @param player The player running the command.
|
||||
* @param player The player running the command.
|
||||
*/
|
||||
private void fixItemLore(ItemStack original, Player player) {
|
||||
ItemStack finalItem = original.clone();
|
||||
@@ -129,6 +143,25 @@ public class CommandToolStats implements TabExecutor {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hard code elytras
|
||||
if (finalItem.getType() == Material.ELYTRA) {
|
||||
if (toolStats.config.getBoolean("enabled.elytra-tag")) {
|
||||
lore.add(toolStats.getLoreFromConfig("looted.found-by", true).replace("{player}", player.getName()));
|
||||
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))));
|
||||
}
|
||||
}
|
||||
finalMeta.setLore(lore);
|
||||
finalItem.setItemMeta(finalMeta);
|
||||
int slot = player.getInventory().getHeldItemSlot();
|
||||
player.getInventory().setItem(slot, finalItem);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (toolStats.checkConfig(original, "created-by")) {
|
||||
if (container.has(toolStats.genericOwner, new UUIDDataType())) {
|
||||
container.set(toolStats.genericOwner, new UUIDDataType(), player.getUniqueId());
|
||||
@@ -143,7 +176,7 @@ public class CommandToolStats implements TabExecutor {
|
||||
break;
|
||||
}
|
||||
case "LOOTED": {
|
||||
lore.add(toolStats.getLoreFromConfig("looted.looted-by", true).replace("{player}", player.getName()));
|
||||
lore.add(toolStats.getLoreFromConfig("looted.found-by", true).replace("{player}", player.getName()));
|
||||
break;
|
||||
}
|
||||
case "TRADED": {
|
||||
@@ -156,26 +189,25 @@ public class CommandToolStats implements TabExecutor {
|
||||
if (toolStats.checkConfig(original, "created-date")) {
|
||||
if (container.has(toolStats.timeCreated, PersistentDataType.LONG)) {
|
||||
Long time = container.get(toolStats.timeCreated, PersistentDataType.LONG);
|
||||
if (time == null) {
|
||||
return;
|
||||
}
|
||||
// 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}", format.format(new Date(time))));
|
||||
break;
|
||||
}
|
||||
case "CAUGHT": {
|
||||
lore.add(toolStats.getLoreFromConfig("fished.caught-on", true).replace("{date}", format.format(new Date(time))));
|
||||
break;
|
||||
}
|
||||
case "LOOTED": {
|
||||
lore.add(toolStats.getLoreFromConfig("looted.looted-on", true).replace("{date}", format.format(new Date(time))));
|
||||
break;
|
||||
}
|
||||
case "TRADED": {
|
||||
lore.add(toolStats.getLoreFromConfig("traded.traded-on", true).replace("{date}", format.format(new Date(time))));
|
||||
break;
|
||||
if (time != null) {
|
||||
// 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))));
|
||||
break;
|
||||
}
|
||||
case "CAUGHT": {
|
||||
lore.add(toolStats.getLoreFromConfig("fished.caught-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
|
||||
break;
|
||||
}
|
||||
case "LOOTED": {
|
||||
lore.add(toolStats.getLoreFromConfig("looted.found-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
|
||||
break;
|
||||
}
|
||||
case "TRADED": {
|
||||
lore.add(toolStats.getLoreFromConfig("traded.traded-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,59 +215,55 @@ public class CommandToolStats implements TabExecutor {
|
||||
if (toolStats.checkConfig(original, "player-kills")) {
|
||||
if (container.has(toolStats.swordPlayerKills, PersistentDataType.INTEGER)) {
|
||||
Integer kills = container.get(toolStats.swordPlayerKills, PersistentDataType.INTEGER);
|
||||
if (kills == null) {
|
||||
return;
|
||||
if (kills != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", NumberFormat.formatInt(kills)));
|
||||
}
|
||||
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", Integer.toString(kills)));
|
||||
}
|
||||
}
|
||||
if (toolStats.checkConfig(original, "mob-kills")) {
|
||||
if (container.has(toolStats.swordMobKills, PersistentDataType.INTEGER)) {
|
||||
Integer kills = container.get(toolStats.swordMobKills, PersistentDataType.INTEGER);
|
||||
if (kills == null) {
|
||||
return;
|
||||
if (kills != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", NumberFormat.formatInt(kills)));
|
||||
}
|
||||
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", Integer.toString(kills)));
|
||||
}
|
||||
}
|
||||
if (toolStats.checkConfig(original, "blocks-mined")) {
|
||||
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
|
||||
Integer blocksMined = container.get(toolStats.genericMined, PersistentDataType.INTEGER);
|
||||
if (blocksMined == null) {
|
||||
return;
|
||||
if (blocksMined != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", NumberFormat.formatInt(blocksMined)));
|
||||
}
|
||||
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", Integer.toString(blocksMined)));
|
||||
}
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.fish-caught")) {
|
||||
if (container.has(toolStats.fishingRodCaught, PersistentDataType.INTEGER)) {
|
||||
Integer fish = container.get(toolStats.fishingRodCaught, PersistentDataType.INTEGER);
|
||||
if (fish == null) {
|
||||
return;
|
||||
if (fish != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", NumberFormat.formatInt(fish)));
|
||||
}
|
||||
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", Integer.toString(fish)));
|
||||
}
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.sheep-sheared")) {
|
||||
if (container.has(toolStats.shearsSheared, PersistentDataType.INTEGER)) {
|
||||
Integer sheep = container.get(toolStats.shearsSheared, PersistentDataType.INTEGER);
|
||||
if (sheep == null) {
|
||||
return;
|
||||
if (sheep != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", NumberFormat.formatInt(sheep)));
|
||||
}
|
||||
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", Integer.toString(sheep)));
|
||||
}
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.armor-damage")) {
|
||||
if (container.has(toolStats.armorDamage, PersistentDataType.INTEGER)) {
|
||||
Integer damage = container.get(toolStats.armorDamage, PersistentDataType.INTEGER);
|
||||
if (damage == null) {
|
||||
return;
|
||||
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}", NumberFormat.formatDouble(damage)));
|
||||
}
|
||||
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", Integer.toString(damage)));
|
||||
}
|
||||
}
|
||||
finalMeta.setLore(lore);
|
||||
finalItem.setItemMeta(finalMeta);
|
||||
int slot = player.getInventory().getHeldItemSlot();
|
||||
player.getInventory().setItem(slot, finalItem);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -244,13 +272,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;
|
||||
|
||||
@@ -18,10 +18,13 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import lol.hyper.toolstats.tools.NumberFormat;
|
||||
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;
|
||||
@@ -30,45 +33,43 @@ import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class BlocksMined implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
private final String[] validTools = {"pickaxe", "axe", "hoe", "shovel", "shear"};
|
||||
|
||||
public BlocksMined(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler (priority = EventPriority.HIGHEST)
|
||||
public void onBreak(BlockBreakEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
Player player = event.getPlayer();
|
||||
// ignore creative mode
|
||||
if (player.getGameMode() != GameMode.SURVIVAL) {
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
// if the player mines something with their fist
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
int heldItemSlot = player.getInventory().getHeldItemSlot();
|
||||
ItemStack heldItem = player.getInventory().getItem(heldItemSlot);
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR) {
|
||||
return;
|
||||
}
|
||||
// only check certain items
|
||||
String itemName = heldItem.getType().toString().toLowerCase();
|
||||
if (Arrays.stream(validTools).noneMatch(itemName::contains)) {
|
||||
if (!ItemChecker.isMineTool(heldItem.getType())) {
|
||||
return;
|
||||
}
|
||||
// if it's an item we want, update the stats
|
||||
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
|
||||
@@ -78,11 +79,13 @@ 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);
|
||||
@@ -94,32 +97,32 @@ public class BlocksMined implements Listener {
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
String newLine = configLoreRaw.replace("{blocks}", NumberFormat.formatInt(blocksMined));
|
||||
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}", Integer.toString(blocksMined)));
|
||||
lore.set(x, newLine);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(configLoreRaw.replace("{blocks}", Integer.toString(blocksMined)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(configLoreRaw.replace("{blocks}", Integer.toString(blocksMined)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
// do we add the lore based on the config?
|
||||
if (toolStats.checkConfig(itemStack, "blocks-mined")) {
|
||||
if (toolStats.checkConfig(playerTool, "blocks-mined")) {
|
||||
meta.setLore(lore);
|
||||
}
|
||||
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 use tag any new elytras player loot
|
||||
// this let's 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;
|
||||
|
||||
@@ -18,11 +18,14 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.UUIDDataType;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import lol.hyper.toolstats.tools.UUIDDataType;
|
||||
import org.bukkit.ChatColor;
|
||||
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;
|
||||
@@ -30,58 +33,56 @@ import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class CraftItem implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
public final String[] validItems = {
|
||||
"pickaxe", "sword", "shovel", "axe", "hoe", "bow", "helmet", "chestplate", "leggings", "boots", "fishing"
|
||||
};
|
||||
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
|
||||
public CraftItem(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onCraft(CraftItemEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
Player player = (Player) event.getWhoClicked();
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
ItemStack itemStack = event.getCurrentItem();
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
return;
|
||||
}
|
||||
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
|
||||
// only check for items we want
|
||||
for (String x : validItems) {
|
||||
if (name.contains(x)) {
|
||||
// if the player shift clicks, send them this warning
|
||||
if (event.isShiftClick()) {
|
||||
String configMessage = toolStats.config.getString("messages.shift-click-warning.crafting");
|
||||
if (configMessage != null) {
|
||||
if (configMessage.length() != 0) {
|
||||
event.getWhoClicked().sendMessage(ChatColor.translateAlternateColorCodes('&', configMessage));
|
||||
}
|
||||
}
|
||||
// only check certain items
|
||||
if (!ItemChecker.isValidItem(itemStack.getType())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// if the player shift clicks, send them this warning
|
||||
if (event.isShiftClick()) {
|
||||
String configMessage = toolStats.config.getString("messages.shift-click-warning.crafting");
|
||||
if (configMessage != null) {
|
||||
if (configMessage.length() != 0) {
|
||||
event.getWhoClicked().sendMessage(ChatColor.translateAlternateColorCodes('&', configMessage));
|
||||
}
|
||||
// test the item before setting it
|
||||
if (addLore(itemStack, player) == null) {
|
||||
return;
|
||||
}
|
||||
// set the result
|
||||
event.setCurrentItem(addLore(itemStack, player));
|
||||
}
|
||||
}
|
||||
// test the item before setting it
|
||||
ItemStack newItem = addLore(itemStack, player);
|
||||
if (newItem != null) {
|
||||
// set the result
|
||||
event.setCurrentItem(newItem);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds crafted tags to item.
|
||||
*
|
||||
* @param itemStack The item add item to.
|
||||
* @param owner The player crafting.
|
||||
* @param owner The player crafting.
|
||||
* @return A copy of the item with the tags + lore.
|
||||
*/
|
||||
private ItemStack addLore(ItemStack itemStack, Player owner) {
|
||||
@@ -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}", format.format(finalDate)));
|
||||
lore.add(createdOnRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
}
|
||||
if (toolStats.checkConfig(itemStack, "created-by")) {
|
||||
lore.add(createdByRaw.replace("{player}", owner.getName()));
|
||||
|
||||
@@ -18,10 +18,13 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import lol.hyper.toolstats.tools.NumberFormat;
|
||||
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;
|
||||
@@ -32,36 +35,41 @@ import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class EntityDamage implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
private final String[] validTools = {"sword", "trident", "axe"};
|
||||
private final DecimalFormat decimalFormat = new DecimalFormat("0.00");
|
||||
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.HIGHEST)
|
||||
public void onDamage(EntityDamageByEntityEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// mob is going to die
|
||||
if (livingEntity.getHealth() - event.getFinalDamage() <= 0) {
|
||||
// a player is killing something
|
||||
if (event.getDamager() instanceof Player) {
|
||||
Player player = (Player) event.getDamager();
|
||||
if (player.getGameMode() != GameMode.SURVIVAL) {
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
// a player killed something with their fist
|
||||
@@ -69,9 +77,8 @@ public class EntityDamage implements Listener {
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR) {
|
||||
return;
|
||||
}
|
||||
// check items we want
|
||||
String itemName = heldItem.getType().toString().toLowerCase();
|
||||
if (Arrays.stream(validTools).noneMatch(itemName::contains)) {
|
||||
// only check certain items
|
||||
if (!ItemChecker.isMeleeWeapon(heldItem.getType())) {
|
||||
return;
|
||||
}
|
||||
// a player is killing another player
|
||||
@@ -104,6 +111,9 @@ public class EntityDamage implements Listener {
|
||||
// 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) {
|
||||
return;
|
||||
}
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
if (heldItem == null) {
|
||||
return;
|
||||
@@ -123,46 +133,75 @@ public class EntityDamage implements Listener {
|
||||
// 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) {
|
||||
return;
|
||||
}
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
for (ItemStack armor : inventory.getArmorContents()) {
|
||||
if (armor != null) {
|
||||
updateArmorDamage(armor, event.getDamage());
|
||||
if (ItemChecker.isArmor(armor.getType())) {
|
||||
updateArmorDamage(armor, event.getFinalDamage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onDamage(EntityDamageEvent event) {
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ignore void and /kill damage
|
||||
EntityDamageEvent.DamageCause cause = event.getCause();
|
||||
if (ignoredCauses.contains(cause)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LivingEntity livingEntity = (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) {
|
||||
return;
|
||||
}
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
for (ItemStack armor : inventory.getArmorContents()) {
|
||||
if (armor != null) {
|
||||
updateArmorDamage(armor, event.getDamage());
|
||||
if (ItemChecker.isArmor(armor.getType())) {
|
||||
updateArmorDamage(armor, event.getFinalDamage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onDamage(EntityDamageByBlockEvent event) {
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ignore void and /kill damage
|
||||
EntityDamageEvent.DamageCause cause = event.getCause();
|
||||
if (ignoredCauses.contains(cause)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LivingEntity livingEntity = (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) {
|
||||
return;
|
||||
}
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
for (ItemStack armor : inventory.getArmorContents()) {
|
||||
if (armor != null) {
|
||||
updateArmorDamage(armor, event.getDamage());
|
||||
if (ItemChecker.isArmor(armor.getType())) {
|
||||
updateArmorDamage(armor, event.getFinalDamage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,6 +209,7 @@ public class EntityDamage implements Listener {
|
||||
|
||||
/**
|
||||
* Updates a weapon's player kills.
|
||||
*
|
||||
* @param itemStack The item to update.
|
||||
* @return A copy of the item.
|
||||
*/
|
||||
@@ -177,6 +217,7 @@ public class EntityDamage implements Listener {
|
||||
ItemStack finalItem = itemStack.clone();
|
||||
ItemMeta meta = finalItem.getItemMeta();
|
||||
if (meta == null) {
|
||||
toolStats.logger.warning(itemStack + " does NOT have any meta! Unable to update stats.");
|
||||
return null;
|
||||
}
|
||||
Integer playerKills = 0;
|
||||
@@ -184,11 +225,13 @@ public class EntityDamage implements Listener {
|
||||
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);
|
||||
@@ -200,27 +243,27 @@ public class EntityDamage implements Listener {
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
String newLine = playerKillsLoreRaw.replace("{kills}", NumberFormat.formatInt(playerKills));
|
||||
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}", Integer.toString(playerKills)));
|
||||
lore.set(x, newLine);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(playerKillsLoreRaw.replace("{kills}", Integer.toString(playerKills)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(playerKillsLoreRaw.replace("{kills}", Integer.toString(playerKills)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
// do we add the lore based on the config?
|
||||
if (toolStats.checkConfig(itemStack, "player-kills")) {
|
||||
@@ -232,6 +275,7 @@ public class EntityDamage implements Listener {
|
||||
|
||||
/**
|
||||
* Updates a weapon's mob kills.
|
||||
*
|
||||
* @param itemStack The item to update.
|
||||
* @return A copy of the item.
|
||||
*/
|
||||
@@ -239,6 +283,7 @@ public class EntityDamage implements Listener {
|
||||
ItemStack finalItem = itemStack.clone();
|
||||
ItemMeta meta = finalItem.getItemMeta();
|
||||
if (meta == null) {
|
||||
toolStats.logger.warning(itemStack + " does NOT have any meta! Unable to update stats.");
|
||||
return null;
|
||||
}
|
||||
Integer mobKills = 0;
|
||||
@@ -246,11 +291,13 @@ public class EntityDamage implements Listener {
|
||||
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);
|
||||
@@ -262,27 +309,27 @@ public class EntityDamage implements Listener {
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
String newLine = mobKillsLoreRaw.replace("{kills}", NumberFormat.formatInt(mobKills));
|
||||
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}", Integer.toString(mobKills)));
|
||||
lore.set(x, newLine);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(mobKillsLoreRaw.replace("{kills}", Integer.toString(mobKills)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(mobKillsLoreRaw.replace("{kills}", Integer.toString(mobKills)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
// do we add the lore based on the config?
|
||||
if (toolStats.checkConfig(itemStack, "mob-kills")) {
|
||||
@@ -294,12 +341,14 @@ public class EntityDamage implements Listener {
|
||||
|
||||
/**
|
||||
* Updates a player's armor damage stats.
|
||||
*
|
||||
* @param itemStack The armor piece.
|
||||
* @param damage How much damage is being added.
|
||||
* @param damage How much damage is being added.
|
||||
*/
|
||||
private void updateArmorDamage(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;
|
||||
@@ -307,12 +356,13 @@ 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.");
|
||||
}
|
||||
decimalFormat.setRoundingMode(RoundingMode.DOWN);
|
||||
|
||||
damageTaken = damageTaken + damage;
|
||||
container.set(toolStats.armorDamage, PersistentDataType.DOUBLE, damageTaken);
|
||||
|
||||
String damageTakenLore = toolStats.getLoreFromConfig("damage-taken", false);
|
||||
@@ -324,27 +374,27 @@ public class EntityDamage implements Listener {
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
String newLine = damageTakenLoreRaw.replace("{damage}", NumberFormat.formatDouble(damageTaken));
|
||||
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}", decimalFormat.format(damageTaken)));
|
||||
lore.set(x, newLine);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(damageTakenLoreRaw.replace("{damage}", decimalFormat.format(damageTaken)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(damageTakenLoreRaw.replace("{damage}", decimalFormat.format(damageTaken)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.armor-damage")) {
|
||||
meta.setLore(lore);
|
||||
|
||||
@@ -18,13 +18,17 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
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.*;
|
||||
|
||||
@@ -36,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) {
|
||||
@@ -45,11 +49,20 @@ 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()) {
|
||||
String name = current.getType().toString().toLowerCase(Locale.ROOT);
|
||||
for (String item : toolStats.craftItem.validItems) {
|
||||
if (name.contains(item)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,13 +72,15 @@ 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;
|
||||
return null;
|
||||
}
|
||||
boolean hasTag = false;
|
||||
|
||||
@@ -74,13 +89,12 @@ public class EntityDeath implements Listener {
|
||||
|
||||
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
|
||||
@@ -98,6 +112,7 @@ public class EntityDeath implements Listener {
|
||||
if (toolStats.config.getBoolean("enabled.dropped-by")) {
|
||||
meta.setLore(lore);
|
||||
}
|
||||
itemStack.setItemMeta(meta);
|
||||
newItem.setItemMeta(meta);
|
||||
return newItem;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,17 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.UUIDDataType;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import lol.hyper.toolstats.tools.UUIDDataType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
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;
|
||||
@@ -32,56 +38,94 @@ import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class GenerateLoot implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
public final String[] validItems = {
|
||||
"pickaxe", "sword", "shovel", "axe", "hoe", "bow", "helmet", "chestplate", "leggings", "boots", "fishing"
|
||||
};
|
||||
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
|
||||
public GenerateLoot(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onGenerateLoot(LootGenerateEvent event) {
|
||||
InventoryHolder inventoryHolder = event.getInventoryHolder();
|
||||
if (inventoryHolder == null) {
|
||||
return;
|
||||
}
|
||||
Inventory chest = inventoryHolder.getInventory();
|
||||
// run task later since if it runs on the same tick it breaks idk
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> {
|
||||
Player player = (Player) chest.getViewers().get(0);
|
||||
// do a classic for loot so we keep track of chest index of item
|
||||
for (int i = 0; i < chest.getContents().length; i++) {
|
||||
ItemStack itemStack = chest.getItem(i);
|
||||
// ignore air
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
continue;
|
||||
}
|
||||
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
|
||||
for (String x : validItems) {
|
||||
if (name.contains(x)) {
|
||||
chest.setItem(i, addLore(itemStack, player));
|
||||
Location lootLocation = event.getLootContext().getLocation();
|
||||
Inventory chestInv = inventoryHolder.getInventory();
|
||||
|
||||
if (inventoryHolder instanceof Chest) {
|
||||
Block openedChest = null;
|
||||
// look at the current list of opened chest and get the distance
|
||||
// between the lootcontext location and chest location
|
||||
// if the distance is less than 1, it's the same chest
|
||||
for (Block chest : toolStats.playerInteract.openedChests.keySet()) {
|
||||
Location chestLocation = chest.getLocation();
|
||||
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
|
||||
if (openedChest == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
},1);
|
||||
// run task later since if it runs on the same tick it breaks idk
|
||||
Block finalOpenedChest = openedChest;
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> {
|
||||
Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest);
|
||||
// do a classic for loop, so we keep track of chest index of item
|
||||
for (int i = 0; i < chestInv.getContents().length; i++) {
|
||||
ItemStack itemStack = chestInv.getItem(i);
|
||||
// ignore air
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
continue;
|
||||
}
|
||||
if (ItemChecker.isValidItem(itemStack.getType())) {
|
||||
ItemStack newItem = addLore(itemStack, player);
|
||||
if (newItem != null) {
|
||||
chestInv.setItem(i, newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}, 1);
|
||||
}
|
||||
if (inventoryHolder instanceof StorageMinecart) {
|
||||
StorageMinecart mineCart = (StorageMinecart) inventoryHolder;
|
||||
if (toolStats.playerInteract.openedMineCarts.containsKey(mineCart)) {
|
||||
Player player = toolStats.playerInteract.openedMineCarts.get(mineCart);
|
||||
// player clicked this minecart
|
||||
for (int i = 0; i < chestInv.getContents().length; i++) {
|
||||
ItemStack itemStack = chestInv.getItem(i);
|
||||
// ignore air
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
continue;
|
||||
}
|
||||
if (ItemChecker.isValidItem(itemStack.getType())) {
|
||||
ItemStack newItem = addLore(itemStack, player);
|
||||
if (newItem != null) {
|
||||
chestInv.setItem(i, newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds lore to newly generated items.
|
||||
*
|
||||
* @param itemStack The item to add lore to.
|
||||
* @param owner The player that found the item.
|
||||
* @param owner The player that found the item.
|
||||
* @return The item with the lore.
|
||||
*/
|
||||
private ItemStack addLore(ItemStack itemStack, Player owner) {
|
||||
@@ -112,12 +156,11 @@ public class GenerateLoot implements Listener {
|
||||
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}", format.format(finalDate)));
|
||||
lore.add(foundOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
|
||||
@@ -18,11 +18,12 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.UUIDDataType;
|
||||
import lol.hyper.toolstats.tools.UUIDDataType;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
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,39 +31,45 @@ import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class PickupItem implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
|
||||
public PickupItem(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onPickup(EntityPickupItemEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
Entity entity = event.getEntity();
|
||||
if (entity instanceof Player) {
|
||||
ItemStack itemStack = event.getItem().getItemStack();
|
||||
if (itemStack.getType() == Material.ELYTRA) {
|
||||
Player player = (Player) entity;
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
Item item = event.getItem();
|
||||
if (item.getType() == EntityType.DROPPED_ITEM) {
|
||||
ItemStack itemStack = event.getItem().getItemStack();
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
}
|
||||
PersistentDataContainer container = meta.getPersistentDataContainer();
|
||||
// the elytra has the new key, set the lore to it
|
||||
if (container.has(toolStats.newElytra, PersistentDataType.INTEGER)) {
|
||||
container.remove(toolStats.newElytra);
|
||||
addLore(itemStack, (Player) event.getEntity());
|
||||
if (itemStack.getType() == Material.ELYTRA) {
|
||||
// the elytra has the new key, set the lore to it
|
||||
if (container.has(toolStats.newElytra, PersistentDataType.INTEGER)) {
|
||||
ItemStack newElytra = addLore(itemStack, (Player) event.getEntity());
|
||||
if (newElytra != null) {
|
||||
item.setItemStack(newElytra);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,40 +77,43 @@ public class PickupItem implements Listener {
|
||||
|
||||
/**
|
||||
* Adds "looted by" tags for elytras.
|
||||
*
|
||||
* @param itemStack The elytra to add lore to.
|
||||
* @param owner The player who found it.
|
||||
* @param owner The player who found it.
|
||||
*/
|
||||
private void addLore(ItemStack itemStack, Player owner) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
private ItemStack addLore(ItemStack itemStack, Player owner) {
|
||||
ItemStack finalItem = itemStack.clone();
|
||||
ItemMeta meta = finalItem.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
long timeCreated = System.currentTimeMillis();
|
||||
Date finalDate = new Date(timeCreated);
|
||||
PersistentDataContainer container = meta.getPersistentDataContainer();
|
||||
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
|
||||
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);
|
||||
|
||||
if (foundByLoreRaw == null || foundOnLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.looted!");
|
||||
return;
|
||||
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}", format.format(finalDate)));
|
||||
lore.add(foundOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
itemStack.setItemMeta(meta);
|
||||
finalItem.setItemMeta(meta);
|
||||
return finalItem;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,15 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.UUIDDataType;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import lol.hyper.toolstats.tools.NumberFormat;
|
||||
import lol.hyper.toolstats.tools.UUIDDataType;
|
||||
import org.bukkit.GameMode;
|
||||
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;
|
||||
@@ -30,25 +34,19 @@ import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class PlayerFish implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
public final String[] validItems = {
|
||||
"pickaxe", "sword", "shovel", "axe", "hoe", "bow", "helmet", "chestplate", "leggings", "boots", "fishing"
|
||||
};
|
||||
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
|
||||
public PlayerFish(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onFish(PlayerFishEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
@@ -59,29 +57,38 @@ public class PlayerFish implements Listener {
|
||||
}
|
||||
|
||||
Player player = event.getPlayer();
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
// make sure the player is holding a fishing rod
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR || heldItem.getType() != Material.FISHING_ROD) {
|
||||
return;
|
||||
}
|
||||
// update the fishing rod to the new one
|
||||
updateFishCount(heldItem);
|
||||
// check if the player caught an item
|
||||
if (event.getCaught() == null) {
|
||||
return;
|
||||
}
|
||||
ItemStack caughtItem = ((Item) event.getCaught()).getItemStack();
|
||||
for (String x : validItems) {
|
||||
if (caughtItem.getType().toString().toLowerCase(Locale.ROOT).contains(x)) {
|
||||
addNewLore(caughtItem, player);
|
||||
Item caughtItemEntity = (Item) event.getCaught();
|
||||
if (ItemChecker.isValidItem(caughtItem.getType())) {
|
||||
ItemStack newItem = addNewLore(caughtItem, player);
|
||||
if (newItem != null) {
|
||||
caughtItemEntity.setItemStack(newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a fishing rod's count.
|
||||
* @param itemStack The fishing rod to update.
|
||||
* Update a fishing rod's fish count.
|
||||
* @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;
|
||||
@@ -89,11 +96,13 @@ 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);
|
||||
@@ -105,50 +114,65 @@ public class PlayerFish implements Listener {
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
String newLine = fishCaughtLoreRaw.replace("{fish}", NumberFormat.formatInt(fishCaught));
|
||||
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}", Integer.toString(fishCaught)));
|
||||
lore.set(x, newLine);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(fishCaughtLoreRaw.replace("{fish}", Integer.toString(fishCaught)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(fishCaughtLoreRaw.replace("{fish}", Integer.toString(fishCaught)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
|
||||
/*
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("EvenMoreFish")) {
|
||||
ListIterator<String> iterator = lore.listIterator();
|
||||
while (iterator.hasNext()) {
|
||||
String line = iterator.next();
|
||||
toolStats.logger.info(line);
|
||||
if (line.equalsIgnoreCase("§f")) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
if (toolStats.config.getBoolean("enabled.fish-caught")) {
|
||||
meta.setLore(lore);
|
||||
}
|
||||
itemStack.setItemMeta(meta);
|
||||
fishingRod.setItemMeta(meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds "caught by" tags to newly fished items.
|
||||
* @param itemStack The item to add lore to.
|
||||
* @param owner The player who caught the item.
|
||||
* Add lore to newly caught 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);
|
||||
@@ -159,7 +183,7 @@ public class PlayerFish implements Listener {
|
||||
|
||||
if (caughtByLoreRaw == null || caughtOnLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.fished!");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
@@ -169,11 +193,12 @@ public class PlayerFish implements Listener {
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
if (toolStats.checkConfig(itemStack, "fished-tag")) {
|
||||
lore.add(caughtOnLoreRaw.replace("{date}", format.format(finalDate)));
|
||||
if (toolStats.checkConfig(newItem, "fished-tag")) {
|
||||
lore.add(caughtOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(caughtByLoreRaw.replace("{player}", owner.getName()));
|
||||
meta.setLore(lore);
|
||||
}
|
||||
meta.setLore(lore);
|
||||
itemStack.setItemMeta(meta);
|
||||
newItem.setItemMeta(meta);
|
||||
return newItem;
|
||||
}
|
||||
}
|
||||
|
||||
84
src/main/java/lol/hyper/toolstats/events/PlayerInteract.java
Normal file
84
src/main/java/lol/hyper/toolstats/events/PlayerInteract.java
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.minecart.StorageMinecart;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PlayerInteract implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
|
||||
public final HashMap<Block, Player> openedChests = new HashMap<>();
|
||||
public final HashMap<StorageMinecart, Player> openedMineCarts = new HashMap<>();
|
||||
|
||||
public PlayerInteract(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInteract(PlayerInteractEvent event) {
|
||||
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
|
||||
return;
|
||||
}
|
||||
|
||||
Block block = event.getClickedBlock();
|
||||
if (block == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = event.getPlayer();
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
// store when a player opens a chest
|
||||
if (block.getType() != Material.AIR && block.getType() == Material.CHEST) {
|
||||
openedChests.put(block, player);
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> openedChests.remove(block), 20);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInteract(PlayerInteractEntityEvent event) {
|
||||
Entity clicked = event.getRightClicked();
|
||||
Player player = event.getPlayer();
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
// store when a player opens a minecart
|
||||
if (clicked.getType() == EntityType.MINECART_CHEST) {
|
||||
StorageMinecart storageMinecart = (StorageMinecart) clicked;
|
||||
openedMineCarts.put(storageMinecart, player);
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> openedMineCarts.remove(storageMinecart), 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,11 +18,14 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.tools.NumberFormat;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
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;
|
||||
@@ -41,18 +44,22 @@ public class SheepShear implements Listener {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onShear(PlayerInteractEntityEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
Player player = event.getPlayer();
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
Entity entity = event.getRightClicked();
|
||||
if (!(entity instanceof Sheep)) {
|
||||
return;
|
||||
}
|
||||
// check if the player is right-clicking with shears only
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
int heldItemSlot = player.getInventory().getHeldItemSlot();
|
||||
ItemStack heldItem = player.getInventory().getItem(heldItemSlot);
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR || heldItem.getType() != Material.SHEARS) {
|
||||
return;
|
||||
}
|
||||
@@ -60,29 +67,37 @@ public class SheepShear implements Listener {
|
||||
Sheep sheep = (Sheep) entity;
|
||||
// make sure the sheep is not sheared
|
||||
if (!sheep.isSheared()) {
|
||||
addLore(heldItem);
|
||||
ItemStack newShears = addLore(heldItem);
|
||||
if (newShears != null) {
|
||||
player.getInventory().setItem(heldItemSlot, newShears);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds tags to shears.
|
||||
* @param itemStack The shears.
|
||||
*
|
||||
* @param oldShears The shears.
|
||||
*/
|
||||
private void addLore(ItemStack itemStack) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
private ItemStack addLore(ItemStack oldShears) {
|
||||
ItemStack newShears = oldShears.clone();
|
||||
ItemMeta meta = newShears.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
toolStats.logger.warning(newShears + " does NOT have any meta! Unable to update stats.");
|
||||
return null;
|
||||
}
|
||||
Integer sheepSheared = 0;
|
||||
PersistentDataContainer container = meta.getPersistentDataContainer();
|
||||
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);
|
||||
@@ -90,35 +105,36 @@ public class SheepShear implements Listener {
|
||||
|
||||
if (sheepShearedLore == null || sheepShearedLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.sheep-sheared!");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
String newLine = sheepShearedLoreRaw.replace("{sheep}", NumberFormat.formatInt(sheepSheared));
|
||||
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}", Integer.toString(sheepSheared)));
|
||||
lore.set(x, newLine);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(sheepShearedLoreRaw.replace("{sheep}", Integer.toString(sheepSheared)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(sheepShearedLoreRaw.replace("{sheep}", Integer.toString(sheepSheared)));
|
||||
lore.add(newLine);
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.sheep-sheared")) {
|
||||
meta.setLore(lore);
|
||||
}
|
||||
itemStack.setItemMeta(meta);
|
||||
newShears.setItemMeta(meta);
|
||||
return newShears;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,14 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.UUIDDataType;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import lol.hyper.toolstats.tools.UUIDDataType;
|
||||
import org.bukkit.Bukkit;
|
||||
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;
|
||||
@@ -33,52 +36,49 @@ import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class VillagerTrade implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
public final String[] validItems = {
|
||||
"pickaxe", "sword", "shovel", "axe", "hoe", "bow", "helmet", "chestplate", "leggings", "boots", "fishing"
|
||||
};
|
||||
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
|
||||
public VillagerTrade(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onTrade(InventoryClickEvent event) {
|
||||
if (event.isCancelled() || event.getCurrentItem() == null) {
|
||||
return;
|
||||
}
|
||||
Inventory inventory = event.getClickedInventory();
|
||||
if (!(event.getWhoClicked() instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
Player player = (Player) event.getWhoClicked();
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
// only check villager inventories
|
||||
if (inventory instanceof MerchantInventory) {
|
||||
// only check the result slot (the item you receive)
|
||||
if (event.getSlotType() == InventoryType.SlotType.RESULT) {
|
||||
ItemStack item = event.getCurrentItem();
|
||||
// only check items we want
|
||||
for (String x : validItems) {
|
||||
if (item.getType().toString().toLowerCase(Locale.ROOT).contains(x)) {
|
||||
// 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) event.getWhoClicked());
|
||||
if (newItem == null) {
|
||||
return;
|
||||
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
|
||||
// if you don't delay this it doesn't work idk
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, ()-> event.setCurrentItem(newItem), 5);
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> event.setCurrentItem(newItem), 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,13 +87,16 @@ 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();
|
||||
@@ -118,16 +121,15 @@ public class VillagerTrade implements Listener {
|
||||
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}", format.format(finalDate)));
|
||||
if (toolStats.checkConfig(newItem, "traded-tag")) {
|
||||
lore.add(tradedOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(tradedByLoreRaw.replace("{player}", owner.getName()));
|
||||
meta.setLore(lore);
|
||||
}
|
||||
meta.setLore(lore);
|
||||
itemStack.setItemMeta(meta);
|
||||
return itemStack;
|
||||
newItem.setItemMeta(meta);
|
||||
return newItem;
|
||||
}
|
||||
}
|
||||
|
||||
71
src/main/java/lol/hyper/toolstats/tools/ItemChecker.java
Normal file
71
src/main/java/lol/hyper/toolstats/tools/ItemChecker.java
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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 org.bukkit.Material;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
|
||||
public class ItemChecker {
|
||||
|
||||
private static final String[] validItems = { "pickaxe", "sword", "shovel", "axe", "hoe", "bow", "helmet", "chestplate", "leggings", "boots", "fishing" };
|
||||
private static final String[] validArmor = { "helmet", "chestplate", "leggings", "boots" };
|
||||
private static final String[] validMelee = {"sword", "trident", "axe"};
|
||||
private static final String[] validMine = { "pickaxe", "axe", "hoe", "shovel", "shear" };
|
||||
|
||||
/**
|
||||
* Check if item is an armor piece.
|
||||
*
|
||||
* @param itemType The item type, not name.
|
||||
* @return If the item is an armor piece.
|
||||
*/
|
||||
public static boolean isArmor(Material itemType) {
|
||||
return Arrays.stream(validArmor).anyMatch(type -> itemType.toString().toLowerCase(Locale.ROOT).contains(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if item is a tool or armor piece we want to track.
|
||||
*
|
||||
* @param itemType The item type, not name.
|
||||
* @return If the item something we want to track.
|
||||
*/
|
||||
public static boolean isValidItem(Material itemType) {
|
||||
return Arrays.stream(validItems).anyMatch(type -> itemType.toString().toLowerCase(Locale.ROOT).contains(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if item is a melee weapon.
|
||||
*
|
||||
* @param itemType The item type, not name.
|
||||
* @return If the item is a melee weapon.
|
||||
*/
|
||||
public static boolean isMeleeWeapon(Material itemType) {
|
||||
return Arrays.stream(validMelee).anyMatch(type -> itemType.toString().toLowerCase(Locale.ROOT).contains(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if item is a mining tool.
|
||||
*
|
||||
* @param itemType The item type, not name.
|
||||
* @return If the item is a mining tool.
|
||||
*/
|
||||
public static boolean isMineTool(Material itemType) {
|
||||
return Arrays.stream(validMine).anyMatch(type -> itemType.toString().toLowerCase(Locale.ROOT).contains(type));
|
||||
}
|
||||
}
|
||||
50
src/main/java/lol/hyper/toolstats/tools/NumberFormat.java
Normal file
50
src/main/java/lol/hyper/toolstats/tools/NumberFormat.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.util.Locale;
|
||||
|
||||
public class NumberFormat {
|
||||
|
||||
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#,###.00", new DecimalFormatSymbols(Locale.getDefault()));
|
||||
private static final DecimalFormat COMMA_FORMAT = new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.getDefault()));
|
||||
|
||||
/**
|
||||
* Formats a number to make it pretty. Example: 4322 to 4,322
|
||||
* @param number The number to format.
|
||||
* @return The formatted number.
|
||||
*/
|
||||
public static 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 static String formatDouble(double number) {
|
||||
String finalNumber = DECIMAL_FORMAT.format(number);
|
||||
finalNumber = finalNumber.replaceAll("[\\x{202f}\\x{00A0}]", " ");
|
||||
return finalNumber;
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,24 @@
|
||||
* along with ToolStats. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lol.hyper.toolstats;
|
||||
/*
|
||||
* 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 org.bukkit.persistence.PersistentDataAdapterContext;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
@@ -98,4 +98,9 @@ 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."
|
||||
|
||||
# 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"
|
||||
|
||||
config-version: 3
|
||||
@@ -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
|
||||
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