BackpackDropOnDeathEvent isn't working?
TetraTheta opened this issue ยท 16 comments
Information
Environment information
Plugin + server version info:
[21:16:55 INFO]: ##### Start Minepacks version info #####
[21:16:55 INFO]: Minepacks: 2.4.3-Release
[21:16:55 INFO]: Server: git-Purpur-1493 (MC: 1.18.1)
[21:16:55 INFO]: Java: 17.0.1
[21:16:55 INFO]: ##### End Minepacks version info #####
Online mode: YES
BungeeCord: NO
Server/crash log
There is no log about this.
Details
Description
BackpackDropOnDeathEvent
doesn't seem to be working at all
Steps to reproduce
- Write plugin for BackpackDropOnDeathEvent
// BackpackDropOnDeath.java
public class BackpackDropOnDeath implements Listener {
@EventHandler
public void onDeath(BackpackDropOnDeathEvent e) {
Bukkit.getLogger().info("Player Backpack Drop Event");
// Define Knowledge Book with Custom Model Data as 1
ItemStack totem = new ItemStack(Material.CARROT_ON_A_STICK);
ItemMeta totemMeta = totem.getItemMeta();
totemMeta.setCustomModelData(1);
totem.setItemMeta(totemMeta);
// Inventory check (Not backpack)
if (e.getOwner().getInventory().contains(totem)) {
// Do I have to check Custom Model Data too?
Bukkit.getLogger().info("totem found");
e.getOwner().getInventory().removeItem(totem);
e.setCancelled(true);
} else {
Bukkit.getLogger().info("totem not found");
}
Bukkit.getLogger().info("Player Backpack Drop Event Processing done");
}
}
// Plugin's main class
Bukkit.getPluginManager().registerEvents(new BackpackDropOnDeath(), plugin);
- In server, give yourself Carrot On A Stick with Custom Model Data
/give @p minecraft:carrot_on_a_stick{CustomModelData:1}
- Suicide.
Expected behavior
As code above, if you have Carrot On A Stick with custom model data 1, you shouldn't lose(drop) item in your backpack (because the event was cancelled).
Actual behavior
I drop every item in my backpack even though I have Carrot On A Stick with Custom Model Data 1.
Other information
I had problem with registering the event. Server threw error message like this:
Plugin EXMCore v2.0.0 has failed to register events for class com.teamyomi.exmcore.event.BackpackDropOnDeath because at/pcgamingfreaks/Minepacks/Bukkit/API/Events/BackpackDropOnDeathEvent does not exist.
I solved that problem with shadowing API into my plugin. There wasn't any note about shadowing in API readme. Maybe that is because I don't use Maven. I use Gradle.
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '7.1.2'
}
repositories {
mavenCentral()
// ...
maven {
name = 'backpack-api'
url = 'https://repo.pcgamingfreaks.at/repository/maven-everything'
}
}
dependencies {
// ...
implementation 'at.pcgamingfreaks:Minepacks-API:2.4.4-SNAPSHOT'
}
If I've coded something wrong, please tell me. I think there is no error in my code logic.
Your server is running on v2.4.3, please use this preview build (v2.4.4-SNAPSHOT).
Oh, I had to use preview build of Minepack as well as API... Thanks!
Also, I think I don't need to shadow entire API because plugin jar has API inside of it.
But I still cannot get the event working. I should get Player Backpack Drop Event
log message but I don't get the message at all. Should I shadow the API for this?
I've set backpack.keepondeath
permission set to false to Luckperms's default
group, and there is no other group present. So even if I am OP, my backpack won't be saved at death, right?
I have confirmed both shadowed and not shadowed situation don't work.
No, you should not shadow the API, if you shadow it, it will not work on spigot 1.16 and newer.
Also the event is only triggered if the backpack would drop, if the player has the permission to keep the backpack on death (given by default to op players) it will not trigger.
You should be able to test if the keepondeath is active for your test user by just putting an item in your backpack and killing yourself. I never actively used luckperms and therefor don't know how it behaves with regard to default values for permissions, but you should be able to check it with /lb user <username> permission check backpack.keepOnDeath
.
I can confirm that me (TetraTheta
) and default group (default
) both set permission backpack.keepondeath
to False
. (LuckPerms seems to be case insensitive). It is set with Tab completion so there should not be spell error.
So, my plugin isn't shadowing the API, LuckPerms permission setting is OK. So I think there should be some problem with my code or API, or Minepacks plugin. My event listener code is above. (I hope only my code has flaw)
Is there anything else I should check too?
I just tested it, with this code MinepacksBackpackKeeper_src.zip. Works as expected (haven't used a permissions plugin and just deoped myself on my test server). I used maven since I don't know gradle (if you have maven installed, you can build it by running mvn package
, jar will then be in the target folder).
If you are willing to share your jar with me I could have a quick look at it. In case you prefer to keep it private you can also send it via mail (it's on my github profile page).
EXMCore-2.0.0.zip & Source Code
It is not closed source, but since the code is part of my 'AIO' plugin, so it might be hard to find out what code you should look at. Most of the code is just in there, doing nothing. I've uploaded compiled jar for your convenience, but if you want some security(safety), you can build it yourself.
How to build yourself:
- Windows:
gradlew.bat jar
- Linux:
./gradlew jar
I will look into your code for finding out what is wrong with me...
EDIT: Since you said you succeeded to cancel the event, there should be problem with my side.
I've tested with latest Paper builds(#141) and only Minepacks and MinepacksBackpackKeeper plugin. I had to op myself to open backpack and deop myself before killing myself (/kill TetraTheta
). I still drop content of my backpack after death.
Maybe standalone mode would affect this?
Working as expected for me. So it must be a configuration issue.
I just put luckperms on my test server. It seems like it is not handling the default permissions correctly, my op user no longer drops the backpack on death when I use luckperms to set it to false.
If then, here is my configurations. paste.gg
In this state, I really don't know where to look at.
Stupid me, I have updated everything except Minepacks when they changed how the maven-shade plugin works.
This build should fix API issues in Standalone mode. It probably still won't work for op's with luckperms.
With LuckPerms, MinepackBackpackKeeper works (mostly). default
group has backpack.use
set to true
, backpack.keepondeath
set to false
.
Only line that didn't work was e.getOwner().getInventory().removeItem(totem);
. It didn't removed 'totem' item from my inventory. (but MinepackBackpackKeeper kept backpack contents)
I don't know how event thing actually works when multiple events is involved in single 'event'(player's death) so I'm not sure the Inventory
is already emptied or not.
If the backpack is loaded the event should fire during the PlayerDeathEvent (monitor priority). If not the event will be triggered once the backpack is loaded. But you will not be able to change the drop list since you don't have access to the PlayerDeathEvent from the BackpackDropOnDeathEvent.
Since there will be changes to the internal data handling with the v3.0 update I would prefer to avoid to implement some hacky solution on my side for now, that will break one v3.0 is released. Therefore I sugesst this for now:
private Set<Player> keepBackpackOnNextDrop = new HashSet<>();
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onDeath(PlayerDeathEvent event) {
ItemStack totem = new ItemStack(Material.CARROT_ON_A_STICK);
ItemMeta totemMeta = totem.getItemMeta();
totemMeta.setCustomModelData(1);
totem.setItemMeta(totemMeta);
if (e.getEntity().getInventory().contains(totem)) {
e.getEntity().getInventory().removeItem(totem);
keepBackpackOnNextDrop.add(e.getEntity());
}
}
@EventHandler
public void onDeath(BackpackDropOnDeathEvent e) {
if(keepBackpackOnNextDrop.contains(e.getOwner())) {
keepBackpackOnNextDrop.remove(e.getOwner());
e.setCancelled(true);
}
}
I think your code is much better than trying to do all things in one event handler. 'Removing totem from inventory' still doesn't work, but I think it is beyond Minepacks' area. Time to talk with Paper guys I think?
Thank you for helping me for long time!
The PlayerDeathEvent has a drop list, if I recall correctly, you have to also remove the item from that.