Skip to content

Commit

Permalink
Add an untested adventure component persister
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Aug 26, 2023
1 parent a5abcb2 commit 555e44d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
Expand Up @@ -382,12 +382,8 @@ public boolean hasCommand(String... parts) {

private boolean hasPermission(CommandInfo method, CommandSender sender) {
Command cmd = method.commandAnnotation;
return cmd.permission().isEmpty() || hasPermission(sender, cmd.permission())
|| hasPermission(sender, "citizens.admin");
}

private boolean hasPermission(CommandSender sender, String perm) {
return sender.hasPermission(perm);
return cmd.permission().isEmpty() || sender.hasPermission(cmd.permission())
|| sender.hasPermission("citizens.admin");
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/api/npc/NPC.java
Expand Up @@ -527,7 +527,7 @@ public enum Metadata {
*/
REMOVE_FROM_PLAYERLIST("removefromplayerlist", Boolean.class),
/**
* Whether to reset entity pitch to <code>0</code> every tick (default Minecraft behaviour). Defaults to true.
* Whether to reset entity pitch to <code>0</code> every tick (default Minecraft behaviour). Defaults to false.
*/
RESET_PITCH_ON_TICK("reset-pitch-on-tick", Boolean.class),
/**
Expand Down
@@ -0,0 +1,21 @@
package net.citizensnpcs.api.persistence;

import com.google.common.io.BaseEncoding;

import net.citizensnpcs.api.util.DataKey;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;

public class ComponentPersister implements Persister<Component> {
@Override
public Component create(DataKey root) {
String component = root.getString("");
return GsonComponentSerializer.gson().deserialize(new String(BaseEncoding.base64().decode(component)));
}

@Override
public void save(Component text, DataKey root) {
String output = GsonComponentSerializer.gson().serialize(text);
root.setString("", BaseEncoding.base64().encode(output.getBytes()));
}
}
Expand Up @@ -26,6 +26,7 @@

import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging;
import net.kyori.adventure.text.Component;

/**
* Performs reflective persistence of objects into {@link DataKey}s. {@link Persist} annotations are used to mark fields
Expand Down Expand Up @@ -401,6 +402,8 @@ private static Persister<?> getDelegate(Field field, Class<?> fieldType) {
if (delegate == null) {
if (registries.containsKey(fieldType))
return registries.get(fieldType);
if (Component.class.isAssignableFrom(fieldType))
return registries.get(Component.class);
return loadedDelegates.get(persistRedirects.get(fieldType));
}
persister = loadedDelegates.get(delegate.value());
Expand Down Expand Up @@ -524,7 +527,7 @@ public static <T> T load(T instance, DataKey root) {

/**
* Registers a {@link Persister} redirect. Fields with the {@link Persist} annotation with a type that has been
* registered using this method will use the Persister by default to load and save data. The
* registered using this method will use the supplied {@link Persister} for (de)serialisation. The
* {@link DelegatePersistence} annotation will be preferred if present.
*
* @param clazz
Expand Down Expand Up @@ -636,6 +639,7 @@ public void fillInStackTrace(StackTraceElement[] elements) {
private static final Map<Class<?>, PersisterRegistry<?>> registries = new WeakHashMap<>();

static {
registerPersistDelegate(Component.class, ComponentPersister.class);
registerPersistDelegate(Location.class, LocationPersister.class);
registerPersistDelegate(ItemStack.class, ItemStackPersister.class);
registerPersistDelegate(EulerAngle.class, EulerAnglePersister.class);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/net/citizensnpcs/api/persistence/Persister.java
Expand Up @@ -2,10 +2,13 @@

import net.citizensnpcs.api.util.DataKey;

/**
* A serialisation primitive to be used with {@link PersistenceLoader} to serialise custom objects.
*/
public interface Persister<T> {
/**
* Creates an object instance from the given {@link DataKey}. Should not return null unless no data is present.
*
*
* @param root
* The root key to load from
* @return The created instance, or null if no data was present
Expand All @@ -14,7 +17,7 @@ public interface Persister<T> {

/**
* Saves the object instance to the given {@link DataKey}.
*
*
* @param instance
* The object instance to save
* @param root
Expand Down

0 comments on commit 555e44d

Please sign in to comment.