mirror of
https://github.com/CarmJos/MineConfiguration.git
synced 2026-06-04 21:58:16 +08:00
[1.4.0]refactor(all): project reformatted.
BREAKING CHANGE: reformatted projects including project settings and package paths.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-bukkit</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
</properties>
|
||||
<artifactId>mineconfiguration-bukkit-base</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-core</artifactId>
|
||||
<version>${easyconfiguration.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>mineconfiguration-common</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.CraftConfigBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.source.CraftConfigProvider;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.source.CraftSectionWrapper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class CraftConfigValue<T> extends CachedConfigValue<T> {
|
||||
|
||||
public static @NotNull CraftConfigBuilder builder() {
|
||||
return new CraftConfigBuilder();
|
||||
}
|
||||
|
||||
public CraftConfigValue(@Nullable CraftConfigProvider provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable T defaultValue) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
|
||||
}
|
||||
|
||||
public CraftConfigProvider getBukkitProvider() {
|
||||
ConfigurationProvider<?> provider = getProvider();
|
||||
if (provider instanceof CraftConfigProvider) return (CraftConfigProvider) getProvider();
|
||||
else throw new IllegalStateException("Provider is not a SpigotConfigProvider");
|
||||
}
|
||||
|
||||
public CraftSectionWrapper getBukkitConfig() {
|
||||
return getBukkitProvider().getConfiguration();
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.builder;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.source.CraftConfigProvider;
|
||||
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
||||
|
||||
public abstract class AbstractCraftBuilder<T, B extends AbstractCraftBuilder<T, B>>
|
||||
extends AbstractConfigBuilder<T, B, CraftConfigProvider> {
|
||||
|
||||
public AbstractCraftBuilder() {
|
||||
super(CraftConfigProvider.class);
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.builder;
|
||||
|
||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.item.ItemConfigBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.message.CraftMessageBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.serializable.SerializableBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.sound.SoundConfigBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.title.TitleConfigBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.ItemConfig;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.value.ConfiguredItem;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class CraftConfigBuilder extends ConfigBuilder {
|
||||
|
||||
public @NotNull SoundConfigBuilder createSound() {
|
||||
return new SoundConfigBuilder();
|
||||
}
|
||||
|
||||
public @NotNull ItemConfigBuilder createItem() {
|
||||
return new ItemConfigBuilder();
|
||||
}
|
||||
|
||||
public @NotNull CraftMessageBuilder createMessage() {
|
||||
return new CraftMessageBuilder();
|
||||
}
|
||||
|
||||
public @NotNull TitleConfigBuilder createTitle() {
|
||||
return new TitleConfigBuilder();
|
||||
}
|
||||
|
||||
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
|
||||
return new SerializableBuilder<>(valueClass);
|
||||
}
|
||||
|
||||
public @NotNull ConfiguredItem ofItem() {
|
||||
return createItem().build();
|
||||
}
|
||||
|
||||
public @NotNull ConfiguredItem ofItem(@Nullable ItemConfig defaultItem) {
|
||||
return createItem().defaults(defaultItem).build();
|
||||
}
|
||||
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.builder.item;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.AbstractCraftBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.ItemConfig;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.value.ConfiguredItem;
|
||||
import cc.carm.lib.configuration.minecraft.common.utils.ParamsUtils;
|
||||
import org.bukkit.Material;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConfigBuilder> {
|
||||
|
||||
protected Material type;
|
||||
protected short data;
|
||||
protected String name;
|
||||
protected List<String> lore = new ArrayList<>();
|
||||
|
||||
protected @NotNull String[] params;
|
||||
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
|
||||
|
||||
public ItemConfigBuilder defaults(@NotNull Material type,
|
||||
@Nullable String name, @NotNull String... lore) {
|
||||
return defaults(type, (short) 0, name, Arrays.asList(lore));
|
||||
}
|
||||
|
||||
public ItemConfigBuilder defaults(@NotNull Material type, short data,
|
||||
@Nullable String name, @NotNull String... lore) {
|
||||
return defaults(type, data, name, Arrays.asList(lore));
|
||||
}
|
||||
|
||||
public ItemConfigBuilder defaults(@NotNull Material type, short data,
|
||||
@Nullable String name, @NotNull List<String> lore) {
|
||||
return defaultType(type).defaultDataID(data).defaultName(name).defaultLore(lore);
|
||||
}
|
||||
|
||||
public ItemConfigBuilder defaultType(@NotNull Material type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemConfigBuilder defaultName(@Nullable String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemConfigBuilder defaultDataID(short dataID) {
|
||||
this.data = dataID;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemConfigBuilder defaultLore(@NotNull String... lore) {
|
||||
return defaultLore(Arrays.asList(lore));
|
||||
}
|
||||
|
||||
public ItemConfigBuilder defaultLore(@NotNull List<String> lore) {
|
||||
this.lore = new ArrayList<>(lore);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemConfigBuilder formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
|
||||
this.paramFormatter = paramFormatter;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public ItemConfigBuilder params(@NotNull String... params) {
|
||||
this.params = params;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public ItemConfigBuilder params(@NotNull List<String> params) {
|
||||
this.params = params.toArray(new String[0]);
|
||||
return getThis();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull ItemConfigBuilder getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
protected @Nullable ItemConfig buildDefault() {
|
||||
if (this.type == null) return null;
|
||||
else return new ItemConfig(type, data, name, lore);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredItem build() {
|
||||
ItemConfig defaultItem = Optional.ofNullable(this.defaultValue).orElse(buildDefault());
|
||||
return new ConfiguredItem(this.provider, this.path, this.headerComments, this.inlineComment, defaultItem, buildParams());
|
||||
}
|
||||
|
||||
protected final String[] buildParams() {
|
||||
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.builder.message;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.TextConfig;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.utils.TextParser;
|
||||
import cc.carm.lib.configuration.minecraft.common.builder.message.MessageConfigBuilder;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class CraftMessageBuilder extends MessageConfigBuilder<CommandSender, TextConfig> {
|
||||
|
||||
|
||||
public CraftMessageBuilder() {
|
||||
super(CommandSender.class, TextConfig.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <M> CraftMessageValueBuilder<M> asValue(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
return new CraftMessageValueBuilder<>(parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <M> CraftMessageListBuilder<M> asList(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
return new CraftMessageListBuilder<>(parser);
|
||||
}
|
||||
|
||||
public @NotNull
|
||||
CraftMessageValueBuilder<String> asStringValue() {
|
||||
return asValue(defaultParser()).whenSend(CommandSender::sendMessage);
|
||||
}
|
||||
|
||||
public @NotNull
|
||||
CraftMessageListBuilder<String> asStringList() {
|
||||
return asList(defaultParser()).whenSend((r, m) -> m.forEach(r::sendMessage));
|
||||
}
|
||||
|
||||
protected static @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable String> defaultParser() {
|
||||
return (receiver, message) -> TextParser.parseText(receiver, message, new HashMap<>());
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.builder.message;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.TextConfig;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.value.ConfiguredMessageList;
|
||||
import cc.carm.lib.configuration.minecraft.common.builder.message.MessageListBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.common.utils.ParamsUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class CraftMessageListBuilder<M>
|
||||
extends MessageListBuilder<M, CommandSender, TextConfig, CraftMessageListBuilder<M>> {
|
||||
|
||||
public CraftMessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
super(CommandSender.class, TextConfig::of, parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull CraftMessageListBuilder<M> getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredMessageList<M> build() {
|
||||
return new ConfiguredMessageList<>(
|
||||
this.provider, this.path, this.headerComments, this.inlineComment,
|
||||
Optional.ofNullable(this.defaultValue).orElse(TextConfig.of(new ArrayList<>())),
|
||||
ParamsUtils.formatParams(this.paramFormatter, this.params),
|
||||
this.messageParser, this.sendFunction
|
||||
);
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.builder.message;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.TextConfig;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.value.ConfiguredMessage;
|
||||
import cc.carm.lib.configuration.minecraft.common.builder.message.MessageValueBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.common.utils.ParamsUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class CraftMessageValueBuilder<M>
|
||||
extends MessageValueBuilder<M, CommandSender, TextConfig, CraftMessageValueBuilder<M>> {
|
||||
|
||||
public CraftMessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
super(CommandSender.class, TextConfig::new, parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull CraftMessageValueBuilder<M> getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredMessage<M> build() {
|
||||
return new ConfiguredMessage<>(
|
||||
this.provider, this.path, this.headerComments, this.inlineComment,
|
||||
Optional.ofNullable(this.defaultValue).orElse(TextConfig.of("")),
|
||||
ParamsUtils.formatParams(this.paramFormatter, this.params),
|
||||
this.messageParser, this.sendHandler
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.builder.serializable;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.AbstractCraftBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.value.ConfiguredSerializable;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SerializableBuilder<T extends ConfigurationSerializable>
|
||||
extends AbstractCraftBuilder<T, SerializableBuilder<T>> {
|
||||
|
||||
protected final @NotNull Class<T> valueClass;
|
||||
|
||||
public SerializableBuilder(@NotNull Class<T> valueClass) {
|
||||
this.valueClass = valueClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull SerializableBuilder<T> getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredSerializable<T> build() {
|
||||
return new ConfiguredSerializable<>(this.provider, this.path, this.headerComments, this.inlineComment, this.valueClass, this.defaultValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.builder.sound;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.AbstractCraftBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.SoundConfig;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.value.ConfiguredSound;
|
||||
import org.bukkit.Sound;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SoundConfigBuilder extends AbstractCraftBuilder<SoundConfig, SoundConfigBuilder> {
|
||||
|
||||
|
||||
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume, float pitch) {
|
||||
return defaults(new SoundConfig(sound.name(), sound, volume, pitch));
|
||||
}
|
||||
|
||||
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume) {
|
||||
return defaults(sound, volume, 1.0f);
|
||||
}
|
||||
|
||||
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound) {
|
||||
return defaults(sound, 1.0f);
|
||||
}
|
||||
|
||||
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName, float volume, float pitch) {
|
||||
return defaults(new SoundConfig(soundName, volume, pitch));
|
||||
}
|
||||
|
||||
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName, float volume) {
|
||||
return defaults(soundName, volume, 1.0f);
|
||||
}
|
||||
|
||||
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName) {
|
||||
return defaults(soundName, 1.0f);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull SoundConfigBuilder getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredSound build() {
|
||||
return new ConfiguredSound(this.provider, this.path, this.headerComments, this.inlineComment, this.defaultValue);
|
||||
}
|
||||
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.builder.title;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.AbstractCraftBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.TitleConfig;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.function.TitleSendConsumer;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.utils.ProtocolLibHelper;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.value.ConfiguredTitle;
|
||||
import cc.carm.lib.configuration.minecraft.common.utils.ParamsUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Range;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class TitleConfigBuilder extends AbstractCraftBuilder<TitleConfig, TitleConfigBuilder> {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
protected static @NotNull TitleSendConsumer DEFAULT_TITLE_CONSUMER = (player, fadeIn, stay, fadeOut, line1, line2) -> {
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||
try {
|
||||
ProtocolLibHelper.sendTitle(player, fadeIn, stay, fadeOut, line1, line2);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
} else {
|
||||
player.sendTitle(line1, line2);
|
||||
}
|
||||
};
|
||||
|
||||
protected @NotNull String[] params;
|
||||
|
||||
protected @Range(from = 0L, to = Integer.MAX_VALUE) int fadeIn = 10;
|
||||
protected @Range(from = 0L, to = Integer.MAX_VALUE) int stay = 60;
|
||||
protected @Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut = 10;
|
||||
|
||||
protected @NotNull TitleSendConsumer sendConsumer;
|
||||
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
|
||||
|
||||
public TitleConfigBuilder() {
|
||||
this.sendConsumer = TitleConfigBuilder.DEFAULT_TITLE_CONSUMER;
|
||||
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
|
||||
}
|
||||
|
||||
public @NotNull TitleConfigBuilder defaults(@Nullable String line1,
|
||||
@Nullable String line2) {
|
||||
return defaults(TitleConfig.of(line1, line2));
|
||||
}
|
||||
|
||||
public @NotNull TitleConfigBuilder whenSend(@NotNull TitleSendConsumer consumer) {
|
||||
this.sendConsumer = consumer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TitleConfigBuilder params(String... params) {
|
||||
this.params = params;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TitleConfigBuilder params(@NotNull List<String> params) {
|
||||
return params(params.toArray(new String[0]));
|
||||
}
|
||||
|
||||
public TitleConfigBuilder fadeIn(@Range(from = 0L, to = Integer.MAX_VALUE) int fadeIn) {
|
||||
this.fadeIn = fadeIn;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TitleConfigBuilder stay(@Range(from = 0L, to = Integer.MAX_VALUE) int stay) {
|
||||
this.stay = stay;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TitleConfigBuilder fadeOut(@Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut) {
|
||||
this.fadeOut = fadeOut;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TitleConfigBuilder formatParam(Function<String, String> paramFormatter) {
|
||||
this.paramFormatter = paramFormatter;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull TitleConfigBuilder getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredTitle build() {
|
||||
return new ConfiguredTitle(
|
||||
this.provider, this.path, this.headerComments, this.inlineComment,
|
||||
this.defaultValue, ParamsUtils.formatParams(this.paramFormatter, this.params),
|
||||
this.sendConsumer, this.fadeIn, this.stay, this.fadeOut
|
||||
);
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.data;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.utils.TextParser;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ItemConfig {
|
||||
|
||||
protected @NotNull Material type;
|
||||
protected short data;
|
||||
protected @Nullable String name;
|
||||
protected @NotNull List<String> lore;
|
||||
|
||||
public ItemConfig(@NotNull Material type, short damage,
|
||||
@Nullable String name, @NotNull List<String> lore) {
|
||||
this.type = type;
|
||||
this.data = damage;
|
||||
this.name = name;
|
||||
this.lore = lore;
|
||||
}
|
||||
|
||||
public @NotNull Material getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public short getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public @Nullable String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public @Nullable String getName(@Nullable Player player, @NotNull Map<String, Object> placeholders) {
|
||||
return Optional.ofNullable(getName())
|
||||
.map(name -> TextParser.parseText(player, name, placeholders))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public @NotNull List<String> getLore() {
|
||||
return lore;
|
||||
}
|
||||
|
||||
public @Nullable List<String> getLore(@Nullable Player player, @NotNull Map<String, Object> placeholders) {
|
||||
if (getLore().isEmpty()) return null;
|
||||
else return TextParser.parseList(player, getLore(), placeholders);
|
||||
}
|
||||
|
||||
public final @NotNull ItemStack getItemStack() {
|
||||
return getItemStack(1);
|
||||
}
|
||||
|
||||
public @NotNull ItemStack getItemStack(int amount) {
|
||||
return getItemStack(null, amount, new HashMap<>());
|
||||
}
|
||||
|
||||
public @NotNull ItemStack getItemStack(@Nullable Player player) {
|
||||
return getItemStack(player, new HashMap<>());
|
||||
}
|
||||
|
||||
public @NotNull ItemStack getItemStack(@Nullable Player player, @NotNull Map<String, Object> placeholders) {
|
||||
return getItemStack(player, 1, placeholders);
|
||||
}
|
||||
|
||||
public @NotNull ItemStack getItemStack(@Nullable Player player, int amount, @NotNull Map<String, Object> placeholders) {
|
||||
ItemStack item = new ItemStack(type, amount, data);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) return item;
|
||||
Optional.ofNullable(getName(player, placeholders)).ifPresent(meta::setDisplayName);
|
||||
Optional.ofNullable(getLore(player, placeholders)).ifPresent(meta::setLore);
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
public @NotNull Map<String, Object> serialize() {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("type", type.name());
|
||||
if (this.data != 0) map.put("data", data);
|
||||
if (name != null) map.put("name", name);
|
||||
if (!lore.isEmpty()) map.put("lore", lore);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public static @NotNull ItemConfig deserialize(@NotNull ConfigurationWrapper section) throws Exception {
|
||||
String typeName = section.getString("type");
|
||||
if (typeName == null) throw new NullPointerException("Item type name is null");
|
||||
|
||||
Material type = Material.matchMaterial(typeName);
|
||||
if (type == null) throw new Exception("Invalid material name: " + typeName);
|
||||
else return new ItemConfig(
|
||||
type, section.getShort("data", (short) 0),
|
||||
section.getString("name"),
|
||||
section.getStringList("lore")
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.data;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SoundConfig {
|
||||
|
||||
protected @NotNull String typeName;
|
||||
protected @Nullable Sound type;
|
||||
protected float volume;
|
||||
protected float pitch;
|
||||
|
||||
public SoundConfig(@NotNull String typeName) {
|
||||
this(typeName, 1, 1);
|
||||
}
|
||||
|
||||
public SoundConfig(@NotNull String typeName, float volume) {
|
||||
this(typeName, volume, 1);
|
||||
}
|
||||
|
||||
public SoundConfig(@NotNull String typeName, float volume, float pitch) {
|
||||
this(typeName, Arrays.stream(Sound.values()).filter(s -> s.name().equalsIgnoreCase(typeName)).findFirst().orElse(null), volume, pitch);
|
||||
}
|
||||
|
||||
public SoundConfig(@NotNull String typeName, @Nullable Sound type, float volume, float pitch) {
|
||||
this.typeName = typeName;
|
||||
this.type = type;
|
||||
this.volume = volume;
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
public void playTo(Player player) {
|
||||
if (type == null) return;
|
||||
player.playSound(player.getLocation(), type, volume, pitch);
|
||||
}
|
||||
|
||||
public void playToAll() {
|
||||
Bukkit.getOnlinePlayers().forEach(this::playTo);
|
||||
}
|
||||
|
||||
public @NotNull String getTypeName() {
|
||||
return typeName;
|
||||
}
|
||||
|
||||
public @Nullable Sound getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public float getPitch() {
|
||||
return pitch;
|
||||
}
|
||||
|
||||
public float getVolume() {
|
||||
return volume;
|
||||
}
|
||||
|
||||
public void setType(@NotNull Sound type) {
|
||||
this.typeName = type.name();
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void setVolume(float volume) {
|
||||
this.volume = volume;
|
||||
}
|
||||
|
||||
public void setPitch(float pitch) {
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
public @NotNull String serialize() {
|
||||
if (pitch != 1) {
|
||||
return typeName + ":" + volume + ":" + pitch;
|
||||
} else if (volume != 1) {
|
||||
return typeName + ":" + volume;
|
||||
} else {
|
||||
return typeName;
|
||||
}
|
||||
}
|
||||
|
||||
@Contract("null -> null")
|
||||
public static @Nullable SoundConfig deserialize(@Nullable String string) throws Exception {
|
||||
if (string == null) return null;
|
||||
|
||||
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
|
||||
if (args.length < 1) return null;
|
||||
|
||||
try {
|
||||
return new SoundConfig(
|
||||
args[0],
|
||||
Sound.valueOf(args[0]),
|
||||
(args.length >= 2) ? Float.parseFloat(args[1]) : 1,
|
||||
(args.length >= 3) ? Float.parseFloat(args[2]) : 1
|
||||
);
|
||||
} catch (Exception exception) {
|
||||
throw new Exception("Sound " + string + " wasn't configured correctly.", exception);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.data;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.common.data.AbstractText;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TextConfig extends AbstractText<CommandSender> {
|
||||
|
||||
public TextConfig(@NotNull String message) {
|
||||
super(CommandSender.class, message);
|
||||
}
|
||||
|
||||
@Contract("!null,-> !null")
|
||||
public static @Nullable TextConfig of(@Nullable String message) {
|
||||
if (message == null) return null;
|
||||
else return new TextConfig(message);
|
||||
}
|
||||
|
||||
public static @NotNull List<TextConfig> of(@Nullable List<String> messages) {
|
||||
if (messages == null || messages.isEmpty()) return new ArrayList<>();
|
||||
else return messages.stream().map(TextConfig::of).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static @NotNull List<TextConfig> of(@NotNull String... messages) {
|
||||
return of(Arrays.asList(messages));
|
||||
}
|
||||
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.data;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.function.TitleSendConsumer;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.utils.TextParser;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Range;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class TitleConfig {
|
||||
|
||||
public static @NotNull TitleConfig of(@Nullable String line1, @Nullable String line2) {
|
||||
return of(
|
||||
Optional.ofNullable(line1).map(TextConfig::of).orElse(null),
|
||||
Optional.ofNullable(line2).map(TextConfig::of).orElse(null)
|
||||
);
|
||||
}
|
||||
|
||||
public static @NotNull TitleConfig of(@Nullable TextConfig line1, @Nullable TextConfig line2) {
|
||||
return new TitleConfig(line1, line2);
|
||||
}
|
||||
|
||||
protected @Nullable TextConfig line1;
|
||||
protected @Nullable TextConfig line2;
|
||||
|
||||
protected TitleConfig(@Nullable TextConfig line1, @Nullable TextConfig line2) {
|
||||
this.line1 = line1;
|
||||
this.line2 = line2;
|
||||
}
|
||||
|
||||
public void send(@NotNull Player player,
|
||||
@Range(from = 0L, to = Long.MAX_VALUE) int fadeIn,
|
||||
@Range(from = 0L, to = Long.MAX_VALUE) int stay,
|
||||
@Range(from = 0L, to = Long.MAX_VALUE) int fadeOut,
|
||||
@NotNull Map<String, Object> placeholders,
|
||||
@Nullable TitleSendConsumer sendConsumer) {
|
||||
if (this.line1 == null && this.line2 == null) return;
|
||||
if (sendConsumer == null) return;
|
||||
sendConsumer.send(
|
||||
player, fadeIn, stay, fadeOut,
|
||||
parseLine(this.line1, player, placeholders),
|
||||
parseLine(this.line2, player, placeholders)
|
||||
);
|
||||
}
|
||||
|
||||
protected @NotNull String parseLine(@Nullable TextConfig text,
|
||||
@NotNull Player player, @NotNull Map<String, Object> placeholders) {
|
||||
if (text == null) return "";
|
||||
else return TextParser.parseText(player, text.getMessage(), placeholders);
|
||||
}
|
||||
|
||||
public @NotNull Map<String, Object> serialize() {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
if (this.line1 != null) map.put("line1", this.line1.getMessage());
|
||||
if (this.line2 != null) map.put("line2", this.line2.getMessage());
|
||||
return map;
|
||||
}
|
||||
|
||||
public static @NotNull TitleConfig deserialize(@NotNull ConfigurationWrapper section) {
|
||||
return of(section.getString("line1"), section.getString("line2"));
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.function;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Range;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface TitleSendConsumer {
|
||||
|
||||
/**
|
||||
* 向目标玩家发送标题文字
|
||||
*
|
||||
* @param player 目标玩家
|
||||
* @param fadeIn 淡入时间 (ticks)
|
||||
* @param stay 保留时间 (ticks)
|
||||
* @param fadeOut 淡出时间 (ticks)
|
||||
* @param line1 第一行文字
|
||||
* @param line2 第二行文字
|
||||
*/
|
||||
void send(@NotNull Player player,
|
||||
@Range(from = 0L, to = Integer.MAX_VALUE) int fadeIn,
|
||||
@Range(from = 0L, to = Integer.MAX_VALUE) int stay,
|
||||
@Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut,
|
||||
@NotNull String line1, @NotNull String line2);
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public abstract class CraftConfigProvider extends FileConfigProvider<CraftSectionWrapper> {
|
||||
|
||||
public static final char SEPARATOR = '.';
|
||||
|
||||
protected ConfigInitializer<? extends CraftConfigProvider> initializer;
|
||||
protected YamlConfiguration configuration;
|
||||
|
||||
public CraftConfigProvider(@NotNull File file) {
|
||||
super(file);
|
||||
}
|
||||
|
||||
public abstract void initializeConfig();
|
||||
|
||||
@Override
|
||||
public @NotNull CraftSectionWrapper getConfiguration() {
|
||||
return CraftSectionWrapper.of(this.configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onReload() throws Exception {
|
||||
configuration.load(getFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws Exception {
|
||||
configuration.save(getFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigInitializer<? extends CraftConfigProvider> getInitializer() {
|
||||
return this.initializer;
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class CraftSectionWrapper implements ConfigurationWrapper {
|
||||
|
||||
protected final ConfigurationSection section;
|
||||
|
||||
protected CraftSectionWrapper(ConfigurationSection section) {
|
||||
this.section = section;
|
||||
}
|
||||
|
||||
public ConfigurationSection getSourceSection() {
|
||||
return section;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<String> getKeys(boolean deep) {
|
||||
return this.section.getKeys(deep);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> getValues(boolean deep) {
|
||||
return this.section.getValues(deep);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@NotNull String path, @Nullable Object value) {
|
||||
this.section.set(path, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull String path) {
|
||||
return this.section.contains(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object get(@NotNull String path) {
|
||||
return this.section.get(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isList(@NotNull String path) {
|
||||
return this.section.isList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<?> getList(@NotNull String path) {
|
||||
return this.section.getList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConfigurationSection(@NotNull String path) {
|
||||
return this.section.isConfigurationSection(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
||||
return Optional.ofNullable(section.getConfigurationSection(path))
|
||||
.map(CraftSectionWrapper::of).orElse(null);
|
||||
}
|
||||
|
||||
public static CraftSectionWrapper of(ConfigurationSection section) {
|
||||
return new CraftSectionWrapper(section);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.utils;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PlaceholderAPIHelper {
|
||||
|
||||
public static String parseMessages(Player player, String message) {
|
||||
try {
|
||||
return PlaceholderAPI.setPlaceholders(player, message);
|
||||
} catch (Exception ignored) {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> parseMessages(Player player, List<String> messages) {
|
||||
try {
|
||||
return PlaceholderAPI.setPlaceholders(player, messages);
|
||||
} catch (Exception ignored) {
|
||||
return messages;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.utils;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.ProtocolManager;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.wrappers.EnumWrappers;
|
||||
import com.comphenix.protocol.wrappers.WrappedChatComponent;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class ProtocolLibHelper {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void sendTitle(Player player, long fadeIn, long stay, long fadeOut, String line1, String line2) throws Exception {
|
||||
ProtocolManager pm = ProtocolLibrary.getProtocolManager();
|
||||
|
||||
if (line1 != null) {
|
||||
PacketContainer packet = pm.createPacket(PacketType.Play.Server.TITLE);
|
||||
packet.getTitleActions().write(0, EnumWrappers.TitleAction.TITLE);
|
||||
packet.getChatComponents().write(0, WrappedChatComponent.fromText(line1));
|
||||
pm.sendServerPacket(player, packet, false);
|
||||
}
|
||||
|
||||
if (line2 != null) {
|
||||
PacketContainer packet = pm.createPacket(PacketType.Play.Server.TITLE);
|
||||
packet.getTitleActions().write(0, EnumWrappers.TitleAction.SUBTITLE);
|
||||
packet.getChatComponents().write(0, WrappedChatComponent.fromText(line2));
|
||||
pm.sendServerPacket(player, packet, false);
|
||||
}
|
||||
|
||||
PacketContainer timePacket = pm.createPacket(PacketType.Play.Server.TITLE);
|
||||
timePacket.getTitleActions().write(0, EnumWrappers.TitleAction.TIMES);
|
||||
timePacket.getIntegers()
|
||||
.write(0, Math.toIntExact(fadeIn))
|
||||
.write(1, Math.toIntExact(stay))
|
||||
.write(2, Math.toIntExact(fadeOut));
|
||||
pm.sendServerPacket(player, timePacket, false);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.utils;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.common.utils.ColorParser;
|
||||
import cc.carm.lib.configuration.minecraft.common.utils.ParamsUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TextParser {
|
||||
|
||||
@Contract("_,!null,_->!null")
|
||||
public static @Nullable String parseText(@Nullable CommandSender sender, @Nullable String message, @NotNull Map<String, Object> placeholders) {
|
||||
if (message == null) return null;
|
||||
if (sender instanceof Player && hasPlaceholderAPI()) {
|
||||
message = PlaceholderAPIHelper.parseMessages((Player) sender, message);
|
||||
}
|
||||
return ColorParser.parse(ParamsUtils.setPlaceholders(message, placeholders));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> parseList(@Nullable CommandSender sender, List<String> messages, @NotNull Map<String, Object> placeholders) {
|
||||
if (sender instanceof Player && hasPlaceholderAPI()) {
|
||||
messages = PlaceholderAPIHelper.parseMessages((Player) sender, messages);
|
||||
}
|
||||
return ColorParser.parse(messages.stream().map(s -> ParamsUtils.setPlaceholders(s, placeholders)).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public static boolean hasPlaceholderAPI() {
|
||||
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
|
||||
}
|
||||
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.value;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.CraftConfigValue;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.ItemConfig;
|
||||
import cc.carm.lib.configuration.minecraft.common.utils.ParamsUtils;
|
||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.item.ItemConfigBuilder;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
|
||||
|
||||
public static ItemConfigBuilder create() {
|
||||
return CraftConfigValue.builder().createItem();
|
||||
}
|
||||
|
||||
public static ConfiguredItem of() {
|
||||
return CraftConfigValue.builder().ofItem();
|
||||
}
|
||||
|
||||
public static ConfiguredItem of(@Nullable ItemConfig defaultItem) {
|
||||
return CraftConfigValue.builder().ofItem(defaultItem);
|
||||
}
|
||||
|
||||
protected final @NotNull String[] params;
|
||||
|
||||
public ConfiguredItem(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable ItemConfig defaultValue, @NotNull String[] params) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, ItemConfig.class, defaultValue, getItemParser(), ItemConfig::serialize);
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
public static ConfigValueParser<ConfigurationWrapper, ItemConfig> getItemParser() {
|
||||
return (s, d) -> ItemConfig.deserialize(s);
|
||||
}
|
||||
|
||||
public @NotNull String[] getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
public @Nullable ItemStack getItem(@Nullable Player player) {
|
||||
return getItem(player, 1);
|
||||
}
|
||||
|
||||
public @Nullable ItemStack getItem(@Nullable Player player, int amount) {
|
||||
return getItem(player, amount, new HashMap<>());
|
||||
}
|
||||
|
||||
public @Nullable ItemStack getItem(@Nullable Player player, int amount, @NotNull Object... values) {
|
||||
return getItem(player, amount, ParamsUtils.buildParams(params, values));
|
||||
}
|
||||
|
||||
public @Nullable ItemStack getItem(@Nullable Player player, int amount, @NotNull String[] params, @NotNull Object[] values) {
|
||||
return getItem(player, amount, ParamsUtils.buildParams(params, values));
|
||||
}
|
||||
|
||||
public @Nullable ItemStack getItem(@Nullable Player player, int amount, @NotNull Map<String, Object> placeholders) {
|
||||
return getOptional().map(item -> item.getItemStack(player, amount, placeholders)).orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.value;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.CraftConfigValue;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.TextConfig;
|
||||
import cc.carm.lib.configuration.minecraft.common.value.ConfigMessage;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.message.CraftMessageValueBuilder;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class ConfiguredMessage<M> extends ConfigMessage<M, TextConfig, CommandSender> {
|
||||
|
||||
@NotNull
|
||||
public static <M> CraftMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
|
||||
return CraftConfigValue.builder().createMessage().asValue(messageParser);
|
||||
}
|
||||
|
||||
public static CraftMessageValueBuilder<String> asString() {
|
||||
return CraftConfigValue.builder().createMessage().asStringValue();
|
||||
}
|
||||
|
||||
public static ConfiguredMessage<String> ofString() {
|
||||
return asString().build();
|
||||
}
|
||||
|
||||
public static ConfiguredMessage<String> ofString(@NotNull String defaultMessage) {
|
||||
return asString().defaults(defaultMessage).build();
|
||||
}
|
||||
|
||||
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@NotNull TextConfig defaultMessage, @NotNull String[] params,
|
||||
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
|
||||
@NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, TextConfig.class, defaultMessage, params, messageParser, sendFunction, TextConfig::of);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcast(@NotNull Map<String, Object> placeholders) {
|
||||
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, placeholders));
|
||||
send(Bukkit.getConsoleSender(), placeholders);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.value;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.CraftConfigValue;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.TextConfig;
|
||||
import cc.carm.lib.configuration.minecraft.common.value.ConfigMessageList;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.message.CraftMessageListBuilder;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class ConfiguredMessageList<M> extends ConfigMessageList<M, TextConfig, CommandSender> {
|
||||
|
||||
@NotNull
|
||||
public static <M> CraftMessageListBuilder<M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
|
||||
return CraftConfigValue.builder().createMessage().asList(messageParser);
|
||||
}
|
||||
|
||||
public static CraftMessageListBuilder<String> asStrings() {
|
||||
return CraftConfigValue.builder().createMessage().asStringList();
|
||||
}
|
||||
|
||||
public static ConfiguredMessageList<String> ofStrings(@NotNull String... defaultMessages) {
|
||||
return asStrings().defaults(defaultMessages).build();
|
||||
}
|
||||
|
||||
public ConfiguredMessageList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@NotNull List<TextConfig> messages, @NotNull String[] params,
|
||||
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
|
||||
@NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, TextConfig.class, messages, params, messageParser, sendFunction, TextConfig::of);
|
||||
}
|
||||
|
||||
public void broadcast(@NotNull Map<String, Object> placeholders) {
|
||||
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, placeholders));
|
||||
send(Bukkit.getConsoleSender(), placeholders);
|
||||
}
|
||||
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.value;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.CraftConfigValue;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.source.CraftConfigProvider;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends CraftConfigValue<T> {
|
||||
|
||||
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass) {
|
||||
return of(valueClass, null);
|
||||
}
|
||||
|
||||
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass,
|
||||
@Nullable V defaultValue) {
|
||||
return builder().ofSerializable(valueClass).defaults(defaultValue).build();
|
||||
}
|
||||
|
||||
protected final @NotNull Class<T> valueClass;
|
||||
|
||||
public ConfiguredSerializable(@Nullable CraftConfigProvider provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
|
||||
this.valueClass = valueClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable T get() {
|
||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||
try {
|
||||
// 若未出现错误,则直接更新缓存并返回。
|
||||
return updateCache(getBukkitConfig().get(getConfigPath(), getDefaultValue(), valueClass));
|
||||
} catch (Exception e) {
|
||||
// 出现了解析错误,提示并返回默认值。
|
||||
e.printStackTrace();
|
||||
return useDefault();
|
||||
}
|
||||
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@Nullable T value) {
|
||||
updateCache(value);
|
||||
setValue(value);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.value;
|
||||
|
||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.CraftConfigValue;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.sound.SoundConfigBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.SoundConfig;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ConfiguredSound extends ConfiguredValue<SoundConfig> {
|
||||
|
||||
public static @NotNull SoundConfigBuilder create() {
|
||||
return CraftConfigValue.builder().createSound();
|
||||
}
|
||||
|
||||
public static @NotNull ConfiguredSound of(Sound sound) {
|
||||
return CraftConfigValue.builder().createSound().defaults(sound).build();
|
||||
}
|
||||
|
||||
public static @NotNull ConfiguredSound of(Sound sound, float volume) {
|
||||
return CraftConfigValue.builder().createSound().defaults(sound, volume).build();
|
||||
}
|
||||
|
||||
public static @NotNull ConfiguredSound of(Sound sound, float volume, float pitch) {
|
||||
return CraftConfigValue.builder().createSound().defaults(sound, volume, pitch).build();
|
||||
}
|
||||
|
||||
public static @NotNull ConfiguredSound of(String soundName) {
|
||||
return CraftConfigValue.builder().createSound().defaults(soundName).build();
|
||||
}
|
||||
|
||||
public static @NotNull ConfiguredSound of(String soundName, float volume) {
|
||||
return CraftConfigValue.builder().createSound().defaults(soundName, volume).build();
|
||||
}
|
||||
|
||||
public static @NotNull ConfiguredSound of(String soundName, float volume, float pitch) {
|
||||
return CraftConfigValue.builder().createSound().defaults(soundName, volume, pitch).build();
|
||||
}
|
||||
|
||||
public ConfiguredSound(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable SoundConfig defaultValue) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, SoundConfig.class, defaultValue, getSoundParser(), SoundConfig::serialize);
|
||||
}
|
||||
|
||||
public void setSound(@NotNull Sound sound) {
|
||||
setSound(sound, 1.0f);
|
||||
}
|
||||
|
||||
public void setSound(@NotNull Sound sound, float volume) {
|
||||
setSound(sound, volume, 1.0f);
|
||||
}
|
||||
|
||||
public void setSound(@NotNull Sound sound, float volume, float pitch) {
|
||||
set(new SoundConfig(sound.name(), sound, volume, pitch));
|
||||
}
|
||||
|
||||
public void playTo(@NotNull Player player) {
|
||||
Optional.ofNullable(get()).ifPresent(c -> c.playTo(player));
|
||||
}
|
||||
|
||||
public void playToAll() {
|
||||
Optional.ofNullable(get()).ifPresent(SoundConfig::playToAll);
|
||||
}
|
||||
|
||||
public static ConfigValueParser<Object, SoundConfig> getSoundParser() {
|
||||
return ConfigValueParser.castToString().andThen((s, d) -> SoundConfig.deserialize(s));
|
||||
}
|
||||
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.value;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.CraftConfigValue;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.data.TitleConfig;
|
||||
import cc.carm.lib.configuration.minecraft.common.utils.ParamsUtils;
|
||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.builder.title.TitleConfigBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.function.TitleSendConsumer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Range;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
|
||||
|
||||
public static TitleConfigBuilder create() {
|
||||
return CraftConfigValue.builder().createTitle();
|
||||
}
|
||||
|
||||
public static ConfiguredTitle of(@Nullable String line1, @Nullable String line2) {
|
||||
return create().defaults(line1, line2).build();
|
||||
}
|
||||
|
||||
public static ConfiguredTitle of(@Nullable String line1, @Nullable String line2,
|
||||
int fadeIn, int stay, int fadeOut) {
|
||||
return create().defaults(line1, line2).fadeIn(fadeIn).stay(stay).fadeOut(fadeOut).build();
|
||||
}
|
||||
|
||||
protected final @NotNull TitleSendConsumer sendConsumer;
|
||||
protected final @NotNull String[] params;
|
||||
|
||||
protected final int fadeIn;
|
||||
protected final int stay;
|
||||
protected final int fadeOut;
|
||||
|
||||
public ConfiguredTitle(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable TitleConfig defaultValue, @NotNull String[] params,
|
||||
@NotNull TitleSendConsumer sendConsumer,
|
||||
int fadeIn, int stay, int fadeOut) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, TitleConfig.class, defaultValue, getTitleParser(), TitleConfig::serialize);
|
||||
this.sendConsumer = sendConsumer;
|
||||
this.params = params;
|
||||
this.fadeIn = fadeIn;
|
||||
this.stay = stay;
|
||||
this.fadeOut = fadeOut;
|
||||
}
|
||||
|
||||
@Range(from = 0L, to = Integer.MAX_VALUE)
|
||||
public int getFadeInTicks() {
|
||||
return fadeIn;
|
||||
}
|
||||
|
||||
@Range(from = 0L, to = Integer.MAX_VALUE)
|
||||
public int getStayTicks() {
|
||||
return stay;
|
||||
}
|
||||
|
||||
@Range(from = 0L, to = Integer.MAX_VALUE)
|
||||
public int getFadeOutTicks() {
|
||||
return fadeOut;
|
||||
}
|
||||
|
||||
public @NotNull TitleSendConsumer getSendConsumer() {
|
||||
return sendConsumer;
|
||||
}
|
||||
|
||||
public void send(@NotNull Player player, Object... values) {
|
||||
send(player, this.params, values);
|
||||
}
|
||||
|
||||
public void send(@NotNull Player player, @NotNull String[] params, @NotNull Object[] values) {
|
||||
send(player, ParamsUtils.buildParams(params, values));
|
||||
}
|
||||
|
||||
public void send(@NotNull Player player, @NotNull Map<String, Object> placeholders) {
|
||||
TitleConfig config = get();
|
||||
if (config != null) {
|
||||
config.send(player, fadeIn, stay, fadeOut, placeholders, sendConsumer);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendAll(Object... values) {
|
||||
sendAll(this.params, values);
|
||||
}
|
||||
|
||||
public void sendAll(@NotNull String[] params, @NotNull Object[] values) {
|
||||
sendAll(ParamsUtils.buildParams(params, values));
|
||||
}
|
||||
|
||||
public void sendAll(@NotNull Map<String, Object> placeholders) {
|
||||
TitleConfig config = get();
|
||||
if (config == null) return;
|
||||
|
||||
Bukkit.getOnlinePlayers().forEach(onlinePlayer -> send(onlinePlayer, placeholders));
|
||||
}
|
||||
|
||||
|
||||
public static ConfigValueParser<ConfigurationWrapper, TitleConfig> getTitleParser() {
|
||||
return (s, d) -> TitleConfig.deserialize(s);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-bukkit</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<artifactId>mineconfiguration-bukkit-general</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>mineconfiguration-bukkit-base</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package cc.carm.lib.configuration.minecraft;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.source.BukkitConfigProvider;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MineConfiguration {
|
||||
|
||||
public static BukkitConfigProvider from(File file, String source) {
|
||||
BukkitConfigProvider provider = new BukkitConfigProvider(file);
|
||||
try {
|
||||
provider.initializeFile(source);
|
||||
provider.initializeConfig();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
public static BukkitConfigProvider from(File file) {
|
||||
return from(file, file.getName());
|
||||
}
|
||||
|
||||
public static BukkitConfigProvider from(String fileName) {
|
||||
return from(fileName, fileName);
|
||||
}
|
||||
|
||||
public static BukkitConfigProvider from(String fileName, String source) {
|
||||
return from(new File(fileName), source);
|
||||
}
|
||||
|
||||
public static BukkitConfigProvider from(Plugin plugin, String fileName) {
|
||||
return from(plugin, fileName, fileName);
|
||||
}
|
||||
|
||||
public static BukkitConfigProvider from(Plugin plugin, String fileName, String source) {
|
||||
return from(new File(plugin.getDataFolder(), fileName), source);
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
public class BukkitConfigProvider extends CraftConfigProvider {
|
||||
|
||||
protected static final char SEPARATOR = '.';
|
||||
|
||||
protected BukkitYAMLComments bukkitComments = new BukkitYAMLComments();
|
||||
|
||||
public BukkitConfigProvider(@NotNull File file) {
|
||||
super(file);
|
||||
}
|
||||
|
||||
public void initializeConfig() {
|
||||
this.configuration = YamlConfiguration.loadConfiguration(file);
|
||||
this.initializer = new ConfigInitializer<>(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull CraftSectionWrapper getConfiguration() {
|
||||
return CraftSectionWrapper.of(this.configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() throws Exception {
|
||||
configuration.load(getFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws Exception {
|
||||
configuration.save(getFile());
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
this.bukkitComments.writeComments(configuration, new BufferedWriter(writer));
|
||||
String value = writer.toString(); // config contents
|
||||
|
||||
Path toUpdatePath = getFile().toPath();
|
||||
if (!value.equals(new String(Files.readAllBytes(toUpdatePath), StandardCharsets.UTF_8))) {
|
||||
Files.write(toUpdatePath, value.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
|
||||
this.bukkitComments.setHeaderComments(path, comments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInlineComment(@NotNull String path, @Nullable String comment) {
|
||||
this.bukkitComments.setInlineComment(path, comment);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
@Unmodifiable
|
||||
public List<String> getHeaderComment(@Nullable String path) {
|
||||
return this.bukkitComments.getHeaderComment(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getInlineComment(@NotNull String path) {
|
||||
return this.bukkitComments.getInlineComment(path);
|
||||
}
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.source;
|
||||
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static cc.carm.lib.configuration.minecraft.bukkit.source.CraftConfigProvider.SEPARATOR;
|
||||
|
||||
public class BukkitYAMLComments {
|
||||
|
||||
protected final @NotNull Map<String, List<String>> headerComments = new HashMap<>();
|
||||
protected final @NotNull Map<String, String> inlineComments = new HashMap<>();
|
||||
|
||||
protected @NotNull Map<String, List<String>> getHeaderComments() {
|
||||
return headerComments;
|
||||
}
|
||||
|
||||
protected @NotNull Map<String, String> getInlineComments() {
|
||||
return inlineComments;
|
||||
}
|
||||
|
||||
public void setHeaderComments(@Nullable String path, @Nullable List<String> comments) {
|
||||
|
||||
if (comments == null) {
|
||||
getHeaderComments().remove(path);
|
||||
} else {
|
||||
getHeaderComments().put(path, comments);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setInlineComment(@NotNull String path, @Nullable String comment) {
|
||||
if (comment == null) {
|
||||
getInlineComments().remove(path);
|
||||
} else {
|
||||
getInlineComments().put(path, comment);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Unmodifiable
|
||||
public List<String> getHeaderComment(@Nullable String path) {
|
||||
return Optional.ofNullable(getHeaderComments().get(path)).map(Collections::unmodifiableList).orElse(null);
|
||||
}
|
||||
|
||||
public @Nullable String getInlineComment(@NotNull String path) {
|
||||
return getInlineComments().get(path);
|
||||
}
|
||||
|
||||
public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) {
|
||||
List<String> comments = getHeaderComment(path);
|
||||
if (comments == null || comments.size() == 0) return null;
|
||||
|
||||
StringJoiner joiner = new StringJoiner("\n");
|
||||
for (String comment : comments) {
|
||||
if (comment.length() == 0) joiner.add(" ");
|
||||
else joiner.add(indents + "# " + comment);
|
||||
}
|
||||
return joiner + "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* 从一个文件读取配置并写入注释到某个写入器中。
|
||||
* 该方法的部分源代码借鉴自 tchristofferson/ConfigUpdater 项目。
|
||||
*
|
||||
* @param source 源配置文件
|
||||
* @param writer 配置写入器
|
||||
* @throws IOException 当写入发生错误时抛出
|
||||
*/
|
||||
public void writeComments(@NotNull YamlConfiguration source, @NotNull BufferedWriter writer) throws IOException {
|
||||
FileConfiguration temp = new YamlConfiguration(); // 该对象用于临时记录配置内容
|
||||
|
||||
String configHeader = buildHeaderComments(null, "");
|
||||
if (configHeader != null) writer.write(configHeader);
|
||||
|
||||
for (String fullKey : source.getKeys(true)) {
|
||||
Object currentValue = source.get(fullKey);
|
||||
|
||||
String indents = getIndents(fullKey);
|
||||
String headerComments = buildHeaderComments(fullKey, indents);
|
||||
String inlineComment = getInlineComment(fullKey);
|
||||
|
||||
if (headerComments != null) writer.write(headerComments);
|
||||
|
||||
String[] splitFullKey = fullKey.split("[" + SEPARATOR + "]");
|
||||
String trailingKey = splitFullKey[splitFullKey.length - 1];
|
||||
|
||||
if (currentValue instanceof ConfigurationSection) {
|
||||
ConfigurationSection section = (ConfigurationSection) currentValue;
|
||||
writer.write(indents + trailingKey + ":");
|
||||
if (inlineComment != null && inlineComment.length() > 0) {
|
||||
writer.write(" # " + inlineComment);
|
||||
}
|
||||
if (!section.getKeys(false).isEmpty()) {
|
||||
writer.write("\n");
|
||||
} else {
|
||||
writer.write(" {}\n");
|
||||
if (indents.length() == 0) writer.write("\n");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
temp.set(trailingKey, currentValue);
|
||||
String yaml = temp.saveToString();
|
||||
temp.set(trailingKey, null);
|
||||
|
||||
yaml = yaml.substring(0, yaml.length() - 1);
|
||||
|
||||
if (inlineComment != null && inlineComment.length() > 0) {
|
||||
if (yaml.contains("\n")) {
|
||||
// section为多行内容,需要 InlineComment 加在首行末尾
|
||||
String[] splitLine = yaml.split("\n", 2);
|
||||
yaml = splitLine[0] + " # " + inlineComment + "\n" + splitLine[1];
|
||||
} else {
|
||||
// 其他情况下就直接加载后面就好。
|
||||
yaml += " # " + inlineComment;
|
||||
}
|
||||
}
|
||||
|
||||
writer.write(indents + yaml.replace("\n", "\n" + indents) + "\n");
|
||||
if (indents.length() == 0) writer.write("\n");
|
||||
}
|
||||
|
||||
writer.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到一个键的缩进。
|
||||
* 该方法的源代码来自 tchristofferson/ConfigUpdater 项目。
|
||||
*
|
||||
* @param key 键
|
||||
* @return 该键的缩进文本
|
||||
*/
|
||||
protected static String getIndents(String key) {
|
||||
String[] splitKey = key.split("[" + BukkitConfigProvider.SEPARATOR + "]");
|
||||
return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-bukkit</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<artifactId>mineconfiguration-bukkit-native</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>mineconfiguration-bukkit-base</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.18.2-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package cc.carm.lib.configuration.minecraft;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.source.SpigotConfigProvider;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MineConfiguration {
|
||||
|
||||
public static SpigotConfigProvider from(File file, String source) {
|
||||
SpigotConfigProvider provider = new SpigotConfigProvider(file);
|
||||
try {
|
||||
provider.initializeFile(source);
|
||||
provider.initializeConfig();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
public static SpigotConfigProvider from(File file) {
|
||||
return from(file, file.getName());
|
||||
}
|
||||
|
||||
public static SpigotConfigProvider from(String fileName) {
|
||||
return from(fileName, fileName);
|
||||
}
|
||||
|
||||
public static SpigotConfigProvider from(String fileName, String source) {
|
||||
return from(new File(fileName), source);
|
||||
}
|
||||
|
||||
public static SpigotConfigProvider from(Plugin plugin, String fileName) {
|
||||
return from(plugin, fileName, fileName);
|
||||
}
|
||||
|
||||
public static SpigotConfigProvider from(Plugin plugin, String fileName, String source) {
|
||||
return from(new File(plugin.getDataFolder(), fileName), source);
|
||||
}
|
||||
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package cc.carm.lib.configuration.minecraft.bukkit.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.minecraft.bukkit.source.CraftConfigProvider;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class SpigotConfigProvider extends CraftConfigProvider {
|
||||
|
||||
public SpigotConfigProvider(@NotNull File file) {
|
||||
super(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializeConfig() {
|
||||
this.configuration = YamlConfiguration.loadConfiguration(file);
|
||||
this.initializer = new ConfigInitializer<>(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
|
||||
if (path == null) {
|
||||
this.configuration.options().setHeader(comments);
|
||||
} else {
|
||||
this.configuration.setComments(path, comments);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInlineComment(@NotNull String path, @Nullable String comment) {
|
||||
if (comment == null) {
|
||||
this.configuration.setInlineComments(path, null);
|
||||
} else {
|
||||
this.configuration.setComments(path, Collections.singletonList(comment));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable @Unmodifiable List<String> getHeaderComment(@Nullable String path) {
|
||||
if (path == null) return Collections.unmodifiableList(this.configuration.options().getHeader());
|
||||
else return this.configuration.getComments(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getInlineComment(@NotNull String path) {
|
||||
return String.join(" ", this.configuration.getInlineComments(path));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.0</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
</properties>
|
||||
<artifactId>mineconfiguration-bukkit</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>base</module>
|
||||
|
||||
<module>general</module>
|
||||
<module>native</module>
|
||||
</modules>
|
||||
|
||||
<name>MineConfiguration-Bukkit</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.bukkit</groupId>
|
||||
<artifactId>bukkit</artifactId>
|
||||
<version>1.8.8-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!--PlaceholderAPI for general placeholder's support-->
|
||||
<dependency>
|
||||
<groupId>me.clip</groupId>
|
||||
<artifactId>placeholderapi</artifactId>
|
||||
<version>2.11.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!--ProtocolLib for general packet's function support-->
|
||||
<dependency>
|
||||
<groupId>com.comphenix.protocol</groupId>
|
||||
<artifactId>ProtocolLib</artifactId>
|
||||
<version>4.8.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,76 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.0</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
</properties>
|
||||
<artifactId>mineconfiguration-bungee</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>MineConfiguration-Bungee</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-core</artifactId>
|
||||
<version>${easyconfiguration.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.md-5</groupId>
|
||||
<artifactId>bungeecord-api</artifactId>
|
||||
<version>1.18-R0.1-SNAPSHOT</version>
|
||||
<type>jar</type>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.md-5</groupId>
|
||||
<artifactId>bungeecord-api</artifactId>
|
||||
<version>1.18-R0.1-SNAPSHOT</version>
|
||||
<type>javadoc</type>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>mineconfiguration-common</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package cc.carm.lib.configuration.minecraft;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bungee.source.BungeeConfigProvider;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.JsonConfiguration;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MineConfiguration {
|
||||
|
||||
protected static BungeeConfigProvider create(File file, String source, ConfigurationProvider loader) {
|
||||
BungeeConfigProvider provider = new BungeeConfigProvider(file, loader);
|
||||
try {
|
||||
provider.initializeFile(source);
|
||||
provider.initializeConfig();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider from(File file, String source) {
|
||||
return fromYAML(file, source);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider from(File file) {
|
||||
return from(file, file.getName());
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider from(String fileName) {
|
||||
return from(fileName, fileName);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider from(String fileName, String source) {
|
||||
return from(new File(fileName), source);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider from(Plugin plugin, String fileName) {
|
||||
return from(plugin, fileName, fileName);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider from(Plugin plugin, String fileName, String source) {
|
||||
return from(new File(plugin.getDataFolder(), fileName), source);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromYAML(File file, String source) {
|
||||
return create(file, source, ConfigurationProvider.getProvider(YamlConfiguration.class));
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromYAML(String fileName, String source) {
|
||||
return fromYAML(new File(fileName), source);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromYAML(File file) {
|
||||
return fromYAML(file, file.getName());
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromYAML(String fileName) {
|
||||
return fromYAML(fileName, fileName);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromYAML(Plugin plugin, String fileName) {
|
||||
return fromYAML(plugin, fileName, fileName);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromYAML(Plugin plugin, String fileName, String source) {
|
||||
return fromYAML(new File(plugin.getDataFolder(), fileName), source);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromJSON(File file, String source) {
|
||||
return create(file, source, ConfigurationProvider.getProvider(JsonConfiguration.class));
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromJSON(String fileName, String source) {
|
||||
return fromJSON(new File(fileName), source);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromJSON(File file) {
|
||||
return fromJSON(file, file.getName());
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromJSON(String fileName) {
|
||||
return fromJSON(fileName, fileName);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromJSON(Plugin plugin, String fileName) {
|
||||
return fromJSON(plugin, fileName, fileName);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromJSON(Plugin plugin, String fileName, String source) {
|
||||
return fromJSON(new File(plugin.getDataFolder(), fileName), source);
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package cc.carm.lib.configuration.minecraft.bungee;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bungee.builder.BungeeConfigBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bungee.source.BungeeConfigProvider;
|
||||
import cc.carm.lib.configuration.minecraft.bungee.source.BungeeSectionWrapper;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class BungeeConfigValue<T> extends CachedConfigValue<T> {
|
||||
|
||||
public static @NotNull BungeeConfigBuilder builder() {
|
||||
return new BungeeConfigBuilder();
|
||||
}
|
||||
|
||||
public BungeeConfigValue(@Nullable BungeeConfigProvider provider, @Nullable String configPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable T defaultValue) {
|
||||
super(provider, configPath, headerComments, inlineComments, defaultValue);
|
||||
}
|
||||
|
||||
public BungeeConfigProvider getBukkitProvider() {
|
||||
ConfigurationProvider<?> provider = getProvider();
|
||||
if (provider instanceof BungeeConfigProvider) return (BungeeConfigProvider) getProvider();
|
||||
else throw new IllegalStateException("Provider is not a SpigotConfigProvider");
|
||||
}
|
||||
|
||||
public BungeeSectionWrapper getBukkitConfig() {
|
||||
return getBukkitProvider().getConfiguration();
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package cc.carm.lib.configuration.minecraft.bungee.builder;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bungee.source.BungeeConfigProvider;
|
||||
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
||||
|
||||
public abstract class AbstractBungeeBuilder<T, B extends AbstractBungeeBuilder<T, B>>
|
||||
extends AbstractConfigBuilder<T, B, BungeeConfigProvider> {
|
||||
|
||||
public AbstractBungeeBuilder() {
|
||||
super(BungeeConfigProvider.class);
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package cc.carm.lib.configuration.minecraft.bungee.builder;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bungee.builder.message.BungeeMessageBuilder;
|
||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class BungeeConfigBuilder extends ConfigBuilder {
|
||||
|
||||
public @NotNull BungeeMessageBuilder createMessage() {
|
||||
return new BungeeMessageBuilder();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package cc.carm.lib.configuration.minecraft.bungee.builder.message;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bungee.data.MessageText;
|
||||
import cc.carm.lib.configuration.minecraft.common.builder.message.MessageConfigBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.common.utils.ColorParser;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class BungeeMessageBuilder extends MessageConfigBuilder<CommandSender, MessageText> {
|
||||
|
||||
|
||||
public BungeeMessageBuilder() {
|
||||
super(CommandSender.class, MessageText.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <M> BungeeMessageValueBuilder<M> asValue(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
return new BungeeMessageValueBuilder<>(parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <M> BungeeMessageListBuilder<M> asList(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
return new BungeeMessageListBuilder<>(parser);
|
||||
}
|
||||
|
||||
public @NotNull
|
||||
BungeeMessageValueBuilder<String> asStringValue() {
|
||||
return asValue(defaultParser()).whenSend(CommandSender::sendMessage);
|
||||
}
|
||||
|
||||
public @NotNull
|
||||
BungeeMessageListBuilder<String> asStringList() {
|
||||
return asList(defaultParser()).whenSend((r, m) -> m.forEach(r::sendMessage));
|
||||
}
|
||||
|
||||
protected static @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable String> defaultParser() {
|
||||
return (receiver, message) -> ColorParser.parse(message);
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package cc.carm.lib.configuration.minecraft.bungee.builder.message;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bungee.data.MessageText;
|
||||
import cc.carm.lib.configuration.minecraft.bungee.value.ConfiguredMessageList;
|
||||
import cc.carm.lib.configuration.minecraft.common.builder.message.MessageListBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.common.utils.ParamsUtils;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class BungeeMessageListBuilder<M>
|
||||
extends MessageListBuilder<M, CommandSender, MessageText, BungeeMessageListBuilder<M>> {
|
||||
|
||||
public BungeeMessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
super(CommandSender.class, MessageText::of, parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull BungeeMessageListBuilder<M> getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredMessageList<M> build() {
|
||||
return new ConfiguredMessageList<>(
|
||||
this.provider, this.path, this.headerComments, this.inlineComment,
|
||||
Optional.ofNullable(this.defaultValue).orElse(MessageText.of(new ArrayList<>())),
|
||||
ParamsUtils.formatParams(this.paramFormatter, this.params),
|
||||
this.messageParser, this.sendFunction
|
||||
);
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package cc.carm.lib.configuration.minecraft.bungee.builder.message;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bungee.data.MessageText;
|
||||
import cc.carm.lib.configuration.minecraft.bungee.value.ConfiguredMessage;
|
||||
import cc.carm.lib.configuration.minecraft.common.builder.message.MessageValueBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.common.utils.ParamsUtils;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class BungeeMessageValueBuilder<M>
|
||||
extends MessageValueBuilder<M, CommandSender, MessageText, BungeeMessageValueBuilder<M>> {
|
||||
|
||||
public BungeeMessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
super(CommandSender.class, MessageText::new, parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull BungeeMessageValueBuilder<M> getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredMessage<M> build() {
|
||||
return new ConfiguredMessage<>(
|
||||
this.provider, this.path, this.headerComments, this.inlineComment,
|
||||
Optional.ofNullable(this.defaultValue).orElse(MessageText.of("")),
|
||||
ParamsUtils.formatParams(this.paramFormatter, this.params),
|
||||
this.messageParser, this.sendHandler
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package cc.carm.lib.configuration.minecraft.bungee.data;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.common.data.AbstractText;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MessageText extends AbstractText<CommandSender> {
|
||||
|
||||
public MessageText(@NotNull String message) {
|
||||
super(CommandSender.class, message);
|
||||
}
|
||||
|
||||
@Contract("!null,-> !null")
|
||||
public static @Nullable MessageText of(@Nullable String message) {
|
||||
if (message == null) return null;
|
||||
else return new MessageText(message);
|
||||
}
|
||||
|
||||
public static @NotNull List<MessageText> of(@Nullable List<String> messages) {
|
||||
if (messages == null || messages.isEmpty()) return new ArrayList<>();
|
||||
else return messages.stream().map(MessageText::of).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static @NotNull List<MessageText> of(@NotNull String... messages) {
|
||||
return of(Arrays.asList(messages));
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package cc.carm.lib.configuration.minecraft.bungee.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> {
|
||||
|
||||
protected ConfigurationProvider loader;
|
||||
protected Configuration configuration;
|
||||
protected ConfigInitializer<BungeeConfigProvider> initializer;
|
||||
|
||||
public BungeeConfigProvider(@NotNull File file, ConfigurationProvider loader) {
|
||||
super(file);
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
public void initializeConfig() throws IOException {
|
||||
this.configuration = getLoader().load(file);
|
||||
this.initializer = new ConfigInitializer<>(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull BungeeSectionWrapper getConfiguration() {
|
||||
return BungeeSectionWrapper.of(configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onReload() throws Exception {
|
||||
this.configuration = getLoader().load(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws Exception {
|
||||
getLoader().save(configuration, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInlineComment(@NotNull String path, @Nullable String comment) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable @Unmodifiable List<String> getHeaderComment(@Nullable String path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getInlineComment(@NotNull String path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigInitializer<BungeeConfigProvider> getInitializer() {
|
||||
return this.initializer;
|
||||
}
|
||||
|
||||
public ConfigurationProvider getLoader() {
|
||||
return loader;
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package cc.carm.lib.configuration.minecraft.bungee.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BungeeSectionWrapper implements ConfigurationWrapper {
|
||||
|
||||
private final Configuration section;
|
||||
|
||||
private BungeeSectionWrapper(@NotNull Configuration section) {
|
||||
this.section = section;
|
||||
}
|
||||
|
||||
@Contract("!null->!null")
|
||||
public static @Nullable BungeeSectionWrapper of(@Nullable Configuration section) {
|
||||
return section == null ? null : new BungeeSectionWrapper(section);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<String> getKeys(boolean deep) {
|
||||
return new LinkedHashSet<>(section.getKeys());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> getValues(boolean deep) {
|
||||
return section.getKeys().stream()
|
||||
.collect(Collectors.toMap(key -> key, section::get, (a, b) -> b, LinkedHashMap::new));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@NotNull String path, @Nullable Object value) {
|
||||
this.section.set(path, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull String path) {
|
||||
return this.section.contains(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object get(@NotNull String path) {
|
||||
return this.section.get(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isList(@NotNull String path) {
|
||||
return get(path) instanceof List<?>;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<?> getList(@NotNull String path) {
|
||||
return this.section.getList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConfigurationSection(@NotNull String path) {
|
||||
return get(path) instanceof Configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
||||
return of(this.section.getSection(path));
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package cc.carm.lib.configuration.minecraft.bungee.value;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bungee.BungeeConfigValue;
|
||||
import cc.carm.lib.configuration.minecraft.bungee.builder.message.BungeeMessageValueBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bungee.data.MessageText;
|
||||
import cc.carm.lib.configuration.minecraft.common.value.ConfigMessage;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class ConfiguredMessage<M> extends ConfigMessage<M, MessageText, CommandSender> {
|
||||
|
||||
@NotNull
|
||||
public static <M> BungeeMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
|
||||
return BungeeConfigValue.builder().createMessage().asValue(messageParser);
|
||||
}
|
||||
|
||||
public static BungeeMessageValueBuilder<String> asString() {
|
||||
return BungeeConfigValue.builder().createMessage().asStringValue();
|
||||
}
|
||||
|
||||
public static ConfiguredMessage<String> ofString() {
|
||||
return asString().build();
|
||||
}
|
||||
|
||||
public static ConfiguredMessage<String> ofString(@NotNull String defaultMessage) {
|
||||
return asString().defaults(defaultMessage).build();
|
||||
}
|
||||
|
||||
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@NotNull MessageText defaultMessage, @NotNull String[] params,
|
||||
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
|
||||
@NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, MessageText.class, defaultMessage, params, messageParser, sendFunction, MessageText::of);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcast(@NotNull Map<String, Object> placeholders) {
|
||||
ProxyServer.getInstance().getPlayers().forEach(pl -> send(pl, placeholders));
|
||||
send(ProxyServer.getInstance().getConsole(), placeholders);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package cc.carm.lib.configuration.minecraft.bungee.value;
|
||||
|
||||
import cc.carm.lib.configuration.minecraft.bungee.BungeeConfigValue;
|
||||
import cc.carm.lib.configuration.minecraft.bungee.builder.message.BungeeMessageListBuilder;
|
||||
import cc.carm.lib.configuration.minecraft.bungee.data.MessageText;
|
||||
import cc.carm.lib.configuration.minecraft.common.value.ConfigMessageList;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class ConfiguredMessageList<M> extends ConfigMessageList<M, MessageText, CommandSender> {
|
||||
|
||||
@NotNull
|
||||
public static <M> BungeeMessageListBuilder<M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
|
||||
return BungeeConfigValue.builder().createMessage().asList(messageParser);
|
||||
}
|
||||
|
||||
public static BungeeMessageListBuilder<String> asStrings() {
|
||||
return BungeeConfigValue.builder().createMessage().asStringList();
|
||||
}
|
||||
|
||||
public static ConfiguredMessageList<String> ofStrings(@NotNull String... defaultMessages) {
|
||||
return asStrings().defaults(defaultMessages).build();
|
||||
}
|
||||
|
||||
public ConfiguredMessageList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@NotNull List<MessageText> messages, @NotNull String[] params,
|
||||
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
|
||||
@NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, MessageText.class, messages, params, messageParser, sendFunction, MessageText::of);
|
||||
}
|
||||
|
||||
public void broadcast(@NotNull Map<String, Object> placeholders) {
|
||||
ProxyServer.getInstance().getPlayers().forEach(pl -> send(pl, placeholders));
|
||||
send(ProxyServer.getInstance().getConsole(), placeholders);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user