Skip to content

Commit

Permalink
Fix menu opening injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed Sep 27, 2024
1 parent 5e985b7 commit e123734
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 12 deletions.
2 changes: 1 addition & 1 deletion forge/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ description=The SpongeAPI implementation for MinecraftForge
forgeVersion=52.0.3
loom.platform=forge
fabric.loom.dontRemap=true
mixinConfigs=mixins.spongeforge.accessors.json,mixins.spongeforge.api.json,mixins.spongeforge.inventory.json,mixins.spongeforge.core.json,mixins.spongeforge.core.shared.json,mixins.spongeforge.tracker.json
mixinConfigs=mixins.spongeforge.accessors.json,mixins.spongeforge.api.json,mixins.spongeforge.inventory.json,mixins.spongeforge.inventory.shared.json,mixins.spongeforge.core.json,mixins.spongeforge.core.shared.json,mixins.spongeforge.tracker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"required": true,
"parent": "mixins.sponge.parent.json",
"package": "org.spongepowered.common.mixin.inventory",
"priority": 1300,
"mixins": [
"event.server.level.ServerPlayerMixin_Shared_Inventory"
],
"overwrites": {
"conformVisibility": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.neoforge.mixin.inventory.event.server.level;

import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.objectweb.asm.Opcodes;
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;
import org.spongepowered.common.event.tracking.PhaseTracker;
import org.spongepowered.common.event.tracking.context.transaction.EffectTransactor;

import java.util.OptionalInt;
import java.util.function.Consumer;

@Mixin(ServerPlayer.class)
public class ServerPlayerMixin_Inventory_Neo {
@Nullable private Object inventory$menuProvider;

@Inject(
method = "openMenu(Lnet/minecraft/world/MenuProvider;Ljava/util/function/Consumer;)Ljava/util/OptionalInt;",
at = @At(
value = "FIELD",
target = "Lnet/minecraft/server/level/ServerPlayer;containerMenu:Lnet/minecraft/world/inventory/AbstractContainerMenu;",
opcode = Opcodes.PUTFIELD,
shift = At.Shift.AFTER
)
)
private void impl$afterOpenMenu(final CallbackInfoReturnable<OptionalInt> cir) {
PhaseTracker.SERVER.getPhaseContext().getTransactor().logContainerSet((ServerPlayer) (Object) this);
}

@Inject(method = "openMenu(Lnet/minecraft/world/MenuProvider;Ljava/util/function/Consumer;)Ljava/util/OptionalInt;", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;initMenu(Lnet/minecraft/world/inventory/AbstractContainerMenu;)V"))
private void impl$onOpenMenu(MenuProvider menuProvider, Consumer<RegistryFriendlyByteBuf> extraDataWriter, CallbackInfoReturnable<OptionalInt> cir) {
this.inventory$menuProvider = menuProvider;
}

@Redirect(
method = "openMenu(Lnet/minecraft/world/MenuProvider;Ljava/util/function/Consumer;)Ljava/util/OptionalInt;",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/MenuProvider;createMenu(ILnet/minecraft/world/entity/player/Inventory;Lnet/minecraft/world/entity/player/Player;)Lnet/minecraft/world/inventory/AbstractContainerMenu;"
)
)
private AbstractContainerMenu impl$transactMenuCreationWithEffect(
final MenuProvider menuProvider, final int containerCounter, final net.minecraft.world.entity.player.Inventory inventory,
final Player player
) {
try (final EffectTransactor ignored = PhaseTracker.SERVER.getPhaseContext()
.getTransactor()
.logOpenInventory((ServerPlayer) (Object) this)
) {
return menuProvider.createMenu(containerCounter, inventory, player);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"target": "@env(DEFAULT)",
"priority": 1300,
"mixins": [
"event.server.level.ServerPlayerMixin_Inventory_Neo",
"event.world.level.block.entity.HopperBlockEntityMixin_Inventory_Neo"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

import java.util.OptionalInt;

// TODO NeoForge
// Forge and Vanilla
@Mixin(ServerPlayer.class)
public class ServerPlayerMixin_Shared_Inventory {
Expand All @@ -55,15 +54,13 @@ public class ServerPlayerMixin_Shared_Inventory {
shift = At.Shift.AFTER
)
)
private void impl$afterOpenMenu(final MenuProvider param0, final CallbackInfoReturnable<OptionalInt> cir) {
PhaseTracker.SERVER.getPhaseContext()
.getTransactor()
.logContainerSet((ServerPlayer) (Object) this);
private void impl$afterOpenMenu(final CallbackInfoReturnable<OptionalInt> cir) {
PhaseTracker.SERVER.getPhaseContext().getTransactor().logContainerSet((ServerPlayer) (Object) this);
}

@Inject(method = "openMenu", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;initMenu(Lnet/minecraft/world/inventory/AbstractContainerMenu;)V"))
private void impl$onOpenMenu(final MenuProvider $$0, final CallbackInfoReturnable<OptionalInt> cir) {
this.inventory$menuProvider = $$0;
private void impl$onOpenMenu(final MenuProvider menuProvider, final CallbackInfoReturnable<OptionalInt> cir) {
this.inventory$menuProvider = menuProvider;
}

@Redirect(
Expand All @@ -74,14 +71,14 @@ public class ServerPlayerMixin_Shared_Inventory {
)
)
private AbstractContainerMenu impl$transactMenuCreationWithEffect(
final MenuProvider menuProvider, final int var1, final net.minecraft.world.entity.player.Inventory var2,
final Player var3
final MenuProvider menuProvider, final int containerCounter, final net.minecraft.world.entity.player.Inventory inventory,
final Player player
) {
try (final EffectTransactor ignored = PhaseTracker.SERVER.getPhaseContext()
.getTransactor()
.logOpenInventory((ServerPlayer) (Object) this)
) {
return menuProvider.createMenu(var1, var2, var3);
return menuProvider.createMenu(containerCounter, inventory, player);
}
}
}
2 changes: 1 addition & 1 deletion vanilla/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ name=SpongeVanilla
implementation=Vanilla
description=The SpongeAPI implementation for Vanilla Minecraft

mixinConfigs=mixins.spongevanilla.accessors.json,mixins.spongevanilla.api.json,mixins.spongevanilla.core.json,mixins.spongevanilla.core.shared.json,mixins.spongevanilla.inventory.json,mixins.spongevanilla.tracker.json
mixinConfigs=mixins.spongevanilla.accessors.json,mixins.spongevanilla.api.json,mixins.spongevanilla.core.json,mixins.spongevanilla.core.shared.json,mixins.spongevanilla.inventory.json,mixins.spongevanilla.inventory.shared.json,mixins.spongevanilla.tracker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"required": true,
"parent": "mixins.sponge.parent.json",
"package": "org.spongepowered.common.mixin.inventory",
"priority": 1200,
"mixins": [
"event.server.level.ServerPlayerMixin_Shared_Inventory"
],
"overwrites": {
"conformVisibility": true
}
}

0 comments on commit e123734

Please sign in to comment.