From 4aee977bfa0a706d7b613a65e09675215588b6f9 Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Tue, 18 Jul 2023 14:35:36 +0200 Subject: [PATCH] Makes legacy item checks configurable and have it disabled by default The legacy checks is causing big performance problems on bigger servers. It will be removed in the future --- .../listeners/PlayerInteractListener.java | 9 +++++- .../epicvouchers/settings/Settings.java | 4 +++ .../songoda/epicvouchers/utils/CachedSet.java | 2 +- .../songoda/epicvouchers/voucher/Voucher.java | 32 ++++++++++++++++--- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/songoda/epicvouchers/listeners/PlayerInteractListener.java b/src/main/java/com/songoda/epicvouchers/listeners/PlayerInteractListener.java index 9219dac..6fb8477 100644 --- a/src/main/java/com/songoda/epicvouchers/listeners/PlayerInteractListener.java +++ b/src/main/java/com/songoda/epicvouchers/listeners/PlayerInteractListener.java @@ -2,6 +2,7 @@ package com.songoda.epicvouchers.listeners; import com.songoda.core.third_party.de.tr7zw.nbtapi.NBTItem; import com.songoda.epicvouchers.EpicVouchers; +import com.songoda.epicvouchers.settings.Settings; import com.songoda.epicvouchers.utils.CachedSet; import com.songoda.epicvouchers.voucher.Voucher; import org.bukkit.ChatColor; @@ -25,6 +26,8 @@ public class PlayerInteractListener implements Listener { @EventHandler public void voucherListener(PlayerInteractEvent e) { + boolean legacyCheckEnabled = Settings.CHECK_FOR_LEGACY_ITEMS.getBoolean(); + ItemStack item = e.getItem(); if (item == null || !isRightClickAction(e.getAction())) { return; @@ -33,7 +36,7 @@ public class PlayerInteractListener implements Listener { NBTItem nbtItem = new NBTItem(item); boolean itemHasVoucher = nbtItem.hasTag("epicvouchers:voucher"); - boolean itemHasBeenLegacyChecked = this.itemsThatGotLegacyChecked.contains(item); + boolean itemHasBeenLegacyChecked = !legacyCheckEnabled || this.itemsThatGotLegacyChecked.contains(item); if (!itemHasVoucher && itemHasBeenLegacyChecked) { return; } @@ -52,6 +55,10 @@ public class PlayerInteractListener implements Listener { return; } + if (!Settings.CHECK_FOR_LEGACY_ITEMS.getBoolean()) { + return; + } + voucher = findVoucherForLegacyItem(item, allVouchers); if (voucher == null) { this.itemsThatGotLegacyChecked.add(item); diff --git a/src/main/java/com/songoda/epicvouchers/settings/Settings.java b/src/main/java/com/songoda/epicvouchers/settings/Settings.java index 1034b6a..e0dd321 100644 --- a/src/main/java/com/songoda/epicvouchers/settings/Settings.java +++ b/src/main/java/com/songoda/epicvouchers/settings/Settings.java @@ -13,6 +13,10 @@ public class Settings implements Listener { public static final ConfigSetting FILL_GLASS = new ConfigSetting(config, "Interface.Fill Interfaces With Glass", true); public static final ConfigSetting COOLDOWN_DELAY = new ConfigSetting(config, "Main.Cooldown Delay", 10); + public static final ConfigSetting CHECK_FOR_LEGACY_ITEMS = new ConfigSetting(config, "Main.Check For Legacy Items", false, + "When you have a really old installation of EpicVouchers, some items in chests etc. might still be created with the old system.", + "This enables checking/detection for those items.", + "2 warnings: Enabling this comes with a performance impact with many vouchers configured + This check will be removed in the future"); public static final ConfigSetting DATABASE_SUPPORT = new ConfigSetting(config, "Database.Activate Mysql Support", false); public static final ConfigSetting DATABASE_IP = new ConfigSetting(config, "Database.IP", "127.0.0.1"); diff --git a/src/main/java/com/songoda/epicvouchers/utils/CachedSet.java b/src/main/java/com/songoda/epicvouchers/utils/CachedSet.java index a805a50..005a450 100644 --- a/src/main/java/com/songoda/epicvouchers/utils/CachedSet.java +++ b/src/main/java/com/songoda/epicvouchers/utils/CachedSet.java @@ -4,7 +4,7 @@ import java.util.Map; import java.util.WeakHashMap; public class CachedSet { - private final Map cache = new WeakHashMap<>(); + private final Map cache = new WeakHashMap<>(0); private final int ttl; private long lastClear = System.currentTimeMillis(); diff --git a/src/main/java/com/songoda/epicvouchers/voucher/Voucher.java b/src/main/java/com/songoda/epicvouchers/voucher/Voucher.java index 7dfb12b..75ede49 100644 --- a/src/main/java/com/songoda/epicvouchers/voucher/Voucher.java +++ b/src/main/java/com/songoda/epicvouchers/voucher/Voucher.java @@ -21,9 +21,9 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; import static org.bukkit.Material.PAPER; @@ -134,15 +134,39 @@ public class Voucher { } public List getLore(boolean applyFormatting) { - return applyFormatting ? lore.stream().map(TextUtils::formatText).collect(Collectors.toList()) : lore; + if (!applyFormatting) { + return Collections.unmodifiableList(this.lore); + } + + List lore = new ArrayList<>(); + for (String line : this.lore) { + lore.add(TextUtils.formatText(line)); + } + return lore; } public List getBroadcasts(boolean applyFormatting) { - return applyFormatting ? broadcasts.stream().map(TextUtils::formatText).collect(Collectors.toList()) : broadcasts; + if (!applyFormatting) { + return this.broadcasts; + } + + List result = new ArrayList<>(); + for (String broadcast : this.broadcasts) { + result.add(TextUtils.formatText(broadcast)); + } + return result; } public List getMessages(boolean applyFormatting) { - return applyFormatting ? messages.stream().map(TextUtils::formatText).collect(Collectors.toList()) : messages; + if (!applyFormatting) { + return this.messages; + } + + List list = new ArrayList<>(); + for (String message : this.messages) { + list.add(TextUtils.formatText(message)); + } + return list; } public void saveSetting(String key, List list) {