Merge remote-tracking branch 'origin/master'

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

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) {
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) {
throw new RuntimeException("Not Found Cache.");
}
Field field = fields.get(fieldName);
if (value == null) {
throw new RuntimeException("Not Found Field.");
}
try {
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) {
return;
}
Field field = fields.get(fieldName);
if (value == null) {
return;
}
field.set(instance, value);
} catch (Exception e) {
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static Object getFieldValue(Class<?> nmsClass, Object instance, String fieldName) {
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) {
throw new RuntimeException("Not Found Cache.");
}
Field field = fields.get(fieldName);
if (field == null) {
throw new RuntimeException("Not Found Field.");
}
try {
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) {
return null;
}
Field field = fields.get(fieldName);
if (field == null) {
return null;
}
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) {
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) {
throw new RuntimeException("Not Found Cache.");
}
Field field = fields.get(fieldName);
if (field == null) {
throw new RuntimeException("Not Found Field.");
}
try {
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) {
return def;
}
Field field = fields.get(fieldName);
if (field == null) {
return def;
}
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();