下面列出了怎么用org.bukkit.attribute.AttributeModifier的API类实例代码及写法,或者点击链接到github查看源代码。
public static AttributeModifier.Operation parseAttributeOperation(Node node, String text)
throws InvalidXMLException {
switch (text.toLowerCase()) {
case "add":
return AttributeModifier.Operation.ADD_NUMBER;
case "base":
return AttributeModifier.Operation.ADD_SCALAR;
case "multiply":
return AttributeModifier.Operation.MULTIPLY_SCALAR_1;
}
throw new InvalidXMLException("Unknown attribute modifier operation '" + text + "'", node);
}
public static Map.Entry<String, AttributeModifier> parseCompactAttributeModifier(
Node node, String text) throws InvalidXMLException {
String[] parts = text.split(":");
if (parts.length != 3) {
throw new InvalidXMLException("Bad attribute modifier format", node);
}
org.bukkit.attribute.Attribute attribute = parseAttribute(node, parts[0]);
AttributeModifier.Operation operation = parseAttributeOperation(node, parts[1]);
double amount = parseNumber(node, parts[2], Double.class);
return new AbstractMap.SimpleImmutableEntry<>(
attribute.getName(), new AttributeModifier("FromXML", amount, operation));
}
public static Map.Entry<String, AttributeModifier> parseAttributeModifier(Element el)
throws InvalidXMLException {
String attribute = parseAttribute(new Node(el)).getName();
double amount = parseNumber(Node.fromRequiredAttr(el, "amount"), Double.class);
AttributeModifier.Operation operation =
parseAttributeOperation(
Node.fromAttr(el, "operation"), AttributeModifier.Operation.ADD_NUMBER);
return new AbstractMap.SimpleImmutableEntry<>(
attribute, new AttributeModifier("FromXML", amount, operation));
}
@Override
protected void applyPostEvent(MatchPlayer player, boolean force, List<ItemStack> displacedItems) {
for (Map.Entry<String, AttributeModifier> entry : modifiers.entries()) {
AttributeInstance attributeValue =
player.getBukkit().getAttribute(Attribute.byName(entry.getKey()));
if (attributeValue != null && !attributeValue.getModifiers().contains(entry.getValue())) {
attributeValue.addModifier(entry.getValue());
}
}
}
@Override
public void remove(MatchPlayer player) {
for (Map.Entry<String, AttributeModifier> entry : modifiers.entries()) {
AttributeInstance attributeValue =
player.getBukkit().getAttribute(Attribute.byName(entry.getKey()));
if (attributeValue != null && attributeValue.getModifiers().contains(entry.getValue())) {
attributeValue.removeModifier(entry.getValue());
}
}
}
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
ItemMeta meta = tool.getItemMeta();
if (meta == null) {
return false;
}
//To check if armor modifiers are on the armor
Collection<AttributeModifier> attributeModifiers = meta.getAttributeModifiers(Attribute.GENERIC_ARMOR);
if (attributeModifiers == null || attributeModifiers.isEmpty()) {
modManager.addArmorAttributes(tool);
meta = tool.getItemMeta();
}
Collection<AttributeModifier> speedModifiers = meta.getAttributeModifiers(Attribute.GENERIC_MOVEMENT_SPEED);
double speedOnItem = 0.0D;
if (!(speedModifiers == null || speedModifiers.isEmpty())) {
HashSet<String> names = new HashSet<>();
for (AttributeModifier am : speedModifiers) {
if (names.add(am.getName())) speedOnItem += am.getAmount();
}
}
meta.removeAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED);
meta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED,
new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.movement_speed" : "generic.movementSpeed", speedOnItem + this.speedPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.LEGS));
meta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED,
new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.movement_speed" : "generic.movementSpeed", speedOnItem + this.speedPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.FEET));
tool.setItemMeta(meta);
return true;
}
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
ItemMeta meta = tool.getItemMeta();
if (meta == null) {
return false;
}
//To check if armor modifiers are on the armor
Collection<AttributeModifier> attributeModifiers = meta.getAttributeModifiers(Attribute.GENERIC_ARMOR);
if (attributeModifiers == null || attributeModifiers.isEmpty()) {
modManager.addArmorAttributes(tool);
meta = tool.getItemMeta();
}
Collection<AttributeModifier> healthModifiers = meta.getAttributeModifiers(Attribute.GENERIC_MAX_HEALTH);
double healthOnItem = 0.0D;
if (!(healthModifiers == null || healthModifiers.isEmpty())) {
HashSet<String> names = new HashSet<>();
for (AttributeModifier am : healthModifiers) {
if (names.add(am.getName())) healthOnItem += am.getAmount();
}
}
meta.removeAttributeModifier(Attribute.GENERIC_MAX_HEALTH);
modManager.addArmorAttributes(tool);
if (ToolType.CHESTPLATE.contains(tool.getType())) {
meta.addAttributeModifier(Attribute.GENERIC_MAX_HEALTH, new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.max_health" : "generic.maxHealth", healthOnItem + this.healthPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.CHEST));
} else {
meta.addAttributeModifier(Attribute.GENERIC_MAX_HEALTH, new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.max_health" : "generic.maxHealth", healthOnItem + this.healthPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.LEGS));
}
tool.setItemMeta(meta);
return true;
}
@Override
public Collection<AttributeModifier> getModifiers() {
List<AttributeModifier> result = new ArrayList<AttributeModifier>();
for (net.minecraft.entity.ai.attributes.AttributeModifier nms : handle.getModifiers()) {
result.add(convert(nms));
}
return result;
}
public static AttributeModifier.Operation parseAttributeOperation(Node node, String text) throws InvalidXMLException {
switch(text.toLowerCase()) {
case "add": return AttributeModifier.Operation.ADD_NUMBER;
case "base": return AttributeModifier.Operation.ADD_SCALAR;
case "multiply": return AttributeModifier.Operation.MULTIPLY_SCALAR_1;
}
throw new InvalidXMLException("Unknown attribute modifier operation '" + text + "'", node);
}
public static Pair<org.bukkit.attribute.Attribute, AttributeModifier> parseCompactAttributeModifier(Node node, String text) throws InvalidXMLException {
final String[] parts = text.split(":");
if(parts.length != 3) {
throw new InvalidXMLException("Bad attribute modifier format", node);
}
return Pair.create(
parseAttribute(node, parts[0]),
new AttributeModifier(
"FromXML",
parseNumber(node, parts[2], Double.class),
parseAttributeOperation(node, parts[1])
)
);
}
public static Pair<org.bukkit.attribute.Attribute, AttributeModifier> parseAttributeModifier(Element el) throws InvalidXMLException {
return Pair.create(
parseAttribute(new Node(el)),
new AttributeModifier(
"FromXML",
parseNumber(Node.fromRequiredAttr(el, "amount"), Double.class),
parseAttributeOperation(Node.fromAttr(el, "operation"), AttributeModifier.Operation.ADD_NUMBER)
)
);
}
private boolean addModifier0(Attribute attribute, AttributeModifier modifier) {
final AttributeInstance attributeInstance = player.getAttribute(attribute);
if(attributeInstance != null && !attributeInstance.getModifiers().contains(modifier)) {
attributeInstance.addModifier(modifier);
return true;
}
return false;
}
private boolean removeModifier0(Attribute attribute, AttributeModifier modifier) {
AttributeInstance attributeValue = player.getAttribute(attribute);
if(attributeValue != null && attributeValue.getModifiers().contains(modifier)) {
attributeValue.removeModifier(modifier);
return true;
}
return false;
}
@Nullable
@Override
public JsonObject getItemAttributes(ItemMeta meta){
if (!meta.hasAttributeModifiers()){
return null;
}
JsonObject attributesJson = new JsonObject();
Multimap<Attribute, AttributeModifier> attributeModifiers = meta.getAttributeModifiers();
for (Attribute attribute : attributeModifiers.keySet()){
JsonArray modifiersJson = new JsonArray();
Collection<AttributeModifier> modifiers = attributeModifiers.get(attribute);
for (AttributeModifier modifier : modifiers){
JsonObject modifierObject = new JsonObject();
modifierObject.addProperty("name", modifier.getName());
modifierObject.addProperty("amount", modifier.getAmount());
modifierObject.addProperty("operation", modifier.getOperation().name());
if (modifier.getSlot() != null){
modifierObject.addProperty("slot", modifier.getSlot().name());
}
modifiersJson.add(modifierObject);
}
attributesJson.add(attribute.name(), modifiersJson);
}
return attributesJson;
}
@Override
public ItemMeta applyItemAttributes(ItemMeta meta, JsonObject attributes){
Set<Map.Entry<String, JsonElement>> entries = attributes.entrySet();
for (Map.Entry<String, JsonElement> attributeEntry : entries){
Attribute attribute = Attribute.valueOf(attributeEntry.getKey());
for (JsonElement jsonElement : attributeEntry.getValue().getAsJsonArray()) {
JsonObject modifier = jsonElement.getAsJsonObject();
String name = modifier.get("name").getAsString();
double amount = modifier.get("amount").getAsDouble();
String operation = modifier.get("operation").getAsString();
EquipmentSlot slot = null;
if (modifier.has("slot")){
slot = EquipmentSlot.valueOf(modifier.get("slot").getAsString());
}
meta.addAttributeModifier(attribute, new AttributeModifier(
UUID.randomUUID(),
name,
amount,
AttributeModifier.Operation.valueOf(operation),
slot
));
}
}
return meta;
}
public static void resetPlayer(Player player, boolean heal) {
if (heal) player.setHealth(player.getMaxHealth());
player.setFoodLevel(20);
player.setSaturation(20);
player.getInventory().clear();
player.getInventory().setArmorContents(new ItemStack[]{new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR)});
for (PotionEffect effect : player.getActivePotionEffects()) {
try {
player.removePotionEffect(effect.getType());
} catch (NullPointerException ignored) {
}
}
player.setTotalExperience(0);
player.setExp(0);
player.setLevel(0);
player.setPotionParticles(false);
player.setWalkSpeed(0.2F);
player.setFlySpeed(0.1F);
player.setKnockbackReduction(0);
player.setArrowsStuck(0);
player.hideTitle();
player.setFastNaturalRegeneration(false);
for (Attribute attribute : Attribute.values()) {
if (player.getAttribute(attribute) == null) continue;
for (AttributeModifier modifier : player.getAttribute(attribute).getModifiers()) {
player.getAttribute(attribute).removeModifier(modifier);
}
}
player.getAttribute(Attribute.GENERIC_ATTACK_SPEED).addModifier(new AttributeModifier(UUID.randomUUID(), "generic.attackSpeed", 4.001D, AttributeModifier.Operation.ADD_SCALAR));
player.getAttribute(Attribute.ARROW_ACCURACY).addModifier(new AttributeModifier(UUID.randomUUID(), "sportbukkit.arrowAccuracy", -1D, AttributeModifier.Operation.ADD_NUMBER));
player.getAttribute(Attribute.ARROW_VELOCITY_TRANSFER).addModifier(new AttributeModifier(UUID.randomUUID(), "sportbukkit.arrowVelocityTransfer", -1D, AttributeModifier.Operation.ADD_NUMBER));
}
private static List<ItemAttributeModifier> parseAttributes(String attributes) {
List<ItemAttributeModifier> list = new ArrayList<>();
for (String attribute : attributes.split(";")) {
String[] attr = attribute.split(":");
list.add(new ItemAttributeModifier(null, new AttributeModifier(UUID.randomUUID(), attr[0], Double.parseDouble(attr[2]), getOperation(attr[1]))));
}
return list;
}
public static AttributeModifier.Operation getOperation(String operation) {
if (NumberUtils.isNumber(operation)) {
return AttributeModifier.Operation.fromOpcode(Integer.parseInt(operation));
} else {
switch (operation.toLowerCase()) {
case("add"):
return AttributeModifier.Operation.ADD_NUMBER;
case("base"):
return AttributeModifier.Operation.ADD_SCALAR;
case("multiply"):
return AttributeModifier.Operation.MULTIPLY_SCALAR_1;
}
}
return AttributeModifier.Operation.ADD_NUMBER;
}
public static AttributeModifier.Operation parseAttributeOperation(Node node)
throws InvalidXMLException {
return parseAttributeOperation(node, node.getValueNormalize());
}
public static AttributeModifier.Operation parseAttributeOperation(
Node node, AttributeModifier.Operation def) throws InvalidXMLException {
return node == null ? def : parseAttributeOperation(node);
}
@Override
public void reset() {
getMatch().callEvent(new PlayerResetEvent(this));
setFrozen(false);
Player bukkit = getBukkit();
bukkit.closeInventory();
resetInventory();
bukkit.setArrowsStuck(0);
bukkit.setExhaustion(0);
bukkit.setFallDistance(0);
bukkit.setFireTicks(0);
bukkit.setFoodLevel(20); // full
bukkit.setHealth(bukkit.getMaxHealth());
bukkit.setLevel(0);
bukkit.setExp(0); // clear xp
bukkit.setSaturation(5); // default
bukkit.setAllowFlight(false);
bukkit.setFlying(false);
bukkit.setSneaking(false);
bukkit.setSprinting(false);
bukkit.setFlySpeed(0.1f);
bukkit.setKnockbackReduction(0);
bukkit.setWalkSpeed(WalkSpeedKit.BUKKIT_DEFAULT);
for (PotionEffect effect : bukkit.getActivePotionEffects()) {
if (effect.getType() != null) {
bukkit.removePotionEffect(effect.getType());
}
}
for (Attribute attribute : ATTRIBUTES) {
AttributeInstance attributes = bukkit.getAttribute(attribute);
if (attributes == null) continue;
for (AttributeModifier modifier : attributes.getModifiers()) {
attributes.removeModifier(modifier);
}
}
NMSHacks.setAbsorption(bukkit, 0);
// we only reset bed spawn here so people don't have to see annoying messages when they respawn
bukkit.setBedSpawnLocation(null);
}
public AttributeKit(SetMultimap<String, AttributeModifier> modifiers) {
this.modifiers = modifiers;
}
public AttributeKit parseAttributeKit(Element el) throws InvalidXMLException {
SetMultimap<String, AttributeModifier> modifiers = parseAttributeModifiers(el);
attributeModifiers.addAll(modifiers.values());
return modifiers.isEmpty() ? null : new AttributeKit(modifiers);
}
@Override
public void addModifier(AttributeModifier modifier) {
Preconditions.checkArgument(modifier != null, "modifier");
handle.applyModifier(convert(modifier));
}
@Override
public void removeModifier(AttributeModifier modifier) {
Preconditions.checkArgument(modifier != null, "modifier");
handle.removeModifier(convert(modifier));
}
private static net.minecraft.entity.ai.attributes.AttributeModifier convert(AttributeModifier bukkit) {
return new net.minecraft.entity.ai.attributes.AttributeModifier(bukkit.getUniqueId(), bukkit.getName(), bukkit.getAmount(), bukkit.getOperation().ordinal());
}
private static AttributeModifier convert(net.minecraft.entity.ai.attributes.AttributeModifier nms) {
return new AttributeModifier(nms.getID(), nms.getName(), nms.getAmount(), AttributeModifier.Operation.values()[nms.getOperation()]);
}
public static AttributeModifier.Operation parseAttributeOperation(Node node) throws InvalidXMLException {
return parseAttributeOperation(node, node.getValueNormalize());
}
public static AttributeModifier.Operation parseAttributeOperation(Node node, AttributeModifier.Operation def) throws InvalidXMLException {
return node == null ? def : parseAttributeOperation(node);
}
public AttributeKit(Pair<Attribute, AttributeModifier> pair) {
this(pair.first, pair.second);
}