Skip to content

Commit

Permalink
Polymorphism and trollery
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Jun 30, 2024
1 parent 8db5791 commit 23fb926
Show file tree
Hide file tree
Showing 26 changed files with 501 additions and 53 deletions.
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ repositories {
maven {
url "https://maven.enjarai.dev/releases"
}
maven {
url "https://maven.enjarai.dev/mirrors"
}

maven {
name = "Ladysnake Mods"
url = 'https://maven.ladysnake.org/releases'
}
}

loom {
Expand Down Expand Up @@ -59,6 +67,9 @@ dependencies {
modImplementation("nl.enjarai:cicada-lib:${property('deps.cicada')}") {
exclude group: "net.fabricmc.fabric-api"
}

include modImplementation("org.ladysnake.cardinal-components-api:cardinal-components-base:${property('deps.cardinal-components-api')}")
include modImplementation("org.ladysnake.cardinal-components-api:cardinal-components-entity:${property('deps.cardinal-components-api')}")
}

processResources {
Expand Down Expand Up @@ -149,6 +160,9 @@ publishMods {
requires {
slug = "lavender"
}
embeds {
slug = "cardinal-components-api"
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ git_branch=master
deps.fabric-api=0.100.1+1.21
deps.owo-lib=0.12.10+1.21
deps.lavender=0.1.10+1.21
deps.cicada=0.8.0+1.21-and-above
deps.cicada=0.8.0+1.21-and-above
deps.cardinal-components-api=6.1.0
21 changes: 21 additions & 0 deletions src/client/java/dev/enjarai/trickster/DisguiseUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.enjarai.trickster;

import com.mojang.authlib.GameProfile;
import dev.enjarai.trickster.cca.DisguiseCumponent;
import net.minecraft.client.MinecraftClient;
import org.jetbrains.annotations.Nullable;

public class DisguiseUtil {
@Nullable
public static GameProfile getGameProfile(DisguiseCumponent component) {
if (component.getUuid() == null) {
return null;
}
var result = MinecraftClient.getInstance().getSessionService()
.fetchProfile(component.getUuid(), true);
if (result == null) {
return null;
}
return result.profile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,52 @@

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.mojang.authlib.GameProfile;
import dev.enjarai.trickster.DisguiseUtil;
import dev.enjarai.trickster.Trickster;
import dev.enjarai.trickster.cca.ModCumponents;
import dev.enjarai.trickster.quack.DisguisePlayerQuack;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.client.util.SkinTextures;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.text.Text;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
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.CallbackInfoReturnable;

@Mixin(AbstractClientPlayerEntity.class)
public abstract class AbstractClientPlayerEntityMixin extends PlayerEntity {
public AbstractClientPlayerEntityMixin(World world, BlockPos pos, float yaw, GameProfile gameProfile) {
super(world, pos, yaw, gameProfile);
public abstract class AbstractClientPlayerEntityMixin extends PlayerEntityMixin implements DisguisePlayerQuack {
@Unique
@Nullable
private PlayerListEntry disguisePlayerListEntry;

protected AbstractClientPlayerEntityMixin(EntityType<? extends LivingEntity> entityType, World world) {
super(entityType, world);
}

public PlayerListEntry trickster$getApplicableEntry() {
var disguise = getComponent(ModCumponents.DISGUISE);
if (disguise.getUuid() != null) {
if (disguisePlayerListEntry == null || !disguisePlayerListEntry.getProfile().getId().equals(disguise.getUuid())) {
// Update the entry if its out of date
var profile = DisguiseUtil.getGameProfile(disguise);
if (profile == null) {
profile = new GameProfile(disguise.getUuid(), "");
}

disguisePlayerListEntry = new PlayerListEntry(profile, false);
}
} else if (disguisePlayerListEntry != null) {
// Reset the entry if we're not disguised
disguisePlayerListEntry = null;
}
return disguisePlayerListEntry;
}

@ModifyExpressionValue(
Expand All @@ -27,4 +60,24 @@ public AbstractClientPlayerEntityMixin(World world, BlockPos pos, float yaw, Gam
private boolean fixFovWhenFrozen(boolean original) {
return original || getAttributes().hasModifierForAttribute(EntityAttributes.GENERIC_MOVEMENT_SPEED, Trickster.NEGATE_ATTRIBUTE.id());
}

@Inject(
method = "getSkinTextures",
at = @At("HEAD"),
cancellable = true
)
private void disguisePlayerSkin(CallbackInfoReturnable<SkinTextures> cir) {
var entry = trickster$getApplicableEntry();
if (entry != null) {
cir.setReturnValue(entry.getSkinTextures());
}
}

@Override
protected void disguiseDisplayName(CallbackInfoReturnable<Text> cir) {
var entry = trickster$getApplicableEntry();
if (entry != null) {
cir.setReturnValue(Text.literal(entry.getProfile().getName()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.enjarai.trickster.mixin.client;

import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.world.World;
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(PlayerEntity.class)
public abstract class PlayerEntityMixin extends LivingEntity {
protected PlayerEntityMixin(EntityType<? extends LivingEntity> entityType, World world) {
super(entityType, world);
}

@Inject(
method = "getName",
at = @At("HEAD"),
cancellable = true
)
protected void disguiseDisplayName(CallbackInfoReturnable<Text> cir) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.enjarai.trickster.mixin.client;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.sugar.Local;
import com.mojang.authlib.GameProfile;
import dev.enjarai.trickster.quack.DisguisePlayerQuack;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import nl.enjarai.cicada.util.SillyHairsFeatureRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(SillyHairsFeatureRenderer.class)
public class SillyHairsFeatureRendererMixin {
@ModifyExpressionValue(
method = "render(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;ILnet/minecraft/client/network/AbstractClientPlayerEntity;FFFFFF)V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/network/AbstractClientPlayerEntity;getGameProfile()Lcom/mojang/authlib/GameProfile;"
)
)
private GameProfile fixSillyHairs(GameProfile original, @Local(argsOnly = true) AbstractClientPlayerEntity player) {
DisguisePlayerQuack theFunny = (DisguisePlayerQuack) player;
var entry = theFunny.trickster$getApplicableEntry();
if (entry != null) {
return entry.getProfile();
}
return original;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.enjarai.trickster.quack;

import net.minecraft.client.network.PlayerListEntry;

public interface DisguisePlayerQuack {
PlayerListEntry trickster$getApplicableEntry();
}
8 changes: 6 additions & 2 deletions src/client/resources/trickster.client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
"AbstractClientPlayerEntityMixin",
"HeldItemRendererMixin",
"InGameHudMixin",
"MouseMixin"
"MouseMixin",
"SillyHairsFeatureRendererMixin"
],
"injectors": {
"defaultRequire": 1
}
},
"mixins": [
"PlayerEntityMixin"
]
}
63 changes: 63 additions & 0 deletions src/main/java/dev/enjarai/trickster/cca/DisguiseCumponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dev.enjarai.trickster.cca;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.Nullable;
import org.ladysnake.cca.api.v3.component.sync.AutoSyncedComponent;

import java.util.UUID;

public class DisguiseCumponent implements AutoSyncedComponent {
private final PlayerEntity player;
private UUID targetUuid = null;

public DisguiseCumponent(PlayerEntity player) {
this.player = player;
}

@Nullable
public UUID getUuid() {
return targetUuid;
}

public void setUuid(@Nullable UUID targetUuid) {
this.targetUuid = targetUuid;
ModCumponents.DISGUISE.sync(player);
}

@Override
public void readFromNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
if (tag.contains("targetUuid")) {
targetUuid = tag.getUuid("targetUuid");
} else {
targetUuid = null;
}
}

@Override
public void writeToNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
if (targetUuid != null) {
tag.putUuid("targetUuid", targetUuid);
}
}

@Override
public void applySyncPacket(RegistryByteBuf buf) {
if (buf.readBoolean()) {
targetUuid = buf.readUuid();
} else {
targetUuid = null;
}
}

@Override
public void writeSyncPacket(RegistryByteBuf buf, ServerPlayerEntity recipient) {
buf.writeBoolean(targetUuid != null);
if (targetUuid != null) {
buf.writeUuid(targetUuid);
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/dev/enjarai/trickster/cca/ModCumponents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.enjarai.trickster.cca;

import dev.enjarai.trickster.Trickster;
import org.ladysnake.cca.api.v3.component.ComponentKey;
import org.ladysnake.cca.api.v3.component.ComponentRegistry;
import org.ladysnake.cca.api.v3.entity.EntityComponentFactoryRegistry;
import org.ladysnake.cca.api.v3.entity.EntityComponentInitializer;
import org.ladysnake.cca.api.v3.entity.RespawnCopyStrategy;

public class ModCumponents implements EntityComponentInitializer {
public static final ComponentKey<DisguiseCumponent> DISGUISE =
ComponentRegistry.getOrCreate(Trickster.id("disguise"), DisguiseCumponent.class);

@Override
public void registerEntityComponentFactories(EntityComponentFactoryRegistry registry) {
registry.registerForPlayers(DISGUISE, DisguiseCumponent::new, RespawnCopyStrategy.LOSSLESS_ONLY);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package dev.enjarai.trickster.mixin;
package dev.enjarai.trickster.mixin.event;

import com.google.common.collect.Multimaps;
import dev.enjarai.trickster.Trickster;
import dev.enjarai.trickster.particle.ModParticles;
import dev.enjarai.trickster.spell.fragment.EntityFragment;
import dev.enjarai.trickster.spell.fragment.NumberFragment;
import dev.enjarai.trickster.spell.fragment.VectorFragment;
import dev.enjarai.trickster.spell.world.SpellCircleEvent;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.AttributeContainer;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.enjarai.trickster.mixin;
package dev.enjarai.trickster.mixin.event;

import dev.enjarai.trickster.particle.ModParticles;
import dev.enjarai.trickster.spell.fragment.BlockTypeFragment;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/enjarai/trickster/spell/tricks/Tricks.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public RegistryEntry.Reference<Trick> add(RegistryKey<Trick> key, Trick value, R
public static final RaycastTrick RAYCAST = register("raycast", new RaycastTrick());
public static final RaycastEntityTrick RAYCAST_ENTITY = register("raycast_entity", new RaycastEntityTrick());
public static final AddVelocityTrick ADD_VELOCITY = register("add_velocity", new AddVelocityTrick());
public static final PolymorphTrick POLYMORPH = register("polymorph", new PolymorphTrick());
public static final DispelPolymorphTrick DISPEL_POLYMORPH = register("dispel_polymorph", new DispelPolymorphTrick());

// Math
public static final AddTrick ADD = register("add", new AddTrick());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dev.enjarai.trickster.spell.tricks.entity;

import dev.enjarai.trickster.cca.ModCumponents;
import dev.enjarai.trickster.spell.Fragment;
import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellContext;
import dev.enjarai.trickster.spell.fragment.BooleanFragment;
import dev.enjarai.trickster.spell.fragment.FragmentType;
import dev.enjarai.trickster.spell.fragment.VectorFragment;
import dev.enjarai.trickster.spell.tricks.Trick;
import dev.enjarai.trickster.spell.tricks.blunder.BlunderException;
import dev.enjarai.trickster.spell.tricks.blunder.UnknownEntityBlunder;
import net.minecraft.server.network.ServerPlayerEntity;
import org.joml.Vector3d;

import java.util.List;

public class DispelPolymorphTrick extends Trick {
public DispelPolymorphTrick() {
super(Pattern.of(1, 0, 4, 8, 7));
}

@Override
public Fragment activate(SpellContext ctx, List<Fragment> fragments) throws BlunderException {
var entity = expectInput(fragments, FragmentType.ENTITY, 0);

var realEntity = entity.getEntity(ctx).orElseThrow(() -> new UnknownEntityBlunder(this));

if (realEntity instanceof ServerPlayerEntity player) {
var cumpoonent = player.getComponent(ModCumponents.DISGUISE);

if (cumpoonent.getUuid() != null) {
cumpoonent.setUuid(null);
return BooleanFragment.TRUE;
}
}
return BooleanFragment.FALSE;
}
}
Loading

1 comment on commit 23fb926

@Benonardo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that feeling when you mixin into your own mod from a different mod

Please sign in to comment.