Implement BukkitMetadataStore, fix legacy setMetadata calls

This commit is contained in:
Shane Freeder 2020-03-04 23:18:15 +00:00
parent 88767fd0a9
commit 5ba410f4cf
No known key found for this signature in database
GPG Key ID: A3F61EA5A085289C
5 changed files with 60 additions and 6 deletions

View File

@ -5,7 +5,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public interface MetadataStore { public interface MetadataStore<E extends MMOEntity<?>> {
/** /**
* @param holder holder of the metadata * @param holder holder of the metadata
@ -14,17 +14,15 @@ public interface MetadataStore {
* @return the metadata value or null * @return the metadata value or null
*/ */
@Nullable @Nullable
<V> V getMetadata(@NotNull MMOEntity holder, @NotNull MetadataKey<V> key); <V> V getMetadata(@NotNull E holder, @NotNull MetadataKey<V> key);
/** /**
* @param holder holder of the metdata * @param holder holder of the metdata
* @param key metadata key * @param key metadata key
* @param value metadata value * @param value metadata value
* @param <V> value type * @param <V> value type
* @return the existing metadata value if set, or null
*/ */
@Nullable <V> void setMetadata(@NotNull E holder, @NotNull MetadataKey<V> key, @NotNull V value);
<V> V setMetadata(@NotNull MMOEntity holder, @NotNull MetadataKey<V> key, @Nullable V value);
/** /**
* @param holder holder of the metadata * @param holder holder of the metadata
@ -33,6 +31,6 @@ public interface MetadataStore {
* @return the removed metadata key * @return the removed metadata key
*/ */
@Nullable @Nullable
<V> V removeMetadata(@NotNull MMOEntity holder, @NotNull MetadataKey<V> key); <V> V removeMetadata(@NotNull E holder, @NotNull MetadataKey<V> key);
} }

View File

@ -1,5 +1,6 @@
package com.gmail.nossr50.mcmmo.bukkit; package com.gmail.nossr50.mcmmo.bukkit;
import com.gmail.nossr50.core.MetadataConstants;
import com.gmail.nossr50.datatypes.player.BukkitMMOPlayer; import com.gmail.nossr50.datatypes.player.BukkitMMOPlayer;
import com.gmail.nossr50.listeners.*; import com.gmail.nossr50.listeners.*;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
@ -11,6 +12,7 @@ import com.gmail.nossr50.mcmmo.api.platform.util.MetadataStore;
import com.gmail.nossr50.mcmmo.api.platform.util.MobHealthBarManager; import com.gmail.nossr50.mcmmo.api.platform.util.MobHealthBarManager;
import com.gmail.nossr50.mcmmo.bukkit.platform.entity.BukkitMMOEntity; import com.gmail.nossr50.mcmmo.bukkit.platform.entity.BukkitMMOEntity;
import com.gmail.nossr50.mcmmo.bukkit.platform.scheduler.BukkitPlatformScheduler; import com.gmail.nossr50.mcmmo.bukkit.platform.scheduler.BukkitPlatformScheduler;
import com.gmail.nossr50.mcmmo.bukkit.platform.util.BukkitMetadataStore;
import com.gmail.nossr50.mcmmo.bukkit.platform.util.BukkitMobHealthBarManager; import com.gmail.nossr50.mcmmo.bukkit.platform.util.BukkitMobHealthBarManager;
import org.bstats.bukkit.Metrics; import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -25,6 +27,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe; import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapelessRecipe; import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -43,6 +46,8 @@ public class BukkitBootstrap extends JavaPlugin implements PlatformProvider {
private mcMMO core = new mcMMO(this); private mcMMO core = new mcMMO(this);
private final BukkitPlatformScheduler scheduler = new BukkitPlatformScheduler(this); private final BukkitPlatformScheduler scheduler = new BukkitPlatformScheduler(this);
private final MobHealthBarManager healthBarManager = new BukkitMobHealthBarManager(this, core); private final MobHealthBarManager healthBarManager = new BukkitMobHealthBarManager(this, core);
private final BukkitMetadataStore bukkitMetadataStore = new BukkitMetadataStore(this);
private PaperCommandManager paperCommandManager; private PaperCommandManager paperCommandManager;
@ -80,6 +85,7 @@ public class BukkitBootstrap extends JavaPlugin implements PlatformProvider {
registerEvents(); registerEvents();
paperCommandManager = new PaperCommandManager(this); paperCommandManager = new PaperCommandManager(this);
paperCommandManager.registerDependency(mcMMO.class, core); paperCommandManager.registerDependency(mcMMO.class, core);
MetadataConstants.metadataValue = new FixedMetadataValue(this, true);
} }
@Override @Override

