Skip to content

Commit

Permalink
feat: add support for multipart entity (close #364)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snownee committed Feb 15, 2024
1 parent 674a8be commit 30fd623
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/main/java/snownee/jade/api/EntityAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public interface EntityAccessor extends Accessor<EntityHitResult> {

Entity getEntity();

/**
* For part entity like ender dragon's, getEntity() will return the parent entity.
*/
//TODO 1.21: remove default implementation
default Entity getRawEntity() {
return getEntity();
}

@Override
default Class<? extends Accessor<?>> getAccessorType() {
return EntityAccessor.class;
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/snownee/jade/impl/EntityAccessorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ public static void handleRequest(SyncData data, ServerPlayer player, Consumer<Ru
@Deprecated
public void toNetwork(FriendlyByteBuf buf) {
buf.writeBoolean(showDetails());
buf.writeVarInt(entity.get().getId());
Entity entity = getEntity();
buf.writeVarInt(entity.getId());
if (CommonProxy.isMultipartEntity(entity)) {
buf.writeVarInt(CommonProxy.getPartEntityIndex(entity));
} else {
buf.writeVarInt(-1);
}
Vec3 hitVec = getHitResult().getLocation();
buf.writeFloat((float) hitVec.x);
buf.writeFloat((float) hitVec.y);
Expand All @@ -73,6 +79,11 @@ public void toNetwork(FriendlyByteBuf buf) {

@Override
public Entity getEntity() {
return CommonProxy.wrapPartEntityParent(getRawEntity());
}

@Override
public Entity getRawEntity() {
return entity.get();
}

Expand Down Expand Up @@ -176,13 +187,13 @@ public EntityAccessor build() {
}
}

public record SyncData(boolean showDetails, int id, Vec3 hitVec) {
public record SyncData(boolean showDetails, int id, int partIndex, Vec3 hitVec) {
public SyncData(EntityAccessor accessor) {
this(accessor.showDetails(), accessor.getEntity().getId(), accessor.getHitResult().getLocation());
this(accessor.showDetails(), accessor.getEntity().getId(), CommonProxy.getPartEntityIndex(accessor.getRawEntity()), accessor.getHitResult().getLocation());
}

public SyncData(FriendlyByteBuf buffer) {
this(buffer.readBoolean(), buffer.readInt(), new Vec3(buffer.readFloat(), buffer.readFloat(), buffer.readFloat()));
this(buffer.readBoolean(), buffer.readVarInt(), buffer.readVarInt(), new Vec3(buffer.readFloat(), buffer.readFloat(), buffer.readFloat()));
}

public void write(FriendlyByteBuf buffer) {
Expand All @@ -194,7 +205,7 @@ public void write(FriendlyByteBuf buffer) {
}

public EntityAccessor unpack(ServerPlayer player) {
Supplier<Entity> entity = Suppliers.memoize(() -> player.level().getEntity(id));
Supplier<Entity> entity = Suppliers.memoize(() -> CommonProxy.getPartEntity(player.level().getEntity(id), partIndex));
return new EntityAccessorImpl.Builder()
.level(player.level())
.player(player)
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/snownee/jade/overlay/RayTracing.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import snownee.jade.impl.ObjectDataCenter;
import snownee.jade.impl.WailaClientRegistration;
import snownee.jade.impl.ui.ItemStackElement;
import snownee.jade.util.CommonProxy;

public class RayTracing {

Expand Down Expand Up @@ -171,6 +172,8 @@ private boolean canBeTarget(Entity target, Entity viewEntity) {
return false;
if (target instanceof Projectile projectile && projectile.tickCount <= 10 && !target.level().tickRateManager().isEntityFrozen(target))
return false;
if (CommonProxy.isMultipartEntity(target) && !target.isPickable())
return false;
if (viewEntity instanceof Player player) {
if (target.isInvisibleTo(player))
return false;
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/snownee/jade/util/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import net.neoforged.neoforge.common.Tags;
import net.neoforged.neoforge.common.UsernameCache;
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.entity.PartEntity;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.fluids.FluidStack;
Expand Down Expand Up @@ -94,7 +95,7 @@ public CommonProxy(IEventBus modBus) {

private void registerPayloadHandlers(RegisterPayloadHandlerEvent event) {
event.registrar(Jade.MODID)
.versioned("1")
.versioned("2")
.optional()
.play(Identifiers.PACKET_RECEIVE_DATA, ReceiveDataPacket::read, handlers -> handlers.client(ReceiveDataPacket::handle))
.play(Identifiers.PACKET_SERVER_PING, ServerPingPacket::read, handlers -> handlers.client(ServerPingPacket::handle))
Expand Down Expand Up @@ -352,4 +353,41 @@ private void loadComplete(FMLLoadCompleteEvent event) {
}
Jade.loadComplete();
}

public static boolean isMultipartEntity(Entity target) {
return target.isMultipartEntity();
}

public static Entity wrapPartEntityParent(Entity target) {
if (target instanceof PartEntity<?> part) {
return part.getParent();
}
return target;
}

public static int getPartEntityIndex(Entity entity) {
if (!(entity instanceof PartEntity<?> part)) {
return -1;
}
Entity parent = wrapPartEntityParent(entity);
PartEntity<?>[] parts = parent.getParts();
if (parts == null) {
return -1;
}
return List.of(parts).indexOf(part);
}

public static Entity getPartEntity(Entity parent, int index) {
if (parent == null) {
return null;
}
if (index < 0) {
return parent;
}
PartEntity<?>[] parts = parent.getParts();
if (parts == null || index >= parts.length) {
return parent;
}
return parts[index];
}
}

0 comments on commit 30fd623

Please sign in to comment.