9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-19 15:09:25 +00:00

date season commands

This commit is contained in:
XiaoMoMi
2024-09-01 22:47:38 +08:00
parent 11a6e803d4
commit ab6d8527ec
13 changed files with 401 additions and 8 deletions

View File

@@ -101,7 +101,7 @@ public class SynchronizedCompoundMap {
listJoiner.add(tag2.getValue().toString());
}
}
tagValue = tag.getName() + "[" + listJoiner + "]";
tagValue = "[" + listJoiner + "]";
}
case TAG_COMPOUND -> tagValue = compoundMapToString(tag.getName(), (CompoundMap) tag.getValue());
default -> {

View File

@@ -203,7 +203,9 @@ public class CustomCropsWorldImpl<W> implements CustomCropsWorld<W> {
saveLazyChunks();
saveLazyRegions();
if (isANewDay()) {
updateSeasonAndDate();
if (setting().autoSeasonChange()) {
updateSeasonAndDate();
}
}
if (setting().enableScheduler()) {
tickChunks();

View File

@@ -18,6 +18,7 @@
package net.momirealms.customcrops.api.core.world;
import net.momirealms.customcrops.api.core.world.adaptor.WorldAdaptor;
import net.momirealms.customcrops.api.integration.SeasonProvider;
import net.momirealms.customcrops.common.plugin.feature.Reloadable;
import org.bukkit.World;
@@ -26,6 +27,8 @@ import java.util.Set;
public interface WorldManager extends Reloadable {
SeasonProvider seasonProvider();
Season getSeason(World world);
int getDate(World world);

View File

@@ -28,5 +28,19 @@ public interface MessageConstants {
TranslatableComponent.Builder SEASON_AUTUMN = Component.translatable().key("season.autumn");
TranslatableComponent.Builder SEASON_WINTER = Component.translatable().key("season.winter");
TranslatableComponent.Builder SEASON_DISABLE = Component.translatable().key("season.disable");
TranslatableComponent.Builder COMMAND_GET_SEASON_SUCCESS = Component.translatable().key("command.season.get.success");
TranslatableComponent.Builder COMMAND_GET_SEASON_FAILURE = Component.translatable().key("command.season.get.failure");
TranslatableComponent.Builder COMMAND_SET_SEASON_SUCCESS = Component.translatable().key("command.season.set.success");
TranslatableComponent.Builder COMMAND_SET_SEASON_FAILURE_DISABLE = Component.translatable().key("command.season.set.failure.disable");
TranslatableComponent.Builder COMMAND_SET_SEASON_FAILURE_REFERENCE = Component.translatable().key("command.season.set.failure.reference");
TranslatableComponent.Builder COMMAND_SET_SEASON_FAILURE_OTHER = Component.translatable().key("command.season.set.failure.other");
TranslatableComponent.Builder COMMAND_SET_SEASON_FAILURE_INVALID = Component.translatable().key("command.season.set.failure.invalid");
TranslatableComponent.Builder COMMAND_GET_DATE_SUCCESS = Component.translatable().key("command.date.get.success");
TranslatableComponent.Builder COMMAND_GET_DATE_FAILURE_DISABLE = Component.translatable().key("command.date.get.failure.disable");
TranslatableComponent.Builder COMMAND_GET_DATE_FAILURE_OTHER = Component.translatable().key("command.date.get.failure.other");
TranslatableComponent.Builder COMMAND_SET_DATE_SUCCESS = Component.translatable().key("command.date.set.success");
TranslatableComponent.Builder COMMAND_SET_DATE_FAILURE_DISABLE = Component.translatable().key("command.date.set.failure.disable");
TranslatableComponent.Builder COMMAND_SET_DATE_FAILURE_REFERENCE = Component.translatable().key("command.date.set.failure.reference");
TranslatableComponent.Builder COMMAND_SET_DATE_FAILURE_OTHER = Component.translatable().key("command.date.set.failure.other");
TranslatableComponent.Builder COMMAND_SET_DATE_FAILURE_INVALID = Component.translatable().key("command.date.set.failure.invalid");
}

View File

@@ -19,8 +19,7 @@ package net.momirealms.customcrops.bukkit.command;
import net.kyori.adventure.util.Index;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.bukkit.command.feature.DebugDataCommand;
import net.momirealms.customcrops.bukkit.command.feature.ReloadCommand;
import net.momirealms.customcrops.bukkit.command.feature.*;
import net.momirealms.customcrops.common.command.AbstractCommandManager;
import net.momirealms.customcrops.common.command.CommandFeature;
import net.momirealms.customcrops.common.sender.Sender;
@@ -37,7 +36,11 @@ public class BukkitCommandManager extends AbstractCommandManager<CommandSender>
private final List<CommandFeature<CommandSender>> FEATURES = List.of(
new ReloadCommand(this),
new DebugDataCommand(this)
new DebugDataCommand(this),
new GetSeasonCommand(this),
new SetSeasonCommand(this),
new GetDateCommand(this),
new SetDateCommand(this)
);
private final Index<String, CommandFeature<CommandSender>> INDEX = Index.create(CommandFeature::getFeatureID, FEATURES);

View File

@@ -0,0 +1,63 @@
/*
* Copyright (C) <2024> <XiaoMoMi>
*
* This program 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
* any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customcrops.bukkit.command.feature;
import net.kyori.adventure.text.Component;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.core.world.Season;
import net.momirealms.customcrops.api.integration.SeasonProvider;
import net.momirealms.customcrops.bukkit.command.BukkitCommandFeature;
import net.momirealms.customcrops.common.command.CustomCropsCommandManager;
import net.momirealms.customcrops.common.locale.MessageConstants;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.incendo.cloud.Command;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.bukkit.parser.WorldParser;
public class GetDateCommand extends BukkitCommandFeature<CommandSender> {
public GetDateCommand(CustomCropsCommandManager<CommandSender> commandManager) {
super(commandManager);
}
@Override
public Command.Builder<? extends CommandSender> assembleCommand(CommandManager<CommandSender> manager, Command.Builder<CommandSender> builder) {
return builder
.required("world", WorldParser.worldParser())
.handler(context -> {
World world = context.get("world");
SeasonProvider provider = BukkitCustomCropsPlugin.getInstance().getWorldManager().seasonProvider();
if (provider.identifier().equals("CustomCrops")) {
int date = BukkitCustomCropsPlugin.getInstance().getWorldManager().getDate(world);
if (date != -1) {
handleFeedback(context, MessageConstants.COMMAND_GET_DATE_SUCCESS, Component.text(world.getName()), Component.text(date));
} else {
handleFeedback(context, MessageConstants.COMMAND_GET_DATE_FAILURE_DISABLE, Component.text(world.getName()));
}
} else {
handleFeedback(context, MessageConstants.COMMAND_GET_DATE_FAILURE_OTHER, Component.text(world.getName()), Component.text(provider.identifier()));
}
});
}
@Override
public String getFeatureID() {
return "get_date";
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) <2024> <XiaoMoMi>
*
* This program 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
* any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customcrops.bukkit.command.feature;
import net.kyori.adventure.text.Component;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.core.world.Season;
import net.momirealms.customcrops.bukkit.command.BukkitCommandFeature;
import net.momirealms.customcrops.common.command.CustomCropsCommandManager;
import net.momirealms.customcrops.common.locale.MessageConstants;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.incendo.cloud.Command;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.bukkit.parser.WorldParser;
public class GetSeasonCommand extends BukkitCommandFeature<CommandSender> {
public GetSeasonCommand(CustomCropsCommandManager<CommandSender> commandManager) {
super(commandManager);
}
@Override
public Command.Builder<? extends CommandSender> assembleCommand(CommandManager<CommandSender> manager, Command.Builder<CommandSender> builder) {
return builder
.required("world", WorldParser.worldParser())
.handler(context -> {
World world = context.get("world");
Season season = BukkitCustomCropsPlugin.getInstance().getWorldManager().getSeason(world);
if (season != Season.DISABLE) {
handleFeedback(context, MessageConstants.COMMAND_GET_SEASON_SUCCESS, Component.text(world.getName()), Component.text(season.translation()));
} else {
handleFeedback(context, MessageConstants.COMMAND_GET_SEASON_FAILURE, Component.text(world.getName()));
}
});
}
@Override
public String getFeatureID() {
return "get_season";
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) <2024> <XiaoMoMi>
*
* This program 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
* any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customcrops.bukkit.command.feature;
import net.kyori.adventure.text.Component;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.core.ConfigManager;
import net.momirealms.customcrops.api.core.world.CustomCropsWorld;
import net.momirealms.customcrops.api.core.world.Season;
import net.momirealms.customcrops.api.integration.SeasonProvider;
import net.momirealms.customcrops.bukkit.command.BukkitCommandFeature;
import net.momirealms.customcrops.common.command.CustomCropsCommandManager;
import net.momirealms.customcrops.common.locale.MessageConstants;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.incendo.cloud.Command;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.bukkit.parser.WorldParser;
import org.incendo.cloud.parser.standard.IntegerParser;
import org.incendo.cloud.parser.standard.StringParser;
import org.incendo.cloud.suggestion.Suggestion;
import org.incendo.cloud.suggestion.SuggestionProvider;
import java.util.Locale;
import java.util.Optional;
public class SetDateCommand extends BukkitCommandFeature<CommandSender> {
public SetDateCommand(CustomCropsCommandManager<CommandSender> commandManager) {
super(commandManager);
}
@Override
public Command.Builder<? extends CommandSender> assembleCommand(CommandManager<CommandSender> manager, Command.Builder<CommandSender> builder) {
return builder
.required("world", WorldParser.worldParser())
.required("date", IntegerParser.integerParser())
.handler(context -> {
World world = context.get("world");
SeasonProvider provider = BukkitCustomCropsPlugin.getInstance().getWorldManager().seasonProvider();
if (provider.identifier().equals("CustomCrops")) {
Optional<CustomCropsWorld<?>> optionalWorld = BukkitCustomCropsPlugin.getInstance().getWorldManager().getWorld(world);
if (optionalWorld.isPresent()) {
CustomCropsWorld<?> customCropsWorld = optionalWorld.get();
int date = context.get("date");
if (date < 1 || date > customCropsWorld.setting().seasonDuration()) {
handleFeedback(context, MessageConstants.COMMAND_SET_DATE_FAILURE_INVALID, Component.text(world.getName()), Component.text(date));
return;
}
if (customCropsWorld.setting().enableSeason()) {
if (ConfigManager.syncSeasons()) {
if (ConfigManager.referenceWorld().equals(world.getName())) {
customCropsWorld.extraData().setDate(date);
handleFeedback(context, MessageConstants.COMMAND_SET_DATE_SUCCESS, Component.text(world.getName()), Component.text(date));
} else {
handleFeedback(context, MessageConstants.COMMAND_SET_DATE_FAILURE_REFERENCE, Component.text(world.getName()));
}
} else {
customCropsWorld.extraData().setDate(date);
handleFeedback(context, MessageConstants.COMMAND_SET_DATE_SUCCESS, Component.text(world.getName()), Component.text(date));
}
} else {
handleFeedback(context, MessageConstants.COMMAND_SET_DATE_FAILURE_DISABLE, Component.text(world.getName()));
}
} else {
handleFeedback(context, MessageConstants.COMMAND_SET_DATE_FAILURE_DISABLE, Component.text(world.getName()));
}
} else {
handleFeedback(context, MessageConstants.COMMAND_SET_DATE_FAILURE_OTHER, Component.text(world.getName()), Component.text(provider.identifier()));
}
});
}
@Override
public String getFeatureID() {
return "set_date";
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) <2024> <XiaoMoMi>
*
* This program 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
* any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customcrops.bukkit.command.feature;
import net.kyori.adventure.text.Component;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.core.ConfigManager;
import net.momirealms.customcrops.api.core.world.CustomCropsWorld;
import net.momirealms.customcrops.api.core.world.Season;
import net.momirealms.customcrops.api.integration.SeasonProvider;
import net.momirealms.customcrops.bukkit.command.BukkitCommandFeature;
import net.momirealms.customcrops.common.command.CustomCropsCommandManager;
import net.momirealms.customcrops.common.locale.MessageConstants;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.Command;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.bukkit.parser.WorldParser;
import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.context.CommandInput;
import org.incendo.cloud.parser.standard.StringParser;
import org.incendo.cloud.suggestion.Suggestion;
import org.incendo.cloud.suggestion.SuggestionProvider;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public class SetSeasonCommand extends BukkitCommandFeature<CommandSender> {
public SetSeasonCommand(CustomCropsCommandManager<CommandSender> commandManager) {
super(commandManager);
}
@Override
public Command.Builder<? extends CommandSender> assembleCommand(CommandManager<CommandSender> manager, Command.Builder<CommandSender> builder) {
return builder
.required("world", WorldParser.worldParser())
.required("season", StringParser.stringComponent().suggestionProvider(SuggestionProvider.suggesting(
Suggestion.suggestion("spring"), Suggestion.suggestion("summer"),Suggestion.suggestion("autumn"),Suggestion.suggestion("winter")
)))
.handler(context -> {
World world = context.get("world");
SeasonProvider provider = BukkitCustomCropsPlugin.getInstance().getWorldManager().seasonProvider();
if (provider.identifier().equals("CustomCrops")) {
Optional<CustomCropsWorld<?>> optionalWorld = BukkitCustomCropsPlugin.getInstance().getWorldManager().getWorld(world);
if (optionalWorld.isPresent()) {
CustomCropsWorld<?> customCropsWorld = optionalWorld.get();
String season = context.get("season");
Season seasonEnum;
try {
seasonEnum = Season.valueOf(season.toUpperCase(Locale.ENGLISH));
if (seasonEnum == Season.DISABLE) {
throw new IllegalArgumentException("Invalid season: " + season);
}
} catch (IllegalArgumentException e) {
handleFeedback(context, MessageConstants.COMMAND_SET_SEASON_FAILURE_INVALID, Component.text(world.getName()), Component.text(season));
return;
}
if (customCropsWorld.setting().enableSeason()) {
if (ConfigManager.syncSeasons()) {
if (ConfigManager.referenceWorld().equals(world.getName())) {
customCropsWorld.extraData().setSeason(seasonEnum);
handleFeedback(context, MessageConstants.COMMAND_SET_SEASON_SUCCESS, Component.text(world.getName()), Component.text(seasonEnum.translation()));
} else {
handleFeedback(context, MessageConstants.COMMAND_SET_SEASON_FAILURE_REFERENCE, Component.text(world.getName()));
}
} else {
customCropsWorld.extraData().setSeason(seasonEnum);
handleFeedback(context, MessageConstants.COMMAND_SET_SEASON_SUCCESS, Component.text(world.getName()), Component.text(seasonEnum.translation()));
}
} else {
handleFeedback(context, MessageConstants.COMMAND_SET_SEASON_FAILURE_DISABLE, Component.text(world.getName()));
}
} else {
handleFeedback(context, MessageConstants.COMMAND_SET_SEASON_FAILURE_DISABLE, Component.text(world.getName()));
}
} else {
handleFeedback(context, MessageConstants.COMMAND_SET_SEASON_FAILURE_OTHER, Component.text(world.getName()), Component.text(provider.identifier()));
}
});
}
@Override
public String getFeatureID() {
return "set_season";
}
}

View File

@@ -66,7 +66,12 @@ public class BukkitWorldManager implements WorldManager, Listener {
@NotNull
@Override
public Season getSeason(@NotNull World world) {
return BukkitWorldManager.this.getWorld(world).map(w -> w.extraData().getSeason()).orElse(Season.DISABLE);
return BukkitWorldManager.this.getWorld(world).map(w -> {
if (!w.setting().enableSeason()) {
return Season.DISABLE;
}
return w.extraData().getSeason();
}).orElse(Season.DISABLE);
}
@Override
public String identifier() {
@@ -79,6 +84,11 @@ public class BukkitWorldManager implements WorldManager, Listener {
this.seasonProvider = seasonProvider;
}
@Override
public SeasonProvider seasonProvider() {
return seasonProvider;
}
@Override
public Season getSeason(World world) {
if (ConfigManager.syncSeasons()) {

View File

@@ -30,6 +30,20 @@ set_season:
- /customcrops season set
- /ccrops season set
get_date:
enable: true
permission: customcrops.command.get_date
usage:
- /customcrops date get
- /ccrops date get
set_date:
enable: true
permission: customcrops.command.set_date
usage:
- /customcrops date set
- /ccrops date set
debug_data:
enable: true
permission: customcrops.command.debug

View File

@@ -39,6 +39,21 @@ argument.parse.failure.aggregate.failure: "<red>Invalid component '<arg:0>': <ar
argument.parse.failure.either: "<red>Could not resolve <arg:1> or <arg:2> from '<arg:0>'</red>"
argument.parse.failure.namedtextcolor: "<red>'<arg:0>' is not a named text color</red>"
command.reload.success: "<white>Reloaded. Took <green><arg:0></green> ms.</white>"
command.season.get.success: "<white>The season in world [<arg:0>] is [<arg:1>]</white>"
command.season.get.failure: "<red>Season is disabled in world [<arg:0>]</red>"
command.season.set.success: "<white>Successfully set season to <arg:1> in world [<arg:0>]</white>"
command.season.set.failure.disable: "<red>Season is disabled in world [<arg:0>]</red>"
command.season.set.failure.reference: "<red>World [<arg:0>] is not the reference world</red>"
command.season.set.failure.other: "<red>Can't set season for world [<arg:0>] because plugin [<arg:1>] takes over the season</red>"
command.season.set.failure.invalid: "<red>Invalid season [<arg:1>]</red>"
command.date.get.success: "<white>The date in world [<arg:0>] is [<arg:1>]</white>"
command.date.get.failure.disable: "<red>Date is disabled in world [<arg:0>]</red>"
command.date.get.failure.other: "<red>Can't get date for world [<arg:0>] because plugin [<arg:1>] takes over the calendar</red>"
command.date.set.success: "<white>Successfully set date to <arg:1> in world [<arg:0>]</white>"
command.date.set.failure.disable: "<red>Date is disabled in world [<arg:0>]</red>"
command.date.set.failure.reference: "<red>World [<arg:0>] is not the reference world</red>"
command.date.set.failure.other: "<red>Can't set date for world [<arg:0>] because plugin [<arg:1>] takes over the calendar</red>"
command.date.set.failure.invalid: "<red>Invalid date [<arg:1>]</red>"
season.spring: "Spring"
season.summer: "Summer"
season.autumn: "Autumn"

View File

@@ -38,7 +38,21 @@ argument.parse.failure.aggregate.missing: "<red>缺少组件 '<arg:0>'</red>"
argument.parse.failure.aggregate.failure: "<red>无效的组件 '<arg:0>': <arg:1></red>"
argument.parse.failure.either: "<red>无法从 '<arg:0>' 解析 <arg:1> 或 <arg:2></red>"
argument.parse.failure.namedtextcolor: "<red>'<arg:0>' 不是颜色代码</red>"
command.reload.success: "<white>重新加载完成.耗时 <green><arg:0></green> 毫秒</white><newline><green><click:open_url:https://github.com/jhqwqmc>译者jhqwqmc</click></green>"
command.reload.success: "<white>重新加载完成. 耗时 <green><arg:0></green> 毫秒</white>"
command.season.get.success: "<white>世界 [<arg:0>] 的季节是 [<arg:1>]</white>"
command.season.get.failure: "<red>季节没有在世界 [<arg:0>] 启用</red>"
command.season.set.success: "<white>成功设置世界 [<arg:0>] 的季节为 [<arg:1>]</white>"
command.season.set.failure.disable: "<red>季节没有在世界 [<arg:0>] 启用</red>"
command.season.set.failure.reference: "<red>世界 [<arg:0>] 不是同步季节设置的参考世界</red>"
command.season.set.failure.other: "<red>无法设置世界 [<arg:0>] 的季节,原因是插件 [<arg:1>] 接管了季节</red>"
command.season.set.failure.invalid: "<red>无效的季节 [<arg:1>]</red>"
command.date.get.success: "<white>世界 [<arg:0>] 的日期是 [<arg:1>]</white>"
command.date.get.failure.disable: "<red>日期没有在世界 [<arg:0>] 启用</red>"
command.date.get.failure.other: "<red>无法获取世界 [<arg:0>] 的日期,原因是插件 [<arg:1>] 接管了日历</red>"
command.date.set.success: "<white>成功设置世界 [<arg:0>] 的日期为 [<arg:1>]</white>"
command.date.set.failure.reference: "<red>世界 [<arg:0>] 不是同步季节设置的参考世界</red>"
command.date.set.failure.other: "<red>无法设置世界 [<arg:0>] 的日期,原因是插件 [<arg:1>] 接管了日历</red>"
command.date.set.failure.invalid: "<red>无效的日期 [<arg:1>]</red>"
season.spring: "春"
season.summer: "夏"
season.autumn: "秋"