Skip to content

Commit

Permalink
Vanished players can no longer block merchant trading
Browse files Browse the repository at this point in the history
The trading system in vanilla is implemented with just one trading
player in mind. Because of that the trade screen of vanished players is
simply closed when another player attempts to trade with the villager to
make it seem like nobody else is trading with the villager to non
vanished players. These changes have low priority and may be removed at
any time, because it is easy to not allow this case to happen as a
vanished player.
resolves #46
  • Loading branch information
DrexHD committed Jul 11, 2024
1 parent e7790de commit 56ff78e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Containers remaining open by vanished players in certain conditions
- Pressure plates remaining pressed by vanished players in certain conditions
- Vanished players blocking merchant (villager / wandering trader) trading

## [1.5.6] - 2024-06-20
### Added
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/me/drex/vanish/mixin/AbstractVillagerMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.drex.vanish.mixin;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import me.drex.vanish.api.VanishAPI;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.npc.AbstractVillager;
import net.minecraft.world.entity.player.Player;
import org.jetbrains.annotations.Nullable;
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;

@Mixin(AbstractVillager.class)
public class AbstractVillagerMixin {
@Shadow
private @Nullable Player tradingPlayer;

@ModifyReturnValue(method = "isTrading", at = @At("RETURN"))
private boolean vanish_allowTradingIfVanished(boolean original) {
if (original) {
assert this.tradingPlayer != null;
return !VanishAPI.isVanished(this.tradingPlayer);
}
return false;
}

@Inject(method = "setTradingPlayer", at = @At("HEAD"))
private void vanish_closeVanishedPlayerTradeScreen(Player player, CallbackInfo ci) {
if (player != null && this.tradingPlayer instanceof ServerPlayer serverPlayer) {
serverPlayer.closeContainer();
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/vanish.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"defaultRequire": 1
},
"mixins": [
"AbstractVillagerMixin",
"CommandSourceStackMixin",
"ContainerMixin",
"ContainerOpenersCountMixin",
Expand Down

0 comments on commit 56ff78e

Please sign in to comment.