Fix some events

This commit is contained in:
shedaniel
2021-01-09 20:14:27 +08:00
parent 12b3ffb909
commit cd5d68e40c
4 changed files with 15 additions and 4 deletions

View File

@@ -71,7 +71,7 @@ public interface PlayerEvent {
}
interface CraftItem {
void craft(Player player, ItemStack smelted, Container inventory);
void craft(Player player, ItemStack constructed, Container inventory);
}
interface SmeltItem {

View File

@@ -26,6 +26,7 @@ import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@@ -35,9 +36,13 @@ public abstract class MixinItemEntity {
@Shadow
public abstract ItemStack getItem();
@Unique
private ItemStack cache;
@Inject(method = "playerTouch",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/ItemStack;getCount()I"), cancellable = true)
private void prePickup(Player player, CallbackInfo ci) {
cache = getItem().copy();
InteractionResult canPickUp = PlayerEvent.PICKUP_ITEM_PRE.invoker().canPickup(player, (ItemEntity) (Object) this, getItem());
if (canPickUp == InteractionResult.FAIL) {
ci.cancel();
@@ -47,6 +52,10 @@ public abstract class MixinItemEntity {
@Inject(method = "playerTouch",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;take(Lnet/minecraft/world/entity/Entity;I)V"))
private void pickup(Player player, CallbackInfo ci) {
PlayerEvent.PICKUP_ITEM_POST.invoker().pickup(player, (ItemEntity) (Object) this, getItem());
if (cache != null) {
PlayerEvent.PICKUP_ITEM_POST.invoker().pickup(player, (ItemEntity) (Object) this, cache);
}
this.cache = null;
}
}

View File

@@ -48,7 +48,7 @@ public class MixinPlayer {
@Inject(method = "drop(Lnet/minecraft/world/item/ItemStack;ZZ)Lnet/minecraft/world/entity/item/ItemEntity;", at = @At("RETURN"), cancellable = true)
private void drop(ItemStack itemStack, boolean bl, boolean bl2, CallbackInfoReturnable<ItemEntity> cir) {
if (PlayerEvent.DROP_ITEM.invoker().drop((Player) (Object) this, cir.getReturnValue()) == InteractionResult.FAIL) {
if (cir.getReturnValue() != null && PlayerEvent.DROP_ITEM.invoker().drop((Player) (Object) this, cir.getReturnValue()) == InteractionResult.FAIL) {
cir.setReturnValue(null);
}
}

View File

@@ -44,7 +44,9 @@ public class MixinMinecraft {
@Inject(method = "clearLevel(Lnet/minecraft/client/gui/screens/Screen;)V",
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/chat/NarratorChatListener;clear()V"))
private void handleLogin(Screen screen, CallbackInfo ci) {
ClientPlayerEvent.CLIENT_PLAYER_QUIT.invoker().quit(player);
if (player != null) {
ClientPlayerEvent.CLIENT_PLAYER_QUIT.invoker().quit(player);
}
}
@Inject(method = "startUseItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/ItemStack;isEmpty()Z", ordinal = 1),