Minecraft Plugins How to create an EventListener for starch drinks?

Fa
- in Plugins
23

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.);
}
}

Ma

https://hub.spigotmc.org/...Event.html

There then check for the starch potion and cancel the event.

La

@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);
}
}
}

Fa

If (item.getType.equals (Material.POTION)

Can it be that there's a getType ()?

Fa

And I get at Potion.fromItemStack… Nen mistake. What is wrong?

Ma

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.

Fa

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)

Ma

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.

Fa

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?

Ma

Then try variant 2

Fa

How do I implement variant 2 in my code do not know me sooo… Good with plugin programming from

Ma

I'm mobile so sporadically:

PotionType ptype = pmeta.getBasePotionData (). GetType ();

Ptype equals PotionType. STRENGTH

Fa

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?

Ma

Watch it when I'm back on the computer

Fa

K thanks

Ma


Tested in 1.13.2. Which version do you use?

Fa

1.8.8

Fa

Have your #Funktion Code 1: 1 Copy & Pasted and it comes again the error with the .getBasePotionData (). Is that dependent on versions?

Ma

Yes. But under 1.9 Potion had to go. I will try it.

Ma


Under the newest spigot 1.8.8 this code works ^^

Fa

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?

Ma

You did not import Potion. My IDE does that automatically, it's missing:

Import org.bukkit.potion.Potion

Fa

Ok, my mistake with import thank you for everything you answered so quickly etc.

Ma

Thank you ^^