From 07e7a8408bcdfe9222f8ad3fec149f50095e9504 Mon Sep 17 00:00:00 2001 From: btwonion Date: Sat, 20 Jan 2024 21:59:25 +0100 Subject: [PATCH] add sweetberry drops and reformat --- changelogs/fabric-2.4.0-1.20.4.md | 3 + telekinesis-fabric/build.gradle.kts | 4 +- .../mixins/AbstractChestedHorseMixin.java | 14 +++-- .../mixins/AbstractHorseMixin.java | 11 ++-- .../nyon/telekinesis/mixins/AllayMixin.java | 22 ++++---- .../nyon/telekinesis/mixins/BlockMixin.java | 55 ++++++++++--------- .../telekinesis/mixins/ChestBoatMixin.java | 8 ++- .../telekinesis/mixins/EnchantmentsMixin.java | 10 +++- .../telekinesis/mixins/EnderManMixin.java | 16 ++++-- .../telekinesis/mixins/FishingHookMixin.java | 12 ++-- .../telekinesis/mixins/InventoryMixin.java | 13 +++-- .../telekinesis/mixins/LivingEntityMixin.java | 24 ++++---- .../dev/nyon/telekinesis/mixins/MobMixin.java | 16 ++++-- .../dev/nyon/telekinesis/mixins/PigMixin.java | 14 +++-- .../nyon/telekinesis/mixins/PiglinMixin.java | 14 +++-- .../nyon/telekinesis/mixins/SheepMixin.java | 49 +++++++++++++---- .../telekinesis/mixins/SnowgolemMixin.java | 19 ++++--- .../nyon/telekinesis/mixins/StriderMixin.java | 14 +++-- .../mixins/SweetBerryBushBlockMixin.java | 50 +++++++++++++++++ .../mixins/VehicleEntityMixin.java | 9 ++- .../telekinesis/mixins/WitherBossMixin.java | 16 ++++-- .../nyon/telekinesis/utils/PlayerUtils.java | 14 ++++- .../telekinesis/utils/TelekinesisUtils.java | 49 +++++++++++++---- .../main/resources/telekinesis.mixins.json | 1 + 24 files changed, 317 insertions(+), 140 deletions(-) create mode 100644 changelogs/fabric-2.4.0-1.20.4.md create mode 100644 telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SweetBerryBushBlockMixin.java diff --git a/changelogs/fabric-2.4.0-1.20.4.md b/changelogs/fabric-2.4.0-1.20.4.md new file mode 100644 index 0000000..36f37a2 --- /dev/null +++ b/changelogs/fabric-2.4.0-1.20.4.md @@ -0,0 +1,3 @@ +# v2.4.0-1.20.4 +- add sweet berry bush drops +- update fabric loader dependency \ No newline at end of file diff --git a/telekinesis-fabric/build.gradle.kts b/telekinesis-fabric/build.gradle.kts index d98e4dd..730bcf5 100644 --- a/telekinesis-fabric/build.gradle.kts +++ b/telekinesis-fabric/build.gradle.kts @@ -19,7 +19,7 @@ plugins { } group = "dev.nyon" -val majorVersion = "2.3.5" +val majorVersion = "2.4.0" val mcVersion = "1.20.4" val supportedMcVersions = listOf("1.20.3", "1.20.4") version = "$majorVersion-1.20.4" @@ -51,7 +51,7 @@ dependencies { officialMojangMappings() }) implementation("org.vineflower:vineflower:1.9.3") - modImplementation("net.fabricmc:fabric-loader:0.15.3") + modImplementation("net.fabricmc:fabric-loader:0.15.6") modImplementation("net.fabricmc:fabric-language-kotlin:1.10.17+kotlin.1.9.22") modImplementation("dev.isxander.yacl:yet-another-config-lib-fabric:3.3.1+1.20.4") modImplementation("com.terraformersmc:modmenu:9.0.0-pre.1") diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AbstractChestedHorseMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AbstractChestedHorseMixin.java index 8437967..2a1847d 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AbstractChestedHorseMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AbstractChestedHorseMixin.java @@ -19,18 +19,20 @@ public class AbstractChestedHorseMixin { target = "Lnet/minecraft/world/entity/animal/horse/AbstractChestedHorse;spawnAtLocation(Lnet/minecraft/world/level/ItemLike;)Lnet/minecraft/world/entity/item/ItemEntity;" ) ) - public boolean redirectEquipmentDrop(AbstractChestedHorse instance, ItemLike item) { + public boolean redirectEquipmentDrop( + AbstractChestedHorse instance, + ItemLike item + ) { final var attacker = instance.getLastAttacker(); if (!(attacker instanceof ServerPlayer serverPlayer)) return true; - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.MobDrops, + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, serverPlayer, serverPlayer.getMainHandItem(), player -> { - if (!player.addItem(item.asItem().getDefaultInstance())) instance.spawnAtLocation(item); - } - ); + if (!player.addItem(item.asItem() + .getDefaultInstance())) instance.spawnAtLocation(item); + }); return !hasTelekinesis; } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AbstractHorseMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AbstractHorseMixin.java index 76f252c..832fb71 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AbstractHorseMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AbstractHorseMixin.java @@ -19,18 +19,19 @@ public class AbstractHorseMixin { target = "Lnet/minecraft/world/entity/animal/horse/AbstractHorse;spawnAtLocation(Lnet/minecraft/world/item/ItemStack;)Lnet/minecraft/world/entity/item/ItemEntity;" ) ) - public boolean redirectEquipmentDrop(AbstractHorse instance, ItemStack stack) { + public boolean redirectEquipmentDrop( + AbstractHorse instance, + ItemStack stack + ) { final var attacker = instance.getLastAttacker(); if (!(attacker instanceof ServerPlayer serverPlayer)) return true; - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.MobDrops, + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, serverPlayer, serverPlayer.getMainHandItem(), player -> { if (!player.addItem(stack)) instance.spawnAtLocation(stack); - } - ); + }); return !hasTelekinesis; } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AllayMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AllayMixin.java index c2a032d..10d8ae8 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AllayMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/AllayMixin.java @@ -27,18 +27,19 @@ public class AllayMixin { target = "Lnet/minecraft/world/entity/animal/allay/Allay;spawnAtLocation(Lnet/minecraft/world/item/ItemStack;)Lnet/minecraft/world/entity/item/ItemEntity;" ) ) - public boolean redirectEquipmentDrop(Allay instance, ItemStack stack) { + public boolean redirectEquipmentDrop( + Allay instance, + ItemStack stack + ) { final var attacker = instance.getLastAttacker(); if (!(attacker instanceof ServerPlayer serverPlayer)) return true; - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.MobDrops, + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, serverPlayer, serverPlayer.getMainHandItem(), player -> { if (!player.addItem(stack)) instance.spawnAtLocation(stack); - } - ); + }); return !hasTelekinesis; } @@ -50,21 +51,22 @@ public boolean redirectEquipmentDrop(Allay instance, ItemStack stack) { target = "Ljava/util/List;forEach(Ljava/util/function/Consumer;)V" ) ) - public void redirectInventoryDrops(List instance, Consumer consumer) { + public void redirectInventoryDrops( + List instance, + Consumer consumer + ) { final var attacker = allay.getLastAttacker(); if (!(attacker instanceof ServerPlayer serverPlayer)) { instance.forEach(consumer); return; } - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.MobDrops, + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, serverPlayer, serverPlayer.getMainHandItem(), player -> instance.forEach(item -> { if (!player.addItem(item)) consumer.accept(item); - }) - ); + })); if (!hasTelekinesis) instance.forEach(consumer); } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/BlockMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/BlockMixin.java index 733817d..c1916fa 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/BlockMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/BlockMixin.java @@ -43,16 +43,9 @@ private static void redirectDrops( ItemStack itemStack ) { args.>set(0, item -> { - if (!TelekinesisUtils.handleTelekinesisBlock( - TelekinesisPolicy.BlockDrops, - entity, - itemStack, - player -> { - if (!player.addItem(item)) { - Block.popResource(level, blockPos, item); - } - }) - ) Block.popResource(level, blockPos, item); + if (!TelekinesisUtils.handleTelekinesisBlock(TelekinesisPolicy.BlockDrops, entity, itemStack, player -> { + if (!player.addItem(item)) Block.popResource(level, blockPos, item); + })) Block.popResource(level, blockPos, item); }); } @@ -65,25 +58,37 @@ private static void redirectDrops( cancellable = true ) private static void manipulateDrops( - BlockState blockState, Level level, BlockPos blockPos, BlockEntity blockEntity, Entity entity, ItemStack itemStack, CallbackInfo ci + BlockState blockState, + Level level, + BlockPos blockPos, + BlockEntity blockEntity, + Entity entity, + ItemStack itemStack, + CallbackInfo ci ) { if (!(level instanceof ServerLevel serverLevel)) return; Block block = blockState.getBlock(); if (EnchantmentHelper.hasSilkTouch(itemStack)) return; - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesisBlock(TelekinesisPolicy.ExpDrops, entity, itemStack, player -> { - int expToAdd = 0; - if (block instanceof DropExperienceBlock expBlock) - expToAdd = ((DropExperienceBlockAccessor) expBlock).getXpRange().sample(level.random); - if (block instanceof RedStoneOreBlock) expToAdd = 1 + level.random.nextInt(5); - if (block instanceof SculkCatalystBlock catalystBlock) - expToAdd = ((CatalystBlockAccessor) catalystBlock).getXpRange().sample(level.random); - if (block instanceof SculkSensorBlock || block instanceof SculkShriekerBlock) - expToAdd = ConstantInt.of(5).sample(level.random); - if (block instanceof SpawnerBlock) expToAdd = level.random.nextInt(15) + level.random.nextInt(15); - if (block instanceof InfestedBlock infestedBlock) - infestedBlock.spawnAfterBreak(blockState, serverLevel, blockPos, itemStack, true); - PlayerUtils.addExpToPlayer(player, expToAdd); - }); + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesisBlock(TelekinesisPolicy.ExpDrops, + entity, + itemStack, + player -> { + int expToAdd = 0; + if (block instanceof DropExperienceBlock expBlock) + expToAdd = ((DropExperienceBlockAccessor) expBlock).getXpRange() + .sample(level.random); + if (block instanceof RedStoneOreBlock) expToAdd = 1 + level.random.nextInt(5); + if (block instanceof SculkCatalystBlock catalystBlock) + expToAdd = ((CatalystBlockAccessor) catalystBlock).getXpRange() + .sample(level.random); + if (block instanceof SculkSensorBlock || block instanceof SculkShriekerBlock) + expToAdd = ConstantInt.of(5) + .sample(level.random); + if (block instanceof SpawnerBlock) expToAdd = level.random.nextInt(15) + level.random.nextInt(15); + if (block instanceof InfestedBlock infestedBlock) + infestedBlock.spawnAfterBreak(blockState, serverLevel, blockPos, itemStack, true); + PlayerUtils.addExpToPlayer(player, expToAdd); + }); if (hasTelekinesis) ci.cancel(); } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/ChestBoatMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/ChestBoatMixin.java index ffa7022..a44ed4d 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/ChestBoatMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/ChestBoatMixin.java @@ -23,14 +23,18 @@ public class ChestBoatMixin { target = "Lnet/minecraft/world/entity/vehicle/ChestBoat;getDropItem()Lnet/minecraft/world/item/Item;" ) ) - private Item changeDroppedItem(Item original, DamageSource damageSource) { + private Item changeDroppedItem( + Item original, + DamageSource damageSource + ) { final var attacker = damageSource.getEntity(); if (!(attacker instanceof ServerPlayer)) return original; AtomicReference toReturn = new AtomicReference<>(original); TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.VehicleDrops, damageSource, player -> { - if (player.addItem(original.asItem().getDefaultInstance())) toReturn.set(ItemStack.EMPTY.getItem()); + if (player.addItem(original.asItem() + .getDefaultInstance())) toReturn.set(ItemStack.EMPTY.getItem()); }); return toReturn.get(); diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/EnchantmentsMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/EnchantmentsMixin.java index 8baf46f..d049caf 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/EnchantmentsMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/EnchantmentsMixin.java @@ -23,11 +23,17 @@ public abstract class EnchantmentsMixin { method = "register", at = @At("RETURN") ) - private static void registerTelekinesis(String identifier, Enchantment enchantment, CallbackInfoReturnable cir) { + private static void registerTelekinesis( + String identifier, + Enchantment enchantment, + CallbackInfoReturnable cir + ) { if (!isTelekinesisRegistered) { MainKt.setTelekinesis(new TelekinesisEnchantment()); isTelekinesisRegistered = true; - Registry.register(BuiltInRegistries.ENCHANTMENT, new ResourceLocation("telekinesis", "telekinesis"), MainKt.getTelekinesis()); + Registry.register(BuiltInRegistries.ENCHANTMENT, + new ResourceLocation("telekinesis", "telekinesis"), + MainKt.getTelekinesis()); } } } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/EnderManMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/EnderManMixin.java index 64c8ce2..f8c0d65 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/EnderManMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/EnderManMixin.java @@ -20,13 +20,21 @@ public class EnderManMixin { target = "Lnet/minecraft/world/entity/monster/EnderMan;spawnAtLocation(Lnet/minecraft/world/item/ItemStack;)Lnet/minecraft/world/entity/item/ItemEntity;" ) ) - public boolean redirectEquipmentDrop(EnderMan instance, ItemStack stack, DamageSource damageSource, int lootingMultiplier, boolean allowDrops) { + public boolean redirectEquipmentDrop( + EnderMan instance, + ItemStack stack, + DamageSource damageSource, + int lootingMultiplier, + boolean allowDrops + ) { final var attacker = damageSource.getEntity(); if (!(attacker instanceof ServerPlayer)) return true; - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, damageSource, player -> { - if (!player.addItem(stack)) instance.spawnAtLocation(stack); - }); + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, + damageSource, + player -> { + if (!player.addItem(stack)) instance.spawnAtLocation(stack); + }); return !hasTelekinesis; } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/FishingHookMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/FishingHookMixin.java index 2091b37..b4503a3 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/FishingHookMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/FishingHookMixin.java @@ -39,25 +39,21 @@ private boolean redirectFishingHookDrops( if (!(getPlayerOwner() instanceof ServerPlayer _serverPlayer)) return true; if (entity instanceof ExperienceOrb expOrb) { - final var hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.ExpDrops, + final var hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.ExpDrops, _serverPlayer, stack, - serverPlayer -> PlayerUtils.addExpToPlayer(serverPlayer, expOrb.getValue()) - ); + serverPlayer -> PlayerUtils.addExpToPlayer(serverPlayer, expOrb.getValue())); return !hasTelekinesis; } if (entity instanceof ItemEntity itemEntity) { - final var hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.FishingDrops, + final var hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.FishingDrops, _serverPlayer, stack, serverPlayer -> { if (!serverPlayer.addItem(itemEntity.getItem())) instance.addFreshEntity(itemEntity); - } - ); + }); return !hasTelekinesis; } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/InventoryMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/InventoryMixin.java index 2e9861f..2e391ef 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/InventoryMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/InventoryMixin.java @@ -20,18 +20,21 @@ public class InventoryMixin { target = "Lnet/minecraft/world/entity/player/Player;drop(Lnet/minecraft/world/item/ItemStack;ZZ)Lnet/minecraft/world/entity/item/ItemEntity;" ) ) - public boolean redirectEquipmentDrop(Player instance, ItemStack stack, boolean throwRandomly, boolean retainOwnership) { + public boolean redirectEquipmentDrop( + Player instance, + ItemStack stack, + boolean throwRandomly, + boolean retainOwnership + ) { final var attacker = instance.getLastAttacker(); if (!(attacker instanceof ServerPlayer serverPlayer)) return true; - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.MobDrops, + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, serverPlayer, serverPlayer.getMainHandItem(), player -> { if (!player.addItem(stack)) instance.spawnAtLocation(stack); - } - ); + }); return !hasTelekinesis; } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/LivingEntityMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/LivingEntityMixin.java index dd83759..c189f9a 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/LivingEntityMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/LivingEntityMixin.java @@ -31,16 +31,18 @@ public abstract class LivingEntityMixin { target = "Lnet/minecraft/world/entity/ExperienceOrb;award(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/world/phys/Vec3;I)V" ) ) - public boolean redirectExp(ServerLevel world, Vec3 pos, int amount) { + public boolean redirectExp( + ServerLevel world, + Vec3 pos, + int amount + ) { final var attacker = livingEntity.getLastAttacker(); if (!(attacker instanceof ServerPlayer serverPlayer)) return true; - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.ExpDrops, + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.ExpDrops, serverPlayer, serverPlayer.getMainHandItem(), - player -> PlayerUtils.addExpToPlayer(player, amount) - ); + player -> PlayerUtils.addExpToPlayer(player, amount)); return !hasTelekinesis; } @@ -52,15 +54,17 @@ public boolean redirectExp(ServerLevel world, Vec3 pos, int amount) { target = "Lnet/minecraft/world/level/storage/loot/LootTable;getRandomItems(Lnet/minecraft/world/level/storage/loot/LootParams;JLjava/util/function/Consumer;)V" ) ) - public void redirectCommonDrops(Args args, DamageSource damageSource, boolean bl) { + public void redirectCommonDrops( + Args args, + DamageSource damageSource, + boolean bl + ) { args.>set(2, item -> { - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.MobDrops, + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, damageSource, player -> { if (!player.addItem(item)) livingEntity.spawnAtLocation(item); - } - ); + }); if (!hasTelekinesis) livingEntity.spawnAtLocation(item); }); diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/MobMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/MobMixin.java index 063fa8b..fa6de6b 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/MobMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/MobMixin.java @@ -20,13 +20,21 @@ public class MobMixin { target = "Lnet/minecraft/world/entity/Mob;spawnAtLocation(Lnet/minecraft/world/item/ItemStack;)Lnet/minecraft/world/entity/item/ItemEntity;" ) ) - public boolean redirectEquipmentDrop(Mob instance, ItemStack stack, DamageSource damageSource, int lootingMultiplier, boolean allowDrops) { + public boolean redirectEquipmentDrop( + Mob instance, + ItemStack stack, + DamageSource damageSource, + int lootingMultiplier, + boolean allowDrops + ) { final var attacker = damageSource.getEntity(); if (!(attacker instanceof ServerPlayer)) return true; - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, damageSource, player -> { - if (!player.addItem(stack)) instance.spawnAtLocation(stack); - }); + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, + damageSource, + player -> { + if (!player.addItem(stack)) instance.spawnAtLocation(stack); + }); return !hasTelekinesis; } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/PigMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/PigMixin.java index d44934f..b7770b2 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/PigMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/PigMixin.java @@ -19,18 +19,20 @@ public class PigMixin { target = "Lnet/minecraft/world/entity/animal/Pig;spawnAtLocation(Lnet/minecraft/world/level/ItemLike;)Lnet/minecraft/world/entity/item/ItemEntity;" ) ) - public boolean redirectEquipmentDrop(Pig instance, ItemLike item) { + public boolean redirectEquipmentDrop( + Pig instance, + ItemLike item + ) { final var attacker = instance.getLastAttacker(); if (!(attacker instanceof ServerPlayer serverPlayer)) return true; - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.MobDrops, + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, serverPlayer, serverPlayer.getMainHandItem(), player -> { - if (!player.addItem(item.asItem().getDefaultInstance())) instance.spawnAtLocation(item); - } - ); + if (!player.addItem(item.asItem() + .getDefaultInstance())) instance.spawnAtLocation(item); + }); return !hasTelekinesis; } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/PiglinMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/PiglinMixin.java index d0bd70f..323efe2 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/PiglinMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/PiglinMixin.java @@ -22,16 +22,20 @@ public class PiglinMixin { target = "Ljava/util/List;forEach(Ljava/util/function/Consumer;)V" ) ) - public void redirectDrops(List instance, Consumer consumer, DamageSource damageSource, int i, boolean bl) { + public void redirectDrops( + List instance, + Consumer consumer, + DamageSource damageSource, + int i, + boolean bl + ) { var piglin = (Piglin) (Object) this; - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.MobDrops, + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, damageSource, player -> instance.forEach(item -> { if (!player.addItem(item)) piglin.spawnAtLocation(item); - }) - ); + })); if (!hasTelekinesis) instance.forEach(piglin::spawnAtLocation); } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SheepMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SheepMixin.java index 1f43393..56bf062 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SheepMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SheepMixin.java @@ -28,12 +28,25 @@ public abstract class SheepMixin { @Shadow @Final private static Map ITEM_BY_DYE; + @Unique private final RandomSource random = RandomSource.create(); - @Redirect(method = "mobInteract", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/animal/Sheep;shear(Lnet/minecraft/sounds/SoundSource;)V")) - public void manipulateWoolDrops(Sheep instance, SoundSource soundSource, Player player, InteractionHand interactionHand) { - instance.level().playSound(null, instance, SoundEvents.SHEEP_SHEAR, soundSource, 1.0F, 1.0F); + @Redirect( + method = "mobInteract", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/world/entity/animal/Sheep;shear(Lnet/minecraft/sounds/SoundSource;)V" + ) + ) + public void manipulateWoolDrops( + Sheep instance, + SoundSource soundSource, + Player player, + InteractionHand interactionHand + ) { + instance.level() + .playSound(null, instance, SoundEvents.SHEEP_SHEAR, soundSource, 1.0F, 1.0F); instance.setSheared(true); int i = 1 + random.nextInt(3); @@ -42,23 +55,35 @@ public void manipulateWoolDrops(Sheep instance, SoundSource soundSource, Player return; } - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.ShearingDrops, serverPlayerr, player.getItemInHand(interactionHand), serverPlayer -> { - for (int j = 0; j < i; ++j) { - if (!serverPlayer.addItem(new ItemStack(ITEM_BY_DYE.get(instance.getColor()), 1))) { - ItemEntity entity = instance.spawnAtLocation(ITEM_BY_DYE.get(instance.getColor()), 1); - entity.setDeltaMovement(entity.getDeltaMovement().add((random.nextFloat() - random.nextFloat()) * 0.1F, random.nextFloat() * 0.05F, (random.nextFloat() - random.nextFloat()) * 0.1F)); + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.ShearingDrops, + serverPlayerr, + player.getItemInHand(interactionHand), + serverPlayer -> { + for (int j = 0; j < i; ++j) { + if (!serverPlayer.addItem(new ItemStack(ITEM_BY_DYE.get(instance.getColor()), 1))) { + ItemEntity entity = instance.spawnAtLocation(ITEM_BY_DYE.get(instance.getColor()), 1); + entity.setDeltaMovement(entity.getDeltaMovement() + .add((random.nextFloat() - random.nextFloat()) * 0.1F, + random.nextFloat() * 0.05F, + (random.nextFloat() - random.nextFloat()) * 0.1F)); + } } - } - }); + }); if (!hasTelekinesis) dropAllNormally(i, instance); } @Unique - void dropAllNormally(int i, Sheep instance) { + void dropAllNormally( + int i, + Sheep instance + ) { for (int j = 0; j < i; ++j) { ItemEntity entity = instance.spawnAtLocation(ITEM_BY_DYE.get(instance.getColor()), 1); - entity.setDeltaMovement(entity.getDeltaMovement().add((random.nextFloat() - random.nextFloat()) * 0.1F, random.nextFloat() * 0.05F, (random.nextFloat() - random.nextFloat()) * 0.1F)); + entity.setDeltaMovement(entity.getDeltaMovement() + .add((random.nextFloat() - random.nextFloat()) * 0.1F, + random.nextFloat() * 0.05F, + (random.nextFloat() - random.nextFloat()) * 0.1F)); } } } \ No newline at end of file diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SnowgolemMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SnowgolemMixin.java index 8f4a8a8..2fa9bed 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SnowgolemMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SnowgolemMixin.java @@ -23,20 +23,25 @@ public class SnowgolemMixin { target = "Lnet/minecraft/world/entity/animal/SnowGolem;shear(Lnet/minecraft/sounds/SoundSource;)V" ) ) - public void manipulateWoolDrops(SnowGolem instance, SoundSource soundSource, Player player, InteractionHand interactionHand) { - instance.level().playSound(null, instance, SoundEvents.SNOW_GOLEM_SHEAR, soundSource, 1.0F, 1.0F); - if (!instance.level().isClientSide()) { + public void manipulateWoolDrops( + SnowGolem instance, + SoundSource soundSource, + Player player, + InteractionHand interactionHand + ) { + instance.level() + .playSound(null, instance, SoundEvents.SNOW_GOLEM_SHEAR, soundSource, 1.0F, 1.0F); + if (!instance.level() + .isClientSide()) { instance.setPumpkin(false); ItemStack item = new ItemStack(Items.CARVED_PUMPKIN); - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.ShearingDrops, + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.ShearingDrops, player, serverPlayer -> { if (!serverPlayer.addItem(item)) instance.spawnAtLocation(item, 1.7F); - } - ); + }); if (!hasTelekinesis) instance.spawnAtLocation(item, 1.7F); } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/StriderMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/StriderMixin.java index 77b0b7c..28e6035 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/StriderMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/StriderMixin.java @@ -19,18 +19,20 @@ public class StriderMixin { target = "Lnet/minecraft/world/entity/monster/Strider;spawnAtLocation(Lnet/minecraft/world/level/ItemLike;)Lnet/minecraft/world/entity/item/ItemEntity;" ) ) - public boolean redirectEquipmentDrop(Strider instance, ItemLike item) { + public boolean redirectEquipmentDrop( + Strider instance, + ItemLike item + ) { final var attacker = instance.getLastAttacker(); if (!(attacker instanceof ServerPlayer serverPlayer)) return true; - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis( - TelekinesisPolicy.MobDrops, + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, serverPlayer, serverPlayer.getMainHandItem(), player -> { - if (!player.addItem(item.asItem().getDefaultInstance())) instance.spawnAtLocation(item); - } - ); + if (!player.addItem(item.asItem() + .getDefaultInstance())) instance.spawnAtLocation(item); + }); return !hasTelekinesis; } diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SweetBerryBushBlockMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SweetBerryBushBlockMixin.java new file mode 100644 index 0000000..b09c519 --- /dev/null +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/SweetBerryBushBlockMixin.java @@ -0,0 +1,50 @@ +package dev.nyon.telekinesis.mixins; + +import com.llamalad7.mixinextras.injector.WrapWithCondition; +import dev.nyon.telekinesis.TelekinesisPolicy; +import dev.nyon.telekinesis.utils.TelekinesisUtils; +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.SweetBerryBushBlock; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.phys.BlockHitResult; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(SweetBerryBushBlock.class) +public class SweetBerryBushBlockMixin { + @WrapWithCondition( + method = "use", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/world/level/block/SweetBerryBushBlock;popResource(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/item/ItemStack;)V" + ) + ) + private boolean manipulateBerryDrops( + Level level, + BlockPos blockPos, + ItemStack itemStack, + BlockState state, + Level _level, + BlockPos pos, + Player player, + InteractionHand hand, + BlockHitResult hit + ) { + if (!(player instanceof ServerPlayer serverPlayer)) return true; + + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.BlockDrops, + serverPlayer, + serverPlayer.getItemInHand(hand), + cPlayer -> { + if (!player.addItem(itemStack)) Block.popResource(level, pos, itemStack); + }); + + return !hasTelekinesis; + } +} \ No newline at end of file diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/VehicleEntityMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/VehicleEntityMixin.java index 1e1c3c7..56bacf1 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/VehicleEntityMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/VehicleEntityMixin.java @@ -8,7 +8,6 @@ import net.minecraft.world.entity.vehicle.VehicleEntity; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -24,14 +23,18 @@ public class VehicleEntityMixin { target = "Lnet/minecraft/world/entity/vehicle/VehicleEntity;getDropItem()Lnet/minecraft/world/item/Item;" ) ) - private Item changeDroppedItem(Item original, DamageSource damageSource) { + private Item changeDroppedItem( + Item original, + DamageSource damageSource + ) { final var attacker = damageSource.getEntity(); if (!(attacker instanceof ServerPlayer)) return original; AtomicReference toReturn = new AtomicReference<>(original); TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.VehicleDrops, damageSource, player -> { - if (player.addItem(original.asItem().getDefaultInstance())) toReturn.set(ItemStack.EMPTY.getItem()); + if (player.addItem(original.asItem() + .getDefaultInstance())) toReturn.set(ItemStack.EMPTY.getItem()); }); return toReturn.get(); diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/WitherBossMixin.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/WitherBossMixin.java index fa90a82..a659502 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/WitherBossMixin.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/mixins/WitherBossMixin.java @@ -20,13 +20,21 @@ public abstract class WitherBossMixin { target = "net/minecraft/world/entity/boss/wither/WitherBoss.spawnAtLocation (Lnet/minecraft/world/level/ItemLike;)Lnet/minecraft/world/entity/item/ItemEntity;" ) ) - protected ItemEntity redirectEquipmentDrop(WitherBoss instance, ItemLike stack, Operation original) { + protected ItemEntity redirectEquipmentDrop( + WitherBoss instance, + ItemLike stack, + Operation original + ) { final var attacker = instance.getLastAttacker(); if (!(attacker instanceof ServerPlayer serverPlayer)) return original.call(instance, stack); - boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, serverPlayer, null, player -> { - if (!player.addItem(stack.asItem().getDefaultInstance())) instance.spawnAtLocation(stack); - }); + boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, + serverPlayer, + null, + player -> { + if (!player.addItem(stack.asItem() + .getDefaultInstance())) instance.spawnAtLocation(stack); + }); if (!hasTelekinesis) return original.call(instance, stack); else return null; diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/utils/PlayerUtils.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/utils/PlayerUtils.java index dda4caf..238b014 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/utils/PlayerUtils.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/utils/PlayerUtils.java @@ -9,13 +9,21 @@ import java.util.Map; public class PlayerUtils { - public static void addExpToPlayer(Player player, Integer exp) { + public static void addExpToPlayer( + Player player, + Integer exp + ) { var remainingExp = repairPlayerItems(player, exp); if (remainingExp > 0) player.giveExperiencePoints(remainingExp); } - private static int repairPlayerItems(Player player, Integer exp) { - Map.Entry entry = EnchantmentHelper.getRandomItemWith(Enchantments.MENDING, player, ItemStack::isDamaged); + private static int repairPlayerItems( + Player player, + Integer exp + ) { + Map.Entry entry = EnchantmentHelper.getRandomItemWith(Enchantments.MENDING, + player, + ItemStack::isDamaged); if (entry != null) { ItemStack itemStack = entry.getValue(); int j = Math.min(exp * 2, itemStack.getDamageValue()); diff --git a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/utils/TelekinesisUtils.java b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/utils/TelekinesisUtils.java index cec53b4..f57cbdc 100644 --- a/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/utils/TelekinesisUtils.java +++ b/telekinesis-fabric/src/main/java/dev/nyon/telekinesis/utils/TelekinesisUtils.java @@ -15,19 +15,32 @@ public class TelekinesisUtils { - public static boolean handleTelekinesisBlock(TelekinesisPolicy neededPolicy, Entity entity, ItemStack tool, Consumer consumer) { + public static boolean handleTelekinesisBlock( + TelekinesisPolicy neededPolicy, + Entity entity, + ItemStack tool, + Consumer consumer + ) { if (!(entity instanceof ServerPlayer serverPlayer)) return false; return handleTelekinesis(neededPolicy, serverPlayer, tool, consumer); } - public static boolean handleTelekinesis(TelekinesisPolicy neededPolicy, LivingEntity target, Consumer consumer) { + public static boolean handleTelekinesis( + TelekinesisPolicy neededPolicy, + LivingEntity target, + Consumer consumer + ) { LivingEntity attacker = target.getLastAttacker(); if (attacker == null) return false; if (!(attacker instanceof ServerPlayer serverPlayer)) return false; return handleTelekinesis(neededPolicy, serverPlayer, null, consumer); } - public static boolean handleTelekinesis(TelekinesisPolicy neededPolicy, DamageSource source, Consumer consumer) { + public static boolean handleTelekinesis( + TelekinesisPolicy neededPolicy, + DamageSource source, + Consumer consumer + ) { if (source == null) return false; Entity attacker = source.getEntity(); if (attacker == null) return false; @@ -36,7 +49,12 @@ public static boolean handleTelekinesis(TelekinesisPolicy neededPolicy, DamageSo } - public static boolean handleTelekinesis(TelekinesisPolicy neededPolicy, ServerPlayer player, @Nullable ItemStack itemStack, Consumer consumer) { + public static boolean handleTelekinesis( + TelekinesisPolicy neededPolicy, + ServerPlayer player, + @Nullable ItemStack itemStack, + Consumer consumer + ) { if (!neededPolicy.isEnabled()) return false; if (!playerMeetsConditions(neededPolicy, player, itemStack)) return false; @@ -44,15 +62,23 @@ public static boolean handleTelekinesis(TelekinesisPolicy neededPolicy, ServerPl return true; } - private static boolean playerMeetsConditions(TelekinesisPolicy policy, ServerPlayer player, @Nullable ItemStack itemStack) { + private static boolean playerMeetsConditions( + TelekinesisPolicy policy, + ServerPlayer player, + @Nullable ItemStack itemStack + ) { boolean conditionsMet = false; - boolean isEnabledByDefault = TelekinesisConfigKt.getConfig().getOnByDefault(); + boolean isEnabledByDefault = TelekinesisConfigKt.getConfig() + .getOnByDefault(); - boolean hasArmorTelekinesis = player.getInventory().armor.stream().allMatch(item -> EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(), item) > 0); - boolean hasMainHandTelekinesis = (itemStack != null && EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(), itemStack) > 0) || - EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(), player.getMainHandItem()) > 0; - boolean hasOffHandTelekinesis = EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(), player.getOffhandItem()) > 0; + boolean hasArmorTelekinesis = player.getInventory().armor.stream() + .allMatch(item -> EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(), item) > 0); + boolean hasMainHandTelekinesis = (itemStack != null && EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(), + itemStack) > 0) || EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(), + player.getMainHandItem()) > 0; + boolean hasOffHandTelekinesis = EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(), + player.getOffhandItem()) > 0; if (isEnabledByDefault) conditionsMet = true; else switch (policy) { @@ -62,7 +88,8 @@ else switch (policy) { case BlockDrops -> conditionsMet = hasMainHandTelekinesis; } - if (TelekinesisConfigKt.getConfig().getOnlyOnSneak() && !player.isCrouching()) conditionsMet = false; + if (TelekinesisConfigKt.getConfig() + .getOnlyOnSneak() && !player.isCrouching()) conditionsMet = false; return conditionsMet; } diff --git a/telekinesis-fabric/src/main/resources/telekinesis.mixins.json b/telekinesis-fabric/src/main/resources/telekinesis.mixins.json index d33862b..ac771f1 100644 --- a/telekinesis-fabric/src/main/resources/telekinesis.mixins.json +++ b/telekinesis-fabric/src/main/resources/telekinesis.mixins.json @@ -23,6 +23,7 @@ "SheepMixin", "SnowgolemMixin", "StriderMixin", + "SweetBerryBushBlockMixin", "VehicleEntityMixin", "WitherBossMixin" ],