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 com.google.gson.JsonParser;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import net.ess3.api.TranslatableException; import net.ess3.api.TranslatableException;
import net.ess3.provider.PotionMetaProvider;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -127,12 +128,12 @@ public class FlatItemDb extends AbstractItemDb {
final ItemStack stack = new ItemStack(material); final ItemStack stack = new ItemStack(material);
stack.setAmount(material.getMaxStackSize()); stack.setAmount(material.getMaxStackSize());
final PotionData potionData = data.getPotionData(); final PotionMetaProvider.AbstractPotionData potionData = data.getPotionData();
final ItemMeta meta = stack.getItemMeta(); final ItemMeta meta = stack.getItemMeta();
if (potionData != null && meta instanceof PotionMeta) { if (potionData != null && meta instanceof PotionMeta) {
final PotionMeta potionMeta = (PotionMeta) meta; 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 // 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(); final Material type = item.getType();
if (MaterialUtil.isPotion(type) && item.getItemMeta() instanceof PotionMeta) { 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); return new ItemData(type, potion);
} else if (type.toString().contains("SPAWNER")) { } else if (type.toString().contains("SPAWNER")) {
final EntityType entity = ess.getSpawnerItemProvider().getEntityType(item); final EntityType entity = ess.getSpawnerItemProvider().getEntityType(item);
@ -224,14 +225,14 @@ public class FlatItemDb extends AbstractItemDb {
public static class ItemData { public static class ItemData {
private Material material; private Material material;
private String[] fallbacks = null; private String[] fallbacks = null;
private PotionData potionData = null; private PotionMetaProvider.AbstractPotionData potionData = null;
private EntityType entity = null; private EntityType entity = null;
ItemData(final Material material) { ItemData(final Material material) {
this.material = material; this.material = material;
} }
ItemData(final Material material, final PotionData potionData) { ItemData(final Material material, final PotionMetaProvider.AbstractPotionData potionData) {
this.material = material; this.material = material;
this.potionData = potionData; this.potionData = potionData;
} }
@ -267,7 +268,7 @@ public class FlatItemDb extends AbstractItemDb {
return material; return material;
} }
public PotionData getPotionData() { public PotionMetaProvider.AbstractPotionData getPotionData() {
return this.potionData; return this.potionData;
} }

View File

@ -68,17 +68,45 @@ public class LegacyPotionMetaProvider implements PotionMetaProvider {
} }
@Override @Override
public boolean isSplash(ItemStack stack) { public AbstractPotionData getPotionData(ItemStack stack) {
//noinspection deprecation return new AbstractPotionData() {
final Potion potion = Potion.fromDamage(stack.getDurability()); final Potion potion = Potion.fromDamage(stack.getDurability());
return potion.isSplash();
@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 @Override
public Collection<PotionEffect> getEffects(ItemStack stack) { public void updatePotionStack(ItemStack stack, AbstractPotionData data) {
//noinspection deprecation return;
final Potion potion = Potion.fromDamage(stack.getDurability()); //todo
return potion.getEffects(); return;
} }
@Override @Override

View File

@ -3,19 +3,32 @@ package net.ess3.provider;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import java.util.Collection; import java.util.Collection;
public interface PotionMetaProvider extends Provider { public interface PotionMetaProvider extends Provider {
ItemStack createPotionItem(Material initial, int effectId); ItemStack createPotionItem(Material initial, int effectId);
/** AbstractPotionData getPotionData(ItemStack stack);
* Should only be used for pre-flattening
*/
boolean isSplash(ItemStack stack);
/** void updatePotionStack(ItemStack stack, AbstractPotionData data);
* Should only be used for pre-flattening
*/ interface AbstractPotionData {
Collection<PotionEffect> getEffects(ItemStack stack); /**
* 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.ItemStack;
import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import java.util.Collection; import java.util.Collection;
@ -15,13 +16,49 @@ public class ModernPotionMetaProvider implements PotionMetaProvider {
} }
@Override @Override
public boolean isSplash(ItemStack stack) { public AbstractPotionData getPotionData(ItemStack stack) {
return stack.getType() == Material.SPLASH_POTION; 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 @Override
public Collection<PotionEffect> getEffects(ItemStack stack) { public void updatePotionStack(ItemStack stack, AbstractPotionData data) {
return ((PotionMeta) stack.getItemMeta()).getCustomEffects(); 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 @Override

View File

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