3
0
Mirror von https://github.com/St3venAU/ArmorStandTools.git synchronisiert 2024-12-29 04:50:09 +01:00

/give command is now called silently (no console output), slight refactor of function createItemLore()

Dieser Commit ist enthalten in:
BuildTools 2023-01-28 13:21:35 +01:00
Ursprung 3883d44273
Commit 96c58199ec
4 geänderte Dateien mit 87 neuen und 87 gelöschten Zeilen

Datei anzeigen

@ -262,11 +262,7 @@ class ArmorStandGUI implements Listener {
Utils.title(p, Config.glow + ": " + (glowing ? Config.isOn : Config.isOff));
break;
case ITEM:
World w = p.getWorld();
boolean commandFeedback = Boolean.TRUE.equals(w.getGameRuleValue(GameRule.SEND_COMMAND_FEEDBACK));
if(commandFeedback) w.setGameRule(GameRule.SEND_COMMAND_FEEDBACK, false);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Utils.createGiveCommand(as, p));
if(commandFeedback) w.setGameRule(GameRule.SEND_COMMAND_FEEDBACK, true);
Reflections.callCommandSilently(Utils.createGiveCommand(as, p));
p.closeInventory();
if(p.getGameMode() != GameMode.CREATIVE) {
as.remove();

Datei anzeigen

@ -1,68 +0,0 @@
package com.gmail.St3venAU.plugins.ArmorStandTools;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
// CREDIT: This class is based on the MiniNBT library: https://github.com/I-Al-Istannen/MiniNBT
public class ItemStackNBT {
private final static Method AS_NMS_COPY;
private final static Method GET_TAG;
private final static Method TAG_TO_STRING;
private static final String SERVER_VERSION;
static {
boolean runningUnderTest = Bukkit.getServer() == null
|| Bukkit.getServer().getClass().getName().contains("Mockito");
String name = runningUnderTest
? "org.bukkit.craftbukkit.v1_14_R1"
: Bukkit.getServer().getClass().getPackage().getName();
String[] split = name.split("\\.");
name = split[split.length - 1];
SERVER_VERSION = name;
Class<?> craftItemStackClass;
Class<?> itemStackClass;
Class<?> compoundTagClass;
try {
craftItemStackClass = Class.forName("org.bukkit.craftbukkit." + SERVER_VERSION + ".inventory.CraftItemStack");
itemStackClass = Class.forName("net.minecraft.world.item.ItemStack");
compoundTagClass = Class.forName("net.minecraft.nbt.NBTTagCompound");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
try {
AS_NMS_COPY = craftItemStackClass.getMethod("asNMSCopy", ItemStack.class);
GET_TAG = itemStackClass.getDeclaredMethod("getTagClone");
GET_TAG.setAccessible(true);
TAG_TO_STRING = compoundTagClass.getMethod("toString");
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
public static String itemToString(ItemStack item){
Object nmsItem;
try {
nmsItem = AS_NMS_COPY.invoke(null, item);
if (nmsItem == null) {
throw new NullPointerException("Unable to find a nms item clone for " + item);
}
Object tag = GET_TAG.invoke(nmsItem);
if (tag == null) {
return null;
}
return (String) TAG_TO_STRING.invoke(tag);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
//
}

Datei anzeigen

@ -0,0 +1,68 @@
package com.gmail.St3venAU.plugins.ArmorStandTools;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ProxiedCommandSender;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
public class Reflections {
private final static Method AS_NMS_COPY;
private final static Method GET_TAG;
private static final String SERVER_VERSION;
private static final ProxiedCommandSender PROXIED_COMMAND_SENDER;
static {
String name = Bukkit.getServer().getClass().getPackage().getName();
String[] split = name.split("\\.");
name = split[split.length - 1];
SERVER_VERSION = name;
try {
AS_NMS_COPY = Class.forName("org.bukkit.craftbukkit." + SERVER_VERSION + ".inventory.CraftItemStack").getMethod("asNMSCopy", ItemStack.class);
GET_TAG = Class.forName("net.minecraft.world.item.ItemStack").getDeclaredMethod("getTagClone");
GET_TAG.setAccessible(true);
Object commandSourceStack = Class.forName("org.bukkit.craftbukkit." + SERVER_VERSION + ".command.VanillaCommandWrapper").getMethod("getListener", CommandSender.class).invoke(null, Bukkit.getConsoleSender());
Method suppressOutput = Arrays.stream(commandSourceStack.getClass().getDeclaredMethods()).filter(m -> m.getParameters().length == 0 && m.getReturnType() == commandSourceStack.getClass()).findAny().orElse(null);
if(suppressOutput == null){
throw new RuntimeException("Cant find method withSuppressedOutput() of net.minecraft.commands.CommandListenerWrapper");
}
PROXIED_COMMAND_SENDER = (ProxiedCommandSender) Class.forName("org.bukkit.craftbukkit." + SERVER_VERSION + ".command.ProxiedNativeCommandSender")
.getConstructor(commandSourceStack.getClass(), CommandSender.class, CommandSender.class)
.newInstance(suppressOutput.invoke(commandSourceStack), Bukkit.getConsoleSender(), Bukkit.getConsoleSender());
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException | InstantiationException e) {
throw new RuntimeException(e);
}
}
// CREDIT: This function is based on the MiniNBT library: https://github.com/I-Al-Istannen/MiniNBT
public static String itemToString(ItemStack item){
Object nmsItem;
try {
nmsItem = AS_NMS_COPY.invoke(null, item);
if (nmsItem == null) {
throw new NullPointerException("Unable to find a nms item clone for " + item);
}
Object tag = GET_TAG.invoke(nmsItem);
if (tag == null) {
return null;
}
return tag.toString();
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
// Run a command with no output to the console
public static void callCommandSilently(String command){
Bukkit.dispatchCommand(PROXIED_COMMAND_SENDER, command);
}
}

Datei anzeigen

@ -2,9 +2,11 @@ package com.gmail.St3venAU.plugins.ArmorStandTools;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.block.CommandBlock;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ProxiedCommandSender;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
@ -13,19 +15,20 @@ import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.EulerAngle;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;
class Utils {
@ -148,7 +151,7 @@ class Utils {
if(is.getAmount() > 0) {
sb.append(",Count:").append(is.getAmount());
}
String itemStackTags = ItemStackNBT.itemToString(is);
String itemStackTags = Reflections.itemToString(is);
if(itemStackTags != null && !itemStackTags.isEmpty()) {
sb.append(",tag:");
sb.append(itemStackTags);
@ -253,12 +256,12 @@ class Utils {
if (e != null) {
int stacks = 0;
int items = 0;
if(e.getHelmet() != null) { stacks++; items += e.getHelmet().getAmount(); }
if(e.getChestplate() != null) { stacks++; items += e.getChestplate().getAmount(); }
if(e.getLeggings() != null) { stacks++; items += e.getLeggings().getAmount(); }
if(e.getBoots() != null) { stacks++; items += e.getBoots().getAmount(); }
if(Material.AIR != e.getItemInMainHand().getType()) { stacks++; items += e.getItemInMainHand().getAmount(); }
if(Material.AIR != e.getItemInOffHand().getType()) { stacks++; items += e.getItemInOffHand().getAmount(); }
for(EquipmentSlot slot : EquipmentSlot.values()){
ItemStack item = e.getItem(slot);
if(item.getType() == Material.AIR) continue;
stacks++;
items += item.getAmount();
}
if(stacks > 0) {
lore.add(Config.inventory + ": " + items + " " + Config.items + " (" + stacks + " " + Config.stacks + ")");
}
@ -392,4 +395,5 @@ class Utils {
clone.setMetadata("clone", new FixedMetadataValue(AST.plugin, true));
return clone;
}
}