Skip to content

Commit

Permalink
Better parrot dismounting
Browse files Browse the repository at this point in the history
  • Loading branch information
N1nn1 committed Apr 23, 2024
1 parent c18b89b commit 4997e49
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
57 changes: 57 additions & 0 deletions src/main/java/com/ninni/spawn/mixin/PlayerMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.ninni.spawn.mixin;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Abilities;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Final;
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(Player.class)
public abstract class PlayerMixin extends LivingEntity {
@Shadow private long timeEntitySatOnShoulder;
@Shadow protected abstract void respawnEntityOnShoulder(CompoundTag compoundTag);
@Shadow public abstract CompoundTag getShoulderEntityLeft();
@Shadow public abstract CompoundTag getShoulderEntityRight();
@Shadow protected abstract void setShoulderEntityLeft(CompoundTag compoundTag);
@Shadow protected abstract void setShoulderEntityRight(CompoundTag compoundTag);

@Shadow @Final private Abilities abilities;

protected PlayerMixin(EntityType<? extends LivingEntity> entityType, Level level) {
super(entityType, level);
}

@Inject(at = @At("HEAD"), method = "removeEntitiesOnShoulder", cancellable = true)
private void S$removeEntitiesOnShoulder(CallbackInfo ci) {
ci.cancel();
}

@Inject(at = @At("TAIL"), method = "aiStep")
private void S$aiStep(CallbackInfo ci) {
if (!this.level().isClientSide && this.isUnderWater() || this.abilities.flying || this.isSleeping() || this.isInPowderSnow) {
this.newRemoveEntitiesOnShoulder();
}
}

@Inject(at = @At("HEAD"), method = "jumpFromGround")
private void S$jumpFromGround(CallbackInfo ci) {
if (this.isShiftKeyDown()) this.newRemoveEntitiesOnShoulder();
}


protected void newRemoveEntitiesOnShoulder() {
if (this.timeEntitySatOnShoulder + 20L < this.level().getGameTime()) {
this.respawnEntityOnShoulder(this.getShoulderEntityLeft());
this.setShoulderEntityLeft(new CompoundTag());
this.respawnEntityOnShoulder(this.getShoulderEntityRight());
this.setShoulderEntityRight(new CompoundTag());
}
}
}
40 changes: 37 additions & 3 deletions src/main/java/com/ninni/spawn/mixin/ServerPlayerMixin.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
package com.ninni.spawn.mixin;

import com.mojang.authlib.GameProfile;
import com.ninni.spawn.client.inventory.HamsterInventoryMenu;
import com.ninni.spawn.entity.Hamster;
import com.ninni.spawn.entity.HamsterOpenContainer;
import com.ninni.spawn.mixin.accessor.PlayerAccessor;
import com.ninni.spawn.registry.SpawnVanillaIntegration;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.game.ClientboundHorseScreenOpenPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.Level;
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 org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ServerPlayer.class)
public abstract class ServerPlayerMixin implements HamsterOpenContainer {
public abstract class ServerPlayerMixin extends Player implements HamsterOpenContainer {
@Shadow protected abstract void nextContainerCounter();

@Shadow protected abstract void initMenu(AbstractContainerMenu abstractContainerMenu);

@Shadow private int containerCounter;

public ServerPlayerMixin(Level level, BlockPos blockPos, float f, GameProfile gameProfile) {
super(level, blockPos, f, gameProfile);
}

@Inject(at = @At("TAIL"), method = "die")
private void S$die(CallbackInfo ci) {
this.newRemoveEntitiesOnShoulder();
}

@Inject(at = @At("HEAD"), method = "setGameMode")
private void S$setGameMode(GameType gameType, CallbackInfoReturnable<Boolean> cir) {
if (gameType == GameType.SPECTATOR) {
this.newRemoveEntitiesOnShoulder();
}
}

@Override
public void openHamsterInventory(Hamster hamster, Container container) {
ServerPlayer $this = (ServerPlayer) (Object) this;
Expand All @@ -37,4 +62,13 @@ public void openHamsterInventory(Hamster hamster, Container container) {
$this.containerMenu = new HamsterInventoryMenu(this.containerCounter, $this.getInventory(), container, hamster);
this.initMenu($this.containerMenu);
}

protected void newRemoveEntitiesOnShoulder() {
if (((PlayerAccessor)this).getTimeEntitySatOnShoulder() + 20L < this.level().getGameTime()) {
((PlayerAccessor)this).callRespawnEntityOnShoulder(this.getShoulderEntityLeft());
this.setShoulderEntityLeft(new CompoundTag());
((PlayerAccessor)this).callRespawnEntityOnShoulder(this.getShoulderEntityRight());
this.setShoulderEntityRight(new CompoundTag());
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/ninni/spawn/mixin/accessor/PlayerAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ninni.spawn.mixin.accessor;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(Player.class)
public interface PlayerAccessor {
@Accessor
long getTimeEntitySatOnShoulder();

@Invoker
void callRespawnEntityOnShoulder(CompoundTag compoundTag);
}
2 changes: 2 additions & 0 deletions src/main/resources/spawn.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"MinecraftServerMixin",
"MobBucketItemMixin",
"OverworldBiomeBuilderMixin",
"accessor.PlayerAccessor",
"PlayerMixin",
"ServerPlayerMixin",
"accessor.NoiseGeneratorSettingsAccessor",
"accessor.TropicalFishAccessor"
Expand Down

0 comments on commit 4997e49

Please sign in to comment.