3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-12-15 19:10:09 +01:00
Paper/patches/server/0432-Add-Destroy-Speed-API.patch
Bjarne Koll 3b9db2b194
Updated Upstream (Bukkit/CraftBukkit) (#11501)
Upstream has released updates that appear to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

Bukkit Changes:
bb4e97c6 Add support for Java 23
bc6874dd Bump asm to 9.7.1
50e8a00b PR-1064: Add specific getTopInventory methods for InventoryView derivatives
758b0a0f SPIGOT-7911: Fix Location#isWorldLoaded() for re-loaded worlds
133a64a7 Improve Registry#getOrThrow messages
be0f5957 PR-1058: Add tests for Minecraft registry <-> Bukkit fields
d1b31df2 PR-1062: Clarify BeaconView documentation
3fab4384 PR-1060: Cache Material to BlockType and ItemType conversion
967a7301 SPIGOT-7906: Increase YAML nesting limit to 100
6ecf033d SPIGOT-7899: Smithing recipes don't require inputs

CraftBukkit Changes:
0a7bd6c81 PR-1493: Improve reroute performance and add some tests
54941524c Add support for Java 23
f4d957fff SPIGOT-7915: Fix World#getKeepSpawnInMemory() using Spawn Radius rather than Spawn Chunk Radius
ded183674 Fix HIDE_ENCHANTS flag in items without enchantments
308785a0a Bump asm to 9.7.1 and re-add ClassReader to ClassWriter
72ce823cd PR-1487: Add specific getTopInventory methods for InventoryView derivatives
11a5e840c SPIGOT-7907, PR-1484: Improve merchant recipe item matching behavior to more closely align with older versions
45b66f7e4 SPIGOT-7909: Always set HIDE_ENCHANTS flag to item if flag is set
963459791 Increase outdated build delay
fc5b2d75f SPIGOT-7910: Fix launching breeze wind charge from API and improve dispenser launch API
c7d6428f2 SPIGOT-7856, PR-1483: End platform not dropping items after replacing blocks
2a5572b52 SPIGOT-7780, PR-1482: Cannot edit chunks during unload event
527041ab5 SPIGOT-7902, PR-1477: Fix CraftMetaPotion#hasCustomEffects() does not check if customEffects (List) is empty
5529a1769 Implement base methods for tags
30fbdbaaf Improve Registry#getOrThrow messages
6b71a7322 PR-1475: Add tests for Minecraft registry <-> Bukkit fields
5f24c255c SPIGOT-7908: Mark junit-platform-suite-engine as test scope
e4c92ef65 PR-1473: Change tests to use suites, to run tests in different environments and feature flags
d25e1e722 PR-1481: Fix BeaconView#set[X]Effect(null)
d69a05362 PR-1480: Fix PerMaterialTest#isEdible test running for legacy materials
bb3284a89 PR-1479: Use custom #isBlock method in legacy init instead of the one in Material, since it relies on legacy being init
98c57cbbe SPIGOT-7904: Fix NPE for PlayerItemBreakEvent
f35bae9ec Fix missing hasJukeboxPlayable
8a6f8b6d8 SPIGOT-7881: CTRL+Pick Block saves position data into item
7913b3be7 SPIGOT-7899: Smithing recipes don't require inputs
2024-10-21 00:06:54 +02:00

221 Zeilen
11 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Ineusia <ineusia@yahoo.com>
Date: Mon, 26 Oct 2020 11:48:06 -0500
Subject: [PATCH] Add Destroy Speed API
Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
diff --git a/src/main/java/net/minecraft/world/entity/ai/attributes/AttributeInstance.java b/src/main/java/net/minecraft/world/entity/ai/attributes/AttributeInstance.java
index 36c943d709c1c0ae49ec0baf0ccf7b6cb9a2ecf3..d28f9e077a50122e86848cfa9db83f6b0e8eef6c 100644
--- a/src/main/java/net/minecraft/world/entity/ai/attributes/AttributeInstance.java
+++ b/src/main/java/net/minecraft/world/entity/ai/attributes/AttributeInstance.java
@@ -143,20 +143,20 @@ public class AttributeInstance {
double d = this.getBaseValue();
for (AttributeModifier attributeModifier : this.getModifiersOrEmpty(AttributeModifier.Operation.ADD_VALUE)) {
- d += attributeModifier.amount();
+ d += attributeModifier.amount(); // Paper - destroy speed API - diff on change
}
double e = d;
for (AttributeModifier attributeModifier2 : this.getModifiersOrEmpty(AttributeModifier.Operation.ADD_MULTIPLIED_BASE)) {
- e += d * attributeModifier2.amount();
+ e += d * attributeModifier2.amount(); // Paper - destroy speed API - diff on change
}
for (AttributeModifier attributeModifier3 : this.getModifiersOrEmpty(AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL)) {
- e *= 1.0 + attributeModifier3.amount();
+ e *= 1.0 + attributeModifier3.amount(); // Paper - destroy speed API - diff on change
}
- return this.attribute.value().sanitizeValue(e);
+ return attribute.value().sanitizeValue(e); // Paper - destroy speed API - diff on change
}
private Collection<AttributeModifier> getModifiersOrEmpty(AttributeModifier.Operation operation) {
diff --git a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
index 9953b6b36cbcbfd1756bac478b568ca5700fc898..33869e725c3b3f2120fa36ca468019a78321e862 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
@@ -721,4 +721,35 @@ public class CraftBlockData implements BlockData {
public BlockState createBlockState() {
return CraftBlockStates.getBlockState(this.state, null);
}
+
+ // Paper start - destroy speed API
+ @Override
+ public float getDestroySpeed(final ItemStack itemStack, final boolean considerEnchants) {
+ net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.unwrap(itemStack);
+ float speed = nmsItemStack.getDestroySpeed(this.state);
+ if (speed > 1.0F && considerEnchants) {
+ final net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute> attribute = net.minecraft.world.entity.ai.attributes.Attributes.MINING_EFFICIENCY;
+ // Logic sourced from AttributeInstance#calculateValue
+ final double initialBaseValue = attribute.value().getDefaultValue();
+ final org.apache.commons.lang3.mutable.MutableDouble modifiedBaseValue = new org.apache.commons.lang3.mutable.MutableDouble(initialBaseValue);
+ final org.apache.commons.lang3.mutable.MutableDouble baseValMul = new org.apache.commons.lang3.mutable.MutableDouble(1);
+ final org.apache.commons.lang3.mutable.MutableDouble totalValMul = new org.apache.commons.lang3.mutable.MutableDouble(1);
+
+ net.minecraft.world.item.enchantment.EnchantmentHelper.forEachModifier(
+ nmsItemStack, net.minecraft.world.entity.EquipmentSlot.MAINHAND, (attributeHolder, attributeModifier) -> {
+ switch (attributeModifier.operation()) {
+ case ADD_VALUE -> modifiedBaseValue.add(attributeModifier.amount());
+ case ADD_MULTIPLIED_BASE -> baseValMul.add(attributeModifier.amount());
+ case ADD_MULTIPLIED_TOTAL -> totalValMul.setValue(totalValMul.doubleValue() * (1D + attributeModifier.amount()));
+ }
+ }
+ );
+
+ final double actualModifier = modifiedBaseValue.doubleValue() * baseValMul.doubleValue() * totalValMul.doubleValue();
+
+ speed += (float) attribute.value().sanitizeValue(actualModifier);
+ }
+ return speed;
+ }
+ // Paper end - destroy speed API
}
diff --git a/src/test/java/io/papermc/paper/block/CraftBlockDataDestroySpeedTest.java b/src/test/java/io/papermc/paper/block/CraftBlockDataDestroySpeedTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..32d38205a5a72c3c1838ed28cb83bcea5ad59b6b
--- /dev/null
+++ b/src/test/java/io/papermc/paper/block/CraftBlockDataDestroySpeedTest.java
@@ -0,0 +1,138 @@
+package io.papermc.paper.block;
+
+import java.util.List;
+import java.util.Optional;
+import net.minecraft.core.Holder;
+import net.minecraft.core.HolderSet;
+import net.minecraft.core.component.DataComponentMap;
+import net.minecraft.core.component.DataComponents;
+import net.minecraft.network.chat.Component;
+import net.minecraft.world.entity.EquipmentSlot;
+import net.minecraft.world.entity.EquipmentSlotGroup;
+import net.minecraft.world.entity.ai.attributes.AttributeInstance;
+import net.minecraft.world.entity.ai.attributes.AttributeModifier;
+import net.minecraft.world.entity.ai.attributes.Attributes;
+import net.minecraft.world.item.Items;
+import net.minecraft.world.item.enchantment.Enchantment;
+import net.minecraft.world.item.enchantment.EnchantmentEffectComponents;
+import net.minecraft.world.item.enchantment.EnchantmentHelper;
+import net.minecraft.world.item.enchantment.ItemEnchantments;
+import net.minecraft.world.item.enchantment.LevelBasedValue;
+import net.minecraft.world.item.enchantment.effects.EnchantmentAttributeEffect;
+import net.minecraft.world.level.block.Blocks;
+import net.minecraft.world.level.block.state.BlockState;
+import org.bukkit.craftbukkit.block.data.CraftBlockData;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.support.environment.AllFeatures;
+import org.bukkit.util.Vector;
+import org.jetbrains.annotations.NotNull;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import static net.minecraft.resources.ResourceLocation.fromNamespaceAndPath;
+
+/**
+ * CraftBlockData's {@link org.bukkit.craftbukkit.block.data.CraftBlockData#getDestroySpeed(ItemStack, boolean)}
+ * uses a reimplementation of AttributeValue without any map to avoid attribute instance allocation and mutation
+ * for 0 gain.
+ * <p>
+ * This test is responsible for ensuring that said logic emits the expected destroy speed under heavy attribute
+ * modifier use.
+ */
+@AllFeatures
+public class CraftBlockDataDestroySpeedTest {
+
+ @Test
+ public void testCorrectEnchantmentDestroySpeedComputation() {
+ // Construct fake enchantment that has *all and multiple of* operations
+ final Enchantment speedEnchantment = speedEnchantment();
+ final BlockState blockStateToMine = Blocks.STONE.defaultBlockState();
+
+ final ItemEnchantments.Mutable mutable = new ItemEnchantments.Mutable(ItemEnchantments.EMPTY);
+ mutable.set(Holder.direct(speedEnchantment), 1);
+
+ final net.minecraft.world.item.ItemStack itemStack = new net.minecraft.world.item.ItemStack(Items.DIAMOND_PICKAXE);
+ itemStack.set(DataComponents.ENCHANTMENTS, mutable.toImmutable());
+
+ // Compute expected value by running the entire attribute instance chain
+ final AttributeInstance dummyInstance = new AttributeInstance(Attributes.MINING_EFFICIENCY, $ -> {
+ });
+ EnchantmentHelper.forEachModifier(itemStack, EquipmentSlot.MAINHAND, (attributeHolder, attributeModifier) -> {
+ if (attributeHolder.is(Attributes.MINING_EFFICIENCY)) dummyInstance.addTransientModifier(attributeModifier);
+ });
+
+ final double toolSpeed = itemStack.getDestroySpeed(blockStateToMine);
+ final double expectedSpeed = toolSpeed <= 1.0F ? toolSpeed : toolSpeed + dummyInstance.getValue();
+
+ // API stack + computation
+ final CraftItemStack craftMirror = CraftItemStack.asCraftMirror(itemStack);
+ final CraftBlockData data = CraftBlockData.createData(blockStateToMine);
+ final float actualSpeed = data.getDestroySpeed(craftMirror, true);
+
+ Assertions.assertEquals(expectedSpeed, actualSpeed, Vector.getEpsilon());
+ }
+
+ /**
+ * Complex enchantment that holds attribute modifiers for the mining efficiency.
+ * The enchantment holds 2 of each operation to also ensure that such behaviour works correctly.
+ *
+ * @return the enchantment.
+ */
+ private static @NotNull Enchantment speedEnchantment() {
+ return new Enchantment(
+ Component.empty(),
+ new Enchantment.EnchantmentDefinition(
+ HolderSet.empty(),
+ Optional.empty(),
+ 0, 0,
+ Enchantment.constantCost(0),
+ Enchantment.constantCost(0),
+ 0,
+ List.of(EquipmentSlotGroup.ANY)
+ ),
+ HolderSet.empty(),
+ DataComponentMap.builder()
+ .set(EnchantmentEffectComponents.ATTRIBUTES, List.of(
+ new EnchantmentAttributeEffect(
+ fromNamespaceAndPath("paper", "base1"),
+ Attributes.MINING_EFFICIENCY,
+ LevelBasedValue.constant(1),
+ AttributeModifier.Operation.ADD_VALUE
+ ),
+ new EnchantmentAttributeEffect(
+ fromNamespaceAndPath("paper", "base2"),
+ Attributes.MINING_EFFICIENCY,
+ LevelBasedValue.perLevel(3),
+ AttributeModifier.Operation.ADD_VALUE
+ ),
+ new EnchantmentAttributeEffect(
+ fromNamespaceAndPath("paper", "base-mul1"),
+ Attributes.MINING_EFFICIENCY,
+ LevelBasedValue.perLevel(7),
+ AttributeModifier.Operation.ADD_MULTIPLIED_BASE
+ ),
+ new EnchantmentAttributeEffect(
+ fromNamespaceAndPath("paper", "base-mul2"),
+ Attributes.MINING_EFFICIENCY,
+ LevelBasedValue.constant(10),
+ AttributeModifier.Operation.ADD_MULTIPLIED_BASE
+ ),
+ new EnchantmentAttributeEffect(
+ fromNamespaceAndPath("paper", "total-mul1"),
+ Attributes.MINING_EFFICIENCY,
+ LevelBasedValue.constant(.2f),
+ AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL
+ ),
+ new EnchantmentAttributeEffect(
+ fromNamespaceAndPath("paper", "total-mul2"),
+ Attributes.MINING_EFFICIENCY,
+ LevelBasedValue.constant(-.5F),
+ AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL
+ )
+ ))
+ .build()
+ );
+ }
+
+}