diff --git a/paper-server/patches/sources/net/minecraft/world/entity/Shearable.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/Shearable.java.patch new file mode 100644 index 0000000000..d3bbca183d --- /dev/null +++ b/paper-server/patches/sources/net/minecraft/world/entity/Shearable.java.patch @@ -0,0 +1,8 @@ +--- a/net/minecraft/world/entity/Shearable.java ++++ b/net/minecraft/world/entity/Shearable.java +@@ -8,4 +8,5 @@ + void shear(ServerLevel world, SoundSource shearedSoundCategory, ItemStack shears); + + boolean readyForShearing(); ++ net.minecraft.world.level.Level level(); // Shearable API - expose default level needed for shearing. + } diff --git a/paper-server/src/main/java/io/papermc/paper/entity/PaperShearable.java b/paper-server/src/main/java/io/papermc/paper/entity/PaperShearable.java new file mode 100644 index 0000000000..b02e2f2ea4 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/entity/PaperShearable.java @@ -0,0 +1,25 @@ +package io.papermc.paper.entity; + +import io.papermc.paper.adventure.PaperAdventure; +import net.kyori.adventure.sound.Sound; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.entity.Shearable; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import org.jetbrains.annotations.NotNull; + +public interface PaperShearable extends io.papermc.paper.entity.Shearable { + + Shearable getHandle(); + + @Override + default boolean readyToBeSheared() { + return this.getHandle().readyForShearing(); + } + + @Override + default void shear(@NotNull Sound.Source source) { + if (!(this.getHandle().level() instanceof final ServerLevel serverLevel)) return; + this.getHandle().shear(serverLevel, PaperAdventure.asVanilla(source), new ItemStack(Items.SHEARS)); + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftBogged.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftBogged.java index 0139e85c07..e8e4704304 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftBogged.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftBogged.java @@ -4,7 +4,7 @@ import org.bukkit.craftbukkit.CraftServer; import org.bukkit.entity.Bogged; import org.bukkit.entity.Skeleton; -public class CraftBogged extends CraftAbstractSkeleton implements Bogged { +public class CraftBogged extends CraftAbstractSkeleton implements Bogged, io.papermc.paper.entity.PaperShearable { // Paper - Shear API public CraftBogged(CraftServer server, net.minecraft.world.entity.monster.Bogged entity) { super(server, entity); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java index 901b751c85..bfe39c7a9d 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java @@ -14,7 +14,7 @@ import org.bukkit.entity.MushroomCow; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -public class CraftMushroomCow extends CraftCow implements MushroomCow { +public class CraftMushroomCow extends CraftCow implements MushroomCow, io.papermc.paper.entity.PaperShearable { // Paper public CraftMushroomCow(CraftServer server, net.minecraft.world.entity.animal.MushroomCow entity) { super(server, entity); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftSheep.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftSheep.java index 030bf7b631..37291d7ad9 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftSheep.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftSheep.java @@ -4,7 +4,7 @@ import org.bukkit.DyeColor; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.entity.Sheep; -public class CraftSheep extends CraftAnimals implements Sheep { +public class CraftSheep extends CraftAnimals implements Sheep, io.papermc.paper.entity.PaperShearable { // Paper public CraftSheep(CraftServer server, net.minecraft.world.entity.animal.Sheep entity) { super(server, entity); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftSnowman.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftSnowman.java index 1e9807b8f4..4ce2373ff7 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftSnowman.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftSnowman.java @@ -4,7 +4,7 @@ import net.minecraft.world.entity.animal.SnowGolem; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.entity.Snowman; -public class CraftSnowman extends CraftGolem implements Snowman, com.destroystokyo.paper.entity.CraftRangedEntity { // Paper +public class CraftSnowman extends CraftGolem implements Snowman, com.destroystokyo.paper.entity.CraftRangedEntity, io.papermc.paper.entity.PaperShearable { // Paper public CraftSnowman(CraftServer server, SnowGolem entity) { super(server, entity); } diff --git a/paper-server/src/test/java/io/papermc/paper/entity/ShearableTest.java b/paper-server/src/test/java/io/papermc/paper/entity/ShearableTest.java new file mode 100644 index 0000000000..709b90f0cd --- /dev/null +++ b/paper-server/src/test/java/io/papermc/paper/entity/ShearableTest.java @@ -0,0 +1,30 @@ +package io.papermc.paper.entity; + +import com.destroystokyo.paper.entity.ai.MobGoalHelper; +import io.github.classgraph.ClassGraph; +import io.github.classgraph.ScanResult; +import java.util.List; +import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.Shearable; +import org.bukkit.support.environment.Normal; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +@Normal +class ShearableTest { + + static List> nmsShearables() { + try (final ScanResult result = new ClassGraph().enableClassInfo().whitelistPackages("net.minecraft.world.entity").scan()) { + return result.getClassesImplementing(Shearable.class.getName()).loadClasses(Shearable.class); + } + } + + @SuppressWarnings("unchecked") + @ParameterizedTest + @MethodSource("nmsShearables") + void ensureImplementsShearable(final Class shearableNmsClass) { + final Class bukkitClass = MobGoalHelper.toBukkitClass((Class) shearableNmsClass); + Assertions.assertTrue(io.papermc.paper.entity.Shearable.class.isAssignableFrom(bukkitClass), bukkitClass.getName() + " does not implement Shearable"); + } +}