Skip to content

Instantly share code, notes, and snippets.

@MineTheCube
Last active November 6, 2024 14:27
Show Gist options
  • Save MineTheCube/bea7247565ab4fb2e439c0689454e07d to your computer and use it in GitHub Desktop.
Save MineTheCube/bea7247565ab4fb2e439c0689454e07d to your computer and use it in GitHub Desktop.
Detect when a player opens or closes his own inventory
package fr.onecraft.inventory;
import com.comphenix.tinyprotocol.TinyProtocol;
import io.netty.channel.Channel;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Field;
/**
* Detect when a player opens or closes his own inventory
*/
public class DetectInventoryOpenClose {
public void onPlayerOpenInventory(Player p) {
Bukkit.broadcastMessage("§aOPEN INVENTORY: " + p.getName());
}
public void onPlayerCloseInventory(Player p) {
Bukkit.broadcastMessage("§cCLOSED INVENTORY: " + p.getName());
}
public DetectInventoryOpenClose(Plugin plugin) {
new TinyProtocol(plugin) {
@Override
public Object onPacketInAsync(Player sender, Channel channel, Object packet) {
String packetName = packet.getClass().getSimpleName();
if ("PacketPlayInClientCommand".equals(packetName)) {
for (Field field : packet.getClass().getDeclaredFields()) {
if ("EnumClientCommand".equals(field.getType().getSimpleName())) {
try {
field.setAccessible(true);
if ("OPEN_INVENTORY_ACHIEVEMENT".equals(field.get(packet).toString())) {
onPlayerOpenInventory(sender);
break;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
} else if ("PacketPlayInCloseWindow".equals(packetName)) {
for (Field field : packet.getClass().getDeclaredFields()) {
if (field.getType() == int.class) {
try {
field.setAccessible(true);
if ((int) field.get(packet) == 0) {
onPlayerCloseInventory(sender);
break;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
return super.onPacketInAsync(sender, channel, packet);
}
};
}
}
<properties>
<!-- Spigot Version -->
<spigotVersion>1.10.2-R0.1-SNAPSHOT</spigotVersion>
</properties>
<repositories>
<!-- Spigot repository -->
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<!-- TinyProtocol repository -->
<repository>
<id>dmulloy2-repo</id>
<url>http://repo.dmulloy2.net/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<!-- Spigot API -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>${spigotVersion}</version>
<scope>provided</scope>
</dependency>
<!-- Spigot server -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>${spigotVersion}</version>
<scope>provided</scope>
</dependency>
<!-- TinyProtocol -->
<dependency>
<groupId>com.comphenix.tinyprotocol</groupId>
<artifactId>TinyProtocol</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment