Skip to content

Commit

Permalink
Tweak mixins to bypass mixin bug (???)
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Mar 12, 2023
1 parent 6a07cd0 commit abcd79c
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 41 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ allprojects {
}
}

afterEvaluate {
afterEvaluate {
if (project.version != "0.0.0-alpha.0") {
publishing {
publications {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric_version=0.74.2+1.19.4

maven_group = eu.pb4

mod_version = 0.4.0-pre.2
mod_version = 0.4.0-pre.3

minecraft_version_supported = ">=1.19.4-"

Expand Down
32 changes: 19 additions & 13 deletions polymer-core/src/testmod/java/eu/pb4/polymertest/TestEntity3.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import eu.pb4.polymer.virtualentity.api.attachment.EntityAttachment;
import eu.pb4.polymer.virtualentity.api.elements.InteractionElement;
import eu.pb4.polymer.virtualentity.api.elements.ItemDisplayElement;
import eu.pb4.polymer.virtualentity.api.elements.MobAnchorElement;
import eu.pb4.polymer.virtualentity.api.tracker.EntityTrackedData;
import eu.pb4.polymertest.mixin.EntityAccessor;
import it.unimi.dsi.fastutil.ints.IntArrayList;
Expand Down Expand Up @@ -39,6 +40,8 @@ public class TestEntity3 extends CreeperEntity implements PolymerEntity {
private final ItemDisplayElement rightLeg = new ItemDisplayElement(Items.RED_CONCRETE);
private final ItemDisplayElement torso = new ItemDisplayElement(PolymerUtils.createPlayerHead("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZjYyYzQ4NWIxODg2ZGJjZTZjMWNhZDE0MGMwZWY4NzYzNTU5ZDQzYTc4NTY0NDY2NGM2ZDVmMzZlMjc1NGVlOCJ9fX0="));
private final InteractionElement interaction = InteractionElement.redirect(this);
private final MobAnchorElement rideAnchor = new MobAnchorElement();

private Matrix4x3fStack stack = new Matrix4x3fStack(8);
private float previousSpeed = Float.MIN_NORMAL;
private float previousLimbPos = Float.MIN_NORMAL;
Expand All @@ -49,26 +52,26 @@ public TestEntity3(EntityType<TestEntity3> entityEntityType, World world) {
this.holder = new ElementHolder() {
@Override
protected void startWatchingExtraPackets(ServerPlayNetworkHandler player, Consumer<Packet<ClientPlayPacketListener>> packetConsumer) {
if (!TestEntity3.this.hasPassengers()) {
packetConsumer.accept(VirtualEntityUtils.createRidePacket(TestEntity3.this.getId(), this.getEntityIds()));
}
packetConsumer.accept(VirtualEntityUtils.createRidePacket(TestEntity3.this.getId(), IntList.of(
TestEntity3.this.leftLeg.getEntityId(),
TestEntity3.this.rightLeg.getEntityId(),
TestEntity3.this.torso.getEntityId(),
TestEntity3.this.interaction.getEntityId()
)));
}

@Override
protected void updatePosition() {
this.currentPos = this.getAttachment().getPos();
protected void notifyElementsOfPositionUpdate(Vec3d newPos, Vec3d delta) {
TestEntity3.this.rideAnchor.notifyMove(this.currentPos, newPos, delta);
}

@Override
public Vec3d getPos() {
return this.getAttachment().getPos();
}
};
this.rideAnchor.setOffset(new Vec3d(0, 1.3f, 0));

this.holder.addElement(interaction);
this.holder.addElement(leftLeg);
this.holder.addElement(rightLeg);
this.holder.addElement(torso);
leftLeg.setInterpolationDuration(2);
rightLeg.setInterpolationDuration(2);
torso.setInterpolationDuration(2);
Expand All @@ -77,6 +80,12 @@ public Vec3d getPos() {
torso.setModelTransformation(ModelTransformationMode.FIXED);
this.interaction.setSize(1.1f, 1.5f);
this.updateAnimation();

this.holder.addElement(interaction);
this.holder.addElement(leftLeg);
this.holder.addElement(rightLeg);
this.holder.addElement(torso);
this.holder.addElement(rideAnchor);
this.attachment = new EntityAttachment(this.holder, this, false);
}

Expand Down Expand Up @@ -153,10 +162,7 @@ public Packet<ClientPlayPacketListener> createSpawnPacket() {
@Override
public void onEntityPacketSent(Consumer<Packet<?>> consumer, Packet<?> packet) {
if (packet instanceof EntityPassengersSetS2CPacket passengersSetS2CPacket) {
var list = new IntArrayList();
list.addAll(IntList.of(passengersSetS2CPacket.getPassengerIds()));
list.addAll(this.holder.getEntityIds());
consumer.accept(VirtualEntityUtils.createRidePacket(passengersSetS2CPacket.getId(), list));
consumer.accept(VirtualEntityUtils.createRidePacket(this.rideAnchor.getEntityId(), IntList.of(passengersSetS2CPacket.getPassengerIds())));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,17 @@ protected void updatePosition() {

if (!this.currentPos.equals(newPos)) {
var delta = newPos.subtract(newPos);
for (var e : this.elements) {
e.notifyMove(this.currentPos, newPos, delta);
}
this.notifyElementsOfPositionUpdate(newPos, delta);
this.currentPos = newPos;
}
}

protected void notifyElementsOfPositionUpdate(Vec3d newPos, Vec3d delta) {
for (var e : this.elements) {
e.notifyMove(this.currentPos, newPos, delta);
}
}

public void sendPacket(Packet<ClientPlayPacketListener> packet) {
for (var player : players) {
player.sendPacket(packet);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package eu.pb4.polymer.virtualentity.api.elements;

import eu.pb4.polymer.virtualentity.api.tracker.EntityTrackedData;
import eu.pb4.polymer.virtualentity.mixin.SlimeEntityAccessor;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;

public class MobAnchorElement extends GenericEntityElement {
public MobAnchorElement() {
this.dataTracker.set(SlimeEntityAccessor.getSLIME_SIZE(), 0);
this.dataTracker.set(EntityTrackedData.SILENT, true);
this.dataTracker.set(EntityTrackedData.NO_GRAVITY, true);
this.dataTracker.set(EntityTrackedData.FLAGS, (byte) ((1 << EntityTrackedData.INVISIBLE_FLAG_INDEX)));
}

@Override
protected EntityType<? extends Entity> getEntityType() {
return EntityType.SLIME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eu.pb4.polymer.virtualentity.impl;

import eu.pb4.polymer.virtualentity.api.elements.DisplayElement;
import eu.pb4.polymer.virtualentity.api.elements.VirtualElement;
import net.minecraft.network.packet.c2s.play.PlayerInteractEntityC2SPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Hand;
import net.minecraft.util.math.Vec3d;

public record PacketInterHandler(ServerPlayerEntity player, VirtualElement.InteractionHandler interactionHandler) implements PlayerInteractEntityC2SPacket.Handler {
@Override
public void interact(Hand hand) {
interactionHandler.interact(player, hand);
}

@Override
public void interactAt(Hand hand, Vec3d pos) {
interactionHandler.interactAt(player, hand, pos);
}

@Override
public void attack() {
interactionHandler.attack(player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@

import eu.pb4.polymer.virtualentity.api.ElementHolder;
import eu.pb4.polymer.virtualentity.impl.HolderHolder;
import eu.pb4.polymer.virtualentity.impl.PacketInterHandler;
import net.minecraft.entity.Entity;
import net.minecraft.network.packet.c2s.play.PlayerInteractEntityC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Hand;
import net.minecraft.util.math.Vec3d;
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.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -55,34 +53,19 @@ public class ServerPlayNetworkHandlerMixin implements HolderHolder {
}
}

@Inject(method = "onPlayerInteractEntity", at = @At("TAIL"), locals = LocalCapture.CAPTURE_FAILSOFT)
private void polymerVE$interactWithHologram(PlayerInteractEntityC2SPacket packet, CallbackInfo ci, ServerWorld world, Entity entity) {
@ModifyVariable(method = "onPlayerInteractEntity", at = @At(value = "STORE", ordinal = 0))
private Entity polymerVE$onInteract(Entity entity, PlayerInteractEntityC2SPacket packet) {
if (entity == null) {
var id = ((PlayerInteractEntityC2SPacketAccessor) packet).getEntityId();
for (var x : this.polymerVE$holders) {
if (x.isPartOf(id)) {
var i = x.getInteraction(id, this.player);
if (i != null) {
packet.handle(new PlayerInteractEntityC2SPacket.Handler() {
@Override
public void interact(Hand hand) {
i.interact(player, hand);
}

@Override
public void interactAt(Hand hand, Vec3d pos) {
i.interactAt(player, hand, pos);
}

@Override
public void attack() {
i.attack(player);
}
});
packet.handle(new PacketInterHandler(this.player, i));
}
return;
}
}
}
return entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package eu.pb4.polymer.virtualentity.mixin;

import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.mob.SlimeEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(SlimeEntity.class)
public interface SlimeEntityAccessor {
@Accessor
static TrackedData<Integer> getSLIME_SIZE() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"PlayerInteractEntityC2SPacketAccessor",
"ServerPlayerEntityMixin",
"ServerPlayNetworkHandlerMixin",
"SlimeEntityAccessor",
"accessors.BlockDisplayEntityAccessor",
"accessors.DisplayEntityAccessor",
"accessors.EntityAccessor",
Expand Down

0 comments on commit abcd79c

Please sign in to comment.