I'm currently programming a varo plugin. The project should prohibit the use of strength potions. I want to achieve this by creating a listener that responds when a potion of this kind is consumed. If I have any, I do not know the name of the Listener, and if it contains all the potions, how can you specify it for potions?
Thank you in advance:
Code Example for Gold Apples Ban:
public class FoodLevelChangeListener
implements Listener
{
@EventHandler
public void onFoodLevelChange (FoodLevelChangeEvent event)
{
event.setCancelled (Varo.getInstance () NotStarted.);
}
}
https://hub.spigotmc.org/...Event.html
There then check for the starch potion and cancel the event.
@EventHandler
public void on (PlayerItemConsumeEvent e) {
ItemStack item = e.getItem ();
if (item.getType.equals (Material.POTION) {
PotionType type = Potion.fromItemStack (item) .getType ();
if (type.equals (PotionType.STRENGTH) {
e.setCancelled (true);
}
}
}
If (item.getType.equals (Material.POTION)
Can it be that there's a getType ()?
And I get at Potion.fromItemStack… Nen mistake. What is wrong?
Is indeed depracated since the 1.9
https://www.spigotmc.org/...ck.319322/
PotionMeta pmeta = (PotionMeta) item.getItemMeta ();
There you can generate a PotionData as in the thread via getBasePotionData. About getType can compare this with the PotionType.
I've revised my code now and there's no mistake in Eclipse, but if I drink a starch potion, nothing happens. Here is my code from my listener class:
public void on (PlayerItemConsumeEvent event) {
ItemStack itemStack = event.getItem ();
if (itemStack.getType (). Equals (Material.POTION)) {
PotionMeta pmeta = (PotionMeta) itemStack.getItemMeta ();
if (pmeta.equals (PotionType.STRENGTH)) {
event.setCancelled (Varo.getInstance () NotStarted.);
}
}
}
This is the code section of my Main that Varo.class is called:
getServer (). GetPluginManager (). RegisterEvents (new StrengthLevelChangeListener (), this);
Have I written off incorrectly somewhere? (I've changed the ItemStack so do not be surprised)
In the event cancel a "true" is enough.
Can you even debug what pmeta outputs.
If not try with PotionData via getBase…
And there via getType match the PotionType.
Can you even debug what pmeta outputs.
How can I debug pmeta if my plugin is running on a server that is not hosted by me?
Then try variant 2
How do I implement variant 2 in my code do not know me sooo… Good with plugin programming from
I'm mobile so sporadically:
PotionType ptype = pmeta.getBasePotionData (). GetType ();
Ptype equals PotionType. STRENGTH
Public void on (PlayerItemConsumeEvent event) {
ItemStack itemStack = event.getItem ();
if (itemStack.getType (). Equals (Material.POTION)) {
PotionMeta pmeta = (PotionMeta) itemStack.getItemMeta ();
PotionType ptype = pmeta.getBasePotionData (). GetType ();
if (pmeta.equals (PotionType.STRENGTH)) {
event.setCancelled (true);
event.getPlayer (). SendMessage ("§4Du may not use starch drinks in §1Salvos§c");
}
}
}
}
That's my entire class. For PotionType ptype = pmeta.getBasePotionData (). GetType (); there's an error and it is not an import error. What have I done wrong?
Watch it when I'm back on the computer
K thanks
Tested in 1.13.2. Which version do you use?
1.8.8
Have your #Funktion Code 1: 1 Copy & Pasted and it comes again the error with the .getBasePotionData (). Is that dependent on versions?
Yes. But under 1.9 Potion had to go. I will try it.
Under the newest spigot 1.8.8 this code works ^^
Package de.nurteam.varo.listeners;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionType;
import de.nurteam.varo.Varo;
public class StrengthLevelChangeListener implements Listener {
@EventHandler
public void on (PlayerItemConsumeEvent event) {
ItemStack itemStack = event.getItem ();
if (itemStack.getType (). Equals (Material.POTION)) {
PotionType type = Potion.fromItemStack (itemStack) .getType ();
if (type.equals (PotionType.STRENGTH)) {
event.setCancelled (true);
event.getPlayer (). SendMessage ("§4Du may not use starch drinks in §1Salvos§c");
}
}
}
}
This is my entire code. I get at Potion.fromItemStack (itemStack) .getType ();
an error. Is this a mistake on a PC?
You did not import Potion. My IDE does that automatically, it's missing:
Import org.bukkit.potion.Potion
Ok, my mistake with import thank you for everything you answered so quickly etc.
Thank you ^^