+ update event api

+ update command api
+ update nbt api
+ update menu api
This commit is contained in:
坏黑
2019-10-22 13:18:18 +08:00
parent 048eb12daf
commit baf2958535
17 changed files with 530 additions and 78 deletions

View File

@@ -110,7 +110,9 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
sender.sendMessage(getEmptyLine());
}
abstract public String getCommandTitle();
public String getCommandTitle() {
return "§e§l----- §6§l" + registerCommand.getPlugin().getName() + " Commands §e§l-----";
}
@Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] args) {

View File

@@ -0,0 +1,53 @@
package io.izzel.taboolib.module.event;
import org.bukkit.event.Cancellable;
/**
* @Author sky
* @Since 2019-10-22 10:41
*/
public abstract class EventCancellable<T extends EventCancellable> extends EventNormal implements Cancellable {
private boolean cancelled;
public T nonCancelled(Runnable runnable) {
if (nonCancelled()) {
try {
runnable.run();
} catch (Throwable t) {
t.printStackTrace();
}
}
return (T) this;
}
public T ifCancelled(Runnable runnable) {
if (isCancelled()) {
try {
runnable.run();
} catch (Throwable t) {
t.printStackTrace();
}
}
return (T) this;
}
@Override
public T call() {
return (T) super.call();
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public boolean isCancelled() {
return cancelled;
}
public boolean nonCancelled() {
return !cancelled;
}
}

View File

@@ -0,0 +1,47 @@
package io.izzel.taboolib.module.event;
import io.izzel.taboolib.util.Ref;
import io.izzel.taboolib.util.Reflection;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import java.lang.reflect.Field;
/**
* @Author sky
* @Since 2019-10-22 10:25
*/
public abstract class EventNormal<T extends EventNormal> extends Event {
protected static final HandlerList handlers = new HandlerList();
protected static HandlerList getHandlerList() {
return handlers;
}
public T call() {
try {
Bukkit.getPluginManager().callEvent(this);
} catch (Throwable t) {
t.printStackTrace();
}
return (T) this;
}
public T async(boolean value) {
try {
Field asyncField = Reflection.getField(Event.class, true, "async");
Ref.forcedAccess(asyncField);
asyncField.setBoolean(this, value);
} catch (Throwable t) {
t.printStackTrace();
}
return (T) this;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
}

View File

@@ -1,12 +1,12 @@
package io.izzel.taboolib.module.lite;
import com.google.common.collect.Maps;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Author 坏黑
@@ -14,23 +14,25 @@ import java.util.Map;
*/
public enum SimpleEquip {
HAND(EquipmentSlot.HAND, -1),
HAND(EquipmentSlot.HAND, "mainhand", -1),
OFF_HAND(EquipmentSlot.OFF_HAND, 40),
OFF_HAND(EquipmentSlot.OFF_HAND, "offhand", 40),
FEET(EquipmentSlot.FEET, 36),
FEET(EquipmentSlot.FEET, "feet", 36),
LEGS(EquipmentSlot.LEGS, 37),
LEGS(EquipmentSlot.LEGS, "legs", 37),
CHEST(EquipmentSlot.CHEST, 38),
CHEST(EquipmentSlot.CHEST, "chest", 38),
HEAD(EquipmentSlot.HEAD, 39);
HEAD(EquipmentSlot.HEAD, "head", 39);
private EquipmentSlot bukkit;
private String nms;
private int slot;
SimpleEquip(EquipmentSlot bukkit, int slot) {
SimpleEquip(EquipmentSlot bukkit, String nms, int slot) {
this.bukkit = bukkit;
this.nms = nms;
this.slot = slot;
}
@@ -50,16 +52,16 @@ public enum SimpleEquip {
}
}
public static SimpleEquip fromNMS(String nms) {
return Arrays.stream(values()).filter(tEquipment -> tEquipment.nms.equalsIgnoreCase(nms)).findFirst().orElse(null);
}
public static SimpleEquip fromBukkit(EquipmentSlot bukkit) {
return Arrays.stream(values()).filter(tEquipment -> tEquipment.bukkit == bukkit).findFirst().orElse(null);
}
public static Map<SimpleEquip, ItemStack> getItems(Player player) {
Map<SimpleEquip, ItemStack> map = Maps.newHashMap();
for (SimpleEquip equipment : values()) {
map.put(equipment, equipment.getItem(player));
}
return map;
return Arrays.stream(values()).collect(Collectors.toMap(equipment -> equipment, equipment -> equipment.getItem(player), (a, b) -> b));
}
// *********************************
@@ -72,6 +74,10 @@ public enum SimpleEquip {
return bukkit;
}
public String getNMS() {
return nms;
}
public int getSlot() {
return slot;
}

View File

@@ -1,12 +1,19 @@
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.NBTAttribute;
import io.izzel.taboolib.module.nms.nbt.NBTCompound;
import io.izzel.taboolib.module.nms.nbt.NBTList;
import org.bukkit.Particle;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
/**
* @Author 坏黑
* @Since 2018-11-09 14:38
@@ -47,4 +54,17 @@ public abstract class NMS {
public ItemStack saveNBT(ItemStack itemStack, NBTCompound compound) {
return _NBT(itemStack, compound);
}
public List<NBTAttribute> getAttribute(ItemStack item) {
NBTCompound nbt = loadNBT(item);
return !nbt.containsKey("AttributeModifiers") ? Lists.newCopyOnWriteArrayList() : nbt.get("AttributeModifiers").asList().stream().map(element -> NBTAttribute.fromNBT(element.asCompound())).collect(Collectors.toCollection(CopyOnWriteArrayList::new));
}
public ItemStack setAttribute(ItemStack item, List<NBTAttribute> attributes) {
NBTCompound nbt = loadNBT(item);
nbt.put("AttributeModifiers", attributes.stream().map(NBTAttribute::toNBT).collect(Collectors.toCollection(NBTList::new)));
return saveNBT(item, nbt);
}
abstract public List<NBTAttribute> getBaseAttribute(ItemStack item);
}

View File

@@ -1,9 +1,13 @@
package io.izzel.taboolib.module.nms;
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.packet.TPacketHandler;
import net.minecraft.server.v1_12_R1.ChatMessageType;
import net.minecraft.server.v1_12_R1.EntityVillager;
@@ -28,6 +32,7 @@ import org.bukkit.inventory.meta.PotionMeta;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* @Author 坏黑
@@ -235,6 +240,29 @@ public class NMSImpl extends NMS {
return new NBTCompound();
}
@Override
public List<NBTAttribute> getBaseAttribute(ItemStack item) {
List<NBTAttribute> list = Lists.newArrayList();
Object nmsItem = CraftItemStack.asNMSCopy(item);
Object attr;
if (Version.isAfter(Version.v1_9)) {
attr = ((net.minecraft.server.v1_12_R1.ItemStack) nmsItem).getItem().a(net.minecraft.server.v1_12_R1.EnumItemSlot.MAINHAND);
} else {
attr = ((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).getItem().i();
}
((Multimap) attr).forEach((k, v) -> {
Object nbt = net.minecraft.server.v1_12_R1.GenericAttributes.a((net.minecraft.server.v1_12_R1.AttributeModifier) v);
list.add(new NBTAttribute(
new UUID(((NBTTagCompound) nbt).getLong("UUIDMost"), ((NBTTagCompound) nbt).getLong("UUIDLeast")),
String.valueOf(k),
((NBTTagCompound) nbt).getString("Name"),
((NBTTagCompound) nbt).getDouble("Amount"),
NBTOperation.fromIndex(((NBTTagCompound) nbt).getInt("Operation"))
));
});
return list;
}
private Object toNBTBase(io.izzel.taboolib.module.nms.nbt.NBTBase base) {
switch (base.getType().getId()) {
case 1:

View File

@@ -0,0 +1,168 @@
package io.izzel.taboolib.module.nms.nbt;
import io.izzel.taboolib.module.lite.SimpleEquip;
import io.izzel.taboolib.util.item.Items;
import java.util.Objects;
import java.util.UUID;
/**
* @Author sky
* @Since 2019-10-22 11:38
*/
public class NBTAttribute {
private UUID id;
private String name;
private String description;
private double amount;
private SimpleEquip slot;
private NBTOperation operation;
public NBTAttribute(String name, String description, double amount, NBTOperation operation) {
this(UUID.randomUUID(), name, description, amount, null, operation);
}
public NBTAttribute(String name, String description, double amount, SimpleEquip slot, NBTOperation operation) {
this(UUID.randomUUID(), name, description, amount, slot, operation);
}
public NBTAttribute(UUID id, String name, String description, double amount, NBTOperation operation) {
this(id, name, description, amount, null, operation);
}
public NBTAttribute(UUID id, String name, String description, double amount, SimpleEquip slot, NBTOperation operation) {
this.id = id;
this.name = name;
this.description = description;
this.amount = amount;
this.slot = slot;
this.operation = operation;
}
public NBTCompound toNBT() {
NBTCompound nbt = new NBTCompound();
nbt.put("UUIDMost", new NBTBase(id.getMostSignificantBits()));
nbt.put("UUIDLeast", new NBTBase(id.getLeastSignificantBits()));
nbt.put("AttributeName", new NBTBase(name));
nbt.put("Name", new NBTBase(description));
nbt.put("Amount", new NBTBase(amount));
nbt.put("Operation", new NBTBase(operation.ordinal()));
if (slot != null) {
nbt.put("Slot", new NBTBase(slot.getNMS()));
}
return nbt;
}
public static NBTAttribute fromNBT(NBTCompound nbt) {
NBTAttribute attribute = new NBTAttribute(
new UUID(nbt.get("UUIDMost").asLong(), nbt.get("UUIDLeast").asLong()),
nbt.get("AttributeName").asString(),
nbt.get("Name").asString(),
nbt.get("Amount").asDouble(),
NBTOperation.fromIndex(nbt.get("Operation").asInt())
);
if (nbt.containsKey("Slot")) {
attribute.slot(SimpleEquip.fromNMS(nbt.get("Slot").asString()));
}
return attribute;
}
public static NBTAttribute create() {
return new NBTAttribute(Items.asAttribute("damage"), "TabooLib Modifiers", 0, NBTOperation.ADD_NUMBER);
}
// *********************************
//
// Getter and Setter
//
// *********************************
public UUID getId() {
return id;
}
public NBTAttribute id(UUID id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public NBTAttribute name(String name) {
this.name = name;
return this;
}
public String getDescription() {
return description;
}
public NBTAttribute description(String description) {
this.description = description;
return this;
}
public double getAmount() {
return amount;
}
public NBTAttribute amount(double amount) {
this.amount = amount;
return this;
}
public SimpleEquip getSlot() {
return slot;
}
public NBTAttribute slot(SimpleEquip slot) {
this.slot = slot;
return this;
}
public NBTOperation getOperation() {
return operation;
}
public NBTAttribute operation(NBTOperation operation) {
this.operation = operation;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof NBTAttribute)) {
return false;
}
NBTAttribute that = (NBTAttribute) o;
return Double.compare(that.getAmount(), getAmount()) == 0 &&
Objects.equals(getId(), that.getId()) &&
Objects.equals(getName(), that.getName()) &&
Objects.equals(getDescription(), that.getDescription()) &&
getSlot() == that.getSlot() &&
getOperation() == that.getOperation();
}
@Override
public int hashCode() {
return Objects.hash(getId(), getName(), getDescription(), getAmount(), getSlot(), getOperation());
}
@Override
public String toString() {
return "NBTAttribute{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
", amount=" + amount +
", slot=" + slot +
", operation=" + operation +
'}';
}
}

View File

@@ -1,7 +1,13 @@
package io.izzel.taboolib.module.nms.nbt;
import io.izzel.taboolib.TabooLib;
import org.bukkit.configuration.ConfigurationSection;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
/**
* @Author 坏黑
@@ -9,6 +15,7 @@ import java.util.Objects;
*/
public class NBTBase {
protected static final Pattern SHORT_PATTERN = Pattern.compile("\\d+s");
protected NBTType type;
protected Object data;
@@ -115,6 +122,65 @@ public class NBTBase {
return type;
}
public static NBTBase formNBTBase(Object obj) {
if (obj instanceof String) {
if (SHORT_PATTERN.matcher(obj.toString()).matches()) {
return formNBTBase(Short.valueOf(obj.toString().substring(0, obj.toString().length() - 1)));
}
return new NBTBase((String) obj);
} else if (obj instanceof Integer) {
return new NBTBase((int) obj);
} else if (obj instanceof Double) {
return new NBTBase((double) obj);
} else if (obj instanceof Float) {
return new NBTBase((float) obj);
} else if (obj instanceof Short) {
return new NBTBase((short) obj);
} else if (obj instanceof Long) {
return new NBTBase((long) obj);
} else if (obj instanceof Byte) {
return new NBTBase((byte) obj);
} else if (obj instanceof List) {
return translateList(new NBTList(), (List) obj);
} else if (obj instanceof Map) {
NBTCompound nbtCompound = new NBTCompound();
((Map) obj).forEach((key, value) -> nbtCompound.put(key.toString(), formNBTBase(value)));
return nbtCompound;
} else if (obj instanceof ConfigurationSection) {
NBTCompound nbtCompound = new NBTCompound();
((ConfigurationSection) obj).getValues(false).forEach((key, value) -> nbtCompound.put(key, formNBTBase(value)));
return nbtCompound;
}
return new NBTBase("error: " + obj);
}
public static NBTList translateList(NBTList nbtListBase, List list) {
for (Object obj : list) {
NBTBase base = formNBTBase(obj);
if (base == null) {
TabooLib.getLogger().warn("Invalid Type: " + obj + " [" + obj.getClass().getSimpleName() + "]");
continue;
}
nbtListBase.add(base);
}
return nbtListBase;
}
public static NBTCompound translateSection(NBTCompound nbt, ConfigurationSection section) {
for (String key : section.getKeys(false)) {
Object obj = section.get(key);
NBTBase base;
if (obj instanceof ConfigurationSection) {
base = translateSection(new NBTCompound(), section.getConfigurationSection(key));
} else if ((base = formNBTBase(obj)) == null) {
TabooLib.getLogger().warn("Invalid Type: " + obj + " [" + obj.getClass().getSimpleName() + "]");
continue;
}
nbt.put(key, base);
}
return nbt;
}
@Override
public boolean equals(Object o) {
if (this == o) {

View File

@@ -49,11 +49,37 @@ public class NBTCompound extends NBTBase implements Map<String, NBTBase> {
return value.get(key);
}
public NBTBase getDeep(String key) {
NBTBase value = this;
for (String keyStr : key.split("\\.")) {
if ((value = value.asCompound().get(keyStr)) == null) {
return null;
}
}
return value;
}
@Override
public NBTBase put(String key, NBTBase value) {
return this.value.put(key, value);
}
public NBTBase putDeep(String key, NBTBase value) {
NBTBase compound = this, temp;
String[] keySplit = key.split("\\.");
for (String keyStr : keySplit) {
if (keyStr.equalsIgnoreCase(keySplit[keySplit.length - 1])) {
return ((NBTCompound) compound).put(keyStr, value);
}
if ((temp = compound.asCompound().get(keyStr)) == null) {
temp = new NBTCompound();
compound.asCompound().put(keyStr, temp);
}
compound = temp;
}
return null;
}
@Override
public NBTBase remove(Object key) {
return value.remove(key);

View File

@@ -0,0 +1,27 @@
package io.izzel.taboolib.module.nms.nbt;
import io.izzel.taboolib.util.KV;
import org.bukkit.util.NumberConversions;
import java.util.Arrays;
/**
* @Author sky
* @Since 2019-10-22 12:06
*/
public enum NBTOperation {
ADD_NUMBER, ADD_SCALAR, MULTIPLY_SCALAR_1;
public static NBTOperation fromIndex(int index) {
return Arrays.stream(values()).filter(operation -> operation.ordinal() == index).findFirst().orElse(ADD_NUMBER);
}
public static KV<Double, NBTOperation> fromSimple(String in) {
if (in.endsWith("%")) {
return new KV(NumberConversions.toDouble(in.substring(0, in.length() - 1)), ADD_SCALAR);
} else {
return new KV(NumberConversions.toDouble(in), ADD_NUMBER);
}
}
}