Noting important

This commit is contained in:
tanyaofei 2024-08-13 17:08:45 +08:00
parent f788729420
commit 7be8b7c675
13 changed files with 56 additions and 56 deletions

View File

@ -11,7 +11,7 @@ import io.github.hello09x.fakeplayer.api.spi.ActionType;
import io.github.hello09x.fakeplayer.core.command.impl.*;
import io.github.hello09x.fakeplayer.core.config.FakeplayerConfig;
import io.github.hello09x.fakeplayer.core.constant.Direction;
import io.github.hello09x.fakeplayer.core.repository.model.FeatureKey;
import io.github.hello09x.fakeplayer.core.repository.model.Feature;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
@ -173,7 +173,7 @@ public class CommandRegistry {
.withShortDescription("fakeplayer.command.set.description")
.withPermission(Permission.set)
.withArguments(
configKey("feature", FeatureKey::hasModifier),
configKey("feature", Feature::hasModifier),
configValue("feature", "option")
)
.withOptionalArguments(fakeplayer("name"))

View File

@ -11,7 +11,7 @@ import io.github.hello09x.fakeplayer.core.Main;
import io.github.hello09x.fakeplayer.core.command.impl.ActionCommand;
import io.github.hello09x.fakeplayer.core.config.FakeplayerConfig;
import io.github.hello09x.fakeplayer.core.manager.FakeplayerManager;
import io.github.hello09x.fakeplayer.core.repository.model.FeatureKey;
import io.github.hello09x.fakeplayer.core.repository.model.Feature;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -126,16 +126,16 @@ public abstract class CommandSupports {
}));
}
public static @NotNull Argument<FeatureKey> configKey(@NotNull String nodeName) {
public static @NotNull Argument<Feature> configKey(@NotNull String nodeName) {
return configKey(nodeName, ignored -> true);
}
public static @NotNull Argument<FeatureKey> configKey(@NotNull String nodeName, @NotNull Predicate<FeatureKey> predicate) {
public static @NotNull Argument<Feature> configKey(@NotNull String nodeName, @NotNull Predicate<Feature> predicate) {
return new CustomArgument<>(new StringArgument(nodeName), info -> {
var arg = info.currentInput();
FeatureKey key;
Feature key;
try {
key = FeatureKey.valueOf(arg);
key = Feature.valueOf(arg);
} catch (Exception e) {
throw CustomArgument.CustomArgumentException.fromAdventureComponent(translatable("fakeplayer.command.config.set.error.invalid-key"));
}
@ -148,13 +148,13 @@ public abstract class CommandSupports {
throw CustomArgument.CustomArgumentException.fromAdventureComponent(translatable("fakeplayer.command.config.set.error.no-permission"));
}
return key;
}).replaceSuggestions(ArgumentSuggestions.strings(Arrays.stream(FeatureKey.values()).filter(predicate).map(Enum::name).toArray(String[]::new)));
}).replaceSuggestions(ArgumentSuggestions.strings(Arrays.stream(Feature.values()).filter(predicate).map(Enum::name).toArray(String[]::new)));
}
public static @NotNull Argument<String> configValue(@NotNull String configKeyNodeName, @NotNull String nodeName) {
return new CustomArgument<String, String>(new StringArgument(nodeName), info -> {
var key = (FeatureKey) info.previousArgs().get(configKeyNodeName);
var key = (Feature) info.previousArgs().get(configKeyNodeName);
if (key == null) {
throw CustomArgument.CustomArgumentException.fromAdventureComponent(translatable("fakeplayer.command.config.set.error.invalid-key"));
}
@ -165,7 +165,7 @@ public abstract class CommandSupports {
return arg;
}).replaceSuggestions(ArgumentSuggestions.stringCollectionAsync(info -> CompletableFuture.supplyAsync(() -> {
var key = (FeatureKey) info.previousArgs().get(configKeyNodeName);
var key = (Feature) info.previousArgs().get(configKeyNodeName);
if (key == null) {
return Collections.emptyList();
}

View File

@ -9,7 +9,7 @@ import io.github.hello09x.devtools.core.translation.TranslatorUtils;
import io.github.hello09x.devtools.core.utils.ComponentUtils;
import io.github.hello09x.fakeplayer.core.Main;
import io.github.hello09x.fakeplayer.core.manager.feature.FakeplayerFeatureManager;
import io.github.hello09x.fakeplayer.core.repository.model.FeatureKey;
import io.github.hello09x.fakeplayer.core.repository.model.Feature;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import org.bukkit.Bukkit;
@ -39,8 +39,8 @@ public class ConfigCommand extends AbstractCommand {
* 设置配置
*/
public void setConfig(@NotNull Player sender, @NotNull CommandArguments args) throws WrapperCommandSyntaxException {
var key = (FeatureKey) Objects.requireNonNull(args.get("feature"));
if (!key.testPermissions(sender)) {
var feature = (Feature) Objects.requireNonNull(args.get("feature"));
if (!feature.testPermissions(sender)) {
throw CommandAPI.failWithString(ComponentUtils.toString(
translatable("fakeplayer.command.config.set.error.no-permission"),
TranslatorUtils.getLocale(sender)
@ -48,10 +48,10 @@ public class ConfigCommand extends AbstractCommand {
}
var option = (String) Objects.requireNonNull(args.get("option"));
featureManager.setFeature(sender, key, option);
featureManager.setFeature(sender, feature, option);
sender.sendMessage(translatable(
"fakeplayer.command.config.set.success",
translatable(key.translationKey(), GOLD),
translatable(feature.translationKey(), GOLD),
text(option, WHITE)
).color(GRAY));
}

View File

@ -3,7 +3,7 @@ package io.github.hello09x.fakeplayer.core.command.impl;
import com.google.inject.Singleton;
import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException;
import dev.jorel.commandapi.executors.CommandArguments;
import io.github.hello09x.fakeplayer.core.repository.model.FeatureKey;
import io.github.hello09x.fakeplayer.core.repository.model.Feature;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@ -18,10 +18,10 @@ public class SetCommand extends AbstractCommand {
public void set(@NotNull CommandSender sender, @NotNull CommandArguments args) throws WrapperCommandSyntaxException {
var target = super.getFakeplayer(sender, args);
var key = (FeatureKey) Objects.requireNonNull(args.get("feature"));
var feature = (Feature) Objects.requireNonNull(args.get("feature"));
var value = (String) Objects.requireNonNull(args.get("option"));
var modifier = key.getModifier();
var modifier = feature.getModifier();
if (modifier == null) {
sender.sendMessage(translatable("fakeplayer.command.config.set.error.invalid-key", RED));
return;
@ -31,7 +31,7 @@ public class SetCommand extends AbstractCommand {
sender.sendMessage(translatable(
"fakeplayer.command.set.success",
text(target.getName(), WHITE),
translatable(key, GOLD),
translatable(feature, GOLD),
text(value, WHITE)
).color(GRAY));

View File

@ -5,7 +5,7 @@ import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException;
import dev.jorel.commandapi.executors.CommandArguments;
import io.github.hello09x.devtools.core.utils.ExperienceUtils;
import io.github.hello09x.fakeplayer.core.command.Permission;
import io.github.hello09x.fakeplayer.core.repository.model.FeatureKey;
import io.github.hello09x.fakeplayer.core.repository.model.Feature;
import io.github.hello09x.fakeplayer.core.util.Mth;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
@ -118,13 +118,13 @@ public class StatusCommand extends AbstractCommand {
private @NotNull Component getFeatureLine(@NotNull Player faker) {
var messages = new ArrayList<Component>();
for (var key : FeatureKey.values()) {
var detector = key.getDetector();
for (var feature : Feature.values()) {
var detector = feature.getDetector();
if (detector == null) {
continue;
}
var name = translatable(key, WHITE);
var options = key.getOptions();
var name = translatable(feature, WHITE);
var options = feature.getOptions();
var status = detector.apply(faker);
messages.add(textOfChildren(
@ -133,7 +133,7 @@ public class StatusCommand extends AbstractCommand {
join(separator(space()), options.stream().map(option -> {
var style = option.equals(status) ? Style.style(GREEN, UNDERLINED) : Style.style(GRAY);
return text("[" + option + "]").style(style).clickEvent(
runCommand("/fp set %s %s %s".formatted(key.name(), option, faker.getName()))
runCommand("/fp set %s %s %s".formatted(feature.name(), option, faker.getName()))
);
}).toList())
));

View File

@ -7,7 +7,7 @@ import com.google.inject.Singleton;
import io.github.hello09x.devtools.core.config.ConfigUtils;
import io.github.hello09x.devtools.core.config.PluginConfig;
import io.github.hello09x.fakeplayer.core.Main;
import io.github.hello09x.fakeplayer.core.repository.model.FeatureKey;
import io.github.hello09x.fakeplayer.core.repository.model.Feature;
import lombok.Getter;
import lombok.ToString;
import org.bukkit.Bukkit;
@ -157,7 +157,7 @@ public class FakeplayerConfig extends PluginConfig {
@Beta
private boolean defaultOnlineSkin;
private Map<FeatureKey, String> defaultFeatures;
private Map<Feature, String> defaultFeatures;
@Inject
public FakeplayerConfig() {
@ -197,7 +197,7 @@ public class FakeplayerConfig extends PluginConfig {
.collect(Collectors.toSet());
this.defaultOnlineSkin = file.getBoolean("default-online-skin", false);
this.defaultFeatures = Arrays.stream(FeatureKey.values())
this.defaultFeatures = Arrays.stream(Feature.values())
.collect(Collectors.toMap(Function.identity(), key -> file.getString("default-features." + key.name(), key.getDefaultOption())));
this.invseeImplement = ConfigUtils.getEnum(file, "invsee-implement", InvseeImplement.class, InvseeImplement.AUTO);
this.debug = file.getBoolean("debug", false);

View File

@ -15,7 +15,7 @@ import io.github.hello09x.fakeplayer.core.entity.Fakeplayer;
import io.github.hello09x.fakeplayer.core.entity.SpawnOption;
import io.github.hello09x.fakeplayer.core.manager.feature.FakeplayerFeatureManager;
import io.github.hello09x.fakeplayer.core.manager.naming.NameManager;
import io.github.hello09x.fakeplayer.core.repository.model.FeatureKey;
import io.github.hello09x.fakeplayer.core.repository.model.Feature;
import io.github.hello09x.fakeplayer.core.util.AddressUtils;
import io.github.hello09x.fakeplayer.core.util.Commands;
import net.kyori.adventure.text.Component;
@ -111,13 +111,13 @@ public class FakeplayerManager {
var configs = featureManager.getFeatures(creator);
return new SpawnOption(
spawnAt,
configs.get(FeatureKey.invulnerable).asBoolean(),
configs.get(FeatureKey.collidable).asBoolean(),
configs.get(FeatureKey.look_at_entity).asBoolean(),
configs.get(FeatureKey.pickup_items).asBoolean(),
configs.get(FeatureKey.skin).asBoolean(),
configs.get(FeatureKey.replenish).asBoolean(),
configs.get(FeatureKey.autofish).asBoolean()
configs.get(Feature.invulnerable).asBoolean(),
configs.get(Feature.collidable).asBoolean(),
configs.get(Feature.look_at_entity).asBoolean(),
configs.get(Feature.pickup_items).asBoolean(),
configs.get(Feature.skin).asBoolean(),
configs.get(Feature.replenish).asBoolean(),
configs.get(Feature.autofish).asBoolean()
);
})
.thenComposeAsync(fp::spawnAsync)

View File

@ -4,7 +4,7 @@ import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.github.hello09x.fakeplayer.core.config.FakeplayerConfig;
import io.github.hello09x.fakeplayer.core.repository.UserConfigRepository;
import io.github.hello09x.fakeplayer.core.repository.model.FeatureKey;
import io.github.hello09x.fakeplayer.core.repository.model.Feature;
import io.github.hello09x.fakeplayer.core.repository.model.UserConfig;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -29,45 +29,45 @@ public class FakeplayerFeatureManager {
this.config = config;
}
private @NotNull String getDefaultOption(@NotNull FeatureKey key) {
private @NotNull String getDefaultOption(@NotNull Feature key) {
return Optional.ofNullable(config.getDefaultFeatures().get(key)).filter(option -> key.getOptions().contains(option)).orElse(key.getDefaultOption());
}
public @NotNull Feature getFeature(@NotNull Player player, @NotNull FeatureKey key) {
public @NotNull FeatureInstance getFeature(@NotNull Player player, @NotNull Feature key) {
if (!key.testPermissions(player)) {
return new Feature(key, this.getDefaultOption(key));
return new FeatureInstance(key, this.getDefaultOption(key));
}
String value = Optional.ofNullable(repository.selectByPlayerIdAndKey(player.getUniqueId(), key))
.map(UserConfig::value)
.orElseGet(() -> this.getDefaultOption(key));
return new Feature(key, value);
return new FeatureInstance(key, value);
}
public @NotNull Map<FeatureKey, Feature> getFeatures(@NotNull CommandSender sender) {
Map<FeatureKey, UserConfig> userConfigs;
public @NotNull Map<Feature, FeatureInstance> getFeatures(@NotNull CommandSender sender) {
Map<Feature, UserConfig> userConfigs;
if (sender instanceof Player player) {
userConfigs = repository.selectByPlayerId(player.getUniqueId()).stream().collect(Collectors.toMap(UserConfig::key, Function.identity()));
} else {
userConfigs = Collections.emptyMap();
}
var configs = new LinkedHashMap<FeatureKey, Feature>(FeatureKey.values().length, 1.0F);
for (var key : FeatureKey.values()) {
var configs = new LinkedHashMap<Feature, FeatureInstance>(Feature.values().length, 1.0F);
for (var key : Feature.values()) {
String value;
if (!key.testPermissions(sender)) {
value = this.getDefaultOption(key);
} else {
value = Optional.ofNullable(userConfigs.get(key)).map(UserConfig::value).orElseGet(() -> this.getDefaultOption(key));
}
configs.put(key, new Feature(key, value));
configs.put(key, new FeatureInstance(key, value));
}
return configs;
}
public void setFeature(@NotNull Player player, @NotNull FeatureKey key, @NotNull String value) {
public void setFeature(@NotNull Player player, @NotNull Feature key, @NotNull String value) {
this.repository.saveOrUpdate(new UserConfig(
null,
player.getUniqueId(),

View File

@ -1,16 +1,16 @@
package io.github.hello09x.fakeplayer.core.manager.feature;
import io.github.hello09x.fakeplayer.core.repository.model.FeatureKey;
import io.github.hello09x.fakeplayer.core.repository.model.Feature;
import org.jetbrains.annotations.NotNull;
/**
* @author tanyaofei
* @since 2024/8/13
**/
public record Feature(
public record FeatureInstance(
@NotNull
FeatureKey key,
Feature key,
@NotNull
String value

View File

@ -3,7 +3,7 @@ package io.github.hello09x.fakeplayer.core.repository;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.github.hello09x.devtools.database.jdbc.JdbcTemplate;
import io.github.hello09x.fakeplayer.core.repository.model.FeatureKey;
import io.github.hello09x.fakeplayer.core.repository.model.Feature;
import io.github.hello09x.fakeplayer.core.repository.model.UserConfig;
import io.github.hello09x.fakeplayer.core.repository.model.UserConfigRowMapper;
import org.jetbrains.annotations.NotNull;
@ -23,9 +23,9 @@ public class UserConfigRepository {
this.initTables();
}
public @Nullable UserConfig selectByPlayerIdAndKey(@NotNull UUID playerId, @NotNull FeatureKey featureKey) {
public @Nullable UserConfig selectByPlayerIdAndKey(@NotNull UUID playerId, @NotNull Feature feature) {
var sql = "select * from user_config where player_id = ? and `key` = ?";
return jdbc.queryForObject(sql, UserConfigRowMapper.instance, playerId.toString(), featureKey.name());
return jdbc.queryForObject(sql, UserConfigRowMapper.instance, playerId.toString(), feature.name());
}
public int saveOrUpdate(@NotNull UserConfig config) {

View File

@ -21,7 +21,7 @@ import java.util.function.Function;
**/
@Getter
@AllArgsConstructor
public enum FeatureKey implements Translatable, Singletons {
public enum Feature implements Translatable, Singletons {
/**
* 是否具有碰撞箱

View File

@ -13,7 +13,7 @@ public record UserConfig(
UUID playerId,
@NotNull
FeatureKey key,
Feature key,
@NotNull
String value

View File

@ -21,7 +21,7 @@ public class UserConfigRowMapper implements RowMapper<UserConfig> {
return new UserConfig(
rs.getInt("id"),
UUID.fromString(rs.getString("player_id")),
FeatureKey.valueOf(rs.getString("key")),
Feature.valueOf(rs.getString("key")),
rs.getString("value")
);
}