mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-07-26 17:15:28 +02:00
super boosted items are now tracked differently
This commit is contained in:
@@ -2,6 +2,8 @@ package com.gmail.nossr50.util.compat.layers.persistentdata;
|
||||
|
||||
import com.gmail.nossr50.util.compat.layers.AbstractCompatibilityLayer;
|
||||
import org.bukkit.block.Furnace;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
@@ -16,4 +18,12 @@ public abstract class AbstractPersistentDataLayer extends AbstractCompatibilityL
|
||||
|
||||
public abstract void setFurnaceOwner(Furnace furnace, UUID uuid);
|
||||
|
||||
public abstract void setSuperAbilityBoostedItem(ItemStack itemStack, int originalDigSpeed);
|
||||
|
||||
public abstract boolean isSuperAbilityBoosted(ItemMeta itemMeta);
|
||||
|
||||
public abstract int getSuperAbilityToolOriginalDigSpeed(ItemMeta itemMeta);
|
||||
|
||||
public abstract void removeBonusDigSpeedOnSuperAbilityTool(ItemStack itemStack);
|
||||
|
||||
}
|
||||
|
@@ -3,6 +3,9 @@ package com.gmail.nossr50.util.compat.layers.persistentdata;
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.block.Furnace;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataHolder;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
@@ -13,8 +16,17 @@ import java.util.UUID;
|
||||
|
||||
public class SpigotPersistentDataLayer extends AbstractPersistentDataLayer {
|
||||
|
||||
/*
|
||||
* Don't modify these keys
|
||||
*/
|
||||
public static final String FURNACE_UUID_MOST_SIG = "furnace_uuid_most_sig";
|
||||
public static final String FURNACE_UUID_LEAST_SIG = "furnace_uuid_least_sig";
|
||||
public static final String SUPER_ABILITY_BOOSTED = "super_ability_boosted";
|
||||
|
||||
private NamespacedKey furnaceOwner_MostSig_Key;
|
||||
private NamespacedKey furnaceOwner_LeastSig_Key;
|
||||
private NamespacedKey superAbilityBoosted;
|
||||
|
||||
|
||||
@Override
|
||||
public boolean initializeLayer() {
|
||||
@@ -23,8 +35,9 @@ public class SpigotPersistentDataLayer extends AbstractPersistentDataLayer {
|
||||
}
|
||||
|
||||
private void initNamespacedKeys() {
|
||||
furnaceOwner_MostSig_Key = getNamespacedKey("furnace_uuid_most_sig");
|
||||
furnaceOwner_LeastSig_Key = getNamespacedKey("furnace_uuid_least_sig");
|
||||
furnaceOwner_MostSig_Key = getNamespacedKey(FURNACE_UUID_MOST_SIG);
|
||||
furnaceOwner_LeastSig_Key = getNamespacedKey(FURNACE_UUID_LEAST_SIG);
|
||||
superAbilityBoosted = getNamespacedKey(SUPER_ABILITY_BOOSTED);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -57,4 +70,62 @@ public class SpigotPersistentDataLayer extends AbstractPersistentDataLayer {
|
||||
|
||||
furnace.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSuperAbilityBoostedItem(ItemStack itemStack, int originalDigSpeed) {
|
||||
ItemMeta itemMeta = itemStack.getItemMeta();
|
||||
PersistentDataContainer dataContainer = ((PersistentDataHolder) itemMeta).getPersistentDataContainer();
|
||||
|
||||
dataContainer.set(superAbilityBoosted, PersistentDataType.INTEGER, originalDigSpeed);
|
||||
|
||||
itemStack.setItemMeta(itemMeta);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSuperAbilityBoosted(@NotNull ItemMeta itemMeta) {
|
||||
//Get container from entity
|
||||
PersistentDataContainer dataContainer = ((PersistentDataHolder) itemMeta).getPersistentDataContainer();
|
||||
|
||||
//If this value isn't null, then the tool can be considered dig speed boosted
|
||||
Integer boostValue = dataContainer.get(superAbilityBoosted, PersistentDataType.INTEGER);
|
||||
|
||||
return boostValue != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSuperAbilityToolOriginalDigSpeed(@NotNull ItemMeta itemMeta) {
|
||||
//Get container from entity
|
||||
PersistentDataContainer dataContainer = ((PersistentDataHolder) itemMeta).getPersistentDataContainer();
|
||||
|
||||
if(dataContainer.get(superAbilityBoosted, PersistentDataType.INTEGER) == null) {
|
||||
mcMMO.p.getLogger().severe("Value should never be null for a boosted item");
|
||||
return 0;
|
||||
} else {
|
||||
//Too lazy to make a custom data type for this stuff
|
||||
Integer boostValue = dataContainer.get(superAbilityBoosted, PersistentDataType.INTEGER);
|
||||
return Math.max(boostValue, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeBonusDigSpeedOnSuperAbilityTool(@NotNull ItemStack itemStack) {
|
||||
ItemMeta itemMeta = itemStack.getItemMeta();
|
||||
|
||||
//TODO: can be optimized
|
||||
int originalSpeed = getSuperAbilityToolOriginalDigSpeed(itemMeta);
|
||||
|
||||
if(itemMeta.hasEnchant(Enchantment.DIG_SPEED)) {
|
||||
itemMeta.removeEnchant(Enchantment.DIG_SPEED);
|
||||
}
|
||||
|
||||
if(originalSpeed > 0) {
|
||||
itemMeta.addEnchant(Enchantment.DIG_SPEED, originalSpeed, true);
|
||||
}
|
||||
|
||||
PersistentDataContainer dataContainer = ((PersistentDataHolder) itemMeta).getPersistentDataContainer();
|
||||
dataContainer.remove(superAbilityBoosted); //Remove persistent data
|
||||
|
||||
//TODO: needed?
|
||||
itemStack.setItemMeta(itemMeta);
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,15 @@
|
||||
package com.gmail.nossr50.util.compat.layers.persistentdata;
|
||||
|
||||
import com.gmail.nossr50.config.AdvancedConfig;
|
||||
import com.gmail.nossr50.datatypes.meta.SuperAbilityToolMeta;
|
||||
import com.gmail.nossr50.datatypes.meta.UUIDMeta;
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import org.bukkit.block.Furnace;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.metadata.Metadatable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -13,6 +19,7 @@ import java.util.UUID;
|
||||
public class SpigotTemporaryDataLayer extends AbstractPersistentDataLayer {
|
||||
|
||||
private final String FURNACE_OWNER_METADATA_KEY = "mcMMO_furnace_owner";
|
||||
private final String ABILITY_TOOL_METADATA_KEY = "mcMMO_super_ability_tool";
|
||||
|
||||
@Override
|
||||
public boolean initializeLayer() {
|
||||
@@ -41,4 +48,51 @@ public class SpigotTemporaryDataLayer extends AbstractPersistentDataLayer {
|
||||
|
||||
metadatable.setMetadata(FURNACE_OWNER_METADATA_KEY, new UUIDMeta(mcMMO.p, uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSuperAbilityBoostedItem(ItemStack itemStack, int originalDigSpeed) {
|
||||
ItemMeta itemMeta = itemStack.getItemMeta();
|
||||
Metadatable metadatable = (Metadatable) itemMeta;
|
||||
metadatable.setMetadata(ABILITY_TOOL_METADATA_KEY, new SuperAbilityToolMeta(originalDigSpeed, mcMMO.p));
|
||||
|
||||
//TODO: needed?
|
||||
itemStack.setItemMeta(itemMeta);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSuperAbilityBoosted(@NotNull ItemMeta itemMeta) {
|
||||
Metadatable metadatable = (Metadatable) itemMeta;
|
||||
return metadatable.getMetadata(ABILITY_TOOL_METADATA_KEY).size() > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSuperAbilityToolOriginalDigSpeed(@NotNull ItemMeta itemMeta) {
|
||||
Metadatable metadatable = (Metadatable) itemMeta;
|
||||
|
||||
if(metadatable.getMetadata(ABILITY_TOOL_METADATA_KEY).size() > 0) {
|
||||
SuperAbilityToolMeta toolMeta = (SuperAbilityToolMeta) metadatable.getMetadata(ABILITY_TOOL_METADATA_KEY).get(0);
|
||||
return toolMeta.asInt();
|
||||
} else {
|
||||
// mcMMO.p.getLogger().info("Original dig enchant speed could not be found on item! Most likely it was lost from a server restart.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeBonusDigSpeedOnSuperAbilityTool(@NotNull ItemStack itemStack) {
|
||||
ItemMeta itemMeta = itemStack.getItemMeta();
|
||||
|
||||
if(itemMeta.hasEnchant(Enchantment.DIG_SPEED)) {
|
||||
itemMeta.removeEnchant(Enchantment.DIG_SPEED);
|
||||
}
|
||||
|
||||
int originalSpeed = getSuperAbilityToolOriginalDigSpeed(itemMeta);
|
||||
|
||||
if(originalSpeed > 0) {
|
||||
itemMeta.addEnchant(Enchantment.DIG_SPEED, originalSpeed, true);
|
||||
}
|
||||
|
||||
//TODO: needed?
|
||||
itemStack.setItemMeta(itemMeta);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user