Skip to content

Commit

Permalink
Prevent attempting to convert annoyingapi_converted key
Browse files Browse the repository at this point in the history
For PDC 1.14+ in `EntityData#convertOldData(...)`
  • Loading branch information
srnyx committed Nov 18, 2024
1 parent 1a571fa commit f858d91
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/xyz/srnyx/annoyingapi/data/EntityData.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public class EntityData extends StringData {
* The name of the table in the database to store entity data
*/
@NotNull public static final String TABLE_NAME = "entities";
/**
* The name of the key for the converted key (1.14+ only)
*/
@NotNull private static final String CONVERTED_KEY = "annoyingapi_converted";

/**
* The entity to manage data for (only used for {@link #convertOldData(boolean, Collection)})
Expand All @@ -47,7 +51,7 @@ public EntityData(@NotNull AnnoyingPlugin plugin, @NotNull Entity entity) {
/**
* Convert all data from the old data storage system (PDC/file) to the new one (SQL)
* <br>This <b>does not</b> run automatically, you must call this method manually (for example, on {@link org.bukkit.event.player.PlayerJoinEvent PlayerJoinEvent})!
* <br>For 1.14+ (PDC), the entity will receive the {@code api_converted} key which indicates that the data has been converted, this will avoid duplicate conversion checks
* <br>For 1.14+ (PDC), the entity will receive the {@link #CONVERTED_KEY} key which indicates that the data has been converted, this will avoid duplicate conversion checks
* <br>All old data (PDC/file) will be removed after conversion (to avoid duplicate/overwriting data)
*
* @param onlyTryOnce 1.14+ only | {@code true} to only try once to convert the data, even if it fails (so if run again, nothing will happen). If {@code false}, the conversion failed previously, and this is run again, it will try to convert again. If the data is successfully converted, this option doesn't matter
Expand All @@ -65,7 +69,7 @@ public Map<String, String> convertOldData(boolean onlyTryOnce, @Nullable Collect
// Get PDC
persistentDataContainer = PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD.invoke(entity);
// Check if already converted
convertedKey = NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, "annoyingapi_converted");
convertedKey = NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, CONVERTED_KEY);
if (PERSISTENT_DATA_CONTAINER_GET_METHOD.invoke(persistentDataContainer, convertedKey, PERSISTENT_DATA_TYPE_BYTE) != null) return Collections.emptyMap();
// Set converted key if onlyTryOnce is true
if (onlyTryOnce) PERSISTENT_DATA_CONTAINER_SET_METHOD.invoke(persistentDataContainer, convertedKey, PERSISTENT_DATA_TYPE_BYTE, (byte) 1);
Expand All @@ -84,7 +88,7 @@ public Map<String, String> convertOldData(boolean onlyTryOnce, @Nullable Collect
final String namespace = (String) NAMESPACED_KEY_GET_NAMESPACE_METHOD.invoke(namespacedKey);
if (!pluginName.equals(namespace.toLowerCase())) continue;
final String key = (String) NAMESPACED_KEY_GET_KEY_METHOD.invoke(namespacedKey);
namespacedKeys.put(key, namespacedKey);
if (!key.equals(CONVERTED_KEY)) namespacedKeys.put(key, namespacedKey);
}
} catch (final ReflectiveOperationException e) {
sendError("convert", e);
Expand Down

0 comments on commit f858d91

Please sign in to comment.