更新多个类以支持过期策略和序列化功能,包括Assert.java、ContainerGui.java、EntryLoader.java、ExpirationListener.java、ExpirationPolicy.java、ExpiringEntryLoader.java、ExpiringMap.java、ExpiringValue.java、Gui.java、NamedThreadFactory.java、PageList.java、SerializeHelper.java,并修改pom.xml以引入必要的依赖。

This commit is contained in:
YuTian 2025-02-06 01:32:01 +08:00
parent ddc438b2d3
commit b117a26310
13 changed files with 1656 additions and 1547 deletions

View File

@ -121,6 +121,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.18.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-afterburner</artifactId>
<version>2.18.2</version>
</dependency>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>

View File

@ -4,8 +4,12 @@ package com.io.yutian.elementoriginlib.expiringmap;
* Determines how ExpiringMap entries should be expired.
*/
public enum ExpirationPolicy {
/** Expires entries based on when they were last accessed */
/**
* Expires entries based on when they were last accessed
*/
ACCESSED,
/** Expires entries based on when they were created */
/**
* Expires entries based on when they were created
*/
CREATED;
}

View File

@ -44,8 +44,8 @@ public final class ExpiringValue<V> {
* @param value the value to store
* @param duration the length of time after an entry is created that it should be removed
* @param timeUnit the unit that {@code duration} is expressed in
* @see ExpiringMap#put(Object, Object, long, TimeUnit)
* @throws NullPointerException on null timeUnit
* @see ExpiringMap#put(Object, Object, long, TimeUnit)
*/
public ExpiringValue(V value, long duration, TimeUnit timeUnit) {
this(value, duration, timeUnit, null);
@ -61,8 +61,8 @@ public final class ExpiringValue<V> {
* @param duration the length of time after an entry is created that it should be removed
* @param timeUnit the unit that {@code duration} is expressed in
* @param expirationPolicy the expiration policy for the value
* @see ExpiringMap#put(Object, Object, ExpirationPolicy, long, TimeUnit)
* @throws NullPointerException on null timeUnit
* @see ExpiringMap#put(Object, Object, ExpirationPolicy, long, TimeUnit)
*/
public ExpiringValue(V value, ExpirationPolicy expirationPolicy, long duration, TimeUnit timeUnit) {
this(value, duration, timeUnit, expirationPolicy);

View File

@ -0,0 +1,66 @@
package com.io.yutian.elementoriginlib.gui;
import com.io.yutian.elementoriginlib.gui.button.Button;
import com.io.yutian.elementoriginlib.gui.button.ClickType;
import net.kyori.adventure.text.Component;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import java.util.Set;
public abstract class ContainerGui extends Gui {
private Set<Integer> slots;
public ContainerGui(Player player, Component title, int size) {
super(player, title, size);
}
public void addSlot(int slot) {
if (slot > inventory.getSize()) {
return;
}
slots.add(slot);
}
@Override
public void handler(Player player, int slot, InventoryClickEvent event) {
if (buttons.containsKey(slot)) {
Button button = buttons.get(slot);
if (button == null) {
if (slot < inventory.getSize()) {
event.setCancelled(true);
} else {
if (event.getAction().equals(InventoryAction.MOVE_TO_OTHER_INVENTORY)) {
event.setCancelled(true);
}
}
return;
}
event.setCancelled(true);
clickButton(event, slot, button);
if (button.isPlaySound()) {
player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 1.0f, 1.0f);
}
InventoryAction action = event.getAction();
ClickType clickType = ClickType.LEFT_CLICK;
if (action.equals(InventoryAction.PICKUP_ALL)) {
clickType = ClickType.LEFT_CLICK;
} else if (action.equals(InventoryAction.PICKUP_HALF)) {
clickType = ClickType.RIGHT_CLICK;
} else if (action.equals(InventoryAction.MOVE_TO_OTHER_INVENTORY)) {
clickType = ClickType.SHIFT_CLICK;
}
if (button.getClickConsumer() != null) {
button.getClickConsumer().accept(player, clickType);
}
} else {
if (slot < inventory.getSize()) {
event.setCancelled(true);
}
}
}
}

View File

@ -15,7 +15,7 @@ import java.util.Map;
public class Gui extends IGui {
private Map<Integer, Button> buttons = new HashMap<>();
public Map<Integer, Button> buttons = new HashMap<>();
public Gui(Player player, Component title, int size) {
super(player, title, size);

View File

@ -4,50 +4,42 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
public class PageList<T> {
private List<T> list;
private Map<Integer, List<T>> map = new HashMap<>();
private int amount;
private final int totalPages;
public PageList(List<T> list, int amount) {
this.list = list;
this.amount = amount;
if (list == null) {
return;
}
if (list.size() <= amount) {
List<T> newList = new ArrayList<T>();
list.forEach(o-> newList.add(o));
map.put(1, newList);
if (list == null || list.isEmpty() || amount <= 0) {
totalPages = 0;
} else {
int x = 0;
int c = list.size() / amount;
for (int j = 0; j <= c;j++) {
int min = j * amount;
int max = (j+1) * amount;
List<T> newList = new ArrayList<T>();
for (int k = min; k < max; k++) {
if (k >= list.size()) {
break;
}
newList.add(list.get(k));
}
map.put(j+1, newList);
}
totalPages = (list.size() - 1) / amount + 1;
IntStream.rangeClosed(1, totalPages).forEach(page -> {
int start = (page - 1) * amount;
int end = Math.min(page * amount, list.size());
map.put(page, new ArrayList<>(list.subList(start, end)));
});
}
}
public int size() {
return map.size();
return totalPages;
}
public List<T> getList(int page) {
if (page <= 0) {
page = 1;
if (page < 1 || page > totalPages) {
throw new IllegalArgumentException("Invalid page number: " + page);
}
return map.get(page);
}
public boolean hasNextPage(int page) {
return page < totalPages;
}
public int getTotalPages() {
return totalPages;
}
}

View File

@ -2,24 +2,27 @@ package com.io.yutian.elementoriginlib.serialize;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.io.yutian.elementoriginlib.serialize.serializers.ItemStackDeserializer;
import com.io.yutian.elementoriginlib.serialize.serializers.ItemStackSerializer;
import com.io.yutian.elementoriginlib.serialize.serializers.UUIDDeserializer;
import com.io.yutian.elementoriginlib.serialize.serializers.UUIDSerializer;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import com.io.yutian.elementoriginlib.logger.Logger;
import com.io.yutian.elementoriginlib.serialize.serializers.*;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public class SerializeHelper {
private static final Logger LOGGER = Logger.getLogger(SerializeHelper.class);
private static final ObjectMapper objectMapper;
private static ObjectWriter objectWriter;
private static ObjectReader objectReader;
static {
objectMapper = new ObjectMapper();
objectMapper.setVisibility(
VisibilityChecker.Std.defaultInstance()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
@ -29,31 +32,61 @@ public class SerializeHelper {
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
);
objectMapper.registerModule(new AfterburnerModule());
objectMapper.disable(MapperFeature.AUTO_DETECT_GETTERS);
objectMapper.disable(MapperFeature.AUTO_DETECT_SETTERS);
objectMapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS);
objectMapper.disable(MapperFeature.USE_GETTERS_AS_SETTERS);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
objectMapper.disable(SerializationFeature.INDENT_OUTPUT);
SimpleModule module = new SimpleModule();
module.addSerializer(UUID.class, new UUIDSerializer());
module.addDeserializer(UUID.class, new UUIDDeserializer());
module.addSerializer(ItemStack.class, new ItemStackSerializer());
module.addDeserializer(ItemStack.class, new ItemStackDeserializer());
objectMapper.registerModule(module);
objectWriter = objectMapper.writer();
objectReader = objectMapper.reader();
}
public static void registerModule(Module module) {
objectMapper.registerModule(module);
objectWriter = objectMapper.writer();
objectReader = objectMapper.reader();
}
public static void registerSerializer(Class clazz, JsonSerializer serializer) {
SimpleModule module = new SimpleModule();
module.addSerializer(clazz, serializer);
registerModule(module);
}
public static void registerDeserializer(Class clazz, JsonDeserializer deserializer) {
SimpleModule module = new SimpleModule();
module.addDeserializer(clazz, deserializer);
registerModule(module);
}
public static String serialize(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
return objectWriter.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to serialize object: " + obj, e);
LOGGER.error("Failed to serialize object: " + obj, e);
return null;
}
}
public static <T> T deserialize(String json, Class<T> clazz) {
try {
return objectMapper.readValue(json, clazz);
return objectReader.forType(clazz).readValue(json);
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to deserialize JSON: " + json, e);
LOGGER.error("Failed to deserialize object: " + json, e);
return null;
}
}
}