Skip to content

Commit

Permalink
Cleanup split-outed mixins.
Browse files Browse the repository at this point in the history
Signed-off-by: 秋雨落 <i@rain.cx>
  • Loading branch information
qyl27 committed Nov 29, 2024
1 parent 1db479a commit 9d075cb
Show file tree
Hide file tree
Showing 28 changed files with 397 additions and 179 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.izzel.arclight.common.bridge.core.entity.player;

import com.mojang.datafixers.util.Either;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import org.bukkit.Location;
Expand All @@ -12,6 +13,8 @@

public interface ServerPlayerEntityBridge extends PlayerEntityBridge {

<L, R> Either<L, R> bridge$fireBedEvent(Either<L, R> e, BlockPos pos);

@Override
CraftPlayer bridge$getBukkitEntity();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,24 +567,14 @@ private Either<Player.BedSleepingProblem, Unit> getBedResult(BlockPos blockposit
return Either.left(Player.BedSleepingProblem.OTHER_PROBLEM);
}

@Redirect(method = "startSleepInBed", require = 0, at = @At(value = "INVOKE", remap = false, target = "Lcom/mojang/datafixers/util/Either;left(Ljava/lang/Object;)Lcom/mojang/datafixers/util/Either;"))
private <L, R> Either<L, R> arclight$failSleep(L value, BlockPos pos) {
Either<L, R> either = Either.left(value);
return arclight$fireBedEvent(either, pos);
}

@Redirect(method = "startSleepInBed", at = @At(value = "INVOKE", remap = false, target = "Lcom/mojang/datafixers/util/Either;ifRight(Ljava/util/function/Consumer;)Lcom/mojang/datafixers/util/Either;"))
private <L, R> Either<L, R> arclight$successSleep(Either<L, R> either, Consumer<? super R> consumer, BlockPos pos) {
return arclight$fireBedEvent(either, pos).ifRight(consumer);
}

@Inject(method = "startSleepInBed", require = 0, at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;setRespawnPosition(Lnet/minecraft/resources/ResourceKey;Lnet/minecraft/core/BlockPos;FZZ)V"))
private void arclight$bedCause(BlockPos p_9115_, CallbackInfoReturnable<Either<Player.BedSleepingProblem, Unit>> cir) {
this.bridge$pushChangeSpawnCause(PlayerSpawnChangeEvent.Cause.BED);
return bridge$fireBedEvent(either, pos).ifRight(consumer);
}

@SuppressWarnings("unchecked")
private <L, R> Either<L, R> arclight$fireBedEvent(Either<L, R> e, BlockPos pos) {
@Override
public <L, R> Either<L, R> bridge$fireBedEvent(Either<L, R> e, BlockPos pos) {
Either<Player.BedSleepingProblem, Unit> either = (Either<Player.BedSleepingProblem, Unit>) e;
if (either.left().orElse(null) == Player.BedSleepingProblem.OTHER_PROBLEM) {
return (Either<L, R>) either;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.izzel.arclight.common.mixin.core.server.level;

import com.mojang.datafixers.util.Either;
import io.izzel.arclight.api.ArclightPlatform;
import io.izzel.arclight.common.bridge.core.entity.player.ServerPlayerEntityBridge;
import io.izzel.arclight.common.mod.mixins.annotation.OnlyInPlatform;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.*;
import net.minecraft.util.Unit;
import net.minecraft.world.entity.player.Player;
import org.bukkit.event.player.*;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ServerPlayer.class)
@OnlyInPlatform(value = {ArclightPlatform.VANILLA, ArclightPlatform.FABRIC})
public abstract class ServerPlayerMixin_Vanilla implements ServerPlayerEntityBridge {

@Inject(method = "startSleepInBed", require = 0, at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;setRespawnPosition(Lnet/minecraft/resources/ResourceKey;Lnet/minecraft/core/BlockPos;FZZ)V"))
private void arclight$bedCause(BlockPos p_9115_, CallbackInfoReturnable<Either<Player.BedSleepingProblem, Unit>> cir) {
this.bridge$pushChangeSpawnCause(PlayerSpawnChangeEvent.Cause.BED);
}

@Redirect(method = "startSleepInBed", require = 0, at = @At(value = "INVOKE", remap = false, target = "Lcom/mojang/datafixers/util/Either;left(Ljava/lang/Object;)Lcom/mojang/datafixers/util/Either;"))
private <L, R> Either<L, R> arclight$failSleep(L value, BlockPos pos) {
Either<L, R> either = Either.left(value);
return bridge$fireBedEvent(either, pos);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package io.izzel.arclight.common.mixin.core.world.effect;

import io.izzel.arclight.api.ArclightPlatform;
import io.izzel.arclight.common.bridge.core.util.DamageSourcesBridge;
import io.izzel.arclight.common.mod.mixins.annotation.OnlyInPlatform;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageSources;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(targets = "net.minecraft.world.effect.PoisonMobEffect")
public class PoisonMobEffectMixin {
@OnlyInPlatform(value = {ArclightPlatform.VANILLA, ArclightPlatform.FABRIC, ArclightPlatform.FORGE})
public class PoisonMobEffectMixin_Vanilla {

@Redirect(method = "applyEffectTick", require = 0, at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/world/damagesource/DamageSources;magic()Lnet/minecraft/world/damagesource/DamageSource;"))
private DamageSource arclight$redirectPoison(DamageSources instance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,6 @@ public void postTick() {
}
}

@Decorate(method = "updateFluidHeightAndDoFluidPushing", require = 0, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/material/FluidState;getFlow(Lnet/minecraft/world/level/BlockGetter;Lnet/minecraft/core/BlockPos;)Lnet/minecraft/world/phys/Vec3;"))
private Vec3 arclight$setLava(FluidState instance, BlockGetter level, BlockPos pos) throws Throwable {
if (instance.getType().is(FluidTags.LAVA)) {
lastLavaContact = pos.immutable();
}
return (Vec3) DecorationOps.callsite().invoke(instance, level, pos);
}

@Override
public void bridge$setLastLavaContact(BlockPos pos) {
this.lastLavaContact = pos;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.izzel.arclight.common.mixin.core.world.entity;

import io.izzel.arclight.api.ArclightPlatform;
import io.izzel.arclight.common.bridge.core.command.ICommandSourceBridge;
import io.izzel.arclight.common.bridge.core.entity.EntityBridge;
import io.izzel.arclight.common.bridge.core.entity.InternalEntityBridge;
import io.izzel.arclight.common.mod.mixins.annotation.OnlyInPlatform;
import io.izzel.arclight.mixin.Decorate;
import io.izzel.arclight.mixin.DecorationOps;
import net.minecraft.core.BlockPos;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.entity.*;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(Entity.class)
@OnlyInPlatform(value = {ArclightPlatform.VANILLA, ArclightPlatform.FABRIC})
public abstract class EntityMixin_Vanilla implements InternalEntityBridge, EntityBridge, ICommandSourceBridge {

@Decorate(method = "updateFluidHeightAndDoFluidPushing", require = 0, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/material/FluidState;getFlow(Lnet/minecraft/world/level/BlockGetter;Lnet/minecraft/core/BlockPos;)Lnet/minecraft/world/phys/Vec3;"))
private Vec3 arclight$setLava(FluidState fluid, BlockGetter level, BlockPos pos) throws Throwable {
if (fluid.getType().is(FluidTags.LAVA)) {
bridge$setLastLavaContact(pos.immutable());
}
return (Vec3) DecorationOps.callsite().invoke(fluid, level, pos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

import io.izzel.arclight.common.bridge.core.util.DamageSourcesBridge;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.damagesource.DamageSources;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.bukkit.craftbukkit.v.event.CraftEventFactory;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -20,8 +15,6 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(SnowGolem.class)
public abstract class SnowGolemMixin extends PathfinderMobMixin {
Expand All @@ -45,12 +38,4 @@ public abstract class SnowGolemMixin extends PathfinderMobMixin {
private void arclight$forceDropOff(SoundSource pCategory, CallbackInfo ci) {
this.forceDrops = false;
}

// Forge: ShearsItem#interactLivingEntity
@Inject(method = "mobInteract", require = 0, cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/animal/SnowGolem;shear(Lnet/minecraft/sounds/SoundSource;)V"))
private void arclight$onShear(Player player, InteractionHand hand, CallbackInfoReturnable<InteractionResult> cir, ItemStack stack) {
if (!CraftEventFactory.handlePlayerShearEntityEvent(player, (Entity) (Object) this, stack, hand)) {
cir.setReturnValue(InteractionResult.PASS);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.izzel.arclight.common.mixin.core.world.entity.animal;

import com.llamalad7.mixinextras.sugar.Local;
import io.izzel.arclight.api.ArclightPlatform;
import io.izzel.arclight.common.mixin.core.world.entity.PathfinderMobMixin;
import io.izzel.arclight.common.mod.mixins.annotation.OnlyInPlatform;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.animal.SnowGolem;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.bukkit.craftbukkit.v.event.CraftEventFactory;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(SnowGolem.class)
@OnlyInPlatform(value = {ArclightPlatform.VANILLA, ArclightPlatform.FABRIC})
public abstract class SnowGolemMixin_Vanilla extends PathfinderMobMixin {

// Forge/NeoForge: ShearsItem#interactLivingEntity
@Inject(method = "mobInteract", require = 0, cancellable = true, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/animal/SnowGolem;shear(Lnet/minecraft/sounds/SoundSource;)V"))
private void arclight$onShear(Player player, InteractionHand hand, CallbackInfoReturnable<InteractionResult> cir, @Local ItemStack stack) {
if (!CraftEventFactory.handlePlayerShearEntityEvent(player, (Entity) (Object) this, stack, hand)) {
cir.setReturnValue(InteractionResult.PASS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
public abstract class ItemStackMixin implements ItemStackBridge {

// @formatter:off
@Shadow private int count;
@Shadow @Final PatchedDataComponentMap components;
@Shadow @Deprecated @Nullable private Item item;
// @formatter:on
Expand All @@ -44,32 +43,6 @@ public void convertStack(int version) {
}
}

@Decorate(method = "hurtAndBreak(ILnet/minecraft/server/level/ServerLevel;Lnet/minecraft/server/level/ServerPlayer;Ljava/util/function/Consumer;)V",
require = 0, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/enchantment/EnchantmentHelper;processDurabilityChange(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/world/item/ItemStack;I)I"))
private int arclight$itemDamage(ServerLevel serverLevel, ItemStack itemStack, int i, @Local(ordinal = 0) ServerPlayer damager) throws Throwable {
int result = (int) DecorationOps.callsite().invoke(serverLevel, itemStack, i);
if (damager != null) {
PlayerItemDamageEvent event = new PlayerItemDamageEvent(((ServerPlayerEntityBridge) damager).bridge$getBukkitEntity(), CraftItemStack.asCraftMirror((ItemStack) (Object) this), result);
event.getPlayer().getServer().getPluginManager().callEvent(event);

if (result != event.getDamage() || event.isCancelled()) {
event.getPlayer().updateInventory();
}
if (event.isCancelled()) {
return (int) DecorationOps.cancel().invoke();
}
result = event.getDamage();
}
return result;
}

@Inject(method = "hurtAndBreak(ILnet/minecraft/server/level/ServerLevel;Lnet/minecraft/server/level/ServerPlayer;Ljava/util/function/Consumer;)V", require = 0, at = @At(value = "INVOKE", target = "Ljava/util/function/Consumer;accept(Ljava/lang/Object;)V"))
private void arclight$itemBreak(int amount, ServerLevel level, @Nullable ServerPlayer serverPlayer, Consumer<Item> onBroken, CallbackInfo ci) {
if (this.count == 1 && serverPlayer != null) {
CraftEventFactory.callPlayerItemBreakEvent(serverPlayer, (ItemStack) (Object) this);
}
}

public void restorePatch(DataComponentPatch datacomponentpatch) {
this.components.restorePatch(datacomponentpatch);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.izzel.arclight.common.mixin.core.world.item;

import io.izzel.arclight.api.ArclightPlatform;
import io.izzel.arclight.common.bridge.core.entity.player.ServerPlayerEntityBridge;
import io.izzel.arclight.common.bridge.core.world.item.ItemStackBridge;
import io.izzel.arclight.common.mod.mixins.annotation.OnlyInPlatform;
import io.izzel.arclight.mixin.Decorate;
import io.izzel.arclight.mixin.DecorationOps;
import io.izzel.arclight.mixin.Local;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.bukkit.craftbukkit.v.event.CraftEventFactory;
import org.bukkit.craftbukkit.v.inventory.CraftItemStack;
import org.bukkit.event.player.PlayerItemDamageEvent;
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;

import java.util.function.Consumer;

@Mixin(ItemStack.class)
@OnlyInPlatform(value = {ArclightPlatform.VANILLA, ArclightPlatform.FABRIC})
public abstract class ItemStackMixin_Vanilla implements ItemStackBridge {

// @formatter:off
@Shadow private int count;
// @formatter:on

@Decorate(method = "hurtAndBreak(ILnet/minecraft/server/level/ServerLevel;Lnet/minecraft/server/level/ServerPlayer;Ljava/util/function/Consumer;)V",
require = 0, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/enchantment/EnchantmentHelper;processDurabilityChange(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/world/item/ItemStack;I)I"))
private int arclight$itemDamage(ServerLevel serverLevel, ItemStack itemStack, int i, @Local(ordinal = 0) ServerPlayer damager) throws Throwable {
int result = (int) DecorationOps.callsite().invoke(serverLevel, itemStack, i);
if (damager != null) {
PlayerItemDamageEvent event = new PlayerItemDamageEvent(((ServerPlayerEntityBridge) damager).bridge$getBukkitEntity(), CraftItemStack.asCraftMirror((ItemStack) (Object) this), result);
event.getPlayer().getServer().getPluginManager().callEvent(event);

if (result != event.getDamage() || event.isCancelled()) {
event.getPlayer().updateInventory();
}
if (event.isCancelled()) {
return (int) DecorationOps.cancel().invoke();
}
result = event.getDamage();
}
return result;
}

@Inject(method = "hurtAndBreak(ILnet/minecraft/server/level/ServerLevel;Lnet/minecraft/server/level/ServerPlayer;Ljava/util/function/Consumer;)V", require = 0, at = @At(value = "INVOKE", target = "Ljava/util/function/Consumer;accept(Ljava/lang/Object;)V"))
private void arclight$itemBreak(int amount, ServerLevel level, @Nullable ServerPlayer serverPlayer, Consumer<Item> onBroken, CallbackInfo ci) {
if (this.count == 1 && serverPlayer != null) {
CraftEventFactory.callPlayerItemBreakEvent(serverPlayer, (ItemStack) (Object) this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,28 +180,6 @@ private boolean processCaptures(BlockPos pos, BlockState newState, int flags) {
return true;
}

@Inject(method = "setBlock(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;II)Z", require = 0, cancellable = true, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/state/BlockState;updateNeighbourShapes(Lnet/minecraft/world/level/LevelAccessor;Lnet/minecraft/core/BlockPos;II)V"))
private void arclight$callBlockPhysics(BlockPos pos, BlockState state, int i, int j, CallbackInfoReturnable<Boolean> cir) {
try {
if (this.world != null) {
BlockPhysicsEvent event = new BlockPhysicsEvent(CraftBlock.at(this, pos), CraftBlockData.fromData(state));
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
cir.setReturnValue(true);
}
}
} catch (StackOverflowError e) {
lastPhysicsProblem = pos;
}
}

@Inject(method = "setBlock(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;II)Z", require = 0, cancellable = true, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;onBlockStateChange(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/state/BlockState;)V"))
private void arclight$preventPoiUpdate(BlockPos blockPos, BlockState blockState, int i, int j, CallbackInfoReturnable<Boolean> cir) {
if (this.preventPoiUpdated) {
cir.setReturnValue(true);
}
}

public void notifyAndUpdatePhysics(BlockPos pos, LevelChunk chunk, BlockState oldBlock, BlockState newBlock, BlockState actualBlock, int i, int j) {
this.bridge$forge$notifyAndUpdatePhysics(pos, chunk, oldBlock, newBlock, i, j);
}
Expand Down
Loading

0 comments on commit 9d075cb

Please sign in to comment.