Merge remote-tracking branch 'origin/master'

This commit is contained in:
IzzelAliz 2020-01-07 22:56:44 +08:00
commit 3849ec950a
15 changed files with 197 additions and 113 deletions

View File

@ -6,7 +6,7 @@ plugins {
}
group = 'me.skymc'
version = '5.12'
version = '5.13'
sourceCompatibility = 1.8
targetCompatibility = 1.8

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -31,6 +31,7 @@ public class LocalPlayer {
files.forEach((name, file) -> {
try {
file.save(toFile(name));
} catch (NullPointerException ignored) {
} catch (Throwable t) {
t.printStackTrace();
}
@ -43,6 +44,7 @@ public class LocalPlayer {
if (toPlayer(name) == null) {
try {
files.remove(name).save(toFile(name));
} catch (NullPointerException ignored) {
} catch (Throwable t) {
t.printStackTrace();
}

View File

@ -63,50 +63,50 @@ public class SimpleReflection {
}
public static void setFieldValue(Class<?> nmsClass, Object instance, String fieldName, Object value) {
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.");
}
try {
field.set(instance, value);
} catch (Exception e) {
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static Object getFieldValue(Class<?> nmsClass, Object instance, String fieldName) {
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.");
}
try {
return field.get(instance);
} catch (Exception e) {
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public static <T> T getFieldValue(Class<?> nmsClass, Object instance, String fieldName, T def) {
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.");
}
try {
return (T) field.get(instance);
} catch (Exception e) {
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return def;

View File

@ -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);
}

View File

@ -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:

View File

@ -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);
}
}

View File

@ -1,6 +1,8 @@
package io.izzel.taboolib.module.nms.nbt;
import com.google.common.collect.Maps;
import io.izzel.taboolib.module.nms.NMS;
import org.bukkit.inventory.ItemStack;
import java.util.Collection;
import java.util.Map;
@ -24,6 +26,10 @@ public class NBTCompound extends NBTBase implements Map<String, NBTBase> {
this.data = this;
}
public void saveTo(ItemStack item) {
item.setItemMeta(NMS.handle().saveNBT(item, this).getItemMeta());
}
@Override
public int size() {
return value.size();

View File

@ -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) {

View File

@ -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().runTask(TabooLib.getPlugin(), () -> ((MenuHolder) e.getInventory().getHolder()).getBuilder().getBuildTask().run(e.getInventory()));
Bukkit.getScheduler().runTaskAsynchronously(TabooLib.getPlugin(), () -> ((MenuHolder) e.getInventory().getHolder()).getBuilder().getBuildTaskAsync().run(e.getInventory()));
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);
}
}

View File

@ -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++) {

View File

@ -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;
}
}

View File

@ -15,5 +15,4 @@ object AsyncTask {
def apply(init: Long, period: Long)(task: => Any)(implicit plugin: Plugin): Int = {
ScalaTaskExecutor(task).runTaskTimerAsynchronously(plugin, init, period).getTaskId
}
}

View File

@ -29,5 +29,4 @@ class Example extends JavaPlugin with Listener {
class a
assert(this == JavaPlugin.getProvidingPlugin(classOf[a]))
}
}