View File

@ -0,0 +1,46 @@
package com.gmail.nossr50.mcmmo.bukkit.platform.util;
import com.gmail.nossr50.mcmmo.api.data.MMOEntity;
import com.gmail.nossr50.mcmmo.api.platform.util.MetadataKey;
import com.gmail.nossr50.mcmmo.api.platform.util.MetadataStore;
import com.gmail.nossr50.mcmmo.bukkit.BukkitBootstrap;
import org.bukkit.entity.Entity;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@SuppressWarnings("unchecked")
public class BukkitMetadataStore implements MetadataStore<MMOEntity<Entity>> {
private final BukkitBootstrap bukkitBootstrap;
public BukkitMetadataStore(BukkitBootstrap bukkitBootstrap) {
this.bukkitBootstrap = bukkitBootstrap;
}
@Override
public <V> @Nullable V getMetadata(@NotNull MMOEntity<Entity> holder, @NotNull MetadataKey<V> key) {
final List<MetadataValue> metadata = holder.getNative().getMetadata(key.getKey());
if (!metadata.isEmpty()) {
return (V) metadata.get(0);
}
return null;
}
@Override
public <V> void setMetadata(@NotNull MMOEntity<Entity> holder, @NotNull MetadataKey<V> key, @NotNull V value) {
holder.getNative().setMetadata(key.getKey(), new FixedMetadataValue(bukkitBootstrap, (V) value));
}
@Override
public <V> @Nullable V removeMetadata(@NotNull MMOEntity<Entity> holder, @NotNull MetadataKey<V> key) {
final List<MetadataValue> metadata = holder.getNative().getMetadata(key.getKey());
if (!metadata.isEmpty()) {
holder.getNative().removeMetadata(key.getKey(), bukkitBootstrap);
}
return (V) metadata.get(0);
}
}

View File

@ -35,6 +35,7 @@ public class MetadataConstants {
public final static MetadataKey<Boolean> PETS_ANIMAL_TRACKING_METAKEY = new MetadataKey<>("mcMMO: Pet Animal"); public final static MetadataKey<Boolean> PETS_ANIMAL_TRACKING_METAKEY = new MetadataKey<>("mcMMO: Pet Animal");
public static final MetadataKey<Boolean> COTW_TEMPORARY_SUMMON = new MetadataKey<>("mcMMO: COTW Entity"); public static final MetadataKey<Boolean> COTW_TEMPORARY_SUMMON = new MetadataKey<>("mcMMO: COTW Entity");
@Deprecated
public static FixedMetadataValue metadataValue; //Gains value in onEnable public static FixedMetadataValue metadataValue; //Gains value in onEnable
} }

View File

@ -190,8 +190,11 @@ public class mcMMO implements McMMOApi {
commandRegistrationManager.registerACFCommands(); commandRegistrationManager.registerACFCommands();
commandRegistrationManager.registerCommands(); commandRegistrationManager.registerCommands();
/*
nbtManager = new NBTManager(); nbtManager = new NBTManager();
*/
//Init Chunk Manager Factory //Init Chunk Manager Factory
chunkManagerFactory = new ChunkManagerFactory(this); chunkManagerFactory = new ChunkManagerFactory(this);
placeStore = chunkManagerFactory.getChunkManager(); // Get our ChunkletManager placeStore = chunkManagerFactory.getChunkManager(); // Get our ChunkletManager