ZNPCsPlus/plugin/src/main/java/lol/pyr/znpcsplus/interaction/ActionRegistry.java

68 lines
2.8 KiB
Java
Raw Normal View History

package lol.pyr.znpcsplus.interaction;
2023-05-21 11:32:55 +00:00
import lol.pyr.znpcsplus.interaction.consolecommand.ConsoleCommandActionType;
import lol.pyr.znpcsplus.interaction.message.MessageActionType;
import lol.pyr.znpcsplus.interaction.playercommand.PlayerCommandActionType;
import lol.pyr.znpcsplus.interaction.switchserver.SwitchServerActionType;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.util.BungeeUtil;
import lol.pyr.znpcsplus.util.StringSerializer;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
2023-05-21 11:32:55 +00:00
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.util.Arrays;
import java.util.HashMap;
2023-05-21 11:32:55 +00:00
import java.util.List;
import java.util.Map;
2023-05-21 11:32:55 +00:00
import java.util.stream.Collectors;
public class ActionRegistry {
2023-05-21 11:32:55 +00:00
private final Map<Class<?>, InteractionActionType<?>> serializerMap = new HashMap<>();
2023-05-21 11:32:55 +00:00
public ActionRegistry() {
}
2023-05-21 11:32:55 +00:00
public void registerTypes(NpcRegistryImpl npcRegistry, TaskScheduler taskScheduler, BukkitAudiences adventure, BungeeUtil bungeeUtil, LegacyComponentSerializer textSerializer) {
register(new ConsoleCommandActionType(taskScheduler, npcRegistry));
register(new PlayerCommandActionType(taskScheduler, npcRegistry));
register(new SwitchServerActionType(bungeeUtil, npcRegistry));
register(new MessageActionType(adventure, textSerializer, npcRegistry));
}
public void register(InteractionActionType<?> type) {
serializerMap.put(type.getActionClass(), type);
}
public void unregister(Class<? extends InteractionAction> clazz) {
serializerMap.remove(clazz);
}
public List<InteractionCommandHandler> getCommands() {
return serializerMap.values().stream()
.filter(type -> type instanceof InteractionCommandHandler)
.map(type -> (InteractionCommandHandler) type)
.collect(Collectors.toList());
}
@SuppressWarnings("unchecked")
public <T extends InteractionAction> T deserialize(String str) {
try {
String[] split = str.split(";");
Class<?> clazz = Class.forName(split[0]);
StringSerializer<T> serializer = (StringSerializer<T>) serializerMap.get(clazz);
if (serializer == null) return null;
return serializer.deserialize(String.join(";", Arrays.copyOfRange(split, 1, split.length)));
} catch (ClassNotFoundException e) {
return null;
}
}
@SuppressWarnings("unchecked")
public <T extends InteractionAction> String serialize(T action) {
StringSerializer<T> serializer = (StringSerializer<T>) serializerMap.get(action.getClass());
if (serializer == null) return null;
return action.getClass().getName() + ";" + serializer.serialize(action);
}
}