Compare commits

..

15 Commits

Author SHA1 Message Date
hyperdefined
3d850ef00c Create FUNDING.yml 2025-03-31 19:39:35 -04:00
hyperdefined
dfd2ef7d7b added missing entries 2025-03-31 19:27:03 -04:00
hyperdefined
957d4d95cb Create damage-done.png 2025-03-31 19:24:57 -04:00
hyperdefined
2143daa133 cleanup 2025-03-31 19:21:09 -04:00
hyperdefined
be8fcc2d57 Update TokenCrafting.java 2025-03-31 19:20:24 -04:00
hyperdefined
ed9afc1812 backwards compatibility with old flight time format 2025-03-31 19:06:43 -04:00
hyperdefined
706175a13a Merge pull request #97 from sebampuero/impr-flighttime-fmt
Elytra flight time as human readable format
2025-03-31 18:44:50 -04:00
sebampuero
256581fa08 fix: use char instead of str 2025-03-31 18:42:47 -04:00
sebampuero
818f2e7a39 feat: update lore formatting 2025-03-31 18:41:33 -04:00
sebampuero
d83bc55eec feat: fix function for several placeholders. It was appending wrongly. Use version11 class for updater. 2025-03-31 18:41:33 -04:00
sebampuero
3196223d30 docs: javadoc fix 2025-03-31 18:39:09 -04:00
sebampuero
02d5d1c17e feat: adding updater for config version 11 2025-03-31 18:39:05 -04:00
sebampuero
d675549209 feat: add format lore that replaces several placeholders with a map 2025-03-31 18:14:51 -04:00
sebampuero
d5324b5db6 feat: some first changes
Add placeholders for time values, new formatting function and call for new function.
2025-03-31 18:13:45 -04:00
hyperdefined
4fe4c2be25 fix #94 2025-03-31 18:03:07 -04:00
13 changed files with 165 additions and 16 deletions

1
,github/FUNDING.yml Normal file
View File

@@ -0,0 +1 @@
buy_me_a_coffee: hyperdefined

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

View File

