From 91bbe058518e56af0fe4830bd91a3efc8e16f08f Mon Sep 17 00:00:00 2001 From: William Date: Tue, 18 Jun 2024 12:31:58 +0100 Subject: [PATCH] fix: fix various Fabric issues Adjusted a mixin Fixed Uniform being relocated causing a ClassNotFound exception (it's a JiJ mod now) --- fabric/build.gradle | 1 - .../husksync/listener/FabricEventListener.java | 9 +++++---- .../husksync/mixins/ItemEntityMixin.java | 18 +++++++++++------- .../mixins/ServerPlayerEntityMixin.java | 5 +++-- .../husksync/mixins/ServerWorldMixin.java | 10 ++++++++++ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/fabric/build.gradle b/fabric/build.gradle index 8f756144..544a4b19 100644 --- a/fabric/build.gradle +++ b/fabric/build.gradle @@ -54,7 +54,6 @@ shadowJar { relocate 'org.intellij', 'net.william278.husksync.libraries' relocate 'com.zaxxer', 'net.william278.husksync.libraries' relocate 'de.exlll', 'net.william278.husksync.libraries' - relocate 'net.william278.uniform', 'net.william278.husksync.libraries.uniform' relocate 'net.william278.desertwell', 'net.william278.husksync.libraries.desertwell' relocate 'net.william278.paginedown', 'net.william278.husksync.libraries.paginedown' relocate 'org.json', 'net.william278.husksync.libraries.json' diff --git a/fabric/src/main/java/net/william278/husksync/listener/FabricEventListener.java b/fabric/src/main/java/net/william278/husksync/listener/FabricEventListener.java index 57f48125..95894e37 100644 --- a/fabric/src/main/java/net/william278/husksync/listener/FabricEventListener.java +++ b/fabric/src/main/java/net/william278/husksync/listener/FabricEventListener.java @@ -69,7 +69,7 @@ public class FabricEventListener extends EventListener implements LockedHandler WorldSaveCallback.EVENT.register(this::handleWorldSave); PlayerDeathDropsCallback.EVENT.register(this::handlePlayerDeathDrops); - // TODO: Events of extra things to cancel if the player has not been set yet + // Locked events handling ItemPickupCallback.EVENT.register(this::handleItemPickup); ItemDropCallback.EVENT.register(this::handleItemDrop); UseBlockCallback.EVENT.register(this::handleBlockInteract); @@ -94,14 +94,15 @@ public class FabricEventListener extends EventListener implements LockedHandler } private void handleWorldSave(@NotNull ServerWorld world) { - saveOnWorldSave(world.getPlayers().stream() - .map(player -> (OnlineUser) FabricUser.adapt(player, plugin)).collect(Collectors.toList())); + this.saveOnWorldSave( + world.getPlayers().stream().map(player -> (OnlineUser) FabricUser.adapt(player, plugin)).toList() + ); } private void handlePlayerDeathDrops(@NotNull ServerPlayerEntity player, @Nullable ItemStack @NotNull [] toKeep, @Nullable ItemStack @NotNull [] toDrop) { final SaveOnDeathSettings settings = plugin.getSettings().getSynchronization().getSaveOnDeath(); - saveOnPlayerDeath( + this.saveOnPlayerDeath( FabricUser.adapt(player, plugin), FabricData.Items.ItemArray.adapt( settings.getItemsToSave() == SaveOnDeathSettings.DeathItemsMode.DROPS ? toDrop : toKeep diff --git a/fabric/src/main/java/net/william278/husksync/mixins/ItemEntityMixin.java b/fabric/src/main/java/net/william278/husksync/mixins/ItemEntityMixin.java index 7a7ebfd0..ee1c7486 100644 --- a/fabric/src/main/java/net/william278/husksync/mixins/ItemEntityMixin.java +++ b/fabric/src/main/java/net/william278/husksync/mixins/ItemEntityMixin.java @@ -20,22 +20,26 @@ package net.william278.husksync.mixins; import net.minecraft.entity.ItemEntity; -import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.util.ActionResult; import net.william278.husksync.event.ItemPickupCallback; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ItemEntity.class) public class ItemEntityMixin { - @Redirect(at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerInventory;insertStack(Lnet/minecraft/item/ItemStack;)Z"), - method = "onPlayerCollision") - public boolean onPlayerCollision(PlayerInventory inventory, ItemStack stack) { - ActionResult result = ItemPickupCallback.EVENT.invoker().interact(inventory.player, stack); - return (result != ActionResult.FAIL && inventory.insertStack(stack)); + @Inject(method = "onPlayerCollision", at = @At("HEAD"), cancellable = true) + private void onPlayerPickupItem(PlayerEntity player, CallbackInfo ci) { + final ItemStack stack = ((ItemEntity) (Object) this).getStack(); + final ActionResult result = ItemPickupCallback.EVENT.invoker().interact(player, stack); + + if (result == ActionResult.FAIL) { + ci.cancel(); + } } } diff --git a/fabric/src/main/java/net/william278/husksync/mixins/ServerPlayerEntityMixin.java b/fabric/src/main/java/net/william278/husksync/mixins/ServerPlayerEntityMixin.java index fa27c7c7..ed44b890 100644 --- a/fabric/src/main/java/net/william278/husksync/mixins/ServerPlayerEntityMixin.java +++ b/fabric/src/main/java/net/william278/husksync/mixins/ServerPlayerEntityMixin.java @@ -21,6 +21,7 @@ package net.william278.husksync.mixins; import net.minecraft.entity.ItemEntity; import net.minecraft.item.ItemStack; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.ActionResult; import net.william278.husksync.event.ItemDropCallback; @@ -35,8 +36,8 @@ public class ServerPlayerEntityMixin { @Inject(method = "dropItem", at = @At("HEAD"), cancellable = true) private void onPlayerDropItem(ItemStack stack, boolean dropAtFeet, boolean saveThrower, final CallbackInfoReturnable ci) { - ServerPlayerEntity player = (ServerPlayerEntity) (Object) this; - ActionResult result = ItemDropCallback.EVENT.invoker().interact(player, stack); + final ServerPlayerEntity player = (ServerPlayerEntity) (Object) this; + final ActionResult result = ItemDropCallback.EVENT.invoker().interact(player, stack); if (result == ActionResult.FAIL) { ci.cancel(); diff --git a/fabric/src/main/java/net/william278/husksync/mixins/ServerWorldMixin.java b/fabric/src/main/java/net/william278/husksync/mixins/ServerWorldMixin.java index 8ccea757..96299ccb 100644 --- a/fabric/src/main/java/net/william278/husksync/mixins/ServerWorldMixin.java +++ b/fabric/src/main/java/net/william278/husksync/mixins/ServerWorldMixin.java @@ -19,9 +19,12 @@ package net.william278.husksync.mixins; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.world.ServerWorld; import net.william278.husksync.event.WorldSaveCallback; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -29,8 +32,15 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ServerWorld.class) public class ServerWorldMixin { + @Final + @Shadow + private MinecraftServer server; + @Inject(method = "saveLevel", at = @At("HEAD")) public void saveLevel(CallbackInfo ci) { + if (server.isStopping() || server.isStopped()) { + return; + } WorldSaveCallback.EVENT.invoker().save((ServerWorld) (Object) this); }