Skip to content

Commit

Permalink
some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
JRoy committed Apr 26, 2024
1 parent ec5c538 commit 9cd7b65
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand Down Expand Up @@ -127,12 +128,12 @@ public ItemStack get(String id, final boolean useResolvers) throws Exception {
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
Expand Down Expand Up @@ -204,7 +205,7 @@ private ItemData lookup(final ItemStack item) {
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);
Expand All @@ -224,14 +225,14 @@ public Collection<String> listNames() {
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;
}
Expand Down Expand Up @@ -267,7 +268,7 @@ public Material getMaterial() {
return material;
}

public PotionData getPotionData() {
public PotionMetaProvider.AbstractPotionData getPotionData() {
return this.potionData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,45 @@ public ItemStack createPotionItem(final Material initial, final int effectId) {
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;

Expand All @@ -15,13 +16,49 @@ public ItemStack createPotionItem(Material initial, int effectId) {
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@
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!");
}

Expand Down

0 comments on commit 9cd7b65

Please sign in to comment.