@@ -48,7 +48,7 @@ public class CommandToolStats implements TabExecutor {
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String @NotNull [] args) {
if (!sender.hasPermission("toolstats.command")) {
sender.sendMessage(Component.text("You do not have permission for this command.", NamedTextColor.RED));
return true;
@@ -210,7 +210,8 @@ public class CommandToolStats implements TabExecutor {
if (flightTime != null) {
if (toolStats.config.getBoolean("enabled.flight-time")) {
Component line = toolStats.configTools.formatLore("flight-time", "{time}", toolStats.numberFormat.formatDouble((double) flightTime / 1000));
Map<String, String> flightTimeFormatted = toolStats.numberFormat.formatTime(flightTime);
Component line = toolStats.configTools.formatLoreMultiplePlaceholders("flight-time", flightTimeFormatted);
lore.add(line);
}
}
@@ -406,6 +407,12 @@ public class CommandToolStats implements TabExecutor {
target.getInventory().addItem(itemStack);
break;
}
case "damage-done": {
ItemStack itemStack = toolStats.tokenItems.damageDone();
itemStack.setAmount(amount);
target.getInventory().addItem(itemStack);
break;
}
case "mob-kills": {
ItemStack itemStack = toolStats.tokenItems.mobKills();
itemStack.setAmount(amount);
@@ -459,7 +466,7 @@ public class CommandToolStats implements TabExecutor {
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String @NotNull [] args) {
if (args.length == 1) {
List<String> suggestions = new ArrayList<>();
if (sender.hasPermission("toolstats.reload")) {

View File

@@ -25,7 +25,6 @@ import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.ItemStack;

View File

@@ -23,6 +23,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@@ -46,6 +47,11 @@ public class InventoryOpen implements Listener {
}
Inventory inventory = event.getInventory();
// only check these
if (inventory.getType() != InventoryType.CHEST || inventory.getType() != InventoryType.BARREL || inventory.getType() != InventoryType.SHULKER_BOX || inventory.getType() != InventoryType.ENDER_CHEST) {
return;
}
Player player = (Player) event.getPlayer();
for (ItemStack itemStack : inventory) {
if (itemStack == null) {

View File

@@ -27,6 +27,7 @@ import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ItemLore {
@@ -767,8 +768,15 @@ public class ItemLore {
}
container.remove(toolStats.flightTime);
if (meta.hasLore()) {
String oldFlightTimeFormatted = toolStats.numberFormat.formatDouble(flightTime);
Component lineToRemove = toolStats.configTools.formatLore("flight-time", "{time}", oldFlightTimeFormatted);
// if the old format is in the config, check to see if the old format is on the elytra
if (toolStats.config.getString("messages.flight-time-old") != null) {
String oldFormatFormatted = toolStats.numberFormat.formatDouble((double) flightTime / 1000);
Component oldFormat = toolStats.configTools.formatLore("flight-time-old", "{time}", oldFormatFormatted);
meta.lore(removeLore(meta.lore(), oldFormat));
}
Map<String, String> oldFlightTimeFormatted = toolStats.numberFormat.formatTime(flightTime);
Component lineToRemove = toolStats.configTools.formatLoreMultiplePlaceholders("flight-time", oldFlightTimeFormatted);
List<Component> newLore = removeLore(meta.lore(), lineToRemove);
meta.lore(newLore);
}
@@ -815,10 +823,16 @@ public class ItemLore {
}
container.set(toolStats.flightTime, PersistentDataType.LONG, flightTime + duration);
String oldFlightFormatted = toolStats.numberFormat.formatDouble((double) flightTime / 1000);
String newFlightFormatted = toolStats.numberFormat.formatDouble((double) (flightTime + duration) / 1000);
Component oldLine = toolStats.configTools.formatLore("flight-time", "{time}", oldFlightFormatted);
Component newLine = toolStats.configTools.formatLore("flight-time", "{time}", newFlightFormatted);
Map<String, String> oldFlightFormatted = toolStats.numberFormat.formatTime(flightTime);
Map<String, String> newFlightFormatted = toolStats.numberFormat.formatTime(flightTime + duration);
// if the old format is in the config, check to see if the old format is on the elytra
if (toolStats.config.getString("messages.flight-time-old") != null) {
String oldFormatFormatted = toolStats.numberFormat.formatDouble((double) flightTime / 1000);
Component oldFormat = toolStats.configTools.formatLore("flight-time-old", "{time}", oldFormatFormatted);
meta.lore(removeLore(meta.lore(), oldFormat));
}
Component oldLine = toolStats.configTools.formatLoreMultiplePlaceholders("flight-time", oldFlightFormatted);
Component newLine = toolStats.configTools.formatLoreMultiplePlaceholders("flight-time", newFlightFormatted);
if (oldLine == null || newLine == null) {
return null;
}

View File

@@ -23,7 +23,9 @@ import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class NumberFormat {
@@ -136,4 +138,59 @@ public class NumberFormat {
public String formatDate(Date date) {
return DATE_FORMAT.format(date);
}
/**
* Returns a human readable form of time in milliseconds.
* E.g. given 3752348000L outputs 1 years, 5 months, 3 days, 14 hours, 12 minutes, 28 seconds.
* @param time The time in ms.
* @return Map with units as keys and time value, e.g. "years" (key) -> 1 (value)
*/
public Map<String, String> formatTime(Long time) {
final int SECONDS_PER_MINUTE = 60;
final int MINUTES_PER_HOUR = 60;
final int HOURS_PER_DAY = 24;
final int DAYS_PER_MONTH = 30; // Approximation
final int DAYS_PER_YEAR = 365; // Approximation
long totalSeconds = time / 1000;
Map<String, String> timeUnits = new HashMap<>();
long years = totalSeconds / (DAYS_PER_YEAR * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE);
if (years > 0) {
timeUnits.put("years", Long.toString(years));
}
totalSeconds %= (DAYS_PER_YEAR * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE);
long months = totalSeconds / (DAYS_PER_MONTH * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE);
if (months > 0) {
timeUnits.put("months", Long.toString(months));
}
totalSeconds %= (DAYS_PER_MONTH * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE);
long days = totalSeconds / (HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE);
if (days > 0) {
timeUnits.put("days", Long.toString(days));
}
totalSeconds %= (HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE);
long hours = totalSeconds / (MINUTES_PER_HOUR * SECONDS_PER_MINUTE);
if (hours > 0) {
timeUnits.put("hours", Long.toString(hours));
}
totalSeconds %= (MINUTES_PER_HOUR * SECONDS_PER_MINUTE);
long minutes = totalSeconds / SECONDS_PER_MINUTE;
if (minutes > 0) {
timeUnits.put("minutes", Long.toString(minutes));
}
totalSeconds %= SECONDS_PER_MINUTE;
long seconds = totalSeconds;
if (seconds > 0 || timeUnits.isEmpty()) { // Always include seconds if everything else is zero
timeUnits.put("seconds", Long.toString(seconds));
}
return timeUnits;
}
}

View File

@@ -115,13 +115,14 @@ public class TokenCrafting {
NamespacedKey removeKey = new NamespacedKey(toolStats, "remove-token");
ShapedRecipe removeRecipe = new ShapedRecipe(removeKey, toolStats.tokenItems.removeToken());
resetRecipe.shape(" P ", "P P", " P ");
resetRecipe.setIngredient('P', Material.PAPER);
removeRecipe.shape(" P ", "P P", " P ");
removeRecipe.setIngredient('P', Material.PAPER);
recipes.add(removeRecipe);
tokenTypes.add("crops-mined");
tokenTypes.add("blocks-mined");
tokenTypes.add("damage-taken");
tokenTypes.add("damage-done");
tokenTypes.add("mob-kills");
tokenTypes.add("player-kills");
tokenTypes.add("arrows-shot");

View File

@@ -27,6 +27,7 @@ import org.bukkit.Material;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -133,6 +134,65 @@ public class ConfigTools {
return component.decorationIfAbsent(TextDecoration.ITALIC, TextDecoration.State.FALSE);
}
/**
* Format a string with several placeholders to be ready for lore usage.
*
* @param configName The message to use.
* @param placeHoldersValues Map containing placeholders names as keys and values.
* @return Formatted string, null if the configName doesn't exist.
*/
public Component formatLoreMultiplePlaceholders(String configName, Map<String, String> placeHoldersValues) {
String lore = toolStats.config.getString("messages." + configName);
if (lore == null) {
toolStats.logger.warning("Unable to find config message for: messages." + configName);
return null;
}
// if the config message is empty, don't send it
if (lore.isEmpty()) {
return null;
}
Pattern pattern = Pattern.compile("\\{([^}]+)\\}(\\S*)\\s*");
Matcher matcher = pattern.matcher(lore);
StringBuilder result = new StringBuilder();
int lastEnd = 0;
while (matcher.find()) {
String placeholder = matcher.group(1);
String unit = matcher.group(2);
result.append(lore, lastEnd, matcher.start());
if (placeHoldersValues.containsKey(placeholder)) {
result.append(placeHoldersValues.get(placeholder)).append(unit).append(" ");
}
// Update lastEnd to end of the match
lastEnd = matcher.end();
}
if (lastEnd < lore.length()) {
result.append(lore.substring(lastEnd));
}
Component component;
// Clean output text
String outputText = result.toString().replaceAll("\\s+", " ").trim();
// if we match the old color codes, then format them as so
Matcher hexMatcher = CONFIG_HEX_PATTERN.matcher(outputText);
Matcher colorMatcher = COLOR_CODES.matcher(outputText);
if (hexMatcher.find() || colorMatcher.find()) {
component = LegacyComponentSerializer.legacyAmpersand().deserialize(outputText);
} else {
// otherwise format them normally
component = MiniMessage.miniMessage().deserialize(outputText);
}
return component.decorationIfAbsent(TextDecoration.ITALIC, TextDecoration.State.FALSE);
}
/**
* Format a string from the config.
*

View File

@@ -62,4 +62,4 @@ public class ConfigUpdater {
version11.update();
}
}
}
}

View File

@@ -25,7 +25,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.List;
public class TokenItems {

View File

@@ -86,6 +86,11 @@ public class Version11 {
toolStats.logger.info("Adding enabled.damage-done.bow to config.yml");
toolStats.logger.info("Adding enabled.damage-done.mace to config.yml");
toolStats.logger.info("Updating entry for messages.flight-time");
String oldFlightTime = toolStats.config.getString("messages.flight-time");
toolStats.config.set("messages.flight-time-old", oldFlightTime);
toolStats.config.set("messages.flight-time", "&7Flight time: &8{years}y {months}m {days}d {hours}h {minutes}m {seconds}s");
// save the config and reload it
try {
toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config.yml");
@@ -96,4 +101,4 @@ public class Version11 {
toolStats.loadConfig();
toolStats.logger.info("Config has been updated to version 11. A copy of version 10 has been saved as config-10.yml");
}
}
}

View File

@@ -196,7 +196,7 @@ messages:
dropped-by: "&7Dropped by: &8{name}" # name will be player/mob name
damage-taken: "&7Damage taken: &8{damage}"
arrows-shot: "&7Arrows shot: &8{arrows}"
flight-time: "&7Flight time: &8{time}"
flight-time: "&7Flight time: &8{years}y {months}m {days}d {hours}h {minutes}m {seconds}s"
damage-done: "&7Damage done: &8{damage}"
# Set display name for mobs. See: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html
mobs: