some stuff

This commit is contained in:
Josh Roy 2024-04-26 11:48:24 -04:00
parent ec5c53893d
commit 9cd7b6534e
No known key found for this signature in database
GPG Key ID: 64C8142336ED1F69
5 changed files with 108 additions and 32 deletions

View File

@ -9,6 +9,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.ess3.api.IEssentials;
import net.ess3.api.TranslatableException;
import net.ess3.provider.PotionMetaProvider;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
@ -127,12 +128,12 @@ public class FlatItemDb extends AbstractItemDb {
final ItemStack stack = new ItemStack(material);
stack.setAmount(material.getMaxStackSize());
final PotionData potionData = data.getPotionData();
final PotionMetaProvider.AbstractPotionData potionData = data.getPotionData();
final ItemMeta meta = stack.getItemMeta();
if (potionData != null && meta instanceof PotionMeta) {
final PotionMeta potionMeta = (PotionMeta) meta;
potionMeta.setBasePotionData(potionData);
potionMeta.setBasePotionType(potionData);
}
// For some reason, Damageable doesn't extend ItemMeta but CB implements them in the same
@ -204,7 +205,7 @@ public class FlatItemDb extends AbstractItemDb {
final Material type = item.getType();
if (MaterialUtil.isPotion(type) && item.getItemMeta() instanceof PotionMeta) {
final PotionData potion = ((PotionMeta) item.getItemMeta()).getBasePotionData();
final PotionMetaProvider.AbstractPotionData potion = ess.getPotionMetaProvider().getPotionData(item);
return new ItemData(type, potion);
} else if (type.toString().contains("SPAWNER")) {
final EntityType entity = ess.getSpawnerItemProvider().getEntityType(item);
@ -224,14 +225,14 @@ public class FlatItemDb extends AbstractItemDb {
public static class ItemData {
private Material material;
private String[] fallbacks = null;
private PotionData potionData = null;
private PotionMetaProvider.AbstractPotionData potionData = null;
private EntityType entity = null;
ItemData(final Material material) {
this.material = material;
}
ItemData(final Material material, final PotionData potionData) {
ItemData(final Material material, final PotionMetaProvider.AbstractPotionData potionData) {
this.material = material;
this.potionData = potionData;
}
@ -267,7 +268,7 @@ public class FlatItemDb extends AbstractItemDb {
return material;
}
public PotionData getPotionData() {
public PotionMetaProvider.AbstractPotionData getPotionData() {
return this.potionData;
}

View File

@ -68,17 +68,45 @@ public class LegacyPotionMetaProvider implements PotionMetaProvider {
}
@Override
public boolean isSplash(ItemStack stack) {
//noinspection deprecation
final Potion potion = Potion.fromDamage(stack.getDurability());
return potion.isSplash();
public AbstractPotionData getPotionData(ItemStack stack) {
return new AbstractPotionData() {
final Potion potion = Potion.fromDamage(stack.getDurability());
@Override
public boolean isSplash() {
return potion.isSplash();
}
@Override
public Collection<PotionEffect> getEffects() {
return potion.getEffects();
}
@Override
public PotionType getType() {
return ((PotionMeta) stack.getItemMeta()).getBasePotionData().getType();
}
@Override
public void setType(PotionType type) {
final PotionMeta itemMeta = (PotionMeta) stack.getItemMeta();
final PotionData data = itemMeta.getBasePotionData();
itemMeta.setBasePotionData(new PotionData(type, data.isExtended(), data.isUpgraded()));
stack.setItemMeta(itemMeta);
}
@Override
public int hashCode() {
return (31 * stack.getType().hashCode()) ^ ((PotionMeta) stack.getItemMeta()).getBasePotionData().hashCode();
}
};
}
@Override
public Collection<PotionEffect> getEffects(ItemStack stack) {
//noinspection deprecation
final Potion potion = Potion.fromDamage(stack.getDurability());
return potion.getEffects();
public void updatePotionStack(ItemStack stack, AbstractPotionData data) {
return;
//todo
return;
}
@Override

View File

@ -3,19 +3,32 @@ package net.ess3.provider;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import java.util.Collection;
public interface PotionMetaProvider extends Provider {
ItemStack createPotionItem(Material initial, int effectId);
/**
* Should only be used for pre-flattening
*/
boolean isSplash(ItemStack stack);
AbstractPotionData getPotionData(ItemStack stack);
/**
* Should only be used for pre-flattening
*/
Collection<PotionEffect> getEffects(ItemStack stack);
void updatePotionStack(ItemStack stack, AbstractPotionData data);
interface AbstractPotionData {
/**
* Should only be used for pre-flattening
*/
boolean isSplash();
/**
* Should only be used for pre-flattening
*/
Collection<PotionEffect> getEffects();
int hashCode();
PotionType getType();
void setType(final PotionType type);
}
}

View File

@ -5,6 +5,7 @@ import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import java.util.Collection;
@ -15,13 +16,49 @@ public class ModernPotionMetaProvider implements PotionMetaProvider {
}
@Override
public boolean isSplash(ItemStack stack) {
return stack.getType() == Material.SPLASH_POTION;
public AbstractPotionData getPotionData(ItemStack stack) {
return new AbstractPotionData() {
@Override
public boolean isSplash() {
return stack.getType() == Material.SPLASH_POTION;
}
@Override
public Collection<PotionEffect> getEffects() {
return ((PotionMeta) stack.getItemMeta()).getCustomEffects();
}
@Override
public PotionType getType() {
return ((PotionMeta) stack.getItemMeta()).getBasePotionType();
}
@Override
public void setType(final PotionType type) {
((PotionMeta) stack.getItemMeta()).setBasePotionType(type);
}
@Override
public int hashCode() {
return stack.getItemMeta().hashCode();
}
};
}
@Override
public Collection<PotionEffect> getEffects(ItemStack stack) {
return ((PotionMeta) stack.getItemMeta()).getCustomEffects();
public void updatePotionStack(ItemStack stack, AbstractPotionData data) {
final PotionMeta meta = (PotionMeta) stack.getItemMeta();
meta.setBasePotionType(data.getType());
meta.clearCustomEffects();
for (PotionEffect effect : data.getEffects()) {
meta.addCustomEffect(effect, true);
}
stack.setItemMeta(meta);
final AbstractPotionData existing = getPotionData(stack);
if (existing.isSplash() != data.isSplash()) {
stack.setType(data.isSplash() ? Material.SPLASH_POTION : Material.POTION);
}
}
@Override

View File

@ -3,26 +3,23 @@ package net.ess3.provider.providers;
import net.ess3.provider.PotionMetaProvider;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import java.util.Collection;
@SuppressWarnings("deprecation")
public class PrehistoricPotionMetaProvider implements PotionMetaProvider {
@Override
public ItemStack createPotionItem(final Material initial, final int effectId) {
final ItemStack potion = new ItemStack(initial, 1);
//noinspection deprecation
potion.setDurability((short) effectId);
return potion;
}
@Override
public boolean isSplash(ItemStack stack) {
public void updatePotionStack(ItemStack stack, AbstractPotionData data) {
throw new UnsupportedOperationException("This should never happen, if this happens please submit a bug report!");
}
@Override
public Collection<PotionEffect> getEffects(ItemStack stack) {
public AbstractPotionData getPotionData(ItemStack stack) {
throw new UnsupportedOperationException("This should never happen, if this happens please submit a bug report!");
}