add "arrows shot" + fix jank

This commit is contained in:
hyperdefined
2023-10-22 19:13:44 -04:00
parent 38c37ea601
commit c328b9e019
5 changed files with 139 additions and 6 deletions

View File

@@ -34,6 +34,7 @@ import space.arim.morepaperlib.MorePaperLib;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.regex.Pattern;
public final class ToolStats extends JavaPlugin {
@@ -85,6 +86,10 @@ public final class ToolStats extends JavaPlugin {
* Key for item has.
*/
public final NamespacedKey hash = new NamespacedKey(this, "hash");
/**
* Key for arrows shot.
*/
public final NamespacedKey arrowsShot = new NamespacedKey(this, "arrows-shot");
/**
* Stores how an item was created.
* 0 = crafted.
@@ -100,6 +105,7 @@ public final class ToolStats extends JavaPlugin {
public final int CONFIG_VERSION = 6;
public final Logger logger = this.getLogger();
public final File configFile = new File(this.getDataFolder(), "config.yml");
private final Pattern COLOR_CODES = Pattern.compile("(?i)&[0-9A-FK-ORX]");
public BlocksMined blocksMined;
public ChunkPopulate chunkPopulate;
@@ -123,6 +129,7 @@ public final class ToolStats extends JavaPlugin {
public HashMaker hashMaker;
public CreativeEvent creativeEvent;
public ItemChecker itemChecker;
public ShootBow shootBow;
@Override
public void onEnable() {
@@ -151,6 +158,7 @@ public final class ToolStats extends JavaPlugin {
playerJoin = new PlayerJoin(this);
creativeEvent = new CreativeEvent(this);
itemChecker = new ItemChecker();
shootBow = new ShootBow(this);
Bukkit.getServer().getPluginManager().registerEvents(blocksMined, this);
Bukkit.getServer().getPluginManager().registerEvents(chunkPopulate, this);
@@ -166,6 +174,7 @@ public final class ToolStats extends JavaPlugin {
Bukkit.getServer().getPluginManager().registerEvents(inventoryOpen, this);
Bukkit.getServer().getPluginManager().registerEvents(playerJoin, this);
Bukkit.getServer().getPluginManager().registerEvents(creativeEvent, this);
Bukkit.getServer().getPluginManager().registerEvents(shootBow, this);
this.getCommand("toolstats").setExecutor(commandToolStats);
@@ -224,7 +233,7 @@ public final class ToolStats extends JavaPlugin {
String itemName = material.toString().toLowerCase();
String itemType = null;
// hardcode these
if (material == Material.BOW || material == Material.SHEARS || material == Material.TRIDENT) {
if (material == Material.BOW || material == Material.CROSSBOW || material == Material.SHEARS || material == Material.TRIDENT) {
if (material == Material.BOW) {
itemType = "bow";
}
@@ -234,6 +243,9 @@ public final class ToolStats extends JavaPlugin {
if (material == Material.TRIDENT) {
itemType = "trident";
}
if (material == Material.CROSSBOW) {
itemType = "crossbow";
}
} else {
itemType = itemName.substring(itemName.indexOf('_') + 1);
}
@@ -257,6 +269,7 @@ public final class ToolStats extends JavaPlugin {
case "shears": {
return config.getBoolean("enabled." + configName + ".shears");
}
case "crossbow":
case "bow": {
return config.getBoolean("enabled." + configName + ".bow");
}
@@ -288,10 +301,11 @@ public final class ToolStats extends JavaPlugin {
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);
// remove all color codes
// this is used to compare the current lore on the item
// Example: [§7Arrows shot: §8] is on the lore
// this will return [Arrows shot: ] so we can match it
lore = COLOR_CODES.matcher(lore).replaceAll("");
if (lore.contains("{player}")) {
lore = lore.replace("{player}", "");
}
@@ -319,6 +333,9 @@ public final class ToolStats extends JavaPlugin {
if (lore.contains("{crops}")) {
lore = lore.replace("{crops}", "");
}
if (lore.contains("{arrows}")) {
lore = lore.replace("{arrows}", "");
}
}
return lore;
}

View File

@@ -121,7 +121,7 @@ public class EntityDamage implements Listener {
heldBow = inventory.getItemInOffHand();
}
// if the player is hold a bow in both hands
// if the player is holding a bow in both hands
// default to main hand since that takes priority
if (isMainHand && isOffHand) {
heldBow = inventory.getItemInMainHand();

View File

@@ -0,0 +1,113 @@
/*
* 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.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.List;
public class ShootBow implements Listener {
private ToolStats toolStats;
public ShootBow(ToolStats toolStats) {
this.toolStats = toolStats;
}
@EventHandler(priority = EventPriority.MONITOR)
public void onShoot(EntityShootBowEvent event) {
Entity shooter = event.getEntity();
// only listen for players
if (!(shooter instanceof Player)) {
return;
}
Player player = (Player) shooter;
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.ADVENTURE) {
return;
}
PlayerInventory inventory = player.getInventory();
boolean isMainHand = inventory.getItemInMainHand().getType() == Material.BOW || inventory.getItemInMainHand().getType() == Material.CROSSBOW;
boolean isOffHand = inventory.getItemInOffHand().getType() == Material.BOW || inventory.getItemInMainHand().getType() == Material.CROSSBOW;
ItemStack heldBow = null;
if (isMainHand) {
heldBow = inventory.getItemInMainHand();
}
if (isOffHand) {
heldBow = inventory.getItemInOffHand();
}
// if the player is holding a bow in both hands
// default to main hand since that takes priority
if (isMainHand && isOffHand) {
heldBow = inventory.getItemInMainHand();
}
// player swapped
if (heldBow == null) {
return;
}
updateArrowsShot(heldBow);
}
private void updateArrowsShot(ItemStack bow) {
ItemMeta meta = bow.getItemMeta();
if (meta == null) {
toolStats.logger.warning(bow + " does NOT have any meta! Unable to update stats.");
return;
}
// read the current stats from the item
// if they don't exist, then start from 0
Integer arrowsShot = 0;
PersistentDataContainer container = meta.getPersistentDataContainer();
if (container.has(toolStats.arrowsShot, PersistentDataType.INTEGER)) {
arrowsShot = container.get(toolStats.arrowsShot, PersistentDataType.INTEGER);
}
if (arrowsShot == null) {
arrowsShot = 0;
toolStats.logger.warning(arrowsShot + " does not have valid arrows-shot set! Resting to zero. This should NEVER happen.");
}
arrowsShot++;
container.set(toolStats.arrowsShot, PersistentDataType.INTEGER, arrowsShot);
// do we add the lore based on the config?
if (toolStats.config.getBoolean("enabled.arrows-shot")) {
String arrowsShotFormatted = toolStats.numberFormat.formatInt(arrowsShot);
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{arrows}", arrowsShotFormatted, "arrows-shot");
meta.setLore(newLore);
}
bow.setItemMeta(meta);
}
}

View File

@@ -54,6 +54,7 @@ public class ItemChecker {
meleeItems.add(Material.TRIDENT);
validItems.add(Material.BOW);
validItems.add(Material.FISHING_ROD);
validItems.add(Material.CROSSBOW);
// combine the lists
validItems.addAll(armorItems);

View File

@@ -80,6 +80,7 @@ enabled:
armor-damage: true
dropped-by: true
elytra-tag: true
arrows-shot: true
messages:
created:
@@ -108,6 +109,7 @@ messages:
sheep-sheared: "&7Sheep sheared: &8{sheep}"
dropped-by: "&7Dropped by: &8{name}" # name will be player/mob name
damage-taken: "&7Damage taken: &8{damage}"
arrows-shot: "&7Arrows shot: &8{arrows}"
# 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: