Compare commits
77 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
53fd7f734c | ||
|
|
75cd4fa17e | ||
|
|
e296c27a5b | ||
|
|
adb1c637dc | ||
|
|
4b8f8061b4 | ||
|
|
464916e08b | ||
|
|
6b6b65d1df | ||
|
|
a1c39dffd0 | ||
|
|
88eab81906 | ||
|
|
be233de90d | ||
|
|
2437424e39 | ||
|
|
63b08062c8 | ||
|
|
d23c2446dd | ||
|
|
77b1be62d4 | ||
|
|
29e6c998cf | ||
|
|
89951611fe | ||
|
|
1c18a1a90c | ||
|
|
f0ec409ef4 | ||
|
|
0f9d75db06 | ||
|
|
975445b8f3 | ||
|
|
c02cbadeb2 | ||
|
|
9abddc9524 | ||
|
|
dba672e60c | ||
|
|
b1c9be3bbd | ||
|
|
f970f17e7f | ||
|
|
8305c52f3b | ||
|
|
7bc5af2a58 | ||
|
|
8736519d34 | ||
|
|
3cf6554984 | ||
|
|
c28cd08795 |
24
README.md
@@ -1,16 +1,29 @@
|
||||
<h1 align="center">ToolStats</h1>
|
||||
|
||||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/Minecraft-1.18+-orange" alt="Minecraft versions">
|
||||
<img src="https://img.shields.io/badge/Minecraft-1.15--1.19-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>
|
||||
</p>
|
||||
|
||||
ToolStats is a small plugin that display various stats about tools. This plugin is inspired off of [GearStats](https://www.spigotmc.org/resources/gearstats.12960/). You can disable/enable which stats are shown on which tools via the config. Note: stats are tracked regardless of config setting.
|
||||
ToolStats is a small plugin that display various stats about tools. This plugin is inspired off of [GearStats](https://www.spigotmc.org/resources/gearstats.12960/). You can disable/enable which stats are shown on which tools via the config. Note: stats are tracked regardless of config setting. The config is to disable the lore on the item.
|
||||
|
||||
Here is everything it tracks:
|
||||
* Blocks mined (pickaxes, shovels, axes, hoes, shears)
|
||||
* Player/mob kills (swords, axes, tridents, bows/crossbows)
|
||||
* Ownership of items when crafted, looted (from chests), traded, and caught from fishing.
|
||||
* Armor damage taken.
|
||||
* Fish caught.
|
||||
* Sheep sheared.
|
||||
|
||||
The best part is, this data is stored on the item itself. You can also change how the lore is displayed on the items!
|
||||
|
||||
If item lore is ever incorrect, you can run `/toolstats reset` to reset the item lore so it's correct.
|
||||
|
||||

|
||||

|
||||
@@ -18,6 +31,13 @@ ToolStats is a small plugin that display various stats about tools. This plugin
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
## 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).
|
||||
BIN
images/image.png
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 28 KiB |
BIN
images/image10.png
Normal file
|
After Width: | Height: | Size: 8.8 KiB |
|
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 18 KiB |
BIN
images/image7.png
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
images/image8.png
Normal file
|
After Width: | Height: | Size: 47 KiB |
BIN
images/image9.png
Normal file
|
After Width: | Height: | Size: 15 KiB |
45
pom.xml
@@ -23,7 +23,7 @@
|
||||
|
||||
<groupId>lol.hyper</groupId>
|
||||
<artifactId>toolstats</artifactId>
|
||||
<version>1.0</version>
|
||||
<version>1.4.2</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.3.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,26 @@
|
||||
<dependency>
|
||||
<groupId>lol.hyper</groupId>
|
||||
<artifactId>github-release-api</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.2</version>
|
||||
<scope>compile</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-text-minimessage</artifactId>
|
||||
<version>4.11.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-platform-bukkit</artifactId>
|
||||
<version>4.1.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -21,8 +21,10 @@ 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;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@@ -31,16 +33,17 @@ 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.SimpleDateFormat;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public final class ToolStats extends JavaPlugin {
|
||||
|
||||
// stores who crafted an item
|
||||
public final NamespacedKey craftedOwner = new NamespacedKey(this, "owner");
|
||||
// stores when an item was crafted
|
||||
public final NamespacedKey craftedTime = new NamespacedKey(this, "time-created");
|
||||
// stores who created an item
|
||||
public final NamespacedKey genericOwner = new NamespacedKey(this, "owner");
|
||||
// stores when an item was created
|
||||
public final NamespacedKey timeCreated = new NamespacedKey(this, "time-created");
|
||||
// stores how many player kills by sword
|
||||
public final NamespacedKey swordPlayerKills = new NamespacedKey(this, "player-kills");
|
||||
// stores how many mob kills by sword
|
||||
@@ -53,24 +56,36 @@ public final class ToolStats extends JavaPlugin {
|
||||
public final NamespacedKey shearsSheared = new NamespacedKey(this, "sheared");
|
||||
// stores how much damage armor has taken
|
||||
public final NamespacedKey armorDamage = new NamespacedKey(this, "damage-taken");
|
||||
// used for tracking new elytras
|
||||
public final NamespacedKey newElytra = new NamespacedKey(this, "new");
|
||||
|
||||
public final Set<NamespacedKey> keys = new HashSet<>();
|
||||
public final SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
public final DecimalFormat decimalFormat = new DecimalFormat("#,###.00");
|
||||
public final DecimalFormat commaFormat = new DecimalFormat("#,###");
|
||||
|
||||
public BlocksMined blocksMined;
|
||||
public ChunkPopulate chunkPopulate;
|
||||
public CraftItem craftItem;
|
||||
public EntityDeath entityDeath;
|
||||
public GenerateLoot generateLoot;
|
||||
public PickupItem pickupItem;
|
||||
public EntityDamage mobKill;
|
||||
public PlayerFish playerFish;
|
||||
public PlayerInteract playerInteract;
|
||||
public SheepShear sheepShear;
|
||||
public VillagerTrade villagerTrade;
|
||||
public CommandToolStats commandToolStats;
|
||||
|
||||
public final Logger logger = this.getLogger();
|
||||
public final File configFile = new File(this.getDataFolder(), "config.yml");
|
||||
public FileConfiguration config;
|
||||
public final int CONFIG_VERSION = 1;
|
||||
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!");
|
||||
@@ -78,33 +93,34 @@ public final class ToolStats extends JavaPlugin {
|
||||
loadConfig();
|
||||
blocksMined = new BlocksMined(this);
|
||||
craftItem = new CraftItem(this);
|
||||
chunkPopulate = new ChunkPopulate(this);
|
||||
entityDeath = new EntityDeath(this);
|
||||
generateLoot = new GenerateLoot(this);
|
||||
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);
|
||||
|
||||
Bukkit.getServer().getPluginManager().registerEvents(blocksMined, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(chunkPopulate, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(craftItem, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(entityDeath, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(generateLoot, this);
|
||||
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);
|
||||
|
||||
this.getCommand("toolstats").setExecutor(commandToolStats);
|
||||
|
||||
new Metrics(this, 14110);
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(this, this::checkForUpdates);
|
||||
|
||||
keys.add(craftedOwner);
|
||||
keys.add(craftedTime);
|
||||
keys.add(swordPlayerKills);
|
||||
keys.add(swordMobKills);
|
||||
keys.add(genericMined);
|
||||
keys.add(fishingRodCaught);
|
||||
keys.add(shearsSheared);
|
||||
keys.add(armorDamage);
|
||||
}
|
||||
|
||||
public void loadConfig() {
|
||||
@@ -137,16 +153,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 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);
|
||||
}
|
||||
@@ -177,10 +203,70 @@ public final class ToolStats extends JavaPlugin {
|
||||
case "bow": {
|
||||
return config.getBoolean("enabled." + configName + ".bow");
|
||||
}
|
||||
case "armor": {
|
||||
case "trident": {
|
||||
return config.getBoolean("enabled." + configName + ".trident");
|
||||
}
|
||||
case "helmet":
|
||||
case "chestplate":
|
||||
case "leggings":
|
||||
case "boots": {
|
||||
return config.getBoolean("enabled." + configName + ".armor");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @return The lore message.
|
||||
*/
|
||||
public String getLoreFromConfig(String configName, boolean raw) {
|
||||
String lore = config.getString("messages." + configName);
|
||||
if (lore == null) {
|
||||
return null;
|
||||
}
|
||||
if (raw) {
|
||||
return ChatColor.translateAlternateColorCodes('&', lore);
|
||||
} else {
|
||||
// we basically add the color codes then remove them
|
||||
// this is a dirty trick to remove color codes
|
||||
lore = ChatColor.translateAlternateColorCodes('&', lore);
|
||||
lore = ChatColor.stripColor(lore);
|
||||
if (lore.contains("{player}")) {
|
||||
lore = lore.replace("{player}", "");
|
||||
}
|
||||
if (lore.contains("{date}")) {
|
||||
lore = lore.replace("{date}", "");
|
||||
}
|
||||
if (lore.contains("{name}")) {
|
||||
lore = lore.replace("{name}", "");
|
||||
}
|
||||
if (lore.contains("{kills}")) {
|
||||
lore = lore.replace("{kills}", "");
|
||||
}
|
||||
if (lore.contains("{blocks}")) {
|
||||
lore = lore.replace("{blocks}", "");
|
||||
}
|
||||
if (lore.contains("{sheep}")) {
|
||||
lore = lore.replace("{sheep}", "");
|
||||
}
|
||||
if (lore.contains("{damage}")) {
|
||||
lore = lore.replace("{damage}", "");
|
||||
}
|
||||
if (lore.contains("{fish}")) {
|
||||
lore = lore.replace("{fish}", "");
|
||||
}
|
||||
}
|
||||
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,36 +18,260 @@
|
||||
package lol.hyper.toolstats.commands;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import org.bukkit.ChatColor;
|
||||
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.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class CommandToolStats implements CommandExecutor {
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (!sender.hasPermission("toolstats.use")) {
|
||||
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.");
|
||||
} else if (args.length == 1) {
|
||||
if (args[0].equalsIgnoreCase("reload")) {
|
||||
if (sender.isOp() || sender.hasPermission("toolstats.reload")) {
|
||||
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.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));
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.RED + "Invalid sub-command.");
|
||||
return true;
|
||||
}
|
||||
case "reset": {
|
||||
if (args.length == 2 && args[1].equalsIgnoreCase("confirm")) {
|
||||
Player player = (Player) sender;
|
||||
ItemStack heldItem = player.getInventory().getItemInMainHand();
|
||||
if (heldItem.getType() == Material.AIR) {
|
||||
audiences.sender(sender).sendMessage(Component.text("You must hold an item!").color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
fixItemLore(heldItem, player);
|
||||
audiences.sender(sender).sendMessage(Component.text("The lore was reset!").color(NamedTextColor.GREEN));
|
||||
return true;
|
||||
}
|
||||
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: {
|
||||
audiences.sender(sender).sendMessage(Component.text("Invalid sub-command.").color(NamedTextColor.RED));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private void fixItemLore(ItemStack original, Player player) {
|
||||
ItemStack finalItem = original.clone();
|
||||
ItemMeta finalMeta = finalItem.getItemMeta();
|
||||
if (finalMeta == null) {
|
||||
return;
|
||||
}
|
||||
PersistentDataContainer container = finalMeta.getPersistentDataContainer();
|
||||
List<String> lore = new ArrayList<>();
|
||||
|
||||
String caughtByLore = toolStats.getLoreFromConfig("fished.caught-by", false);
|
||||
String lootedByLore = toolStats.getLoreFromConfig("looted.found-by", false);
|
||||
String tradedByLore = toolStats.getLoreFromConfig("traded.traded-by", false);
|
||||
|
||||
// make sure the config messages are not null
|
||||
if (caughtByLore == null || lootedByLore == null || tradedByLore == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// determine how the item was originally created
|
||||
// this doesn't get saved, so we just rely on the lore
|
||||
// if there isn't a tag, default to crafted
|
||||
String type = "DEFAULT";
|
||||
if (finalMeta.hasLore()) {
|
||||
if (finalMeta.getLore() != null) {
|
||||
for (String line : finalMeta.getLore()) {
|
||||
if (line.contains(caughtByLore)) {
|
||||
type = "CAUGHT";
|
||||
}
|
||||
if (line.contains(lootedByLore)) {
|
||||
type = "LOOTED";
|
||||
}
|
||||
if (line.contains(tradedByLore)) {
|
||||
type = "TRADED";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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());
|
||||
// show how the item was created based on the previous lore
|
||||
switch (type) {
|
||||
case "DEFAULT": {
|
||||
lore.add(toolStats.getLoreFromConfig("created.created-by", true).replace("{player}", player.getName()));
|
||||
break;
|
||||
}
|
||||
case "CAUGHT": {
|
||||
lore.add(toolStats.getLoreFromConfig("fished.caught-by", true).replace("{player}", player.getName()));
|
||||
break;
|
||||
}
|
||||
case "LOOTED": {
|
||||
lore.add(toolStats.getLoreFromConfig("looted.found-by", true).replace("{player}", player.getName()));
|
||||
break;
|
||||
}
|
||||
case "TRADED": {
|
||||
lore.add(toolStats.getLoreFromConfig("traded.traded-by", true).replace("{player}", player.getName()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (toolStats.checkConfig(original, "created-date")) {
|
||||
if (container.has(toolStats.timeCreated, PersistentDataType.LONG)) {
|
||||
Long time = container.get(toolStats.timeCreated, PersistentDataType.LONG);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (toolStats.checkConfig(original, "player-kills")) {
|
||||
if (container.has(toolStats.swordPlayerKills, PersistentDataType.INTEGER)) {
|
||||
Integer kills = container.get(toolStats.swordPlayerKills, PersistentDataType.INTEGER);
|
||||
if (kills != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", toolStats.commaFormat.format(kills)));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (toolStats.checkConfig(original, "mob-kills")) {
|
||||
if (container.has(toolStats.swordMobKills, PersistentDataType.INTEGER)) {
|
||||
Integer kills = container.get(toolStats.swordMobKills, PersistentDataType.INTEGER);
|
||||
if (kills != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", toolStats.commaFormat.format(kills)));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (toolStats.checkConfig(original, "blocks-mined")) {
|
||||
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
|
||||
Integer blocksMined = container.get(toolStats.genericMined, PersistentDataType.INTEGER);
|
||||
if (blocksMined != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", toolStats.commaFormat.format(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) {
|
||||
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", toolStats.commaFormat.format(sheep)));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.armor-damage")) {
|
||||
if (container.has(toolStats.armorDamage, PersistentDataType.DOUBLE)) {
|
||||
Double damage = container.get(toolStats.armorDamage, PersistentDataType.DOUBLE);
|
||||
if (damage != null) {
|
||||
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", toolStats.commaFormat.format(damage)));
|
||||
}
|
||||
}
|
||||
}
|
||||
finalMeta.setLore(lore);
|
||||
finalItem.setItemMeta(finalMeta);
|
||||
int slot = player.getInventory().getHeldItemSlot();
|
||||
player.getInventory().setItem(slot, finalItem);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||
if (args.length == 1) {
|
||||
if (sender.hasPermission("toolstats.reload")) {
|
||||
return Arrays.asList("reset", "reload");
|
||||
} else {
|
||||
return Collections.singletonList("reset");
|
||||
}
|
||||
}
|
||||
if (args.length == 2) {
|
||||
if (args[0].equalsIgnoreCase("reset")) {
|
||||
return Collections.singletonList("confirm");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import org.bukkit.ChatColor;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -31,14 +31,11 @@ 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"};
|
||||
private final String blocksMinedLore = ChatColor.GRAY + "Blocks mined: " + ChatColor.DARK_GRAY + "X";
|
||||
|
||||
public BlocksMined(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
@@ -46,18 +43,23 @@ public class BlocksMined implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onBreak(BlockBreakEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (player.getGameMode() != GameMode.SURVIVAL) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
Player player = event.getPlayer();
|
||||
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());
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR) {
|
||||
return;
|
||||
}
|
||||
String itemName = heldItem.getType().toString().toLowerCase();
|
||||
if (Arrays.stream(validTools).noneMatch(itemName::contains)) {
|
||||
// only check certain items
|
||||
if (!ItemChecker.isMineTool(heldItem.getType())) {
|
||||
return;
|
||||
}
|
||||
// if it's an item we want, update the stats
|
||||
updateBlocksMined(heldItem);
|
||||
}
|
||||
|
||||
@@ -66,6 +68,8 @@ public class BlocksMined implements Listener {
|
||||
if (meta == null) {
|
||||
return;
|
||||
}
|
||||
// read the current stats from the item
|
||||
// if they don't exist, then start from 0
|
||||
Integer blocksMined = 0;
|
||||
PersistentDataContainer container = meta.getPersistentDataContainer();
|
||||
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
|
||||
@@ -77,6 +81,13 @@ public class BlocksMined implements Listener {
|
||||
blocksMined++;
|
||||
}
|
||||
container.set(toolStats.genericMined, PersistentDataType.INTEGER, blocksMined);
|
||||
String configLore = toolStats.getLoreFromConfig("blocks-mined", false);
|
||||
String configLoreRaw = toolStats.getLoreFromConfig("blocks-mined", true);
|
||||
|
||||
if (configLore == null || configLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.blocks-mined!");
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
@@ -86,21 +97,22 @@ public class BlocksMined implements Listener {
|
||||
// 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("Blocks mined")) {
|
||||
if (lore.get(x).contains(configLore)) {
|
||||
hasLore = true;
|
||||
lore.set(x, blocksMinedLore.replace("X", Integer.toString(blocksMined)));
|
||||
lore.set(x, configLoreRaw.replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(blocksMinedLore.replace("X", Integer.toString(blocksMined)));
|
||||
lore.add(configLoreRaw.replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(blocksMinedLore.replace("X", Integer.toString(blocksMined)));
|
||||
lore.add(configLoreRaw.replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
|
||||
}
|
||||
// do we add the lore based on the config?
|
||||
if (toolStats.checkConfig(itemStack, "blocks-mined")) {
|
||||
meta.setLore(lore);
|
||||
}
|
||||
|
||||
76
src/main/java/lol/hyper/toolstats/events/ChunkPopulate.java
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.Chunk;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.ItemFrame;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.world.ChunkPopulateEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
public class ChunkPopulate implements Listener {
|
||||
|
||||
// this tags all elytras with a "new" tag
|
||||
// this let's us track any new elytras player loot
|
||||
|
||||
private final ToolStats toolStats;
|
||||
|
||||
public ChunkPopulate(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPopulate(ChunkPopulateEvent event) {
|
||||
if (event.getChunk().getWorld().getEnvironment() != World.Environment.THE_END) {
|
||||
return;
|
||||
}
|
||||
// this is delayed because entities are not loaded instantly
|
||||
// we just check 1 second later
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> {
|
||||
Chunk chunk = event.getChunk();
|
||||
for (Entity entity : chunk.getEntities()) {
|
||||
// if there is a new item frame
|
||||
if (entity instanceof ItemFrame) {
|
||||
ItemFrame itemFrame = (ItemFrame) entity;
|
||||
// if the item frame has an elytra
|
||||
if (itemFrame.getItem().getType() == Material.ELYTRA) {
|
||||
ItemStack elytraCopy = itemFrame.getItem();
|
||||
ItemMeta meta = elytraCopy.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
}
|
||||
// add the new tag so we know it's new
|
||||
PersistentDataContainer container = meta.getPersistentDataContainer();
|
||||
container.set(toolStats.newElytra, PersistentDataType.INTEGER, 1);
|
||||
elytraCopy.setItemMeta(meta);
|
||||
itemFrame.setItem(elytraCopy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 20);
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,10 @@
|
||||
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;
|
||||
@@ -30,18 +32,11 @@ 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 String timeCreatedLore = ChatColor.GRAY + "Crafted on: " + ChatColor.DARK_GRAY + "X";
|
||||
private final String ownerLore = ChatColor.GRAY + "Crafted by: " + ChatColor.DARK_GRAY + "X";
|
||||
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
|
||||
public CraftItem(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
@@ -49,42 +44,93 @@ public class CraftItem implements Listener {
|
||||
|
||||
@EventHandler
|
||||
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);
|
||||
for (String x : validItems) {
|
||||
if (name.contains(x)) {
|
||||
event.setCurrentItem(addLore(itemStack, player));
|
||||
// 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
|
||||
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.
|
||||
* @return A copy of the item with the tags + lore.
|
||||
*/
|
||||
private ItemStack addLore(ItemStack itemStack, Player owner) {
|
||||
// clone the item
|
||||
ItemStack newItem = itemStack.clone();
|
||||
ItemMeta meta = newItem.getItemMeta();
|
||||
if (meta == null) {
|
||||
return null;
|
||||
}
|
||||
// get the current time
|
||||
long timeCreated = System.currentTimeMillis();
|
||||
Date finalDate = new Date(timeCreated);
|
||||
PersistentDataContainer container = meta.getPersistentDataContainer();
|
||||
container.set(toolStats.craftedTime, PersistentDataType.LONG, timeCreated);
|
||||
container.set(toolStats.craftedOwner, new UUIDDataType(), owner.getUniqueId());
|
||||
|
||||
// if the item already has the tag
|
||||
// this is to prevent duplicate tags
|
||||
if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.genericOwner, PersistentDataType.LONG)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
|
||||
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
|
||||
|
||||
String createdByRaw = toolStats.getLoreFromConfig("created.created-by", true);
|
||||
String createdOnRaw = toolStats.getLoreFromConfig("created.created-on", true);
|
||||
|
||||
if (createdOnRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.created.created-on!");
|
||||
return null;
|
||||
}
|
||||
if (createdByRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.created.created-by!");
|
||||
return null;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
// get the current lore the item
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
if (toolStats.checkConfig(itemStack, "crafted-date")) {
|
||||
lore.add(timeCreatedLore.replace("X", format.format(finalDate)));
|
||||
// do we add the lore based on the config?
|
||||
if (toolStats.checkConfig(itemStack, "created-date")) {
|
||||
lore.add(createdOnRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
}
|
||||
if (toolStats.checkConfig(itemStack, "crafted-by")) {
|
||||
lore.add(ownerLore.replace("X", owner.getName()));
|
||||
if (toolStats.checkConfig(itemStack, "created-by")) {
|
||||
lore.add(createdByRaw.replace("{player}", owner.getName()));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
newItem.setItemMeta(meta);
|
||||
|
||||
@@ -18,12 +18,10 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import org.bukkit.ChatColor;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Trident;
|
||||
import org.bukkit.entity.*;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDamageByBlockEvent;
|
||||
@@ -35,18 +33,11 @@ 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 String playerKillsLore = ChatColor.GRAY + "Player kills: " + ChatColor.DARK_GRAY + "X";
|
||||
private final String mobKillsLore = ChatColor.GRAY + "Mob kills: " + ChatColor.DARK_GRAY + "X";
|
||||
private final String damageTakenLore = ChatColor.GRAY + "Damage taken: " + ChatColor.DARK_GRAY + "X";
|
||||
private final DecimalFormat decimalFormat = new DecimalFormat("0.00");
|
||||
public final Set<UUID> trackedMobs = new HashSet<>();
|
||||
|
||||
public EntityDamage(ToolStats toolStats) {
|
||||
@@ -55,21 +46,28 @@ public class EntityDamage implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onDamage(EntityDamageByEntityEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
return;
|
||||
}
|
||||
LivingEntity livingEntity = (LivingEntity) event.getEntity();
|
||||
// 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
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR) {
|
||||
return;
|
||||
}
|
||||
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
|
||||
@@ -81,9 +79,11 @@ public class EntityDamage implements Listener {
|
||||
player.getInventory().setItem(player.getInventory().getHeldItemSlot(), updateMobKills(heldItem));
|
||||
trackedMobs.add(livingEntity.getUniqueId());
|
||||
}
|
||||
// trident is being thrown at something
|
||||
if (event.getDamager() instanceof Trident) {
|
||||
Trident trident = (Trident) event.getDamager();
|
||||
ItemStack clone;
|
||||
// trident is killing player
|
||||
if (livingEntity instanceof Player) {
|
||||
clone = updatePlayerKills(trident.getItem());
|
||||
} else {
|
||||
@@ -94,14 +94,43 @@ public class EntityDamage implements Listener {
|
||||
}
|
||||
trident.setItem(clone);
|
||||
}
|
||||
// arrow is being shot
|
||||
if (event.getDamager() instanceof Arrow) {
|
||||
Arrow arrow = (Arrow) event.getDamager();
|
||||
// if the shooter is a player
|
||||
if (arrow.getShooter() instanceof Player) {
|
||||
Player player = (Player) arrow.getShooter();
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
if (heldItem == null) {
|
||||
return;
|
||||
}
|
||||
// if the player is holding the bow/crossbow
|
||||
// if they switch then oh well
|
||||
if (heldItem.getType() == Material.BOW || heldItem.getType() == Material.CROSSBOW) {
|
||||
if (livingEntity instanceof Player) {
|
||||
player.getInventory().setItem(player.getInventory().getHeldItemSlot(), updatePlayerKills(heldItem));
|
||||
} else {
|
||||
player.getInventory().setItem(player.getInventory().getHeldItemSlot(), updateMobKills(heldItem));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// player is 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,13 +138,22 @@ public class EntityDamage implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onDamage(EntityDamageEvent event) {
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,18 +161,33 @@ public class EntityDamage implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onDamage(EntityDamageByBlockEvent event) {
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a weapon's player kills.
|
||||
*
|
||||
* @param itemStack The item to update.
|
||||
* @return A copy of the item.
|
||||
*/
|
||||
private ItemStack updatePlayerKills(ItemStack itemStack) {
|
||||
ItemStack finalItem = itemStack.clone();
|
||||
ItemMeta meta = finalItem.getItemMeta();
|
||||
@@ -153,6 +206,14 @@ public class EntityDamage implements Listener {
|
||||
}
|
||||
container.set(toolStats.swordPlayerKills, PersistentDataType.INTEGER, playerKills);
|
||||
|
||||
String playerKillsLore = toolStats.getLoreFromConfig("kills.player", false);
|
||||
String playerKillsLoreRaw = toolStats.getLoreFromConfig("kills.player", true);
|
||||
|
||||
if (playerKillsLore == null || playerKillsLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.kills.player!");
|
||||
return null;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
@@ -161,21 +222,22 @@ public class EntityDamage implements Listener {
|
||||
// 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("Player kills")) {
|
||||
if (lore.get(x).contains(playerKillsLore)) {
|
||||
hasLore = true;
|
||||
lore.set(x, playerKillsLore.replace("X", Integer.toString(playerKills)));
|
||||
lore.set(x, playerKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(playerKills)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(playerKillsLore.replace("X", Integer.toString(playerKills)));
|
||||
lore.add(playerKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(playerKills)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(playerKillsLore.replace("X", Integer.toString(playerKills)));
|
||||
lore.add(playerKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(playerKills)));
|
||||
}
|
||||
// do we add the lore based on the config?
|
||||
if (toolStats.checkConfig(itemStack, "player-kills")) {
|
||||
meta.setLore(lore);
|
||||
}
|
||||
@@ -183,6 +245,12 @@ public class EntityDamage implements Listener {
|
||||
return finalItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a weapon's mob kills.
|
||||
*
|
||||
* @param itemStack The item to update.
|
||||
* @return A copy of the item.
|
||||
*/
|
||||
private ItemStack updateMobKills(ItemStack itemStack) {
|
||||
ItemStack finalItem = itemStack.clone();
|
||||
ItemMeta meta = finalItem.getItemMeta();
|
||||
@@ -201,6 +269,14 @@ public class EntityDamage implements Listener {
|
||||
}
|
||||
container.set(toolStats.swordMobKills, PersistentDataType.INTEGER, mobKills);
|
||||
|
||||
String mobKillsLore = toolStats.getLoreFromConfig("kills.mob", false);
|
||||
String mobKillsLoreRaw = toolStats.getLoreFromConfig("kills.mob", true);
|
||||
|
||||
if (mobKillsLore == null || mobKillsLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.kills.mob!");
|
||||
return null;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
@@ -209,21 +285,22 @@ public class EntityDamage implements Listener {
|
||||
// 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("Mob kills")) {
|
||||
if (lore.get(x).contains(mobKillsLore)) {
|
||||
hasLore = true;
|
||||
lore.set(x, mobKillsLore.replace("X", Integer.toString(mobKills)));
|
||||
lore.set(x, mobKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(mobKills)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(mobKillsLore.replace("X", Integer.toString(mobKills)));
|
||||
lore.add(mobKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(mobKills)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(mobKillsLore.replace("X", Integer.toString(mobKills)));
|
||||
lore.add(mobKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(mobKills)));
|
||||
}
|
||||
// do we add the lore based on the config?
|
||||
if (toolStats.checkConfig(itemStack, "mob-kills")) {
|
||||
meta.setLore(lore);
|
||||
}
|
||||
@@ -231,6 +308,12 @@ public class EntityDamage implements Listener {
|
||||
return finalItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a player's armor damage stats.
|
||||
*
|
||||
* @param itemStack The armor piece.
|
||||
* @param damage How much damage is being added.
|
||||
*/
|
||||
private void updateArmorDamage(ItemStack itemStack, double damage) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta == null) {
|
||||
@@ -246,9 +329,16 @@ public class EntityDamage implements Listener {
|
||||
} else {
|
||||
damageTaken = damageTaken + damage;
|
||||
}
|
||||
decimalFormat.setRoundingMode(RoundingMode.DOWN);
|
||||
container.set(toolStats.armorDamage, PersistentDataType.DOUBLE, damageTaken);
|
||||
|
||||
String damageTakenLore = toolStats.getLoreFromConfig("damage-taken", false);
|
||||
String damageTakenLoreRaw = toolStats.getLoreFromConfig("damage-taken", true);
|
||||
|
||||
if (damageTakenLore == null || damageTakenLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.damage-taken!");
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
@@ -257,20 +347,20 @@ public class EntityDamage implements Listener {
|
||||
// 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("Damage taken")) {
|
||||
if (lore.get(x).contains(damageTakenLore)) {
|
||||
hasLore = true;
|
||||
lore.set(x, damageTakenLore.replace("X", decimalFormat.format(damageTaken)));
|
||||
lore.set(x, damageTakenLoreRaw.replace("{damage}", toolStats.decimalFormat.format(damageTaken)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(damageTakenLore.replace("X", decimalFormat.format(damageTaken)));
|
||||
lore.add(damageTakenLoreRaw.replace("{damage}", toolStats.decimalFormat.format(damageTaken)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(damageTakenLore.replace("X", decimalFormat.format(damageTaken)));
|
||||
lore.add(damageTakenLoreRaw.replace("{damage}", toolStats.decimalFormat.format(damageTaken)));
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.armor-damage")) {
|
||||
meta.setLore(lore);
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import org.bukkit.ChatColor;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -32,7 +32,6 @@ import java.util.*;
|
||||
public class EntityDeath implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
private final String droppedLore = ChatColor.GRAY + "Dropped by: " + ChatColor.DARK_GRAY + "X";
|
||||
|
||||
public EntityDeath(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
@@ -45,33 +44,46 @@ public class EntityDeath implements Listener {
|
||||
return;
|
||||
}
|
||||
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());
|
||||
}
|
||||
if (ItemChecker.isValidItem(current.getType())) {
|
||||
addLore(current, livingEntity.getName());
|
||||
}
|
||||
}
|
||||
toolStats.mobKill.trackedMobs.remove(livingEntityUUID);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds "drop by" tag to item.
|
||||
*
|
||||
* @param itemStack The item to add lore to.
|
||||
* @param mob The mob or player name.
|
||||
*/
|
||||
private void addLore(ItemStack itemStack, String mob) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
}
|
||||
boolean hasTag = false;
|
||||
|
||||
String droppedByLore = toolStats.getLoreFromConfig("dropped-by", false);
|
||||
String droppedByLoreRaw = toolStats.getLoreFromConfig("dropped-by", true);
|
||||
|
||||
if (droppedByLore == null || droppedByLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.dropped-by!");
|
||||
return;
|
||||
}
|
||||
|
||||
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("Dropped by")) {
|
||||
if (lore.get(x).contains(droppedByLore)) {
|
||||
// replace existing tag
|
||||
lore.set(x, droppedLore.replace("X", mob));
|
||||
lore.set(x, droppedByLoreRaw.replace("{name}", mob));
|
||||
hasTag = true;
|
||||
}
|
||||
}
|
||||
@@ -80,7 +92,7 @@ public class EntityDeath implements Listener {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
if (!hasTag) {
|
||||
lore.add(droppedLore.replace("X", mob));
|
||||
lore.add(droppedByLoreRaw.replace("{name}", mob));
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.dropped-by")) {
|
||||
meta.setLore(lore);
|
||||
|
||||
170
src/main/java/lol/hyper/toolstats/events/GenerateLoot.java
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* 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 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.Listener;
|
||||
import org.bukkit.event.world.LootGenerateEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class GenerateLoot implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
|
||||
public GenerateLoot(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onGenerateLoot(LootGenerateEvent event) {
|
||||
InventoryHolder inventoryHolder = event.getInventoryHolder();
|
||||
if (inventoryHolder == null) {
|
||||
return;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
// 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.
|
||||
* @return The item with the lore.
|
||||
*/
|
||||
private ItemStack addLore(ItemStack itemStack, Player owner) {
|
||||
ItemStack newItem = itemStack.clone();
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta == null) {
|
||||
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 null;
|
||||
}
|
||||
|
||||
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
|
||||
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
|
||||
|
||||
String foundByLoreRaw = toolStats.getLoreFromConfig("looted.found-by", true);
|
||||
String foundOnLoreRaw = toolStats.getLoreFromConfig("looted.found-on", true);
|
||||
|
||||
if (foundByLoreRaw == null || foundOnLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.looted!");
|
||||
return null;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
if (toolStats.checkConfig(newItem, "looted-tag")) {
|
||||
lore.add(foundOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
newItem.setItemMeta(meta);
|
||||
return newItem;
|
||||
}
|
||||
}
|
||||
119
src/main/java/lol/hyper/toolstats/events/PickupItem.java
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* 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 lol.hyper.toolstats.tools.UUIDDataType;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.*;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityPickupItemEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class PickupItem implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
|
||||
public PickupItem(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPickup(EntityPickupItemEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
Entity entity = event.getEntity();
|
||||
if (entity instanceof Player) {
|
||||
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();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds "looted by" tags for elytras.
|
||||
*
|
||||
* @param itemStack The elytra to add lore to.
|
||||
* @param owner The player who found it.
|
||||
*/
|
||||
private ItemStack addLore(ItemStack itemStack, Player owner) {
|
||||
ItemStack finalItem = itemStack.clone();
|
||||
ItemMeta meta = finalItem.getItemMeta();
|
||||
if (meta == null) {
|
||||
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 null;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.elytra-tag")) {
|
||||
lore.add(foundOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
finalItem.setItemMeta(meta);
|
||||
return finalItem;
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,11 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import org.bukkit.ChatColor;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
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.Listener;
|
||||
@@ -30,12 +33,12 @@ import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class PlayerFish implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
private final String fishCaughtLore = ChatColor.GRAY + "Fish caught: " + ChatColor.DARK_GRAY + "X";
|
||||
|
||||
public PlayerFish(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
@@ -43,20 +46,38 @@ public class PlayerFish implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onFish(PlayerFishEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
// only listen to when a player catches a fish
|
||||
if (event.getState() != PlayerFishEvent.State.CAUGHT_FISH) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = event.getPlayer();
|
||||
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||
return;
|
||||
}
|
||||
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR || heldItem.getType() != Material.FISHING_ROD) {
|
||||
return;
|
||||
}
|
||||
addLore(heldItem);
|
||||
updateFishCount(heldItem);
|
||||
if (event.getCaught() == null) {
|
||||
return;
|
||||
}
|
||||
ItemStack caughtItem = ((Item) event.getCaught()).getItemStack();
|
||||
if (ItemChecker.isValidItem(caughtItem.getType())) {
|
||||
addNewLore(caughtItem, player);
|
||||
}
|
||||
}
|
||||
|
||||
private void addLore(ItemStack itemStack) {
|
||||
/**
|
||||
* Updates a fishing rod's count.
|
||||
*
|
||||
* @param itemStack The fishing rod to update.
|
||||
*/
|
||||
private void updateFishCount(ItemStack itemStack) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
@@ -72,6 +93,15 @@ public class PlayerFish implements Listener {
|
||||
fishCaught++;
|
||||
}
|
||||
container.set(toolStats.fishingRodCaught, PersistentDataType.INTEGER, fishCaught);
|
||||
|
||||
String fishCaughtLore = toolStats.getLoreFromConfig("fished.fish-caught", false);
|
||||
String fishCaughtLoreRaw = toolStats.getLoreFromConfig("fished.fish-caught", true);
|
||||
|
||||
if (fishCaughtLore == null || fishCaughtLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.fish-caught!");
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
@@ -80,24 +110,69 @@ public class PlayerFish implements Listener {
|
||||
// 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("Fish caught")) {
|
||||
if (lore.get(x).contains(fishCaughtLore)) {
|
||||
hasLore = true;
|
||||
lore.set(x, fishCaughtLore.replace("X", Integer.toString(fishCaught)));
|
||||
lore.set(x, fishCaughtLoreRaw.replace("{fish}", toolStats.commaFormat.format(fishCaught)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(fishCaughtLore.replace("X", Integer.toString(fishCaught)));
|
||||
lore.add(fishCaughtLoreRaw.replace("{fish}", toolStats.commaFormat.format(fishCaught)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(fishCaughtLore.replace("X", Integer.toString(fishCaught)));
|
||||
lore.add(fishCaughtLoreRaw.replace("{fish}", toolStats.commaFormat.format(fishCaught)));
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.fish-caught")) {
|
||||
meta.setLore(lore);
|
||||
}
|
||||
itemStack.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.
|
||||
*/
|
||||
private void addNewLore(ItemStack itemStack, Player owner) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
|
||||
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
|
||||
|
||||
String caughtByLoreRaw = toolStats.getLoreFromConfig("fished.caught-by", true);
|
||||
String caughtOnLoreRaw = toolStats.getLoreFromConfig("fished.caught-on", true);
|
||||
|
||||
if (caughtByLoreRaw == null || caughtOnLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.fished!");
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
if (toolStats.checkConfig(itemStack, "fished-tag")) {
|
||||
lore.add(caughtOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(caughtByLoreRaw.replace("{player}", owner.getName()));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
itemStack.setItemMeta(meta);
|
||||
}
|
||||
}
|
||||
|
||||
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,7 +18,7 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -37,7 +37,6 @@ import java.util.List;
|
||||
public class SheepShear implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
private final String sheepShearLore = ChatColor.GRAY + "Sheep sheared: " + ChatColor.DARK_GRAY + "X";
|
||||
|
||||
public SheepShear(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
@@ -45,22 +44,35 @@ public class SheepShear implements Listener {
|
||||
|
||||
@EventHandler
|
||||
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());
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR || heldItem.getType() != Material.SHEARS) {
|
||||
return;
|
||||
}
|
||||
|
||||
Sheep sheep = (Sheep) entity;
|
||||
// make sure the sheep is not sheared
|
||||
if (!sheep.isSheared()) {
|
||||
addLore(heldItem);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds tags to shears.
|
||||
*
|
||||
* @param itemStack The shears.
|
||||
*/
|
||||
private void addLore(ItemStack itemStack) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta == null) {
|
||||
@@ -77,6 +89,15 @@ public class SheepShear implements Listener {
|
||||
sheepSheared++;
|
||||
}
|
||||
container.set(toolStats.shearsSheared, PersistentDataType.INTEGER, sheepSheared);
|
||||
|
||||
String sheepShearedLore = toolStats.getLoreFromConfig("sheep-sheared", false);
|
||||
String sheepShearedLoreRaw = toolStats.getLoreFromConfig("sheep-sheared", true);
|
||||
|
||||
if (sheepShearedLore == null || sheepShearedLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.sheep-sheared!");
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
@@ -85,20 +106,20 @@ public class SheepShear implements Listener {
|
||||
// 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("Sheep sheared")) {
|
||||
if (lore.get(x).contains(sheepShearedLore)) {
|
||||
hasLore = true;
|
||||
lore.set(x, sheepShearLore.replace("X", Integer.toString(sheepSheared)));
|
||||
lore.set(x, sheepShearedLoreRaw.replace("{sheep}", toolStats.commaFormat.format(sheepSheared)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if the item has lore but doesn't have the tag, add it
|
||||
if (!hasLore) {
|
||||
lore.add(sheepShearLore.replace("X", Integer.toString(sheepSheared)));
|
||||
lore.add(sheepShearedLoreRaw.replace("{sheep}", toolStats.commaFormat.format(sheepSheared)));
|
||||
}
|
||||
} else {
|
||||
// if the item has no lore, create a new list and add the string
|
||||
lore = new ArrayList<>();
|
||||
lore.add(sheepShearLore.replace("X", Integer.toString(sheepSheared)));
|
||||
lore.add(sheepShearedLoreRaw.replace("{sheep}", toolStats.commaFormat.format(sheepSheared)));
|
||||
}
|
||||
if (toolStats.config.getBoolean("enabled.sheep-sheared")) {
|
||||
meta.setLore(lore);
|
||||
|
||||
134
src/main/java/lol/hyper/toolstats/events/VillagerTrade.java
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* 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 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.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.MerchantInventory;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class VillagerTrade implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
|
||||
public VillagerTrade(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
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
|
||||
if (ItemChecker.isValidItem(item.getType())) {
|
||||
// if the player shift clicks, show the warning
|
||||
if (event.isShiftClick()) {
|
||||
String configMessage = toolStats.config.getString("messages.shift-click-warning.trading");
|
||||
if (configMessage != null) {
|
||||
event.getWhoClicked().sendMessage(ChatColor.translateAlternateColorCodes('&', configMessage));
|
||||
}
|
||||
}
|
||||
ItemStack newItem = addLore(item, player);
|
||||
if (newItem != null) {
|
||||
// this gets delayed since villager inventories suck for no reason
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> event.setCurrentItem(newItem), 5);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds "traded by" tags to item.
|
||||
*
|
||||
* @param itemStack 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();
|
||||
if (meta == null) {
|
||||
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 null;
|
||||
}
|
||||
|
||||
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
|
||||
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
|
||||
|
||||
String tradedByLoreRaw = toolStats.getLoreFromConfig("traded.traded-by", true);
|
||||
String tradedOnLoreRaw = toolStats.getLoreFromConfig("traded.traded-on", true);
|
||||
|
||||
if (tradedByLoreRaw == null || tradedOnLoreRaw == null) {
|
||||
toolStats.logger.warning("There is no lore message for messages.traded!");
|
||||
return null;
|
||||
}
|
||||
|
||||
List<String> lore;
|
||||
if (meta.hasLore()) {
|
||||
lore = meta.getLore();
|
||||
assert lore != null;
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
if (toolStats.checkConfig(itemStack, "traded-tag")) {
|
||||
lore.add(tradedOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
|
||||
lore.add(tradedByLoreRaw.replace("{player}", owner.getName()));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
itemStack.setItemMeta(meta);
|
||||
return itemStack;
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
@@ -1,5 +1,6 @@
|
||||
enabled:
|
||||
crafted-by:
|
||||
# Will show ownership of items when they are created/found.
|
||||
created-by:
|
||||
pickaxe: true
|
||||
sword: true
|
||||
shovel: true
|
||||
@@ -8,7 +9,38 @@ enabled:
|
||||
shears: true
|
||||
bow: true
|
||||
armor: true
|
||||
crafted-date:
|
||||
# Will show time the item is created
|
||||
created-date:
|
||||
pickaxe: true
|
||||
sword: true
|
||||
shovel: true
|
||||
axe: true
|
||||
hoe: true
|
||||
shears: true
|
||||
bow: true
|
||||
armor: true
|
||||
# Will show "Fished by <player>"
|
||||
fished-tag:
|
||||
pickaxe: true
|
||||
sword: true
|
||||
shovel: true
|
||||
axe: true
|
||||
hoe: true
|
||||
shears: true
|
||||
bow: true
|
||||
armor: true
|
||||
# Will show "Found by <player>"
|
||||
looted-tag:
|
||||
pickaxe: true
|
||||
sword: true
|
||||
shovel: true
|
||||
axe: true
|
||||
hoe: true
|
||||
shears: true
|
||||
bow: true
|
||||
armor: true
|
||||
# Will show "Trade by <player>"
|
||||
traded-tag:
|
||||
pickaxe: true
|
||||
sword: true
|
||||
shovel: true
|
||||
@@ -21,10 +53,12 @@ enabled:
|
||||
sword: true
|
||||
axe: true
|
||||
trident: true
|
||||
bow: true
|
||||
mob-kills:
|
||||
sword: true
|
||||
axe: true
|
||||
trident: true
|
||||
bow: true
|
||||
blocks-mined:
|
||||
pickaxe: true
|
||||
shovel: true
|
||||
@@ -35,5 +69,33 @@ enabled:
|
||||
sheep-sheared: true
|
||||
armor-damage: true
|
||||
dropped-by: true
|
||||
elytra-tag: true
|
||||
|
||||
config-version: 1
|
||||
messages:
|
||||
created:
|
||||
created-by: "&7Crafted by: &8{player}"
|
||||
created-on: "&7Crafted on: &8{date}"
|
||||
fished:
|
||||
caught-by: "&7Caught by: &8{player}"
|
||||
caught-on: "&7Caught on: &8{date}"
|
||||
fish-caught: "&7Fish caught: &8{fish}"
|
||||
looted:
|
||||
found-by: "&7Found by: &8{player}"
|
||||
found-on: "&7Found on: &8{date}"
|
||||
traded:
|
||||
traded-by: "&7Traded by: &8{player}"
|
||||
traded-on: "&7Traded on: &8{date}"
|
||||
kills:
|
||||
mob: "&7Mob kills: &8{kills}"
|
||||
player: "&7Player kills: &8{kills}"
|
||||
blocks-mined: "&7Blocks mined: &8{blocks}"
|
||||
sheep-sheared: "&7Sheep sheared: &8{sheep}"
|
||||
dropped-by: "&7Dropped by: &8{name}" # name will be player/mob name
|
||||
damage-taken: "&7Damage taken: &8{damage}"
|
||||
# Display this message if the player shift click trades/crafts items. It's not really easy to get every single item
|
||||
# that is crafted. The tag will only be added to the first item. If you don't want this message, simply replace them both with ""
|
||||
shift-click-warning:
|
||||
crafting: "&cCrafting items via shift clicking does not fully apply tags to each item. This is a limitation with the Bukkit API."
|
||||
trading: "&cTrading items via shift clicking does not fully apply tags to each item. This is a limitation with the Bukkit API."
|
||||
|
||||
config-version: 3
|
||||
@@ -1,15 +1,18 @@
|
||||
name: ToolStats
|
||||
version: '${project.version}'
|
||||
main: lol.hyper.toolstats.ToolStats
|
||||
api-version: 1.18
|
||||
api-version: 1.15
|
||||
author: hyperdefined
|
||||
description: Track various tool stats!
|
||||
commands:
|
||||
toolstats:
|
||||
usage: /toolstats
|
||||
description: Main command.
|
||||
permission: randomenchant.reload
|
||||
permission: toolstats.main
|
||||
permissions:
|
||||
randomenchant.reload:
|
||||
description: Allows the usage of /randomenchant reload
|
||||
toolstats.main:
|
||||
description: Allows the usage of /toolstats
|
||||
default: true
|
||||
toolstats.reload:
|
||||
description: Allows the usage of /toolstats reload
|
||||
default: op
|
||||