1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2026-06-05 00:58:17 +08:00

Compare commits

..

4 Commits

16 changed files with 180 additions and 22 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -2,11 +2,14 @@ package cc.carm.lib.easyplugin.gui.configuration;
import cc.carm.lib.easyplugin.gui.GUI;
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.easyplugin.utils.ItemStackFactory;
import cc.carm.lib.easyplugin.utils.MessageUtils;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
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;
@@ -15,6 +18,8 @@ import java.util.stream.Collectors;
public class GUIItemConfiguration {
@Nullable ItemStack original;
@NotNull Material type;
int amount;
int data;
@@ -28,6 +33,15 @@ public class GUIItemConfiguration {
@Nullable String name, @NotNull List<String> lore,
@NotNull List<GUIActionConfiguration> actions,
@NotNull List<Integer> slots) {
this(null, type, amount, data, name, lore, actions, slots);
}
public GUIItemConfiguration(@Nullable ItemStack original,
@NotNull Material type, int amount, int data,
@Nullable String name, @NotNull List<String> lore,
@NotNull List<GUIActionConfiguration> actions,
@NotNull List<Integer> slots) {
this.original = original;
this.type = type;
this.amount = amount;
this.data = data;
@@ -38,22 +52,58 @@ public class GUIItemConfiguration {
}
public void setupItems(Player player, GUI gui) {
ItemStackFactory icon = new ItemStackFactory(this.type, this.amount, this.data);
if (this.name != null) icon.setDisplayName(this.name);
icon.setLore(MessageUtils.setPlaceholders(player, this.lore));
ItemStack itemStack;
if (original != null) {
ItemStack tmp = original.clone();
ItemMeta originalMeta = original.getItemMeta();
if (originalMeta != null) {
if (originalMeta.hasDisplayName()) {
originalMeta.setDisplayName(parseText(player, originalMeta.getDisplayName()));
}
if (originalMeta.getLore() != null) {
originalMeta.setLore(parseTexts(player, originalMeta.getLore()));
}
GUIItem item = new GUIItem(icon.toItemStack());
}
tmp.setItemMeta(originalMeta);
itemStack = tmp;
} else {
ItemStackFactory icon = new ItemStackFactory(this.type, this.amount, this.data);
if (this.name != null) {
icon.setDisplayName(parseText(player, this.name));
}
if (!this.lore.isEmpty()) {
icon.setLore(parseTexts(player, this.lore));
}
itemStack = icon.toItemStack();
}
GUIItem item = new GUIItem(itemStack);
this.actions.stream().map(GUIActionConfiguration::toClickAction).forEach(item::addClickAction);
this.slots.forEach(slot -> gui.setItem(slot, item));
}
private List<String> parseTexts(Player player, List<String> lore) {
return ColorParser.parse(MessageUtils.setPlaceholders(player, lore));
}
@NotNull
private String parseText(Player player, @NotNull String name) {
return ColorParser.parse(MessageUtils.setPlaceholders(player, name));
}
public @NotNull Map<String, Object> serialize() {
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
if (original != null) map.put("original", original);
else {
map.put("type", this.type.name());
if (this.data != 0) map.put("data", this.data);
}
map.put("type", this.type.name());
if (this.name != null) map.put("name", this.name);
if (this.amount != 1) map.put("amount", this.amount);
if (this.data != 0) map.put("data", this.data);
if (!this.lore.isEmpty()) map.put("lore", this.lore);
if (this.slots.size() > 1) {
map.put("slots", this.slots);
@@ -69,6 +119,10 @@ public class GUIItemConfiguration {
@Nullable
public static GUIItemConfiguration readFrom(@Nullable ConfigurationSection itemSection) {
if (itemSection == null) return null;
ItemStack original = null;
if (itemSection.contains("original")) original = itemSection.getItemStack("original");
String material = Optional.ofNullable(itemSection.getString("type")).orElse("STONE");
Material type = Optional.ofNullable(Material.matchMaterial(material)).orElse(Material.STONE);
int data = itemSection.getInt("data", 0);
@@ -88,7 +142,7 @@ public class GUIItemConfiguration {
}
return new GUIItemConfiguration(
type, amount, data, name, lore, actions,
original, type, amount, data, name, lore, actions,
slots.size() > 0 ? slots : Collections.singletonList(slot)
);
}
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -0,0 +1,104 @@
package cc.carm.lib.easyplugin.utils;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* <a href="https://gist.github.com/CarmJos/402cb5aad0ec14ab25c2fa0d21571703">Easy cooldown time utils.</a>
*
* @param <P> Cooldown key provider
* @param <K> Cooldown key
* @author CarmJos
*/
public class EasyCooldown<P, K> {
protected final NumberFormat numberFormatter;
protected final @NotNull Map<K, Long> cooldown = new HashMap<>();
protected final @NotNull Function<P, K> providerToKey;
protected long defaultDuration;
public EasyCooldown(@NotNull Function<P, K> providerToKey) {
this(defaultFormatter(), providerToKey, 1000L);
}
public EasyCooldown(@NotNull NumberFormat numberFormatter,
@NotNull Function<P, K> providerToKey) {
this(numberFormatter, providerToKey, 1000L);
}
public EasyCooldown(@NotNull NumberFormat numberFormatter,
@NotNull Function<P, K> providerToKey,
long defaultDuration) {
this.numberFormatter = numberFormatter;
this.providerToKey = providerToKey;
this.defaultDuration = defaultDuration;
}
public long getCooldown(@NotNull P provider) {
Long time = this.cooldown.get(this.providerToKey.apply(provider));
if (time == null || time < 0) return 0;
long duration = getDuration(provider);
if (duration <= 0) return 0;
long past = System.currentTimeMillis() - time;
return duration - past;
}
public @NotNull String getCooldownSeconds(@NotNull P provider) {
return formatSeconds(getCooldown(provider));
}
public void updateTime(@NotNull P provider) {
this.cooldown.put(this.providerToKey.apply(provider), System.currentTimeMillis());
}
public void clear(@NotNull P provider) {
clearCooldown(this.providerToKey.apply(provider));
}
public void clearCooldown(@NotNull K key) {
this.cooldown.remove(key);
}
public boolean isCoolingDown(@NotNull P provider) {
return getCooldown(provider) > 0;
}
public long getDuration(@NotNull P provider) {
return this.defaultDuration;
}
public @NotNull String formatSeconds(long cooldownMillis) {
return numberFormatter.format((double) cooldownMillis / 1000D);
}
public static NumberFormat createFormatter(Consumer<NumberFormat> consumer) {
NumberFormat format = NumberFormat.getInstance();
consumer.accept(format);
return format;
}
public static NumberFormat defaultFormatter() {
return createFormatter((f) -> f.setMaximumFractionDigits(2));
}
public static EasyCooldown<Player, UUID> playerCooldown(Function<Player, Long> durationProvider) {
return new EasyCooldown<Player, UUID>(Player::getUniqueId) {
@Override
public long getDuration(@NotNull Player provider) {
return durationProvider.apply(provider);
}
};
}
}
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.14</version>
<version>1.4.15</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+2 -2
View File
@@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging>
<version>1.4.14</version>
<version>1.4.15</version>
<modules>
<module>base/main</module>
@@ -106,7 +106,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<artifactId>spigot-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>