Files
A FODIN
Code
7 247
A FODIN/src/main/java/com/example/hidenbt/HideNBTPlugin.java
package com.example.hidenbt;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.List;
public final class HideNBTPlugin extends JavaPlugin implements Listener {
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
getLogger().info("HideNBTPlugin has been enabled successfully for Paper 1.21!");
}
@Override
public void onDisable() {
getLogger().info("HideNBTPlugin has been disabled.");
}
@EventHandler
public void onCommandPreprocess(PlayerCommandPreprocessEvent event) {
Player player = event.getPlayer();
String message = event.getMessage().toLowerCase();
String commandLabel = message.split(" ")[0].substring(1);
// 1. Hide plugin from /plugins, /pl for non-OPs
if (!player.isOp()) {
if (commandLabel.equalsIgnoreCase("plugins") || commandLabel.equalsIgnoreCase("pl") ||
commandLabel.equalsIgnoreCase("bukkit:plugins") || commandLabel.equalsIgnoreCase("bukkit:pl")) {
event.setCancelled(true);
List<String> pluginNames = new ArrayList<>();
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (!plugin.getName().equalsIgnoreCase("HideNBT")) {
String color = plugin.isEnabled() ? ChatColor.GREEN.toString() : ChatColor.RED.toString();
pluginNames.add(color + plugin.getName());
}
}
player.sendMessage(ChatColor.GREEN + "Plugins (" + pluginNames.size() + "): " + String.join(ChatColor.WHITE + ", ", pluginNames));
return;
}
}
// 2. Prevent NBT editing (Allow /data get, block /data modify, merge, remove, set)
if (commandLabel.equalsIgnoreCase("data") || commandLabel.endsWith(":data")) {
if (message.contains("merge") || message.contains("modify") || message.contains("remove") || message.contains("set")) {
event.setCancelled(true);
player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cYou are not allowed to edit NBT data."));
return;
}
}
// 3. Attribute inspection: Allow /attribute ... get, but make it show the NORMAL (default) value even if changed!
if (commandLabel.equalsIgnoreCase("attribute") || commandLabel.endsWith(":attribute")) {
if (message.contains("modifier") || message.contains("base")) {
event.setCancelled(true);
player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cYou are not allowed to edit attributes."));
return;
}
String[] args = event.getMessage().split(" ");
if (args.length >= 4 && args[3].equalsIgnoreCase("get")) {
event.setCancelled(true); // Allow command execution by intercepting and outputting the normal/default value
String targetName = args[1];
Player target = Bukkit.getPlayerExact(targetName);
if (target == null) {
player.sendMessage(ChatColor.RED + "Target player not found or offline.");
return;
}
String attrKeyStr = args[2].toLowerCase();
if (attrKeyStr.startsWith("minecraft:")) {
attrKeyStr = attrKeyStr.substring(10);
}
Attribute attribute = null;
try {
NamespacedKey key = NamespacedKey.minecraft(attrKeyStr);
attribute = Registry.ATTRIBUTE.get(key);
} catch (Exception ignored) {}
if (attribute == null) {
for (Attribute attr : Attribute.values()) {
if (attr.key().getKey().equalsIgnoreCase(attrKeyStr) || attr.name().equalsIgnoreCase(attrKeyStr)) {
attribute = attr;
break;
}
}
}
if (attribute != null) {
AttributeInstance instance = target.getAttribute(attribute);
double defaultValue = (instance != null) ? instance.getDefaultValue() : 0.0;
player.sendMessage(ChatColor.translateAlternateColorCodes('&',
target.getName() + " has " + attribute.key().getKey() + " (" + defaultValue + ")"));
} else {
player.sendMessage(ChatColor.RED + "Invalid attribute specified."));
}
return;
}
}
// 4. Prevent Item NBT/component editing
if (commandLabel.equalsIgnoreCase("item") || commandLabel.endsWith(":item")) {
if (message.contains("modify") || message.contains("component")) {
event.setCancelled(true);
player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cYou are not allowed to edit item NBT or components."));
return;
}
}
}
}