Skip to content

Commit

Permalink
Reduce mixins duplication in codebase (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Mar 6, 2024
1 parent 7c545fd commit 095a575
Show file tree
Hide file tree
Showing 49 changed files with 294 additions and 1,262 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,16 @@
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.entity.event;

import java.util.Iterator;
package net.legacyfabric.fabric.mixin.entity.event.versioned;

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.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.world.ServerWorld;

import net.legacyfabric.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.legacyfabric.fabric.api.entity.event.v1.ServerPlayerEvents;

@Mixin(PlayerManager.class)
Expand All @@ -39,13 +33,4 @@ abstract class PlayerManagerMixin {
private void afterRespawn(ServerPlayerEntity oldPlayer, int dimension, boolean alive, CallbackInfoReturnable<ServerPlayerEntity> cir) {
ServerPlayerEvents.AFTER_RESPAWN.invoker().afterRespawn(oldPlayer, cir.getReturnValue(), oldPlayer.server.getWorld(dimension), alive);
}

/**
* This is called by both "moveToWorld" and "teleport".
* So this is suitable to handle the after event from both call sites.
*/
@Inject(method = "teleportToDimension", at = @At("TAIL"), locals = LocalCapture.CAPTURE_FAILHARD)
private void afterWorldChanged(ServerPlayerEntity player, int dimension, CallbackInfo ci, int i, ServerWorld serverWorld, ServerWorld serverWorld2, Iterator iterator) {
ServerEntityWorldChangeEvents.AFTER_PLAYER_CHANGE_WORLD.invoker().afterChangeWorld(player, serverWorld, serverWorld2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.entity.event;
package net.legacyfabric.fabric.mixin.entity.event.versioned;

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.CallbackInfo;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.ServerPlayerEntity;

import net.legacyfabric.fabric.api.entity.event.v1.ServerEntityCombatEvents;
import net.legacyfabric.fabric.api.entity.event.v1.ServerPlayerEvents;

@Mixin(LivingEntity.class)
abstract class LivingEntityMixin {
@Inject(method = "onKilled", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;onKilledOther(Lnet/minecraft/entity/LivingEntity;)V", shift = At.Shift.AFTER))
public void onEntityKilledOther(DamageSource source, CallbackInfo ci) {
ServerEntityCombatEvents.AFTER_KILLED_OTHER_ENTITY.invoker().afterKilledOtherEntity(source.getAttacker(), (LivingEntity) (Object) this);
@Mixin(ServerPlayerEntity.class)
abstract class ServerPlayerEntityMixin {
@Inject(method = "method_14968", at = @At("TAIL"))
private void onCopyFrom(ServerPlayerEntity player, boolean alive, CallbackInfo ci) {
ServerPlayerEvents.COPY_FROM.invoker().copyFromPlayer((ServerPlayerEntity) player, (ServerPlayerEntity) (Object) this, alive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
"compatibilityLevel": "JAVA_8",
"mixins": [
"EntityMixin",
"LivingEntityMixin",
"PlayerManagerMixin",
"ServerPlayerEntityMixin"
"versioned.PlayerManagerMixin",
"versioned.ServerPlayerEntityMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,17 @@
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.entity.event;

import java.util.Iterator;
package net.legacyfabric.fabric.mixin.entity.event.versioned;

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.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.world.ServerWorld;

import net.legacyfabric.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.legacyfabric.fabric.api.entity.event.v1.ServerPlayerEvents;

@Mixin(PlayerManager.class)
Expand All @@ -40,13 +34,4 @@ abstract class PlayerManagerMixin {
private void afterRespawn(ServerPlayerEntity oldPlayer, int dimension, boolean alive, CallbackInfoReturnable<ServerPlayerEntity> cir) {
ServerPlayerEvents.AFTER_RESPAWN.invoker().afterRespawn(oldPlayer, cir.getReturnValue(), MinecraftServer.getServer().getWorld(dimension), alive);
}

/**
* This is called by both "moveToWorld" and "teleport".
* So this is suitable to handle the after event from both call sites.
*/
@Inject(method = "teleportToDimension", at = @At("TAIL"), locals = LocalCapture.CAPTURE_FAILHARD)
private void afterWorldChanged(ServerPlayerEntity player, int dimension, CallbackInfo ci, int i, ServerWorld serverWorld, ServerWorld serverWorld2, Iterator iterator) {
ServerEntityWorldChangeEvents.AFTER_PLAYER_CHANGE_WORLD.invoker().afterChangeWorld(player, serverWorld, serverWorld2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.entity.event;
package net.legacyfabric.fabric.mixin.entity.event.versioned;

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.CallbackInfo;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;

import net.legacyfabric.fabric.api.entity.event.v1.ServerEntityCombatEvents;
import net.legacyfabric.fabric.api.entity.event.v1.ServerPlayerEvents;

@Mixin(LivingEntity.class)
abstract class LivingEntityMixin {
@Inject(method = "onKilled", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;onKilledOther(Lnet/minecraft/entity/LivingEntity;)V", shift = At.Shift.AFTER))
public void onEntityKilledOther(DamageSource source, CallbackInfo ci) {
ServerEntityCombatEvents.AFTER_KILLED_OTHER_ENTITY.invoker().afterKilledOtherEntity(source.getAttacker(), (LivingEntity) (Object) this);
@Mixin(ServerPlayerEntity.class)
abstract class ServerPlayerEntityMixin {
@Inject(method = "copyFrom", at = @At("TAIL"))
private void onCopyFrom(PlayerEntity player, boolean alive, CallbackInfo ci) {
ServerPlayerEvents.COPY_FROM.invoker().copyFromPlayer((ServerPlayerEntity) player, (ServerPlayerEntity) (Object) this, alive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
"compatibilityLevel": "JAVA_8",
"mixins": [
"EntityMixin",
"LivingEntityMixin",
"PlayerManagerMixin",
"ServerPlayerEntityMixin"
"versioned.PlayerManagerMixin",
"versioned.ServerPlayerEntityMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2020 - 2024 Legacy Fabric
* Copyright (c) 2016 - 2022 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.entity.event.versioned;

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;

import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.server.PlayerManager;

import net.legacyfabric.fabric.api.entity.event.v1.ServerPlayerEvents;

@Mixin(PlayerManager.class)
abstract class PlayerManagerMixin {
@Inject(method = "respawnPlayer", at = @At("TAIL"))
private void afterRespawn(ServerPlayerEntity oldPlayer, int dimension, boolean alive, CallbackInfoReturnable<ServerPlayerEntity> cir) {
ServerPlayerEvents.AFTER_RESPAWN.invoker().afterRespawn(oldPlayer, cir.getReturnValue(), oldPlayer.server.getWorld(dimension), alive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,20 @@
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.entity.event;
package net.legacyfabric.fabric.mixin.entity.event.versioned;

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.CallbackInfo;

import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;

import net.legacyfabric.fabric.api.entity.event.v1.ServerEntityCombatEvents;
import net.legacyfabric.fabric.api.entity.event.v1.ServerPlayerEvents;

@Mixin(ServerPlayerEntity.class)
abstract class ServerPlayerEntityMixin extends LivingEntityMixin {
/**
* Minecraft by default does not call Entity#onKilledOther for a ServerPlayerEntity being killed.
* This is a Mojang bug.
* This is implements the method call on the server player entity and then calls the corresponding event.
*/
@Inject(method = "onKilled", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/ServerPlayerEntity;getOpponent()Lnet/minecraft/entity/LivingEntity;"))
private void callOnKillForPlayer(DamageSource source, CallbackInfo ci) {
final Entity attacker = source.getAttacker();

// If the damage source that killed the player was an entity, then fire the event.
if (attacker != null) {
attacker.onKilledOther((ServerPlayerEntity) (Object) this);
ServerEntityCombatEvents.AFTER_KILLED_OTHER_ENTITY.invoker().afterKilledOtherEntity(attacker, (ServerPlayerEntity) (Object) this);
}
}

abstract class ServerPlayerEntityMixin {
@Inject(method = "copyFrom", at = @At("TAIL"))
private void onCopyFrom(PlayerEntity player, boolean alive, CallbackInfo ci) {
ServerPlayerEvents.COPY_FROM.invoker().copyFromPlayer((ServerPlayerEntity) player, (ServerPlayerEntity) (Object) this, alive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
"compatibilityLevel": "JAVA_8",
"mixins": [
"EntityMixin",
"LivingEntityMixin",
"PlayerManagerMixin",
"ServerPlayerEntityMixin"
"versioned.PlayerManagerMixin",
"versioned.ServerPlayerEntityMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,16 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.world.ServerWorld;

import net.legacyfabric.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.legacyfabric.fabric.api.entity.event.v1.ServerPlayerEvents;

@Mixin(PlayerManager.class)
abstract class PlayerManagerMixin {
@Inject(method = "respawnPlayer", at = @At("TAIL"))
private void afterRespawn(ServerPlayerEntity oldPlayer, int dimension, boolean alive, CallbackInfoReturnable<ServerPlayerEntity> cir) {
ServerPlayerEvents.AFTER_RESPAWN.invoker().afterRespawn(oldPlayer, cir.getReturnValue(), oldPlayer.server.getWorld(dimension), alive);
}

public class PlayerManagerMixin {
/**
* This is called by both "moveToWorld" and "teleport".
* So this is suitable to handle the after event from both call sites.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import net.minecraft.entity.player.ServerPlayerEntity;

import net.legacyfabric.fabric.api.entity.event.v1.ServerEntityCombatEvents;
import net.legacyfabric.fabric.api.entity.event.v1.ServerPlayerEvents;

@Mixin(ServerPlayerEntity.class)
abstract class ServerPlayerEntityMixin extends LivingEntityMixin {
public class ServerPlayerEntityMixin {
/**
* Minecraft by default does not call Entity#onKilledOther for a ServerPlayerEntity being killed.
* This is a Mojang bug.
Expand All @@ -46,9 +45,4 @@ private void callOnKillForPlayer(DamageSource source, CallbackInfo ci) {
ServerEntityCombatEvents.AFTER_KILLED_OTHER_ENTITY.invoker().afterKilledOtherEntity(attacker, (ServerPlayerEntity) (Object) this);
}
}

@Inject(method = "method_14968", at = @At("TAIL"))
private void onCopyFrom(ServerPlayerEntity player, boolean alive, CallbackInfo ci) {
ServerPlayerEvents.COPY_FROM.invoker().copyFromPlayer((ServerPlayerEntity) player, (ServerPlayerEntity) (Object) this, alive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"depends": {
"minecraft": "${minecraft_version}"
},
"mixins": [],
"mixins": [
"legacy-fabric-entity-events-v1-common.mixins.json"
],
"custom": {
"modmenu": {
"badges": [ "library" ],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"required": true,
"package": "net.legacyfabric.fabric.mixin.entity.event",
"compatibilityLevel": "JAVA_8",
"mixins": [
"LivingEntityMixin",
"PlayerManagerMixin",
"ServerPlayerEntityMixin"
],
"injectors": {
"defaultRequire": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public void mouseDragged(MinecraftClient client, int mouseX, int mouseY) {
GlStateManager.disableLighting();
GlStateManager.color(1F, 1F, 1F, 1F);
} catch (NoClassDefFoundError e) { // 1.7.10-
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
}

Expand Down

0 comments on commit 095a575

Please sign in to comment.