9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-27 19:09:18 +00:00
This commit is contained in:
XiaoMoMi
2023-09-23 22:28:55 +08:00
parent e359239c98
commit 2c69d7e2a5
9 changed files with 120 additions and 10 deletions

View File

@@ -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;

View File

@@ -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<String, Object> 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")

View File

@@ -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);
}
/**

View File

@@ -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();
}

View File

@@ -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<String, Object> 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 -> "<!i>" + it).toList())

View File

@@ -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);
}

View File

@@ -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");

View File

@@ -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