From 2c69d7e2a55c2ab5a2a4bd33a85a14e572c98810 Mon Sep 17 00:00:00 2001 From: XiaoMoMi <972454774@qq.com> Date: Sat, 23 Sep 2023 22:28:55 +0800 Subject: [PATCH] pool --- .../api/event/CompetitionEvent.java | 43 +++++++++++++++++ .../command/sub/CompetitionCommand.java | 2 + .../command/sub/ItemCommand.java | 48 ++++++++++++++++++- .../mechanic/competition/Competition.java | 16 +++++-- .../competition/CompetitionManagerImpl.java | 4 ++ .../mechanic/hook/HookManagerImpl.java | 5 ++ .../scheduler/SchedulerImpl.java | 2 +- .../customfishing/setting/CFConfig.java | 6 +-- plugin/src/main/resources/config.yml | 4 +- 9 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 api/src/main/java/net/momirealms/customfishing/api/event/CompetitionEvent.java diff --git a/api/src/main/java/net/momirealms/customfishing/api/event/CompetitionEvent.java b/api/src/main/java/net/momirealms/customfishing/api/event/CompetitionEvent.java new file mode 100644 index 00000000..bd47b617 --- /dev/null +++ b/api/src/main/java/net/momirealms/customfishing/api/event/CompetitionEvent.java @@ -0,0 +1,43 @@ +package net.momirealms.customfishing.api.event; + +import net.momirealms.customfishing.api.mechanic.competition.CompetitionConfig; +import net.momirealms.customfishing.api.mechanic.competition.FishingCompetition; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class CompetitionEvent extends Event { + + private static final HandlerList handlerList = new HandlerList(); + private final State state; + private final FishingCompetition competition; + + public CompetitionEvent(State state, FishingCompetition competition) { + this.state = state; + this.competition = competition; + } + + public State getState() { + return state; + } + + public FishingCompetition getCompetition() { + return competition; + } + + public static HandlerList getHandlerList() { + return handlerList; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return getHandlerList(); + } + + public static enum State { + END, + STOP, + START + } +} diff --git a/plugin/src/main/java/net/momirealms/customfishing/command/sub/CompetitionCommand.java b/plugin/src/main/java/net/momirealms/customfishing/command/sub/CompetitionCommand.java index 9f8afc1a..e3aa9cbc 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/command/sub/CompetitionCommand.java +++ b/plugin/src/main/java/net/momirealms/customfishing/command/sub/CompetitionCommand.java @@ -25,10 +25,12 @@ import dev.jorel.commandapi.arguments.BooleanArgument; import dev.jorel.commandapi.arguments.StringArgument; import net.momirealms.customfishing.adventure.AdventureManagerImpl; import net.momirealms.customfishing.api.CustomFishingPlugin; +import net.momirealms.customfishing.api.event.CompetitionEvent; import net.momirealms.customfishing.api.mechanic.competition.FishingCompetition; import net.momirealms.customfishing.setting.CFConfig; import net.momirealms.customfishing.setting.CFLocale; import net.momirealms.customfishing.storage.method.database.nosql.RedisManager; +import org.bukkit.Bukkit; import java.util.Set; diff --git a/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java b/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java index b3a86140..26e1dc49 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java +++ b/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java @@ -17,6 +17,7 @@ package net.momirealms.customfishing.command.sub; +import de.tr7zw.changeme.nbtapi.NBTItem; import dev.jorel.commandapi.CommandAPICommand; import dev.jorel.commandapi.arguments.ArgumentSuggestions; import dev.jorel.commandapi.arguments.EntitySelectorArgument; @@ -27,13 +28,21 @@ import net.momirealms.customfishing.api.CustomFishingPlugin; import net.momirealms.customfishing.api.common.Key; import net.momirealms.customfishing.api.mechanic.condition.Condition; import net.momirealms.customfishing.api.mechanic.item.BuildableItem; +import net.momirealms.customfishing.api.util.LogUtils; import net.momirealms.customfishing.setting.CFLocale; +import net.momirealms.customfishing.util.ConfigUtils; import net.momirealms.customfishing.util.ItemUtils; +import net.momirealms.customfishing.util.NBTUtils; +import org.bukkit.Material; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import java.io.File; +import java.io.IOException; import java.util.Collection; import java.util.HashMap; +import java.util.Map; public class ItemCommand { @@ -64,10 +73,47 @@ public class ItemCommand { return new CommandAPICommand(namespace) .withSubcommands( getCommand(namespace), - giveCommand(namespace) + giveCommand(namespace), + importCommand(namespace) ); } + @SuppressWarnings("ResultOfMethodCallIgnored") + private CommandAPICommand importCommand(String namespace) { + return new CommandAPICommand("import") + .withArguments(new StringArgument("key")) + .withOptionalArguments(new StringArgument("file")) + .executesPlayer((player, args) -> { + String key = (String) args.get("key"); + String fileName = args.getOrDefault("file","import") + ".yml"; + ItemStack itemStack = player.getInventory().getItemInMainHand(); + if (itemStack.getType() == Material.AIR) + return; + File file = new File(CustomFishingPlugin.get().getDataFolder(), + "contents" + File.separator + namespace + File.separator + fileName); + try { + if (!file.exists()) { + file.createNewFile(); + } + YamlConfiguration config = YamlConfiguration.loadConfiguration(file); + config.set(key + ".material", itemStack.getType().toString()); + config.set(key + ".amount", itemStack.getAmount()); + Map nbtMap = NBTUtils.compoundToMap(new NBTItem(itemStack)); + if (nbtMap.size() != 0) { + config.createSection(key + ".nbt", nbtMap); + } + try { + config.save(file); + AdventureManagerImpl.getInstance().sendMessageWithPrefix(player, "Imported! Saved to " + file.getAbsolutePath()); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (IOException e) { + LogUtils.warn("Failed to create imported file.", e); + } + }); + } + private CommandAPICommand getCommand(String namespace) { return new CommandAPICommand("get") .withArguments(new StringArgument("id") diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/Competition.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/Competition.java index 61405c71..a4c3a143 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/Competition.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/Competition.java @@ -19,6 +19,7 @@ package net.momirealms.customfishing.mechanic.competition; import net.momirealms.customfishing.api.CustomFishingPlugin; import net.momirealms.customfishing.api.common.Pair; +import net.momirealms.customfishing.api.event.CompetitionEvent; import net.momirealms.customfishing.api.mechanic.action.Action; import net.momirealms.customfishing.api.mechanic.competition.CompetitionConfig; import net.momirealms.customfishing.api.mechanic.competition.CompetitionGoal; @@ -90,7 +91,6 @@ public class Competition implements FishingCompetition { this.actionBarManager.load(); } - Action[] actions = config.getStartActions(); if (actions != null) { Condition condition = new Condition(null, null, new HashMap<>()); @@ -98,6 +98,9 @@ public class Competition implements FishingCompetition { action.trigger(condition); } } + + CompetitionEvent competitionStartEvent = new CompetitionEvent(CompetitionEvent.State.START, this); + Bukkit.getPluginManager().callEvent(competitionStartEvent); } /** @@ -150,6 +153,9 @@ public class Competition implements FishingCompetition { if (this.actionBarManager != null) this.actionBarManager.unload(); this.ranking.clear(); this.remainingTime = 0; + + CompetitionEvent competitionEvent = new CompetitionEvent(CompetitionEvent.State.STOP, this); + Bukkit.getPluginManager().callEvent(competitionEvent); } /** @@ -207,8 +213,12 @@ public class Competition implements FishingCompetition { } } - // 1.5 seconds delay for other servers to read the redis data - CustomFishingPlugin.get().getScheduler().runTaskAsyncLater(this.ranking::clear, 1500, TimeUnit.MILLISECONDS); + // call event + CompetitionEvent competitionEndEvent = new CompetitionEvent(CompetitionEvent.State.END, this); + Bukkit.getPluginManager().callEvent(competitionEndEvent); + + // 1 seconds delay for other servers to read the redis data + CustomFishingPlugin.get().getScheduler().runTaskAsyncLater(this.ranking::clear, 1, TimeUnit.SECONDS); } /** diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/CompetitionManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/CompetitionManagerImpl.java index d5c4910d..a61bd303 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/CompetitionManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/CompetitionManagerImpl.java @@ -19,6 +19,7 @@ package net.momirealms.customfishing.mechanic.competition; import net.momirealms.customfishing.api.CustomFishingPlugin; import net.momirealms.customfishing.api.common.Pair; +import net.momirealms.customfishing.api.event.CompetitionEvent; import net.momirealms.customfishing.api.manager.CompetitionManager; import net.momirealms.customfishing.api.mechanic.action.Action; import net.momirealms.customfishing.api.mechanic.competition.*; @@ -305,12 +306,15 @@ public class CompetitionManagerImpl implements CompetitionManager { private void start(CompetitionConfig config) { if (getOnGoingCompetition() != null) { + // END currentCompetition.end(); plugin.getScheduler().runTaskAsyncLater(() -> { + // start one second later this.currentCompetition = new Competition(config); this.currentCompetition.start(); }, 1, TimeUnit.SECONDS); } else { + // start instantly this.currentCompetition = new Competition(config); this.currentCompetition.start(); } diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/hook/HookManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/hook/HookManagerImpl.java index 23439f09..e2a336de 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/hook/HookManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/hook/HookManagerImpl.java @@ -25,6 +25,7 @@ import net.momirealms.customfishing.api.manager.RequirementManager; import net.momirealms.customfishing.api.mechanic.condition.Condition; import net.momirealms.customfishing.api.mechanic.effect.EffectCarrier; import net.momirealms.customfishing.api.mechanic.hook.HookSetting; +import net.momirealms.customfishing.api.util.LogUtils; import net.momirealms.customfishing.util.ItemUtils; import org.bukkit.Bukkit; import org.bukkit.GameMode; @@ -104,6 +105,10 @@ public class HookManagerImpl implements Listener, HookManager { YamlConfiguration config = YamlConfiguration.loadConfiguration(file); for (Map.Entry entry : config.getValues(false).entrySet()) { if (entry.getValue() instanceof ConfigurationSection section) { + if (!section.contains("max-durability")) { + LogUtils.warn("Please set max-durability to hook: " + entry.getKey()); + continue; + } var setting = new HookSetting.Builder(entry.getKey()) .durability(section.getInt("max-durability", 16)) .lore(section.getStringList("lore-on-rod").stream().map(it -> "" + it).toList()) diff --git a/plugin/src/main/java/net/momirealms/customfishing/scheduler/SchedulerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/scheduler/SchedulerImpl.java index 1f22d2d9..6ae14cfd 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/scheduler/SchedulerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/scheduler/SchedulerImpl.java @@ -53,9 +53,9 @@ public class SchedulerImpl implements Scheduler { */ public void reload() { try { + this.schedule.setMaximumPoolSize(CFConfig.maximumPoolSize); this.schedule.setCorePoolSize(CFConfig.corePoolSize); this.schedule.setKeepAliveTime(CFConfig.keepAliveTime, TimeUnit.SECONDS); - this.schedule.setMaximumPoolSize(CFConfig.maximumPoolSize); } catch (IllegalArgumentException e) { LogUtils.warn("Failed to create thread pool. Please lower the corePoolSize in config.yml.", e); } diff --git a/plugin/src/main/java/net/momirealms/customfishing/setting/CFConfig.java b/plugin/src/main/java/net/momirealms/customfishing/setting/CFConfig.java index b4016042..38d1cde6 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/setting/CFConfig.java +++ b/plugin/src/main/java/net/momirealms/customfishing/setting/CFConfig.java @@ -120,9 +120,9 @@ public class CFConfig { metrics = config.getBoolean("metrics"); eventPriority = EventPriority.valueOf(config.getString("other-settings.event-priority", "NORMAL").toUpperCase(Locale.ENGLISH)); - corePoolSize = config.getInt("other-settings.thread-pool-settings.corePoolSize", 4); - maximumPoolSize = config.getInt("other-settings.thread-pool-settings.maximumPoolSize", 8); - keepAliveTime = config.getInt("other-settings.thread-pool-settings.keepAliveTime", 10); + corePoolSize = config.getInt("other-settings.thread-pool-settings.corePoolSize", 1); + maximumPoolSize = config.getInt("other-settings.thread-pool-settings.maximumPoolSize", 1); + keepAliveTime = config.getInt("other-settings.thread-pool-settings.keepAliveTime", 30); itemDetectOrder = config.getStringList("other-settings.item-detection-order"); blockDetectOrder = config.getStringList("other-settings.block-detection-order"); diff --git a/plugin/src/main/resources/config.yml b/plugin/src/main/resources/config.yml index 3b59fd0c..1e102e1b 100644 --- a/plugin/src/main/resources/config.yml +++ b/plugin/src/main/resources/config.yml @@ -126,9 +126,9 @@ other-settings: thread-pool-settings: # The size of the core Thread pool, that is, the size of the Thread pool when there is no task to execute # Increase the size of corePoolSize when you are running a large server with many players fishing at the same time - corePoolSize: 4 + corePoolSize: 10 # The maximum number of threads allowed to be created in the Thread pool. The current number of threads in the Thread pool will not exceed this value - maximumPoolSize: 4 + maximumPoolSize: 10 # If a thread is idle for more than this attribute value, it will exit due to timeout keepAliveTime: 30