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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user