Update NMS
Update MenuBuilder Update Reflection Update BaseCommand
This commit is contained in:
parent
9aa72cdc91
commit
5908428403
@ -6,7 +6,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = 'me.skymc'
|
||||
version = '5.12'
|
||||
version = '5.13'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
targetCompatibility = 1.8
|
||||
|
@ -46,42 +46,31 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
|
||||
public static void loadCommandRegister(BaseMainCommand baseMainCommand) {
|
||||
List<Method> methods = new ArrayList<>();
|
||||
List<CommandField> fields = new ArrayList<>();
|
||||
baseMainCommand.getLinkClasses().forEach(clazz -> java.util.Arrays.stream(clazz.getDeclaredMethods()).filter(method -> method.getAnnotation(SubCommand.class) != null).forEach(methods::add));
|
||||
baseMainCommand.getLinkClasses()
|
||||
.forEach(clazz -> java.util.Arrays.stream(clazz.getDeclaredMethods())
|
||||
.filter(method -> method.getAnnotation(SubCommand.class) != null).forEach(m -> {
|
||||
m.setAccessible(true);
|
||||
methods.add(m);
|
||||
}));
|
||||
if (methods.size() > 0) {
|
||||
methods.sort(Comparator.comparingDouble(a -> a.getAnnotation(SubCommand.class).priority()));
|
||||
methods.forEach(method -> {
|
||||
BaseSubCommand subCommand = null;
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
BaseSubCommand subCommand = null;
|
||||
// lite parameter
|
||||
if (Arrays.equals(method.getParameterTypes(), new Class[] {CommandSender.class, String[].class})) {
|
||||
subCommand = new BaseSubCommand() {
|
||||
@Override
|
||||
public void onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
try {
|
||||
method.invoke(baseMainCommand, sender, args);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
subCommand = buildSubCommand(baseMainCommand, method)
|
||||
.label(method.getName())
|
||||
.annotation(method.getAnnotation(SubCommand.class));
|
||||
}
|
||||
// fully parameter
|
||||
else if (Arrays.equals(method.getParameterTypes(), new Class[] {CommandSender.class, Command.class, String.class, String[].class})) {
|
||||
subCommand = new BaseSubCommand() {
|
||||
@Override
|
||||
public void onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
try {
|
||||
method.invoke(baseMainCommand, sender, command, label, args);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
// player only parameter
|
||||
else if (Arrays.equals(method.getParameterTypes(), new Class[] {Player.class, String[].class})) {
|
||||
subCommand = buildSubCommand(baseMainCommand, method)
|
||||
.player()
|
||||
.label(method.getName())
|
||||
.annotation(method.getAnnotation(SubCommand.class));
|
||||
}
|
||||
if (subCommand != null) {
|
||||
subCommand.setLabel(method.getName());
|
||||
subCommand.setAnnotation(method.getAnnotation(SubCommand.class));
|
||||
baseMainCommand.registerSubCommand(subCommand);
|
||||
}
|
||||
} catch (Throwable ignored) {
|
||||
@ -95,8 +84,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
|
||||
try {
|
||||
commandField.getField().setAccessible(true);
|
||||
BaseSubCommand subCommand = (BaseSubCommand) commandField.getField().get(commandField.getParent().newInstance());
|
||||
subCommand.setLabel(commandField.getField().getName());
|
||||
subCommand.setAnnotation(commandField.getField().getAnnotation(SubCommand.class));
|
||||
subCommand.label(commandField.getField().getName()).annotation(commandField.getField().getAnnotation(SubCommand.class));
|
||||
baseMainCommand.registerSubCommand(subCommand);
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
@ -107,6 +95,19 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private static BaseSubCommand buildSubCommand(BaseMainCommand baseMainCommand, Method method) {
|
||||
return new BaseSubCommand() {
|
||||
@Override
|
||||
public void onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
try {
|
||||
method.invoke(baseMainCommand, sender, args);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void setRegisterCommand(PluginCommand registerCommand) {
|
||||
this.registerCommand = registerCommand;
|
||||
}
|
||||
|
@ -16,15 +16,10 @@ import java.util.stream.IntStream;
|
||||
public abstract class BaseSubCommand {
|
||||
|
||||
private String label;
|
||||
private boolean player;
|
||||
private SubCommand annotation;
|
||||
|
||||
public void setAnnotation(SubCommand annotation) {
|
||||
this.annotation = annotation;
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
abstract public void onCommand(CommandSender sender, Command command, String label, String[] args);
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
@ -43,7 +38,7 @@ public abstract class BaseSubCommand {
|
||||
}
|
||||
|
||||
public CommandType getType() {
|
||||
return annotation.type();
|
||||
return player ? CommandType.PLAYER : annotation.type();
|
||||
}
|
||||
|
||||
public boolean ignoredLabel() {
|
||||
@ -70,5 +65,18 @@ public abstract class BaseSubCommand {
|
||||
return TLocale.asString(Strings.isEmpty(getDescription()) ? "COMMANDS.INTERNAL.COMMAND-HELP-EMPTY" : "COMMANDS.INTERNAL.COMMAND-HELP", label, getLabel(), Arrays.stream(getArguments()).map(parameter -> parameter.toString() + " ").collect(Collectors.joining()), getDescription());
|
||||
}
|
||||
|
||||
abstract public void onCommand(CommandSender sender, Command command, String label, String[] args);
|
||||
protected BaseSubCommand label(String label) {
|
||||
this.label = label;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected BaseSubCommand player() {
|
||||
player = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected BaseSubCommand annotation(SubCommand annotation) {
|
||||
this.annotation = annotation;
|
||||
return this;
|
||||
}
|
||||
}
|
@ -66,11 +66,11 @@ public class SimpleReflection {
|
||||
try {
|
||||
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
|
||||
if (fields == null) {
|
||||
return;
|
||||
throw new RuntimeException("Not Found Cache.");
|
||||
}
|
||||
Field field = fields.get(fieldName);
|
||||
if (value == null) {
|
||||
return;
|
||||
throw new RuntimeException("Not Found Field.");
|
||||
}
|
||||
field.set(instance, value);
|
||||
} catch (Exception e) {
|
||||
@ -82,11 +82,11 @@ public class SimpleReflection {
|
||||
try {
|
||||
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
|
||||
if (fields == null) {
|
||||
return null;
|
||||
throw new RuntimeException("Not Found Cache.");
|
||||
}
|
||||
Field field = fields.get(fieldName);
|
||||
if (field == null) {
|
||||
return null;
|
||||
throw new RuntimeException("Not Found Field.");
|
||||
}
|
||||
return field.get(instance);
|
||||
} catch (Exception e) {
|
||||
@ -99,11 +99,11 @@ public class SimpleReflection {
|
||||
try {
|
||||
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
|
||||
if (fields == null) {
|
||||
return def;
|
||||
throw new RuntimeException("Not Found Cache.");
|
||||
}
|
||||
Field field = fields.get(fieldName);
|
||||
if (field == null) {
|
||||
return def;
|
||||
throw new RuntimeException("Not Found Field.");
|
||||
}
|
||||
return (T) field.get(instance);
|
||||
} catch (Exception e) {
|
||||
|
@ -2,6 +2,7 @@ package io.izzel.taboolib.module.nms;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import io.izzel.taboolib.module.inject.TInject;
|
||||
import io.izzel.taboolib.module.nms.nbt.Attribute;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTAttribute;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTCompound;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTList;
|
||||
@ -67,4 +68,6 @@ public abstract class NMS {
|
||||
}
|
||||
|
||||
abstract public List<NBTAttribute> getBaseAttribute(ItemStack item);
|
||||
|
||||
abstract public Object toNMS(Attribute attribute);
|
||||
}
|
||||
|
@ -4,10 +4,7 @@ import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Multimap;
|
||||
import io.izzel.taboolib.Version;
|
||||
import io.izzel.taboolib.module.lite.SimpleReflection;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTAttribute;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTCompound;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTList;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTOperation;
|
||||
import io.izzel.taboolib.module.nms.nbt.*;
|
||||
import io.izzel.taboolib.module.packet.TPacketHandler;
|
||||
import net.minecraft.server.v1_12_R1.ChatMessageType;
|
||||
import net.minecraft.server.v1_12_R1.EntityVillager;
|
||||
@ -16,6 +13,7 @@ import net.minecraft.server.v1_12_R1.NBTTagCompound;
|
||||
import net.minecraft.server.v1_13_R2.EnumHand;
|
||||
import net.minecraft.server.v1_13_R2.IRegistry;
|
||||
import net.minecraft.server.v1_8_R3.*;
|
||||
import net.minecraft.server.v1_8_R3.NBTBase;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Particle;
|
||||
import org.bukkit.craftbukkit.v1_12_R1.CraftParticle;
|
||||
@ -263,6 +261,12 @@ public class NMSImpl extends NMS {
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toNMS(Attribute attribute) {
|
||||
SimpleReflection.checkAndSave(GenericAttributes.class);
|
||||
return SimpleReflection.getFieldValue(GenericAttributes.class, null, attribute.name());
|
||||
}
|
||||
|
||||
private Object toNBTBase(io.izzel.taboolib.module.nms.nbt.NBTBase base) {
|
||||
switch (base.getType().getId()) {
|
||||
case 1:
|
||||
|
@ -0,0 +1,62 @@
|
||||
package io.izzel.taboolib.module.nms.nbt;
|
||||
|
||||
import io.izzel.taboolib.module.nms.NMS;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2019-12-11 19:31
|
||||
*/
|
||||
public enum Attribute {
|
||||
|
||||
MAX_HEALTH("generic.maxHealth", new String[] {"health", "maxHealth"}),
|
||||
|
||||
FOLLOW_RANGE("generic.followRange", new String[] {"follow", "followRange"}),
|
||||
|
||||
KNOCKBACK_RESISTANCE("generic.knockbackResistance", new String[] {"knockback", "knockbackResistance"}),
|
||||
|
||||
MOVEMENT_SPEED("generic.movementSpeed", new String[] {"speed", "movementSpeed", "walkSpeed"}),
|
||||
|
||||
FLYING_SPEED("generic.flyingSpeed", new String[] {"flySpeed", "flyingSpeed"}),
|
||||
|
||||
ATTACK_DAMAGE("generic.attackDamage", new String[] {"damage", "attackDamage"}),
|
||||
|
||||
ATTACK_KNOCKBACK("generic.attackKnockback", new String[] {"damageKnockback", "attackKnockback"}),
|
||||
|
||||
ATTACK_SPEED("generic.attackSpeed", new String[] {"damageSpeed", "attackSpeed"}),
|
||||
|
||||
ARMOR("generic.armor", new String[] {"armor"}),
|
||||
|
||||
ARMOR_TOUGHNESS("generic.armorToughness", new String[] {"toughness", "armorToughness"}),
|
||||
|
||||
LUCK("generic.luck", new String[] {"luck"});
|
||||
|
||||
String minecraftKey;
|
||||
String[] simplifiedKey;
|
||||
|
||||
Attribute(String minecraftKey, String[] simplifiedKey) {
|
||||
this.minecraftKey = minecraftKey;
|
||||
this.simplifiedKey = simplifiedKey;
|
||||
}
|
||||
|
||||
public String getMinecraftKey() {
|
||||
return minecraftKey;
|
||||
}
|
||||
|
||||
public String[] getSimplifiedKey() {
|
||||
return simplifiedKey;
|
||||
}
|
||||
|
||||
public Object toNMS() {
|
||||
return NMS.handle().toNMS(this);
|
||||
}
|
||||
|
||||
public boolean match(String source) {
|
||||
return name().equalsIgnoreCase(source) || minecraftKey.equalsIgnoreCase(source) || Arrays.stream(simplifiedKey).anyMatch(key -> key.equalsIgnoreCase(source));
|
||||
}
|
||||
|
||||
public static Attribute parse(String source) {
|
||||
return Arrays.stream(values()).filter(attribute -> attribute.match(source)).findFirst().orElse(null);
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import io.izzel.taboolib.module.lite.SimpleEquip;
|
||||
import io.izzel.taboolib.module.lite.SimpleI18n;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.nms.NMS;
|
||||
import io.izzel.taboolib.module.nms.nbt.Attribute;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTBase;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTCompound;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTList;
|
||||
@ -54,6 +55,14 @@ public class Items {
|
||||
return !isNull(item);
|
||||
}
|
||||
|
||||
public static boolean hasName(ItemStack i) {
|
||||
return !isNull(i) && i.getItemMeta().hasDisplayName();
|
||||
}
|
||||
|
||||
public static boolean hasName(ItemStack i, String a) {
|
||||
return hasName(i) && getName(i).contains(a);
|
||||
}
|
||||
|
||||
public static boolean hasLore(ItemStack i, String a) {
|
||||
return hasLore(i) && i.getItemMeta().getLore().toString().contains(a);
|
||||
}
|
||||
@ -62,10 +71,6 @@ public class Items {
|
||||
return !isNull(i) && i.getItemMeta().hasLore();
|
||||
}
|
||||
|
||||
public static boolean hasName(ItemStack i) {
|
||||
return !isNull(i) && i.getItemMeta().hasDisplayName();
|
||||
}
|
||||
|
||||
public static Material asMaterial(String args) {
|
||||
try {
|
||||
Material material = Material.getMaterial(args.toUpperCase());
|
||||
@ -111,24 +116,7 @@ public class Items {
|
||||
}
|
||||
|
||||
public static String asAttribute(String name) {
|
||||
switch (name.toLowerCase()) {
|
||||
case "damage":
|
||||
return "generic.attackDamage";
|
||||
case "attackspeed":
|
||||
return "generic.attackSpeed";
|
||||
case "health":
|
||||
return "generic.maxHealth";
|
||||
case "speed":
|
||||
return "generic.movementSpeed";
|
||||
case "knockback":
|
||||
return "generic.knockbackResistance";
|
||||
case "armor":
|
||||
return "generic.armor";
|
||||
case "luck":
|
||||
return "generic.luck";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
return Attribute.parse(name).getMinecraftKey();
|
||||
}
|
||||
|
||||
public static ItemStack replaceName(ItemStack item, String nameOld, String nameNew) {
|
||||
|
@ -30,25 +30,27 @@ class ClickListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void e(PluginDisableEvent e) {
|
||||
Bukkit.getOnlinePlayers().stream().filter(player -> player.getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && e.getPlugin().equals(((MenuHolder) player.getOpenInventory().getTopInventory().getHolder()).getBuilder().getPlugin())).forEach(HumanEntity::closeInventory);
|
||||
Bukkit.getOnlinePlayers().stream().filter(player -> MenuHolder.get(player.getOpenInventory().getTopInventory()) != null).forEach(HumanEntity::closeInventory);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void e(InventoryOpenEvent e) {
|
||||
if (e.getInventory().getHolder() instanceof MenuHolder) {
|
||||
Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), () -> ((MenuHolder) e.getInventory().getHolder()).getBuilder().getBuildTask().run(e.getInventory()), 1);
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(TabooLib.getPlugin(), () -> ((MenuHolder) e.getInventory().getHolder()).getBuilder().getBuildTaskAsync().run(e.getInventory()), 1);
|
||||
MenuBuilder builder = MenuHolder.get(e.getInventory());
|
||||
if (builder != null) {
|
||||
Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), () -> builder.getBuildTask().run(e.getInventory()), 1);
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(TabooLib.getPlugin(), () -> builder.getBuildTaskAsync().run(e.getInventory()), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void e(InventoryClickEvent e) {
|
||||
if (e.getInventory().getHolder() instanceof MenuHolder) {
|
||||
MenuBuilder builder = MenuHolder.get(e.getInventory());
|
||||
if (builder != null) {
|
||||
// lock hand
|
||||
if (((MenuHolder) e.getInventory().getHolder()).getBuilder().isLockHand() && (e.getRawSlot() - e.getInventory().getSize() - 27 == e.getWhoClicked().getInventory().getHeldItemSlot() || (e.getClick() == org.bukkit.event.inventory.ClickType.NUMBER_KEY && e.getHotbarButton() == e.getWhoClicked().getInventory().getHeldItemSlot()))) {
|
||||
if (builder.isLockHand() && (e.getRawSlot() - e.getInventory().getSize() - 27 == e.getWhoClicked().getInventory().getHeldItemSlot() || (e.getClick() == org.bukkit.event.inventory.ClickType.NUMBER_KEY && e.getHotbarButton() == e.getWhoClicked().getInventory().getHeldItemSlot()))) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getClickTask()).ifPresent(t -> t.run(new ClickEvent(ClickType.CLICK, e, ((MenuHolder) e.getInventory().getHolder()).getBuilder().getSlot(e.getRawSlot()))));
|
||||
Optional.ofNullable(builder.getClickTask()).ifPresent(t -> t.run(new ClickEvent(ClickType.CLICK, e, builder.getSlot(e.getRawSlot()))));
|
||||
// drop on empty area
|
||||
if (!e.isCancelled() && Items.nonNull(e.getCurrentItem()) && e.getClick() == org.bukkit.event.inventory.ClickType.DROP) {
|
||||
Item item = Vectors.itemDrop((Player) e.getWhoClicked(), e.getCurrentItem());
|
||||
@ -78,15 +80,17 @@ class ClickListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void e(InventoryDragEvent e) {
|
||||
if (e.getInventory().getHolder() instanceof MenuHolder) {
|
||||
Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getClickTask()).ifPresent(t -> t.run(new ClickEvent(ClickType.DRAG, e, ' ')));
|
||||
MenuBuilder builder = MenuHolder.get(e.getInventory());
|
||||
if (builder != null) {
|
||||
builder.getClickTask().run(new ClickEvent(ClickType.DRAG, e, ' '));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void e(InventoryCloseEvent e) {
|
||||
if (e.getInventory().getHolder() instanceof MenuHolder) {
|
||||
Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getCloseTask()).ifPresent(t -> t.run(e));
|
||||
MenuBuilder builder = MenuHolder.get(e.getInventory());
|
||||
if (builder != null) {
|
||||
builder.getCloseTask().run(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,10 +23,14 @@ public class MenuBuilder {
|
||||
private String title;
|
||||
private int rows;
|
||||
private char[][] items = new char[0][0];
|
||||
private ClickTask clickTask;
|
||||
private CloseTask closeTask;
|
||||
private BuildTask buildTask;
|
||||
private BuildTask buildTaskAsync;
|
||||
private ClickTask clickTask = r -> {
|
||||
};
|
||||
private CloseTask closeTask = r -> {
|
||||
};
|
||||
private BuildTask buildTask = r -> {
|
||||
};
|
||||
private BuildTask buildTaskAsync = r -> {
|
||||
};
|
||||
private boolean lockHand;
|
||||
|
||||
public MenuBuilder(Plugin plugin) {
|
||||
@ -41,6 +45,10 @@ public class MenuBuilder {
|
||||
return new MenuBuilder(Ref.getCallerPlugin(Ref.getCallerClass(3).orElse(TabooLib.class)));
|
||||
}
|
||||
|
||||
public void open(Player player) {
|
||||
player.openInventory(build());
|
||||
}
|
||||
|
||||
public MenuBuilder lockHand() {
|
||||
this.lockHand = true;
|
||||
return this;
|
||||
@ -94,10 +102,6 @@ public class MenuBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void open(Player player) {
|
||||
player.openInventory(build());
|
||||
}
|
||||
|
||||
public Inventory build() {
|
||||
Inventory inventory = Bukkit.createInventory(new MenuHolder(this), rows, String.valueOf(title));
|
||||
for (int i = 0; i < items.length && i < rows; i++) {
|
||||
|
@ -23,4 +23,8 @@ class MenuHolder implements InventoryHolder {
|
||||
public Inventory getInventory() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static MenuBuilder get(Inventory inventory) {
|
||||
return inventory.getHolder() instanceof MenuHolder ? ((MenuHolder) inventory.getHolder()).getBuilder() : null;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user