idk
This commit is contained in:
commit
428877fbf6
1072 files changed
+160209
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package com.ticxo.modelengine.v1_19_R3;
|
||||
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils.ReflectionEnum;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.control.MoveControl;
|
||||
import net.minecraft.world.level.Level;
|
||||
|
||||
public enum NMSFields implements ReflectionEnum {
|
||||
ENTITY_dimensions(Entity.class, "be", "dimensions"),
|
||||
ENTITY_eyeHeight(Entity.class, "bf", "eyeHeight"),
|
||||
LIVING_ENTITY_noJumpDelay(LivingEntity.class, "bY", "noJumpDelay"),
|
||||
LIVING_ENTITY_jumping(LivingEntity.class, "bi", "jumping"),
|
||||
MOB_lookControl(Mob.class, "bJ", "lookControl"),
|
||||
MOB_moveControl(Mob.class, "bK", "moveControl"),
|
||||
MOB_navigation(Mob.class, "bM", "navigation"),
|
||||
MOB_goalSelector(Mob.class, "bN", "goalSelector"),
|
||||
MOB_bodyRotationControl(Mob.class, "bS", "bodyRotationControl"),
|
||||
SERVER_GAME_PACKET_LISTENER_IMPL_clientIsFloating(ServerGamePacketListenerImpl.class, "G", "clientIsFloating"),
|
||||
SERVER_GAME_PACKET_LISTENER_IMPL_connection(ServerGamePacketListenerImpl.class, "h", "connection"),
|
||||
MOVE_CONTROL_operation(MoveControl.class, "k", "operation"),
|
||||
TRACKED_ENTITY_serverEntity(TrackedEntity.class, "b", "serverEntity"),
|
||||
TRACKED_ENTITY_range(TrackedEntity.class, "d", "range"),
|
||||
LEVEL_threadSafeRandom(Level.class, "C", "threadSafeRandom"),
|
||||
CLIENTBOUND_MOVE_ENTITY_PACKET_entityId(ClientboundMoveEntityPacket.class, "a", "entityId"),
|
||||
CLIENTBOUND_ROTATE_HEAD_PACKET_entityId(ClientboundRotateHeadPacket.class, "a", "entityId");
|
||||
|
||||
private final Class<?> target;
|
||||
private final String obfuscated;
|
||||
private final String mapped;
|
||||
|
||||
private NMSFields(Class<?> target, String obfuscated, String mapped) {
|
||||
this.target = target;
|
||||
this.obfuscated = obfuscated;
|
||||
this.mapped = mapped;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getTarget() {
|
||||
return this.target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getObfuscated() {
|
||||
return this.obfuscated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMapped() {
|
||||
return this.mapped;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package com.ticxo.modelengine.v1_19_R3;
|
||||
|
||||
import com.mojang.math.MatrixUtil;
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.ServerInfo;
|
||||
import com.ticxo.modelengine.api.entity.data.BukkitEntityData;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.PivotOverride;
|
||||
import com.ticxo.modelengine.api.model.PivotOverrideRegistry;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.LeashRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.NameTagRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SegmentRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SubHitboxRenderer;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayRenderer;
|
||||
import com.ticxo.modelengine.api.model.render.ModelRenderer;
|
||||
import com.ticxo.modelengine.api.model.render.ModelRendererParser;
|
||||
import com.ticxo.modelengine.api.nms.NMSHandler;
|
||||
import com.ticxo.modelengine.api.nms.RenderParsers;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.nms.network.NetworkHandler;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXDisplayRenderer;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXRenderer;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXRendererParser;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.EntityHandlerImpl;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.NetworkHandlerImpl;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.parser.behavior.LeashParser;
|
||||
import com.ticxo.modelengine.v1_19_R3.parser.behavior.MountParser;
|
||||
import com.ticxo.modelengine.v1_19_R3.parser.behavior.NameTagParser;
|
||||
import com.ticxo.modelengine.v1_19_R3.parser.behavior.SegmentParser;
|
||||
import com.ticxo.modelengine.v1_19_R3.parser.behavior.SubHitboxParser;
|
||||
import com.ticxo.modelengine.v1_19_R3.parser.model.DisplayParser;
|
||||
import com.ticxo.modelengine.v1_19_R3.parser.vfx.VFXDisplayParser;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import org.apache.commons.lang3.tuple.Triple;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.util.Transformation;
|
||||
import org.joml.Matrix3f;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Quaternionfc;
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector3fc;
|
||||
|
||||
public class NMSHandler_v1_19_R3 implements NMSHandler {
|
||||
private final EntityHandler entityHandler = new EntityHandlerImpl();
|
||||
private final NetworkHandler networkHandler = new NetworkHandlerImpl();
|
||||
private final RenderParsers globalParsers = this.createParsers();
|
||||
private final VFXDisplayParser vfxDisplayParser = new VFXDisplayParser();
|
||||
|
||||
@Override
|
||||
public RenderParsers createParsers() {
|
||||
RenderParsers parsers = new RenderParsers();
|
||||
parsers.registerModelParser(renderer -> renderer instanceof DisplayRenderer, DisplayParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof MountRenderer, MountParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof LeashRenderer, LeashParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof NameTagRenderer, NameTagParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof SubHitboxRenderer, SubHitboxParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof SegmentRenderer, SegmentParser::new);
|
||||
return parsers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPivotOverrides() {
|
||||
PivotOverrideRegistry registry = ModelEngineAPI.getPivotOverrideRegistry();
|
||||
|
||||
for (PivotOverride override : registry.getAll()) {
|
||||
ModeledEntity modeledEntity = ModelEngineAPI.getModeledEntity(override.getUuid());
|
||||
BukkitEntityData data = modeledEntity != null && modeledEntity.getBase().getData() instanceof BukkitEntityData d ? d : null;
|
||||
boolean passengersDirty = override.getPassengers().isDirty();
|
||||
Set<UUID> lateTrackers = data != null ? data.getStartTracking() : Set.of();
|
||||
if (passengersDirty || !lateTrackers.isEmpty()) {
|
||||
FriendlyByteBuf byteBuf = NetworkUtils.createByteBuf(buf -> {
|
||||
buf.writeVarInt(override.getId());
|
||||
Collection<Integer> list = override.getPassengers().get();
|
||||
ArrayList<Integer> allPassengers = new ArrayList<>(list);
|
||||
if (modeledEntity != null) {
|
||||
List<Entity> basePassengers = modeledEntity.getBase().getPassengers();
|
||||
int[] snapshot = new int[basePassengers.size()];
|
||||
int i = 0;
|
||||
|
||||
for (Entity passenger : basePassengers) {
|
||||
int pid = passenger.getEntityId();
|
||||
snapshot[i++] = pid;
|
||||
if (!allPassengers.contains(pid)) {
|
||||
allPassengers.add(pid);
|
||||
}
|
||||
}
|
||||
|
||||
override.setBasePassengerSnapshot(snapshot);
|
||||
} else {
|
||||
for (int pid : override.getBasePassengerSnapshot()) {
|
||||
if (!allPassengers.contains(pid)) {
|
||||
allPassengers.add(pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buf.writeVarInt(allPassengers.size());
|
||||
allPassengers.forEach(buf::writeVarInt);
|
||||
});
|
||||
Set<UUID> recipients;
|
||||
if (data == null) {
|
||||
recipients = ServerInfo.getOnlinePlayers();
|
||||
} else if (passengersDirty) {
|
||||
recipients = new HashSet<>(data.getMutableTracking().keySet());
|
||||
recipients.addAll(lateTrackers);
|
||||
} else {
|
||||
recipients = lateTrackers;
|
||||
}
|
||||
|
||||
NetworkUtils.send(recipients, new ClientboundSetPassengersPacket(byteBuf));
|
||||
override.getPassengers().clearDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends ModelRenderer> ModelRendererParser<T> getModelRendererParser(T renderer) {
|
||||
return this.globalParsers.getModelParser(renderer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends BehaviorRenderer> BehaviorRendererParser<T> getBehaviorRendererParser(T renderer) {
|
||||
return this.globalParsers.getBehaviorParser(renderer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends VFXRenderer> VFXRendererParser<T> getVFXRendererParser(T renderer) {
|
||||
return renderer instanceof VFXDisplayRenderer ? (VFXRendererParser<T>)(Object)this.vfxDisplayParser : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMountPassengerUpdate(Set<UUID> targets, int mountId, Collection<Integer> passengerIds) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(mountId);
|
||||
buf.writeVarInt(passengerIds.size());
|
||||
passengerIds.forEach(buf::writeVarInt);
|
||||
ClientboundSetPassengersPacket packet = new ClientboundSetPassengersPacket(buf);
|
||||
|
||||
for (UUID target : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlushNow(packet));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Transformation decompose(Matrix4f transform) {
|
||||
float f = 1.0F / transform.m33();
|
||||
Triple<Quaternionf, Vector3f, Quaternionf> triple = MatrixUtil.svdDecompose(new Matrix3f(transform).scale(f));
|
||||
return new Transformation(
|
||||
transform.getTranslation(new Vector3f()).mul(f),
|
||||
new Quaternionf((Quaternionfc)triple.getLeft()),
|
||||
new Vector3f((Vector3fc)triple.getMiddle()),
|
||||
new Quaternionf((Quaternionfc)triple.getRight())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityHandler getEntityHandler() {
|
||||
return this.entityHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkHandler getNetworkHandler() {
|
||||
return this.networkHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RenderParsers getGlobalParsers() {
|
||||
return this.globalParsers;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.ticxo.modelengine.v1_19_R3;
|
||||
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils.MethodEnum;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.world.item.InstrumentItem;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public enum NMSMethods implements MethodEnum {
|
||||
TRACKED_ENTITY_getEffectiveRange(TrackedEntity.class, "b", "getEffectiveRange"),
|
||||
SERVER_GAME_PACKET_LISTENER_IMPL_checkLimit(ServerGamePacketListenerImpl.class, "checkLimit", "checkLimit", long.class),
|
||||
INSTRUMENT_ITEM_getInstrument(InstrumentItem.class, "d", "getInstrument", ItemStack.class);
|
||||
|
||||
private final Class<?> target;
|
||||
private final String obfuscated;
|
||||
private final String mapped;
|
||||
private final Class<?>[] parameterClasses;
|
||||
|
||||
private NMSMethods(Class<?> target, String obfuscated, String mapped, Class<?>... parameterClasses) {
|
||||
this.target = target;
|
||||
this.obfuscated = obfuscated;
|
||||
this.mapped = mapped;
|
||||
this.parameterClasses = parameterClasses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getTarget() {
|
||||
return this.target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getObfuscated() {
|
||||
return this.obfuscated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMapped() {
|
||||
return this.mapped;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?>[] getParameterClasses() {
|
||||
return this.parameterClasses;
|
||||
}
|
||||
}
|
||||
+687
@@ -0,0 +1,687 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.ServerInfo;
|
||||
import com.ticxo.modelengine.api.entity.BaseEntity;
|
||||
import com.ticxo.modelengine.api.entity.Hitbox;
|
||||
import com.ticxo.modelengine.api.entity.data.BukkitEntityData;
|
||||
import com.ticxo.modelengine.api.interaction.DynamicHitbox;
|
||||
import com.ticxo.modelengine.api.model.bone.ModelBone;
|
||||
import com.ticxo.modelengine.api.model.bone.type.SubHitbox;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityState;
|
||||
import com.ticxo.modelengine.api.nms.entity.HitboxEntity;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler.BoxRelToCam;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler.InteractionResult;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.BodyRotationController;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.LookController;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.MoveController;
|
||||
import com.ticxo.modelengine.api.nms.impl.TempTrackedEntity;
|
||||
import com.ticxo.modelengine.api.utils.RaceConditionUtil;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.api.utils.config.ConfigProperty;
|
||||
import com.ticxo.modelengine.api.utils.config.DebugToggle;
|
||||
import com.ticxo.modelengine.api.utils.logger.TLogger;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.api.utils.promise.Promise;
|
||||
import com.ticxo.modelengine.v1_19_R3.NMSFields;
|
||||
import com.ticxo.modelengine.v1_19_R3.NMSMethods;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.controller.BodyRotationControlWrapper;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.controller.LookControlWrapper;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.controller.MoveControlWrapper;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.hitbox.HitboxEntityImpl;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.navigation.AmphibiousNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.navigation.FlyingNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.navigation.GroundNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.navigation.WallClimberNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.navigation.WaterBoundNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.patch.PatchedServerGamePacketListener;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.Packets;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSoundEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundUseItemPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.damagesource.DamageSource;
|
||||
import net.minecraft.world.entity.EntityDimensions;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.MoverType;
|
||||
import net.minecraft.world.entity.ai.goal.Goal;
|
||||
import net.minecraft.world.entity.ai.goal.GoalSelector;
|
||||
import net.minecraft.world.entity.ai.goal.WrappedGoal;
|
||||
import net.minecraft.world.entity.ai.navigation.AmphibiousPathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.WallClimberNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.WaterBoundPathNavigation;
|
||||
import net.minecraft.world.item.Instrument;
|
||||
import net.minecraft.world.item.InstrumentItem;
|
||||
import net.minecraft.world.item.UseAnim;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Particle;
|
||||
import org.bukkit.Particle.DustOptions;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.CraftWorld;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftPlayer;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.inventory.CraftItemStack;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.util.CraftLocation;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.util.CraftMagicNumbers;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.BoundingBox;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class EntityHandlerImpl implements EntityHandler {
|
||||
private static final MethodHandle JUMPING_GETTER = createJumpingGetter();
|
||||
private final Set<UUID> forceInvisible = ConcurrentHashMap.newKeySet();
|
||||
private double forceRenderWidth;
|
||||
private double forceRenderHeight;
|
||||
|
||||
private static MethodHandle createJumpingGetter() {
|
||||
Field field = ReflectionUtils.getField(NMSFields.LIVING_ENTITY_jumping);
|
||||
if (field == null) {
|
||||
return null;
|
||||
} else {
|
||||
try {
|
||||
return MethodHandles.lookup().unreflectGetter(field);
|
||||
} catch (IllegalAccessException var2) {
|
||||
var2.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public EntityHandlerImpl() {
|
||||
ModelEngineAPI.getAPI().getConfigManager().registerReferenceUpdate(this::updateConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateConfig() {
|
||||
this.forceRenderWidth = ConfigProperty.BLOCK_CULL_IGNORE_SIZE_WIDTH.getDouble();
|
||||
this.forceRenderHeight = ConfigProperty.BLOCK_CULL_IGNORE_SIZE_HEIGHT.getDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextEntityId() {
|
||||
return CraftMagicNumbers.INSTANCE.nextEntityId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHitbox(Entity entity, @NotNull Hitbox hitbox) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
EntityDimensions box = new EntityDimensions((float)hitbox.getMaxWidth(), (float)hitbox.getHeight(), true);
|
||||
ReflectionUtils.set(nms, NMSFields.ENTITY_dimensions, box);
|
||||
ReflectionUtils.set(nms, NMSFields.ENTITY_eyeHeight, (float)hitbox.getEyeHeight());
|
||||
nms.setBoundingBox(box.makeBoundingBox(nms.position()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStepHeight(Entity entity, double height) {
|
||||
EntityUtils.nms(entity).setMaxUpStep((float)height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getStepHeight(Entity entity) {
|
||||
return EntityUtils.nms(entity).maxUpStep();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Entity entity, double x, double y, double z) {
|
||||
EntityUtils.nms(entity).setPos(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void movePassenger(Entity entity, double x, double y, double z) {
|
||||
net.minecraft.world.entity.Entity nmsEntity = EntityUtils.nms(entity);
|
||||
double seatY = y + nmsEntity.getMyRidingOffset();
|
||||
nmsEntity.setPos(x, seatY, z);
|
||||
nmsEntity.setDeltaMovement(Vec3.ZERO);
|
||||
nmsEntity.resetFallDistance();
|
||||
if (nmsEntity instanceof ServerPlayer player) {
|
||||
ReflectionUtils.set(player.connection, NMSFields.SERVER_GAME_PACKET_LISTENER_IMPL_clientIsFloating, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forceSpawn(BaseEntity<?> entity, Player player) {
|
||||
if (player != null) {
|
||||
if (entity.getData() instanceof BukkitEntityData bukkitEntityData) {
|
||||
bukkitEntityData.getTracked().sendPairingData(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forceDespawn(BaseEntity<?> entity, Player player) {
|
||||
if (player != null) {
|
||||
NetworkUtils.send(player.getUniqueId(), new ClientboundRemoveEntitiesPacket(new int[]{entity.getEntityId()}));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setForcedInvisible(Entity entity, boolean flag) {
|
||||
if (this.isForcedInvisible(entity) != flag) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
byte data = 0;
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
data = TMath.setBit(data, i, nms.getSharedFlag(i));
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
this.forceInvisible.add(entity.getUniqueId());
|
||||
data = TMath.setBit(data, 5, true);
|
||||
} else {
|
||||
this.forceInvisible.remove(entity.getUniqueId());
|
||||
}
|
||||
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(entity.getEntityId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, data);
|
||||
buf.writeByte(255);
|
||||
ClientboundSetEntityDataPacket packet = new ClientboundSetEntityDataPacket(buf);
|
||||
NetworkUtils.send(ServerInfo.getOnlinePlayers(), packet);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isForcedInvisible(UUID uuid) {
|
||||
return this.forceInvisible.contains(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearForcedInvisible(UUID uuid) {
|
||||
this.forceInvisible.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyRotationController wrapBodyRotationControl(Entity entity, Supplier<BodyRotationController> def) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
BodyRotationControlWrapper var5 = new BodyRotationControlWrapper(mob);
|
||||
return (BodyRotationController)(ReflectionUtils.set(mob, NMSFields.MOB_bodyRotationControl, var5) ? var5 : def.get());
|
||||
} else {
|
||||
return def.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MoveController wrapMoveController(Entity entity, Supplier<MoveController> def) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
if (mob.getMoveControl() instanceof MoveController controller) {
|
||||
return controller;
|
||||
} else {
|
||||
MoveControlWrapper controller = new MoveControlWrapper(mob, mob.getMoveControl());
|
||||
return (MoveController)(ReflectionUtils.set(mob, NMSFields.MOB_moveControl, controller) ? controller : def.get());
|
||||
}
|
||||
} else {
|
||||
return def.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LookController wrapLookController(Entity entity, Supplier<LookController> def) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
if (mob.getLookControl() instanceof LookController controller) {
|
||||
return controller;
|
||||
} else {
|
||||
LookControlWrapper controller = new LookControlWrapper(mob, mob.getLookControl());
|
||||
return (LookController)(ReflectionUtils.set(mob, NMSFields.MOB_lookControl, controller) ? controller : def.get());
|
||||
}
|
||||
} else {
|
||||
return def.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wrapNavigation(Entity entity) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
try {
|
||||
Field navField = ReflectionUtils.getField(NMSFields.MOB_navigation);
|
||||
PathNavigation oldNav = mob.getNavigation();
|
||||
PathNavigation newNav;
|
||||
if (oldNav instanceof WallClimberNavigation wallClimberNavigation) {
|
||||
newNav = new WallClimberNavigationWrapper(mob, wallClimberNavigation);
|
||||
} else if (oldNav instanceof GroundPathNavigation groundPathNavigation) {
|
||||
newNav = new GroundNavigationWrapper(mob, groundPathNavigation);
|
||||
} else if (oldNav instanceof FlyingPathNavigation flyingPathNavigation) {
|
||||
newNav = new FlyingNavigationWrapper(mob, flyingPathNavigation);
|
||||
} else if (oldNav instanceof WaterBoundPathNavigation) {
|
||||
newNav = new WaterBoundNavigationWrapper(mob);
|
||||
} else {
|
||||
if (!(oldNav instanceof AmphibiousPathNavigation amphibiousPathNavigation)) {
|
||||
TLogger.warn("Failed to create custom navigation for " + mob.getType() + ": " + mob.getUUID());
|
||||
TLogger.warn("Reason: Navigation class type is " + oldNav.getClass().getSimpleName() + ".");
|
||||
return;
|
||||
}
|
||||
|
||||
newNav = new AmphibiousNavigationWrapper(mob, amphibiousPathNavigation);
|
||||
}
|
||||
|
||||
navField.set(mob, newNav);
|
||||
GoalSelector goalSelector = (GoalSelector)ReflectionUtils.getField(NMSFields.MOB_goalSelector).get(mob);
|
||||
RaceConditionUtil.wrapConmod(() -> this.replaceNavigation(goalSelector, newNav));
|
||||
} catch (IllegalAccessException var10) {
|
||||
var10.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void replaceNavigation(GoalSelector goalSelector, PathNavigation newNav) {
|
||||
try {
|
||||
for (WrappedGoal wrappedGoal : goalSelector.getAvailableGoals()) {
|
||||
Goal goal = wrappedGoal.getGoal();
|
||||
|
||||
for (Field field : goal.getClass().getDeclaredFields()) {
|
||||
field = ReflectionUtils.getField(goal.getClass(), field.getName());
|
||||
Object f = field.get(goal);
|
||||
if (f instanceof PathNavigation) {
|
||||
field.set(goal, newNav);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException var11) {
|
||||
var11.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public HitboxEntity createHitbox(Location location, ModelBone bone, SubHitbox subHitbox) {
|
||||
ServerLevel level = ((CraftWorld)location.getWorld()).getHandle();
|
||||
HitboxEntityImpl entity = new HitboxEntityImpl(level, bone, subHitbox);
|
||||
entity.queueLocation(new Vector3f().set(location.getX(), location.getY(), location.getZ()));
|
||||
entity.setPos(location.getX(), location.getY(), location.getZ());
|
||||
ModelEngineAPI.setRenderCanceled(entity.getId(), true);
|
||||
Promise.start(entity.getBukkitEntity()).thenRunSync(() -> {
|
||||
level.addFreshEntity(entity);
|
||||
ModelEngineAPI.getInteractionTracker().addHitbox(entity);
|
||||
});
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public HitboxEntity castHitbox(Entity entity) {
|
||||
return EntityUtils.nms(entity) instanceof HitboxEntity hitbox ? hitbox : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hurt(Entity entity, Object source, float amount) {
|
||||
if (source instanceof DamageSource damageSource) {
|
||||
return EntityUtils.nms(entity).hurt(damageSource, amount);
|
||||
} else {
|
||||
throw new RuntimeException("Passed in source is not an NMS DamageSource.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InteractionResult interact(Entity entity, HumanEntity player, EquipmentSlot hand) {
|
||||
if (EntityUtils.nms(entity) instanceof LivingEntity livingEntity) {
|
||||
net.minecraft.world.InteractionResult result = livingEntity.interact(
|
||||
((CraftPlayer)player).getHandle(), hand == EquipmentSlot.HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND
|
||||
);
|
||||
|
||||
return switch (result) {
|
||||
case SUCCESS -> InteractionResult.SUCCESS;
|
||||
case CONSUME -> InteractionResult.CONSUME;
|
||||
case CONSUME_PARTIAL -> InteractionResult.CONSUME_PARTIAL;
|
||||
case PASS -> InteractionResult.PASS;
|
||||
case FAIL -> InteractionResult.FAIL;
|
||||
default -> throw new IncompatibleClassChangeError();
|
||||
};
|
||||
} else {
|
||||
return InteractionResult.FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spawnDynamicHitbox(DynamicHitbox hitbox) {
|
||||
Vector location = hitbox.getPositionTracker().get();
|
||||
final Packets.PacketSupplier pivotSpawn = NetworkUtils.createPivotSpawn(
|
||||
DynamicHitbox.getPivotId(), DynamicHitbox.getPivotUUID(), location.toVector3f().add(0.0F, -0.5202F, 0.0F)
|
||||
);
|
||||
FriendlyByteBuf pivotDataBuf = NetworkUtils.createByteBuf();
|
||||
pivotDataBuf.writeVarInt(DynamicHitbox.getPivotId());
|
||||
EntityUtils.writeData(pivotDataBuf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(pivotDataBuf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
pivotDataBuf.writeByte(255);
|
||||
final ClientboundSetEntityDataPacket pivotData = new ClientboundSetEntityDataPacket(pivotDataBuf);
|
||||
final ClientboundAddEntityPacket hitboxSpawn = new ClientboundAddEntityPacket(
|
||||
DynamicHitbox.getHitboxId(),
|
||||
DynamicHitbox.getHitboxUUID(),
|
||||
location.getX(),
|
||||
location.getY() - 0.5202,
|
||||
location.getZ(),
|
||||
0.0F,
|
||||
0.0F,
|
||||
EntityType.SLIME,
|
||||
0,
|
||||
Vec3.ZERO,
|
||||
0.0
|
||||
);
|
||||
FriendlyByteBuf hitboxDataBuf = NetworkUtils.createByteBuf();
|
||||
hitboxDataBuf.writeVarInt(DynamicHitbox.getHitboxId());
|
||||
EntityUtils.writeData(hitboxDataBuf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(hitboxDataBuf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(hitboxDataBuf, 16, EntityDataSerializers.INT, 2);
|
||||
hitboxDataBuf.writeByte(255);
|
||||
final ClientboundSetEntityDataPacket hitboxData = new ClientboundSetEntityDataPacket(hitboxDataBuf);
|
||||
FriendlyByteBuf mountBuf = NetworkUtils.createByteBuf();
|
||||
mountBuf.writeVarInt(DynamicHitbox.getPivotId());
|
||||
mountBuf.writeVarInt(1);
|
||||
mountBuf.writeVarInt(DynamicHitbox.getHitboxId());
|
||||
final ClientboundSetPassengersPacket mount = new ClientboundSetPassengersPacket(mountBuf);
|
||||
NetworkUtils.sendBundled(Set.of(hitbox.getPlayer().getUniqueId()), new Packets() {
|
||||
{
|
||||
this.add(pivotSpawn);
|
||||
this.add(pivotData);
|
||||
this.add(hitboxSpawn);
|
||||
this.add(hitboxData);
|
||||
this.add(mount);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDynamicHitbox(DynamicHitbox hitbox) {
|
||||
Vector3f vector = hitbox.getPositionTracker().get().toVector3f().add(0.0F, -0.5202F, 0.0F);
|
||||
NetworkUtils.send(
|
||||
hitbox.getPlayer().getUniqueId(), NetworkUtils.createPivotTeleport(DynamicHitbox.getPivotId(), vector).supply(hitbox.getPlayer().getUniqueId())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyDynamicHitbox(DynamicHitbox hitbox) {
|
||||
ClientboundRemoveEntitiesPacket destroy = new ClientboundRemoveEntitiesPacket(new int[]{DynamicHitbox.getHitboxId(), DynamicHitbox.getPivotId()});
|
||||
NetworkUtils.send(hitbox.getPlayer().getUniqueId(), destroy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forceUseItem(Player player, EquipmentSlot hand) {
|
||||
ItemStack stack = player.getEquipment().getItem(hand);
|
||||
net.minecraft.world.item.ItemStack nmsStack = ((CraftItemStack)stack).handle;
|
||||
ServerPlayer nmsPlayer = (ServerPlayer)EntityUtils.nms(player);
|
||||
ServerGamePacketListenerImpl connection = nmsPlayer.connection;
|
||||
ServerboundUseItemPacket useItemPacket = new ServerboundUseItemPacket(
|
||||
hand == EquipmentSlot.HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND, 0
|
||||
);
|
||||
useItemPacket.timestamp = System.currentTimeMillis();
|
||||
PatchedServerGamePacketListener.handleUseItem(
|
||||
useItemPacket,
|
||||
connection,
|
||||
interactionResult -> {
|
||||
if (nmsStack.getUseAnimation() != UseAnim.NONE && interactionResult == net.minecraft.world.InteractionResult.CONSUME) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(player.getEntityId());
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.BYTE, (byte)(hand == EquipmentSlot.HAND ? 1 : 3));
|
||||
buf.writeByte(255);
|
||||
ClientboundSetEntityDataPacket packet = new ClientboundSetEntityDataPacket(buf);
|
||||
NetworkUtils.send(player.getUniqueId(), packet);
|
||||
if (nmsStack.getItem() instanceof InstrumentItem instrumentItem) {
|
||||
Optional<? extends Holder<Instrument>> optional = ReflectionUtils.call(instrumentItem, NMSMethods.INSTRUMENT_ITEM_getInstrument, nmsStack);
|
||||
optional.ifPresent(
|
||||
instrumentHolder -> {
|
||||
Instrument instrument = (Instrument)instrumentHolder.value();
|
||||
Holder<SoundEvent> soundEvent = instrument.soundEvent();
|
||||
float f = instrument.range() / 16.0F;
|
||||
RandomSource random = ReflectionUtils.get(nmsPlayer.level, NMSFields.LEVEL_threadSafeRandom);
|
||||
ClientboundSoundEntityPacket soundPacket = new ClientboundSoundEntityPacket(
|
||||
soundEvent, SoundSource.RECORDS, nmsPlayer, f, 1.0F, random.nextLong()
|
||||
);
|
||||
NetworkUtils.send(player.getUniqueId(), soundPacket);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYRot(Entity entity) {
|
||||
return EntityUtils.nms(entity).getYRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYHeadRot(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
return nms instanceof LivingEntity livingEntity ? livingEntity.getYHeadRot() : nms.getYRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getXHeadRot(Entity entity) {
|
||||
return EntityUtils.nms(entity).getXRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYBodyRot(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
return nms instanceof LivingEntity livingEntity ? livingEntity.yBodyRot : nms.getYRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYRot(Entity entity, float angle) {
|
||||
EntityUtils.nms(entity).setYRot(angle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYHeadRot(Entity entity, float angle) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
if (nms instanceof LivingEntity livingEntity) {
|
||||
livingEntity.setYHeadRot(angle);
|
||||
} else {
|
||||
nms.setYRot(angle);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setXHeadRot(Entity entity, float angle) {
|
||||
EntityUtils.nms(entity).setXRot(angle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYBodyRot(Entity entity, float angle) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
if (nms instanceof LivingEntity livingEntity) {
|
||||
livingEntity.setYBodyRot(angle);
|
||||
} else {
|
||||
nms.setYRot(angle);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(Entity entity, double x, double y, double z) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
nms.move(MoverType.SELF, new Vec3(x, y, z));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWalking(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
if (nms.tickCount < 1) {
|
||||
return false;
|
||||
} else {
|
||||
double dX = nms.getX() - nms.xOld;
|
||||
double dZ = nms.getZ() - nms.zOld;
|
||||
return dX * dX + dZ * dZ > 2.5000003E-7F;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStrafing(Entity entity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isJumping(Entity entity) {
|
||||
if (JUMPING_GETTER != null && EntityUtils.nms(entity) instanceof LivingEntity livingEntity) {
|
||||
try {
|
||||
return (boolean)JUMPING_GETTER.invokeExact((LivingEntity)livingEntity);
|
||||
} catch (Throwable var4) {
|
||||
var4.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFlying(Entity entity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getHealth(Entity entity) {
|
||||
return EntityUtils.nms(entity) instanceof LivingEntity livingEntity ? livingEntity.getHealth() : 20.0F;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMaxHealth(Entity entity) {
|
||||
return EntityUtils.nms(entity) instanceof LivingEntity livingEntity ? livingEntity.getMaxHealth() : 20.0F;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector3f getPivotOffset(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nmsEntity = EntityUtils.nms(entity);
|
||||
return new Vector3f(0.0F, (float)nmsEntity.getPassengersRidingOffset(), 0.0F);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRemoved(Entity entity) {
|
||||
return EntityUtils.nms(entity).isRemoved();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBoundingBox(Entity entity, BoundingBox dest) {
|
||||
AABB nms = EntityUtils.nms(entity).getBoundingBox();
|
||||
dest.resize(nms.minX, nms.minY, nms.minZ, nms.maxX, nms.maxY, nms.maxZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getGlowColor(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
return nms.getTeamColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDeathTick(Entity entity, int tick) {
|
||||
if (EntityUtils.nms(entity) instanceof LivingEntity livingEntity) {
|
||||
livingEntity.deathTime = tick;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public com.ticxo.modelengine.api.nms.entity.wrapper.TrackedEntity wrapTrackedEntity(Entity entity) {
|
||||
ServerLevel level = ((CraftWorld)entity.getWorld()).getHandle();
|
||||
Int2ObjectMap<TrackedEntity> map = level.getChunkSource().chunkMap.entityMap;
|
||||
int entityId = entity.getEntityId();
|
||||
TrackedEntity tracker = (TrackedEntity)map.get(entityId);
|
||||
return (com.ticxo.modelengine.api.nms.entity.wrapper.TrackedEntity)(tracker == null
|
||||
? new TempTrackedEntity(entity)
|
||||
: new TrackedEntityImpl(entity, () -> {
|
||||
ServerLevel lvl = ((CraftWorld)entity.getWorld()).getHandle();
|
||||
Int2ObjectMap<TrackedEntity> m = lvl.getChunkSource().chunkMap.entityMap;
|
||||
return m == null ? null : (TrackedEntity)m.get(entity.getEntityId());
|
||||
}, tracker));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldCull(Player player, Location eyePosition, BoundingBox box) {
|
||||
CraftWorld world = (CraftWorld)player.getWorld();
|
||||
Vec3 start = CraftLocation.toVec3D(eyePosition);
|
||||
if (!(box.getWidthX() >= this.forceRenderWidth) && !(box.getWidthZ() >= this.forceRenderWidth) && !(box.getHeight() >= this.forceRenderHeight)) {
|
||||
int minX = Mth.floor(box.getMinX());
|
||||
int minY = Mth.floor(box.getMinY());
|
||||
int minZ = Mth.floor(box.getMinZ());
|
||||
int maxX = Mth.ceil(box.getMaxX()) - 1;
|
||||
int maxY = Mth.ceil(box.getMaxY()) - 1;
|
||||
int maxZ = Mth.ceil(box.getMaxZ()) - 1;
|
||||
BoxRelToCam relX = BoxRelToCam.from(minX, maxX, Mth.floor(start.x));
|
||||
BoxRelToCam relY = BoxRelToCam.from(minY, maxY, Mth.floor(start.y));
|
||||
BoxRelToCam relZ = BoxRelToCam.from(minZ, maxZ, Mth.floor(start.z));
|
||||
if (relX == BoxRelToCam.INSIDE && relY == BoxRelToCam.INSIDE && relZ == BoxRelToCam.INSIDE) {
|
||||
return false;
|
||||
} else {
|
||||
if (DebugToggle.isDebugging(DebugToggle.SHOW_CULL_POINTS)) {
|
||||
EntityHandler.sweepCullPoints(minX, minY, minZ, maxX, maxY, maxZ, relX, relY, relZ, (x, y, z) -> {
|
||||
world.spawnParticle(Particle.REDSTONE, x, y, z, 1, new DustOptions(Color.RED, 0.2F));
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
return !EntityHandler.sweepCullPoints(minX, minY, minZ, maxX, maxY, maxZ, relX, relY, relZ, (x, y, z) -> isVisible(world, start, new Vec3(x, y, z)));
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isVisible(CraftWorld world, Vec3 startPos, Vec3 endPos) {
|
||||
return OcclusionClipContext.isClear(world.getHandle(), startPos, endPos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readEntityState(Entity entity, EntityState state) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
state.yRot = nms.getYRot();
|
||||
state.xHeadRot = nms.getXRot();
|
||||
if (nms instanceof LivingEntity living) {
|
||||
state.yHeadRot = living.getYHeadRot();
|
||||
state.yBodyRot = living.yBodyRot;
|
||||
state.health = living.getHealth();
|
||||
} else {
|
||||
state.yHeadRot = state.yRot;
|
||||
state.yBodyRot = state.yRot;
|
||||
state.health = 20.0F;
|
||||
}
|
||||
|
||||
boolean walking = false;
|
||||
if (nms.tickCount >= 1) {
|
||||
double dX = nms.getX() - nms.xOld;
|
||||
double dZ = nms.getZ() - nms.zOld;
|
||||
walking = dX * dX + dZ * dZ > 2.5000003E-7F;
|
||||
}
|
||||
|
||||
state.walking = walking;
|
||||
state.strafing = false;
|
||||
state.flying = false;
|
||||
state.removed = nms.isRemoved();
|
||||
state.jumping = this.isJumping(entity);
|
||||
state.glowing = this.isGlowing(entity);
|
||||
state.onFire = this.isVisualFire(entity);
|
||||
this.getBoundingBox(entity, state.boundingBox);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity;
|
||||
|
||||
public class EntityMetadataID {
|
||||
public static final int ENTITY_SHARED_DATA = 0;
|
||||
public static final int ENTITY_AIR = 1;
|
||||
public static final int ENTITY_CUSTOM_NAME = 2;
|
||||
public static final int ENTITY_CUSTOM_NAME_VISIBLE = 3;
|
||||
public static final int INTERACTION_WIDTH = 8;
|
||||
public static final int INTERACTION_HEIGHT = 9;
|
||||
public static final int INTERACTION_RESPONSE = 10;
|
||||
public static final int DISPLAY_INTERPOLATION_DELAY = 8;
|
||||
public static final int DISPLAY_TRANSFORM_DURATION = 9;
|
||||
public static final int DISPLAY_TRANSLATION = 10;
|
||||
public static final int DISPLAY_SCALE = 11;
|
||||
public static final int DISPLAY_ROTATION_LEFT = 12;
|
||||
public static final int DISPLAY_ROTATION_RIGHT = 13;
|
||||
public static final int DISPLAY_BILLBOARD = 14;
|
||||
public static final int DISPLAY_BRIGHTNESS = 15;
|
||||
public static final int DISPLAY_VIEW_RANGE = 16;
|
||||
public static final int DISPLAY_SHADOW_RADIUS = 17;
|
||||
public static final int DISPLAY_WIDTH = 19;
|
||||
public static final int DISPLAY_HEIGHT = 20;
|
||||
public static final int DISPLAY_GLOW_COLOR = 21;
|
||||
public static final int ITEM_DISPLAY_ITEM = 22;
|
||||
public static final int ITEM_DISPLAY_TYPE = 23;
|
||||
public static final int TEXT_DISPLAY_TEXT = 23;
|
||||
public static final int TEXT_LINE_WIDTH = 24;
|
||||
public static final int TEXT_DISPLAY_BACKGROUND = 25;
|
||||
public static final int TEXT_DISPLAY_OPACITY = 26;
|
||||
public static final int TEXT_DISPLAY_OPTIONS = 27;
|
||||
public static final int AREA_EFFECT_CLOUD_RADIUS = 8;
|
||||
public static final int LIVING_ENTITY_HAND_DATA = 8;
|
||||
public static final int LIVING_ENTITY_HEALTH = 9;
|
||||
public static final int ARMOR_STAND_SHARED_DATA = 15;
|
||||
public static final int SLIME_SIZE = 16;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity;
|
||||
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.syncher.EntityDataSerializer;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftEntity;
|
||||
|
||||
public class EntityUtils {
|
||||
public static Entity nms(org.bukkit.entity.Entity entity) {
|
||||
return ((CraftEntity)entity).getHandle();
|
||||
}
|
||||
|
||||
public static <T> void writeData(FriendlyByteBuf byteBuf, int id, EntityDataSerializer<T> serializer, T val) {
|
||||
int serializedId = EntityDataSerializers.getSerializedId(serializer);
|
||||
byteBuf.writeByte(id);
|
||||
byteBuf.writeVarInt(serializedId);
|
||||
serializer.write(byteBuf, val);
|
||||
}
|
||||
|
||||
public static void writeDataUnsafe(FriendlyByteBuf byteBuf, int id, EntityDataSerializer serializer, Object val) {
|
||||
int serializedId = EntityDataSerializers.getSerializedId(serializer);
|
||||
byteBuf.writeByte(id);
|
||||
byteBuf.writeVarInt(serializedId);
|
||||
serializer.write(byteBuf, val);
|
||||
}
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.BlockPos.MutableBlockPos;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.ClipContext;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.ClipContext.Block;
|
||||
import net.minecraft.world.level.ClipContext.Fluid;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.ChunkAccess;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraft.world.phys.shapes.Shapes;
|
||||
import net.minecraft.world.phys.shapes.VoxelShape;
|
||||
|
||||
public class OcclusionClipContext extends ClipContext {
|
||||
public OcclusionClipContext(Vec3 start, Vec3 end) {
|
||||
super(start, end, Block.VISUAL, Fluid.NONE, null);
|
||||
}
|
||||
|
||||
public VoxelShape getBlockShape(BlockState state, BlockGetter world, BlockPos pos) {
|
||||
return occlusionShape(state, world, pos);
|
||||
}
|
||||
|
||||
private static VoxelShape occlusionShape(BlockState state, BlockGetter world, BlockPos pos) {
|
||||
return state.isOpaque() && state.getOcclusionShape(world, pos) == Shapes.block() ? Shapes.block() : Shapes.empty();
|
||||
}
|
||||
|
||||
static boolean isClear(Level level, Vec3 from, Vec3 to) {
|
||||
if (from.equals(to)) {
|
||||
return true;
|
||||
} else {
|
||||
double toX = Mth.lerp(-1.0E-7, to.x, from.x);
|
||||
double toY = Mth.lerp(-1.0E-7, to.y, from.y);
|
||||
double toZ = Mth.lerp(-1.0E-7, to.z, from.z);
|
||||
double fromX = Mth.lerp(-1.0E-7, from.x, to.x);
|
||||
double fromY = Mth.lerp(-1.0E-7, from.y, to.y);
|
||||
double fromZ = Mth.lerp(-1.0E-7, from.z, to.z);
|
||||
int x = Mth.floor(fromX);
|
||||
int y = Mth.floor(fromY);
|
||||
int z = Mth.floor(fromZ);
|
||||
double dx = toX - fromX;
|
||||
double dy = toY - fromY;
|
||||
double dz = toZ - fromZ;
|
||||
int signX = Mth.sign(dx);
|
||||
int signY = Mth.sign(dy);
|
||||
int signZ = Mth.sign(dz);
|
||||
double tDeltaX = signX == 0 ? Double.MAX_VALUE : signX / dx;
|
||||
double tDeltaY = signY == 0 ? Double.MAX_VALUE : signY / dy;
|
||||
double tDeltaZ = signZ == 0 ? Double.MAX_VALUE : signZ / dz;
|
||||
double tX = tDeltaX * (signX > 0 ? 1.0 - Mth.frac(fromX) : Mth.frac(fromX));
|
||||
double tY = tDeltaY * (signY > 0 ? 1.0 - Mth.frac(fromY) : Mth.frac(fromY));
|
||||
double tZ = tDeltaZ * (signZ > 0 ? 1.0 - Mth.frac(fromZ) : Mth.frac(fromZ));
|
||||
MutableBlockPos pos = new MutableBlockPos(x, y, z);
|
||||
ChunkAccess chunk = null;
|
||||
int chunkX = 0;
|
||||
int chunkZ = 0;
|
||||
|
||||
while (true) {
|
||||
pos.set(x, y, z);
|
||||
BlockState state;
|
||||
if (level.captureTreeGeneration) {
|
||||
state = level.getBlockStateIfLoaded(pos);
|
||||
} else if (level.isOutsideBuildHeight(pos)) {
|
||||
state = Blocks.VOID_AIR.defaultBlockState();
|
||||
} else {
|
||||
if (chunk == null || x >> 4 != chunkX || z >> 4 != chunkZ) {
|
||||
chunkX = x >> 4;
|
||||
chunkZ = z >> 4;
|
||||
chunk = level.getChunkIfLoadedImmediately(chunkX, chunkZ);
|
||||
}
|
||||
|
||||
state = chunk == null ? null : chunk.getBlockState(pos);
|
||||
}
|
||||
|
||||
if (state == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!state.isAir()) {
|
||||
VoxelShape shape = occlusionShape(state, level, pos);
|
||||
if (!shape.isEmpty()) {
|
||||
BlockHitResult hit = level.clipWithInteractionOverride(from, to, pos, shape, state);
|
||||
if (hit != null && from.distanceToSqr(hit.getLocation()) <= Double.MAX_VALUE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (tX > 1.0 && tY > 1.0 && tZ > 1.0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (tX < tY) {
|
||||
if (tX < tZ) {
|
||||
x += signX;
|
||||
tX += tDeltaX;
|
||||
} else {
|
||||
z += signZ;
|
||||
tZ += tDeltaZ;
|
||||
}
|
||||
} else if (tY < tZ) {
|
||||
y += signY;
|
||||
tY += tDeltaY;
|
||||
} else {
|
||||
z += signZ;
|
||||
tZ += tDeltaZ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+286
@@ -0,0 +1,286 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.NMSFields;
|
||||
import com.ticxo.modelengine.v1_19_R3.NMSMethods;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerPlayerConnection;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TrackedEntityImpl implements com.ticxo.modelengine.api.nms.entity.wrapper.TrackedEntity {
|
||||
private final Entity entity;
|
||||
private final Supplier<TrackedEntity> trackedEntitySupplier;
|
||||
private final Set<UUID> forcedPairing = Sets.newConcurrentHashSet();
|
||||
private final Set<UUID> forcedRemove = Sets.newConcurrentHashSet();
|
||||
private Predicate<Player> playerPredicate = DEFAULT_PREDICATE;
|
||||
@NotNull
|
||||
private TrackedEntity lastTrackedEntity;
|
||||
private static final int RECONCILE_INTERVAL = 20;
|
||||
private Object[] lastSeenBy;
|
||||
private volatile boolean viewersDirty = true;
|
||||
private volatile boolean mayHaveForced;
|
||||
private int ticksSinceReconcile;
|
||||
private final int initialBaseRange;
|
||||
|
||||
public TrackedEntityImpl(Entity entity, Supplier<TrackedEntity> trackedEntitySupplier, @NotNull TrackedEntity lastTrackedEntity) {
|
||||
this.entity = entity;
|
||||
this.trackedEntitySupplier = trackedEntitySupplier;
|
||||
this.lastTrackedEntity = lastTrackedEntity;
|
||||
this.initialBaseRange = this.getBaseRange();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInitialBaseRange() {
|
||||
return this.initialBaseRange;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBaseRange() {
|
||||
Integer range = ReflectionUtils.get(this.getTrackedEntity(), NMSFields.TRACKED_ENTITY_range);
|
||||
if (range == null) {
|
||||
throw new NullPointerException(String.format("Unable to retrieve base range of entity with UUID %s.", this.entity.getUniqueId()));
|
||||
} else {
|
||||
return range;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBaseRange(int range) {
|
||||
ReflectionUtils.set(this.getTrackedEntity(), NMSFields.TRACKED_ENTITY_range, range);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEffectiveRange() {
|
||||
Integer range = ReflectionUtils.call(this.getTrackedEntity(), NMSMethods.TRACKED_ENTITY_getEffectiveRange);
|
||||
if (range == null) {
|
||||
throw new NullPointerException(String.format("Unable to retrieve range of entity with UUID %s.", this.entity.getUniqueId()));
|
||||
} else {
|
||||
return range;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getTrackedPlayer() {
|
||||
boolean hasForced = !this.forcedPairing.isEmpty();
|
||||
boolean hasRemoved = !this.forcedRemove.isEmpty();
|
||||
Set<ServerPlayerConnection> seenBy = this.getTrackedEntity().seenBy;
|
||||
HashSet<UUID> set = new HashSet<>(Math.max(8, (int)((seenBy.size() + (hasForced ? this.forcedPairing.size() : 0)) / 0.75F) + 1));
|
||||
if (hasForced) {
|
||||
set.addAll(this.forcedPairing);
|
||||
}
|
||||
|
||||
for (ServerPlayerConnection connection : seenBy) {
|
||||
Player player = connection.getPlayer().getBukkitEntity();
|
||||
if (player != null && this.playerPredicate.test(player) && (!hasRemoved || !this.forcedRemove.contains(player.getUniqueId()))) {
|
||||
set.add(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getTrackedPlayer(Predicate<Player> predicate) {
|
||||
boolean hasForced = !this.forcedPairing.isEmpty();
|
||||
boolean hasRemoved = !this.forcedRemove.isEmpty();
|
||||
Set<ServerPlayerConnection> seenBy = this.getTrackedEntity().seenBy;
|
||||
HashSet<UUID> set = new HashSet<>(Math.max(8, (int)((seenBy.size() + (hasForced ? this.forcedPairing.size() : 0)) / 0.75F) + 1));
|
||||
if (hasForced) {
|
||||
set.addAll(this.forcedPairing);
|
||||
}
|
||||
|
||||
for (ServerPlayerConnection connection : seenBy) {
|
||||
Player player = connection.getPlayer().getBukkitEntity();
|
||||
if (player != null
|
||||
&& predicate.test(player)
|
||||
&& this.playerPredicate.test(player)
|
||||
&& (!hasRemoved || !this.forcedRemove.contains(player.getUniqueId()))) {
|
||||
set.add(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getTrackedPlayer(Predicate<Player> predicate, Set<UUID> dest) {
|
||||
dest.clear();
|
||||
boolean hasForced = !this.forcedPairing.isEmpty();
|
||||
boolean hasRemoved = !this.forcedRemove.isEmpty();
|
||||
Set<ServerPlayerConnection> seenBy = this.getTrackedEntity().seenBy;
|
||||
if (hasForced) {
|
||||
dest.addAll(this.forcedPairing);
|
||||
}
|
||||
|
||||
for (ServerPlayerConnection connection : seenBy) {
|
||||
Player player = connection.getPlayer().getBukkitEntity();
|
||||
if (player != null
|
||||
&& predicate.test(player)
|
||||
&& this.playerPredicate.test(player)
|
||||
&& (!hasRemoved || !this.forcedRemove.contains(player.getUniqueId()))) {
|
||||
dest.add(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isViewerSetUnchanged() {
|
||||
if ((!this.mayHaveForced || this.forcedPairing.isEmpty() && this.forcedRemove.isEmpty()) && this.playerPredicate == DEFAULT_PREDICATE) {
|
||||
if (!this.viewersDirty && ++this.ticksSinceReconcile < 20) {
|
||||
return true;
|
||||
} else {
|
||||
this.viewersDirty = false;
|
||||
this.ticksSinceReconcile = 0;
|
||||
return this.snapshotUnchanged(this.getTrackedEntity().seenBy);
|
||||
}
|
||||
} else {
|
||||
this.lastSeenBy = null;
|
||||
this.viewersDirty = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markViewersDirty() {
|
||||
this.viewersDirty = true;
|
||||
}
|
||||
|
||||
private boolean snapshotUnchanged(Collection<?> viewers) {
|
||||
int size = viewers.size();
|
||||
Object[] previous = this.lastSeenBy;
|
||||
if (previous != null && previous.length == size) {
|
||||
int index = 0;
|
||||
boolean same = true;
|
||||
|
||||
for (Object viewer : viewers) {
|
||||
if (index >= size || previous[index] != viewer) {
|
||||
same = false;
|
||||
break;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
if (same && index == size) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Object[] snapshot = new Object[size];
|
||||
int index = 0;
|
||||
|
||||
for (Object viewer : viewers) {
|
||||
if (index >= size) {
|
||||
break;
|
||||
}
|
||||
|
||||
snapshot[index++] = viewer;
|
||||
}
|
||||
|
||||
this.lastSeenBy = index == size ? snapshot : Arrays.copyOf(snapshot, index);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPairingData(Player player) {
|
||||
if (player != null) {
|
||||
ServerPlayer nms = ((CraftPlayer)player).getHandle();
|
||||
this.getTrackedEntity().serverEntity.sendPairingData(packet -> NetworkUtils.send(player.getUniqueId(), packet), nms);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcastSpawn() {
|
||||
for (UUID player : this.getTrackedPlayer()) {
|
||||
this.sendPairingData(Bukkit.getPlayer(player));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcastRemove() {
|
||||
this.getTrackedEntity().broadcastRemoved();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addForcedPairing(UUID uuid) {
|
||||
this.mayHaveForced = true;
|
||||
this.forcedPairing.add(uuid);
|
||||
this.removeForcedHidden(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeForcedPairing(UUID uuid) {
|
||||
this.forcedPairing.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearForcedPairing() {
|
||||
this.forcedPairing.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addForcedHidden(UUID uuid) {
|
||||
this.mayHaveForced = true;
|
||||
this.forcedRemove.add(uuid);
|
||||
this.removeForcedPairing(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeForcedHidden(UUID uuid) {
|
||||
this.forcedRemove.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearForcedHidden() {
|
||||
this.forcedRemove.clear();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private TrackedEntity getTrackedEntity() {
|
||||
TrackedEntity trackedEntity = this.trackedEntitySupplier.get();
|
||||
if (trackedEntity != null && this.lastTrackedEntity != trackedEntity) {
|
||||
this.syncInstance(trackedEntity);
|
||||
this.lastTrackedEntity = trackedEntity;
|
||||
}
|
||||
|
||||
return this.lastTrackedEntity;
|
||||
}
|
||||
|
||||
private void syncInstance(TrackedEntity target) {
|
||||
Integer range = ReflectionUtils.get(this.lastTrackedEntity, NMSFields.TRACKED_ENTITY_range);
|
||||
if (range != null) {
|
||||
ReflectionUtils.set(target, NMSFields.TRACKED_ENTITY_range, range);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity getEntity() {
|
||||
return this.entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<Player> getPlayerPredicate() {
|
||||
return this.playerPredicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayerPredicate(Predicate<Player> playerPredicate) {
|
||||
this.playerPredicate = playerPredicate;
|
||||
}
|
||||
}
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity.controller;
|
||||
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.BodyRotationController;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.control.BodyRotationControl;
|
||||
|
||||
public class BodyRotationControlWrapper extends BodyRotationControl implements BodyRotationController {
|
||||
private final Mob mob;
|
||||
private boolean isHeadClampUneven;
|
||||
private boolean isBodyClampUneven;
|
||||
private float maxHeadAngle;
|
||||
private float maxBodyAngle;
|
||||
private float minHeadAngle;
|
||||
private float minBodyAngle;
|
||||
private boolean playerMode;
|
||||
private float stableAngle = 15.0F;
|
||||
private int rotationDelay = 10;
|
||||
private int rotationDuration = 10;
|
||||
private int headStableTime;
|
||||
private float lastStableYHeadRot;
|
||||
|
||||
public BodyRotationControlWrapper(Mob mob) {
|
||||
super(mob);
|
||||
this.mob = mob;
|
||||
this.maxHeadAngle = mob.getMaxHeadYRot();
|
||||
this.maxBodyAngle = mob.getMaxHeadYRot();
|
||||
this.minHeadAngle = -this.maxHeadAngle;
|
||||
this.minBodyAngle = -this.maxBodyAngle;
|
||||
}
|
||||
|
||||
public void clientTick() {
|
||||
if (!this.getMob().isDeadOrDying()) {
|
||||
if (this.isMoving()) {
|
||||
this.mob.yBodyRot = this.mob.getYRot();
|
||||
this.rotateHeadIfNecessary();
|
||||
this.lastStableYHeadRot = this.mob.yHeadRot;
|
||||
this.headStableTime = 0;
|
||||
} else if (this.notCarryingMobPassengers()) {
|
||||
if (Math.abs(this.mob.yHeadRot - this.lastStableYHeadRot) > this.stableAngle) {
|
||||
this.headStableTime = 0;
|
||||
this.lastStableYHeadRot = this.mob.yHeadRot;
|
||||
this.rotateBodyIfNecessary();
|
||||
} else if (!this.playerMode) {
|
||||
this.headStableTime++;
|
||||
if (this.headStableTime > this.rotationDelay) {
|
||||
this.rotateHeadTowardsFront();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYHeadRot() {
|
||||
return this.mob.yHeadRot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYHeadRot(float rot) {
|
||||
this.mob.yHeadRot = rot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getXHeadRot() {
|
||||
return this.mob.getXRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setXHeadRot(float rot) {
|
||||
this.mob.setXRot(rot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYBodyRot() {
|
||||
return this.mob.yBodyRot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYBodyRot(float rot) {
|
||||
this.mob.yBodyRot = rot;
|
||||
}
|
||||
|
||||
private void rotateBodyIfNecessary() {
|
||||
this.mob.yBodyRot = TMath.rotateIfNecessary(
|
||||
this.mob.yBodyRot, this.mob.yHeadRot, this.isBodyClampUneven ? this.minBodyAngle : -this.maxBodyAngle, this.maxBodyAngle
|
||||
);
|
||||
}
|
||||
|
||||
private void rotateHeadIfNecessary() {
|
||||
this.mob.yHeadRot = TMath.rotateIfNecessary(
|
||||
this.mob.yHeadRot, this.mob.yBodyRot, this.isHeadClampUneven ? this.minHeadAngle : -this.maxHeadAngle, this.maxHeadAngle
|
||||
);
|
||||
}
|
||||
|
||||
private void rotateHeadTowardsFront() {
|
||||
float ratio = (float)(this.headStableTime - this.rotationDelay) / this.rotationDuration;
|
||||
float clampedRatio = TMath.clamp(ratio, 0.0F, 1.0F);
|
||||
float maxClamp = this.maxHeadAngle * (1.0F - clampedRatio);
|
||||
float minClamp = (this.isHeadClampUneven ? this.minHeadAngle : -this.maxHeadAngle) * (1.0F - clampedRatio);
|
||||
this.mob.yBodyRot = TMath.rotateIfNecessary(this.mob.yBodyRot, this.mob.yHeadRot, minClamp, maxClamp);
|
||||
}
|
||||
|
||||
private boolean notCarryingMobPassengers() {
|
||||
return !(this.mob.getFirstPassenger() instanceof Mob);
|
||||
}
|
||||
|
||||
private boolean isMoving() {
|
||||
double dX = this.mob.getX() - this.mob.xo;
|
||||
double dZ = this.mob.getZ() - this.mob.zo;
|
||||
return dX * dX + dZ * dZ > 2.5000003E-7F;
|
||||
}
|
||||
|
||||
public Mob getMob() {
|
||||
return this.mob;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isHeadClampUneven() {
|
||||
return this.isHeadClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBodyClampUneven() {
|
||||
return this.isBodyClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMaxHeadAngle() {
|
||||
return this.maxHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMaxBodyAngle() {
|
||||
return this.maxBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMinHeadAngle() {
|
||||
return this.minHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMinBodyAngle() {
|
||||
return this.minBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlayerMode() {
|
||||
return this.playerMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getStableAngle() {
|
||||
return this.stableAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRotationDelay() {
|
||||
return this.rotationDelay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRotationDuration() {
|
||||
return this.rotationDuration;
|
||||
}
|
||||
|
||||
public int getHeadStableTime() {
|
||||
return this.headStableTime;
|
||||
}
|
||||
|
||||
public float getLastStableYHeadRot() {
|
||||
return this.lastStableYHeadRot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeadClampUneven(boolean isHeadClampUneven) {
|
||||
this.isHeadClampUneven = isHeadClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBodyClampUneven(boolean isBodyClampUneven) {
|
||||
this.isBodyClampUneven = isBodyClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxHeadAngle(float maxHeadAngle) {
|
||||
this.maxHeadAngle = maxHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxBodyAngle(float maxBodyAngle) {
|
||||
this.maxBodyAngle = maxBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMinHeadAngle(float minHeadAngle) {
|
||||
this.minHeadAngle = minHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMinBodyAngle(float minBodyAngle) {
|
||||
this.minBodyAngle = minBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayerMode(boolean playerMode) {
|
||||
this.playerMode = playerMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStableAngle(float stableAngle) {
|
||||
this.stableAngle = stableAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRotationDelay(int rotationDelay) {
|
||||
this.rotationDelay = rotationDelay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRotationDuration(int rotationDuration) {
|
||||
this.rotationDuration = rotationDuration;
|
||||
}
|
||||
|
||||
public void setHeadStableTime(int headStableTime) {
|
||||
this.headStableTime = headStableTime;
|
||||
}
|
||||
|
||||
public void setLastStableYHeadRot(float lastStableYHeadRot) {
|
||||
this.lastStableYHeadRot = lastStableYHeadRot;
|
||||
}
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity.controller;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.bone.BoneBehaviorTypes;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.BoneBehaviorType;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.BehaviorManager;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountData;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountManager;
|
||||
import com.ticxo.modelengine.api.model.bone.type.Mount;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.LookController;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.control.LookControl;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.spigotmc.ActivationRange;
|
||||
|
||||
public class LookControlWrapper extends LookControl implements LookController {
|
||||
private final LookControl original;
|
||||
private ModeledEntity modeledEntity;
|
||||
|
||||
public LookControlWrapper(Mob mob, LookControl control) {
|
||||
super(mob);
|
||||
this.original = control;
|
||||
}
|
||||
|
||||
public void setLookAt(Vec3 var0) {
|
||||
this.original.setLookAt(var0);
|
||||
}
|
||||
|
||||
public void setLookAt(Entity var0) {
|
||||
this.original.setLookAt(var0);
|
||||
}
|
||||
|
||||
public void setLookAt(Entity var0, float var1, float var2) {
|
||||
this.original.setLookAt(var0, var1, var2);
|
||||
}
|
||||
|
||||
public void setLookAt(double var0, double var2, double var4) {
|
||||
this.original.setLookAt(var0, var2, var4);
|
||||
}
|
||||
|
||||
public void setLookAt(double var0, double var2, double var4, float var6, float var7) {
|
||||
this.original.setLookAt(var0, var2, var4, var6, var7);
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
if (this.modeledEntity == null) {
|
||||
this.modeledEntity = ModelEngineAPI.getModeledEntity(this.mob.getUUID());
|
||||
}
|
||||
|
||||
BehaviorManager mountManager = this.getMainManager();
|
||||
if (mountManager != null && ((MountManager)mountManager).isControlled()) {
|
||||
this.controlledTick(asMountManager(mountManager));
|
||||
} else {
|
||||
this.defaultTick();
|
||||
}
|
||||
}
|
||||
|
||||
protected <T extends BehaviorManager<? extends Mount> & MountManager> void controlledTick(T manager) {
|
||||
org.bukkit.entity.Entity driver = manager.getDriver();
|
||||
if (driver != null) {
|
||||
MountController controller = ModelEngineAPI.getMountPairManager().getController(driver.getUniqueId());
|
||||
if (controller != null) {
|
||||
controller.updateDirection(this, manager.getActiveModel());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.defaultTick();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defaultTick() {
|
||||
if (this.isActive()) {
|
||||
this.original.tick();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLookingAtTarget() {
|
||||
return this.original.isLookingAtTarget();
|
||||
}
|
||||
|
||||
public double getWantedX() {
|
||||
return this.original.getWantedX();
|
||||
}
|
||||
|
||||
public double getWantedY() {
|
||||
return this.original.getWantedY();
|
||||
}
|
||||
|
||||
public double getWantedZ() {
|
||||
return this.original.getWantedZ();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lookAt(double x, double y, double z) {
|
||||
double dX = x - this.mob.getX();
|
||||
double dY = y - this.mob.getEyeY();
|
||||
double dZ = z - this.mob.getZ();
|
||||
double hMagnitude = Math.sqrt(dX * dX + dZ * dZ);
|
||||
float pitch = (float)Math.toDegrees(Math.atan2(-dY, hMagnitude));
|
||||
float yaw = (float)Math.toDegrees(Math.atan2(-dX, dZ));
|
||||
this.setPitch(pitch);
|
||||
this.setHeadYaw(yaw);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPitch(float pitch) {
|
||||
this.mob.setXRot(pitch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeadYaw(float yaw) {
|
||||
this.mob.setYRot(yaw);
|
||||
this.mob.yHeadRot = yaw;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBodyYaw(float yaw) {
|
||||
this.mob.yBodyRot = yaw;
|
||||
}
|
||||
|
||||
private <T extends BehaviorManager<? extends Mount> & MountManager> T getMainManager() {
|
||||
if (this.modeledEntity == null) {
|
||||
return null;
|
||||
} else {
|
||||
return this.modeledEntity.getGlobalBehaviorData(BoneBehaviorTypes.MOUNT) instanceof MountData mountData
|
||||
? mountData.getMainMountManager()
|
||||
: null;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isActive() {
|
||||
return ActivationRange.checkIfActive(this.mob);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <M extends BehaviorManager<? extends Mount> & MountManager> M asMountManager(Object manager) {
|
||||
return (M)manager;
|
||||
}
|
||||
}
|
||||
+247
@@ -0,0 +1,247 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity.controller;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.animation.rootmotion.RootMotionDelta;
|
||||
import com.ticxo.modelengine.api.model.ActiveModel;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.BoneBehavior;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.GlobalBehaviorData;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.BehaviorManager;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountData;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountManager;
|
||||
import com.ticxo.modelengine.api.model.bone.type.Mount;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController.MountInput;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.MoveController;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_19_R3.NMSFields;
|
||||
import java.util.Queue;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.MoverType;
|
||||
import net.minecraft.world.entity.ai.attributes.Attributes;
|
||||
import net.minecraft.world.entity.ai.control.MoveControl;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.apache.logging.log4j.util.TriConsumer;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.util.CraftVector;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
public class MoveControlWrapper extends MoveControl implements MoveController {
|
||||
protected final MoveControl original;
|
||||
protected final Queue<Runnable> runnables = new ConcurrentLinkedQueue<>();
|
||||
protected boolean isOnGround;
|
||||
|
||||
public MoveControlWrapper(Mob mob, MoveControl control) {
|
||||
super(mob);
|
||||
this.original = control;
|
||||
}
|
||||
|
||||
public boolean hasWanted() {
|
||||
return this.original.hasWanted();
|
||||
}
|
||||
|
||||
public double getSpeedModifier() {
|
||||
return this.original.getSpeedModifier();
|
||||
}
|
||||
|
||||
public void setWantedPosition(double x, double y, double z, double speed) {
|
||||
this.original.setWantedPosition(x, y, z, speed);
|
||||
}
|
||||
|
||||
public void strafe(float forward, float sideways) {
|
||||
this.original.strafe(forward, sideways);
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
this.isOnGround = this.mob.isOnGround();
|
||||
ModeledEntity modeledEntity = ModelEngineAPI.getModeledEntity(this.mob.getUUID());
|
||||
if (modeledEntity == null) {
|
||||
this.defaultTick();
|
||||
} else {
|
||||
GlobalBehaviorData mountData = modeledEntity.getMountData();
|
||||
BehaviorManager mainMountManager = mountData == null ? null : ((MountData)mountData).getMainMountManager();
|
||||
if (mainMountManager != null && ((MountManager)mainMountManager).isControlled()) {
|
||||
this.mob.setOnGround(true);
|
||||
this.disableWaterJumping();
|
||||
this.driverTick(asMountManager(mainMountManager));
|
||||
} else {
|
||||
this.defaultTick();
|
||||
}
|
||||
|
||||
this.passengerTick(modeledEntity, asMountManager(mainMountManager));
|
||||
|
||||
while (!this.runnables.isEmpty()) {
|
||||
this.runnables.poll().run();
|
||||
}
|
||||
|
||||
Vector mobMotion = this.toVector();
|
||||
RootMotionDelta rootMotion = modeledEntity.getRootMotionHandler().calculateRootMotion(mobMotion);
|
||||
this.fromVector(mobMotion);
|
||||
if (rootMotion != null) {
|
||||
Vector velocity = rootMotion.delta();
|
||||
if (!velocity.isZero() || !rootMotion.onGround()) {
|
||||
this.nullifyFallDistance();
|
||||
if (!TMath.isSimilar((float)velocity.getY(), 0.0F) || !rootMotion.onGround()) {
|
||||
this.mob.setDeltaMovement(this.mob.getDeltaMovement().multiply(1.0, 0.0, 1.0));
|
||||
}
|
||||
|
||||
this.mob.move(MoverType.SELF, new Vec3(velocity.getX(), velocity.getY(), velocity.getZ()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public double getWantedX() {
|
||||
return this.original.getWantedX();
|
||||
}
|
||||
|
||||
public double getWantedY() {
|
||||
return this.original.getWantedY();
|
||||
}
|
||||
|
||||
public double getWantedZ() {
|
||||
return this.original.getWantedZ();
|
||||
}
|
||||
|
||||
protected <T extends BehaviorManager<? extends Mount> & MountManager> void driverTick(T manager) {
|
||||
this.mob.setZza(0.0F);
|
||||
this.mob.setXxa(0.0F);
|
||||
this.updateRider(manager.getDriver(), manager.getActiveModel(), manager.getDriverBone(), MountController::updateDriverMovement);
|
||||
}
|
||||
|
||||
protected <T extends BehaviorManager<? extends Mount> & MountManager> void passengerTick(ModeledEntity modeledEntity, T manager) {
|
||||
for (ActiveModel activeModel : modeledEntity.getModels().values()) {
|
||||
activeModel.getMountManager()
|
||||
.ifPresent(
|
||||
mountManager -> {
|
||||
for (BoneBehavior seat : ((MountManager)mountManager).getSeats().values()) {
|
||||
for (Entity passenger : ((Mount)seat).getPassengers()) {
|
||||
this.updateRider(passenger, activeModel, (Mount)seat, MountController::updatePassengerMovement);
|
||||
}
|
||||
}
|
||||
|
||||
if (mountManager != manager && ((MountManager)mountManager).isControlled()) {
|
||||
this.updateRider(
|
||||
((MountManager)mountManager).getDriver(),
|
||||
activeModel,
|
||||
((MountManager)mountManager).getDriverBone(),
|
||||
MountController::updatePassengerMovement
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateRider(Entity entity, ActiveModel activeModel, Mount mountBone, TriConsumer<MountController, MoveController, ActiveModel> updateMethod) {
|
||||
MountController controller = this.getController(entity.getUniqueId());
|
||||
if (controller != null) {
|
||||
if (controller.getInput() == null) {
|
||||
controller.setInput(new MountInput());
|
||||
}
|
||||
|
||||
updateMethod.accept(controller, this, activeModel);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defaultTick() {
|
||||
this.original.tick();
|
||||
}
|
||||
|
||||
private void disableWaterJumping() {
|
||||
if (this.mob.isInWater()) {
|
||||
ReflectionUtils.set(this.mob, NMSFields.LIVING_ENTITY_noJumpDelay, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(float side, float up, float front, float speedModifier) {
|
||||
float speed = this.getSpeed();
|
||||
this.mob.setSpeed(speed * speedModifier);
|
||||
this.mob.setZza(front);
|
||||
this.mob.setYya(up);
|
||||
this.mob.setXxa(side);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void globalMove(float x, float y, float z, float speedModifier) {
|
||||
float speed = this.getSpeed();
|
||||
this.mob.setSpeed(speed * speedModifier);
|
||||
Vec3 vec = new Vec3(x, y, z).yRot(-this.mob.getYRot() * (float) (Math.PI / 180.0));
|
||||
this.mob.setXxa((float)vec.x);
|
||||
this.mob.setYya((float)vec.y);
|
||||
this.mob.setZza((float)vec.z);
|
||||
}
|
||||
|
||||
private void fromVector(Vector vector) {
|
||||
Vec3 vec = new Vec3(vector.getX(), vector.getY(), vector.getZ()).yRot(-this.mob.getYRot() * (float) (Math.PI / 180.0));
|
||||
this.mob.setXxa((float)vec.x);
|
||||
this.mob.setYya((float)vec.y);
|
||||
this.mob.setZza((float)vec.z);
|
||||
}
|
||||
|
||||
private Vector toVector() {
|
||||
return new Vector(
|
||||
Float.isNaN(this.mob.xxa) ? 0.0F : this.mob.xxa, Float.isNaN(this.mob.yya) ? 0.0F : this.mob.yya, Float.isNaN(this.mob.zza) ? 0.0F : this.mob.zza
|
||||
)
|
||||
.rotateAroundY(this.mob.getYRot() * (float) (Math.PI / 180.0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jump() {
|
||||
this.mob.getJumpControl().jump();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVelocity(double x, double y, double z) {
|
||||
this.mob.setDeltaMovement(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addVelocity(double x, double y, double z) {
|
||||
this.mob.setDeltaMovement(this.mob.getDeltaMovement().add(x, y, z));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullifyFallDistance() {
|
||||
this.mob.resetFallDistance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnGround() {
|
||||
return this.isOnGround;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInWater() {
|
||||
return this.mob.isInWater();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getSpeed() {
|
||||
return (float)this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector getVelocity() {
|
||||
return CraftVector.toBukkit(this.mob.getDeltaMovement());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queuePostTick(Runnable runnable) {
|
||||
this.runnables.add(runnable);
|
||||
}
|
||||
|
||||
private MountController getController(UUID uuid) {
|
||||
return ModelEngineAPI.getMountPairManager().getController(uuid);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <M extends BehaviorManager<? extends Mount> & MountManager> M asMountManager(Object manager) {
|
||||
return (M)manager;
|
||||
}
|
||||
}
|
||||
+284
@@ -0,0 +1,284 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity.hitbox;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.model.bone.ModelBone;
|
||||
import com.ticxo.modelengine.api.model.bone.type.SubHitbox;
|
||||
import com.ticxo.modelengine.api.nms.entity.HitboxEntity;
|
||||
import com.ticxo.modelengine.api.utils.math.OrientedBoundingBox;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.EntityUtils;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.core.NonNullList;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.damagesource.DamageSource;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.EquipmentSlot;
|
||||
import net.minecraft.world.entity.HumanoidArm;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Entity.RemovalReason;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftEntity;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftHumanEntity;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftLivingEntity;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.util.CraftLocation;
|
||||
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class HitboxEntityImpl extends LivingEntity implements HitboxEntity {
|
||||
private final NonNullList<ItemStack> handItems = NonNullList.withSize(2, ItemStack.EMPTY);
|
||||
private final NonNullList<ItemStack> armorItems = NonNullList.withSize(4, ItemStack.EMPTY);
|
||||
private final ModelBone bone;
|
||||
private final SubHitbox subHitbox;
|
||||
private OBB obb;
|
||||
private volatile Vector3f location;
|
||||
private boolean markRemoved;
|
||||
private CraftEntity bukkitEntity;
|
||||
|
||||
public HitboxEntityImpl(Level world, @NotNull ModelBone bone, @NotNull SubHitbox subHitbox) {
|
||||
super(EntityType.SILVERFISH, world);
|
||||
this.bone = bone;
|
||||
this.subHitbox = subHitbox;
|
||||
this.setInvulnerable(true);
|
||||
this.setNoGravity(true);
|
||||
this.noPhysics = true;
|
||||
}
|
||||
|
||||
public HumanoidArm getMainArm() {
|
||||
return HumanoidArm.RIGHT;
|
||||
}
|
||||
|
||||
public Iterable<ItemStack> getArmorSlots() {
|
||||
return this.armorItems;
|
||||
}
|
||||
|
||||
public ItemStack getItemBySlot(EquipmentSlot slot) {
|
||||
return switch (slot.getType()) {
|
||||
case HAND -> (ItemStack)this.handItems.get(slot.getIndex());
|
||||
case ARMOR -> (ItemStack)this.armorItems.get(slot.getIndex());
|
||||
default -> throw new IncompatibleClassChangeError();
|
||||
};
|
||||
}
|
||||
|
||||
public void setItemSlot(EquipmentSlot slot, ItemStack stack) {
|
||||
this.verifyEquippedItem(stack);
|
||||
switch (slot.getType()) {
|
||||
case HAND:
|
||||
this.onEquipItem(slot, this.handItems.set(slot.getIndex(), stack), stack);
|
||||
break;
|
||||
case ARMOR:
|
||||
this.onEquipItem(slot, this.armorItems.set(slot.getIndex(), stack), stack);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInvisible() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void push(Entity entity) {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected AABB makeBoundingBox() {
|
||||
if (this.subHitbox == null) {
|
||||
return super.makeBoundingBox();
|
||||
} else {
|
||||
Vector3f pos = this.subHitbox.getLocation();
|
||||
Vector3f dim = this.subHitbox.getDimension();
|
||||
if (this.subHitbox.isOBB()) {
|
||||
float halfX = dim.x * 0.5F;
|
||||
float halfY = dim.y * 0.5F;
|
||||
float halfZ = dim.z * 0.5F;
|
||||
Quaternionf rot = this.subHitbox.getRotation();
|
||||
float yaw = this.subHitbox.getYaw();
|
||||
this.obb = new OBB(pos.x - halfX, pos.y - halfY, pos.z - halfZ, pos.x + halfX, pos.y + halfY, pos.z + halfZ, rot, yaw);
|
||||
return this.obb;
|
||||
} else {
|
||||
float halfX = dim.x * 0.5F;
|
||||
float halfZ = dim.z * 0.5F;
|
||||
return new AABB(pos.x - halfX, pos.y, pos.z - halfZ, pos.x + halfX, pos.y + dim.y, pos.z + halfZ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
if (this.markRemoved) {
|
||||
this.discard();
|
||||
} else {
|
||||
super.tick();
|
||||
if (this.bone == null || this.subHitbox == null) {
|
||||
this.discard();
|
||||
} else if (!this.bone.getActiveModel().getModeledEntity().getBase().isAlive()) {
|
||||
this.discard();
|
||||
} else {
|
||||
Vector3f loc = this.location;
|
||||
if (loc != null && loc.isFinite()) {
|
||||
Vec3 vec = new Vec3(loc);
|
||||
this.setPos(vec);
|
||||
|
||||
for (org.bukkit.entity.Entity entity : this.subHitbox.getBoundEntities().values()) {
|
||||
EntityUtils.nms(entity).setPos(vec);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean fireImmune() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void pushEntities() {
|
||||
}
|
||||
|
||||
protected void doPush(Entity entity) {
|
||||
}
|
||||
|
||||
public void aiStep() {
|
||||
}
|
||||
|
||||
public boolean isPushable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hurt(DamageSource source, float amount) {
|
||||
for (org.bukkit.entity.Entity entity : this.subHitbox.getBoundEntities().values()) {
|
||||
EntityUtils.nms(entity).hurt(source, amount);
|
||||
}
|
||||
|
||||
if (this.subHitbox.getDamageMultiplier() <= 1.0E-5F) {
|
||||
return false;
|
||||
} else {
|
||||
CraftHumanEntity cause = source.getEntity() instanceof Player player ? player.getBukkitEntity() : null;
|
||||
return this.bone.getActiveModel().getModeledEntity().getBase().hurt(this, cause, source, amount * this.subHitbox.getDamageMultiplier());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public InteractionResult interact(Player player, InteractionHand hand) {
|
||||
if (player.getBukkitEntity() instanceof org.bukkit.entity.Player craftPlayer) {
|
||||
for (org.bukkit.entity.Entity entity : this.subHitbox.getBoundEntities().values()) {
|
||||
PlayerInteractAtEntityEvent event = new PlayerInteractAtEntityEvent(
|
||||
craftPlayer,
|
||||
entity,
|
||||
new Vector(0, 0, 0),
|
||||
hand == InteractionHand.OFF_HAND ? org.bukkit.inventory.EquipmentSlot.OFF_HAND : org.bukkit.inventory.EquipmentSlot.HAND
|
||||
);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if (!event.isCancelled()) {
|
||||
EntityUtils.nms(entity).interact(player, hand);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.subHitbox.getDamageMultiplier() <= 1.0E-5F) {
|
||||
return InteractionResult.PASS;
|
||||
} else {
|
||||
com.ticxo.modelengine.api.nms.entity.EntityHandler.InteractionResult result = this.bone
|
||||
.getActiveModel()
|
||||
.getModeledEntity()
|
||||
.getBase()
|
||||
.interact(
|
||||
this,
|
||||
player.getBukkitEntity(),
|
||||
hand == InteractionHand.MAIN_HAND ? org.bukkit.inventory.EquipmentSlot.HAND : org.bukkit.inventory.EquipmentSlot.OFF_HAND
|
||||
);
|
||||
|
||||
return switch (result) {
|
||||
case SUCCESS, SUCCESS_NO_ITEM_USED -> InteractionResult.SUCCESS;
|
||||
case CONSUME -> InteractionResult.CONSUME;
|
||||
case CONSUME_PARTIAL -> InteractionResult.CONSUME_PARTIAL;
|
||||
case PASS -> InteractionResult.PASS;
|
||||
case FAIL -> InteractionResult.FAIL;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public boolean shouldBeSaved() {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected int decreaseAirSupply(int air) {
|
||||
return air;
|
||||
}
|
||||
|
||||
protected int increaseAirSupply(int air) {
|
||||
return air;
|
||||
}
|
||||
|
||||
public CraftEntity getBukkitEntity() {
|
||||
if (this.bukkitEntity == null) {
|
||||
this.bukkitEntity = new CraftLivingEntity(this.level.getCraftServer(), this) {
|
||||
public org.bukkit.entity.EntityType getType() {
|
||||
return org.bukkit.entity.EntityType.SILVERFISH;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return this.bukkitEntity;
|
||||
}
|
||||
|
||||
public void remove(RemovalReason reason) {
|
||||
super.remove(reason);
|
||||
ModelEngineAPI.setRenderCanceled(this.getId(), false);
|
||||
ModelEngineAPI.getInteractionTracker().removeHitbox(this.getEntityId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEntityId() {
|
||||
return this.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return this.getUUID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queueLocation(Vector3f location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getLocation() {
|
||||
return CraftLocation.toBukkit(this.position(), this.level.getWorld(), this.getBukkitYaw(), this.getXRot());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public OrientedBoundingBox getOrientedBoundingBox() {
|
||||
return this.obb == null ? null : this.obb.getBukkitOBB();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return !this.isRemoved() && !this.markRemoved;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markRemoved() {
|
||||
this.markRemoved = true;
|
||||
ModelEngineAPI.setRenderCanceled(this.getId(), false);
|
||||
ModelEngineAPI.getInteractionTracker().removeHitbox(this.getEntityId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModelBone getBone() {
|
||||
return this.bone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SubHitbox getSubHitbox() {
|
||||
return this.subHitbox;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity.hitbox;
|
||||
|
||||
import com.ticxo.modelengine.api.utils.math.OrientedBoundingBox;
|
||||
import java.util.Optional;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class OBB extends AABB {
|
||||
private final Quaternionf rotation;
|
||||
private final float yaw;
|
||||
private final OrientedBoundingBox bukkitOBB;
|
||||
|
||||
public OBB(Vec3 cornerA, Vec3 cornerB, Quaternionf rotation, float yaw) {
|
||||
this(cornerA.x, cornerA.y, cornerA.z, cornerB.x, cornerB.y, cornerB.z, rotation, yaw);
|
||||
}
|
||||
|
||||
public OBB(double minX, double minY, double minZ, double maxX, double maxY, double maxZ, Quaternionf rotation, float yaw) {
|
||||
super(minX, minY, minZ, maxX, maxY, maxZ);
|
||||
this.rotation = rotation;
|
||||
this.yaw = yaw;
|
||||
this.bukkitOBB = new OrientedBoundingBox(
|
||||
this.getCenter().toVector3f(), new Vector3f((float)this.getXsize(), (float)this.getYsize(), (float)this.getZsize()), rotation, yaw
|
||||
);
|
||||
}
|
||||
|
||||
public OBB makeOBBInstance(Vec3 position, Quaternionf rotation, float yaw) {
|
||||
return new OBB(position.add(this.minX, this.minY, this.minZ), position.add(this.maxX, this.maxY, this.maxZ), rotation, yaw);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public AABB inflate(double xInflate, double yInflate, double zInflate) {
|
||||
double minX = this.minX - xInflate;
|
||||
double minY = this.minY - yInflate;
|
||||
double minZ = this.minZ - zInflate;
|
||||
double maxX = this.maxX + xInflate;
|
||||
double maxY = this.maxY + yInflate;
|
||||
double maxZ = this.maxZ + zInflate;
|
||||
return new OBB(minX, minY, minZ, maxX, maxY, maxZ, this.rotation, this.yaw);
|
||||
}
|
||||
|
||||
public boolean intersects(@NotNull AABB aabb) {
|
||||
return aabb instanceof OBB obb ? this.intersects(obb) : super.intersects(aabb);
|
||||
}
|
||||
|
||||
public boolean intersects(OBB obb) {
|
||||
return this.bukkitOBB.intersects(obb.bukkitOBB);
|
||||
}
|
||||
|
||||
public boolean intersects(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
|
||||
OrientedBoundingBox obbB = new OrientedBoundingBox((float)minX, (float)minY, (float)minZ, (float)maxX, (float)maxY, (float)maxZ);
|
||||
return this.bukkitOBB.intersects(obbB);
|
||||
}
|
||||
|
||||
public Optional<Vec3> clip(Vec3 from, Vec3 to) {
|
||||
Quaternionf inverse = this.rotation.conjugate(new Quaternionf());
|
||||
Vec3 center = this.getCenter();
|
||||
float yaw = this.yaw * (float) (Math.PI / 180.0);
|
||||
Vec3 rFrom = new Vec3(from.subtract(center).toVector3f().rotateY(-yaw).rotate(inverse)).add(center);
|
||||
Vec3 rTo = new Vec3(to.subtract(center).toVector3f().rotateY(-yaw).rotate(inverse)).add(center);
|
||||
Optional<Vec3> result = super.clip(rFrom, rTo);
|
||||
if (result.isEmpty()) {
|
||||
return result;
|
||||
} else {
|
||||
Vec3 clip = new Vec3(result.get().subtract(center).toVector3f().rotate(this.rotation).rotateY(yaw)).add(center);
|
||||
return Optional.of(clip);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "OBB(rotation=" + this.getRotation() + ", yaw=" + this.getYaw() + ", bukkitOBB=" + this.getBukkitOBB() + ")";
|
||||
}
|
||||
|
||||
public Quaternionf getRotation() {
|
||||
return this.rotation;
|
||||
}
|
||||
|
||||
public float getYaw() {
|
||||
return this.yaw;
|
||||
}
|
||||
|
||||
public OrientedBoundingBox getBukkitOBB() {
|
||||
return this.bukkitOBB;
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.AmphibiousPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class AmphibiousNavigationWrapper extends AmphibiousPathNavigation {
|
||||
private final AmphibiousPathNavigation oldNav;
|
||||
|
||||
public AmphibiousNavigationWrapper(Mob mob, AmphibiousPathNavigation oldNav) {
|
||||
super(mob, mob.level);
|
||||
this.oldNav = oldNav;
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isStableDestination(BlockPos var0) {
|
||||
return this.oldNav.isStableDestination(var0);
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class FlyingNavigationWrapper extends FlyingPathNavigation {
|
||||
public FlyingNavigationWrapper(Mob mob, FlyingPathNavigation oldNav) {
|
||||
super(mob, mob.level);
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
this.setCanOpenDoors(oldNav.canOpenDoors());
|
||||
this.setCanPassDoors(oldNav.canPassDoors());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class GroundNavigationWrapper extends GroundPathNavigation {
|
||||
public GroundNavigationWrapper(Mob mob, GroundPathNavigation oldNav) {
|
||||
super(mob, mob.level);
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
this.setCanOpenDoors(oldNav.canOpenDoors());
|
||||
this.setCanPassDoors(oldNav.canPassDoors());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.WallClimberNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class WallClimberNavigationWrapper extends WallClimberNavigation {
|
||||
public WallClimberNavigationWrapper(Mob mob, WallClimberNavigation oldNav) {
|
||||
super(mob, mob.level);
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
this.setCanOpenDoors(oldNav.canOpenDoors());
|
||||
this.setCanPassDoors(oldNav.canPassDoors());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.WaterBoundPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class WaterBoundNavigationWrapper extends WaterBoundPathNavigation {
|
||||
public WaterBoundNavigationWrapper(Mob mob) {
|
||||
super(mob, mob.level);
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+452
@@ -0,0 +1,452 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.network;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.interaction.DynamicHitbox;
|
||||
import com.ticxo.modelengine.api.model.ActiveModel;
|
||||
import com.ticxo.modelengine.api.model.ModelUpdaters;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.PivotOverride;
|
||||
import com.ticxo.modelengine.api.model.PivotOverrideRegistry;
|
||||
import com.ticxo.modelengine.api.model.bone.BoneBehaviorTypes;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.BoneBehavior;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer.Mount;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController.MountInput;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.nms.network.CancelType;
|
||||
import com.ticxo.modelengine.api.nms.network.ClientDesyncMonitor;
|
||||
import com.ticxo.modelengine.api.nms.network.PipelineWrapper;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.api.utils.data.tracker.CollectionDataTracker;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_19_R3.NMSFields;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.patch.ServerboundInteractPacketWrapper;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.PacketInterceptor;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
import it.unimi.dsi.fastutil.Pair;
|
||||
import it.unimi.dsi.fastutil.ints.IntLinkedOpenHashSet;
|
||||
import it.unimi.dsi.fastutil.ints.IntListIterator;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.PacketListener;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddPlayerPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundAnimatePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundEntityEventPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundKeepAlivePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveMobEffectPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundPlayerInputPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundPongPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Pos;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.PosRot;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Rot;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.network.syncher.SynchedEntityData.DataValue;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ModelEngineChannelHandler extends ChannelDuplexHandler {
|
||||
private static final MethodHandle MOVE_ENTITY_ID = entityIdGetter(NMSFields.CLIENTBOUND_MOVE_ENTITY_PACKET_entityId);
|
||||
private static final MethodHandle ROTATE_HEAD_ID = entityIdGetter(NMSFields.CLIENTBOUND_ROTATE_HEAD_PACKET_entityId);
|
||||
private final Player player;
|
||||
private final ServerPlayer serverPlayer;
|
||||
private final ModelUpdaters updaters;
|
||||
private final EntityHandler entityHandler;
|
||||
private final ClientDesyncMonitor desyncMonitor;
|
||||
private final PacketInterceptor writeInterceptors;
|
||||
private final PacketInterceptor readInterceptors;
|
||||
|
||||
private static MethodHandle entityIdGetter(NMSFields field) {
|
||||
try {
|
||||
return MethodHandles.lookup().unreflectGetter(ReflectionUtils.getField(field));
|
||||
} catch (IllegalAccessException var2) {
|
||||
throw new ExceptionInInitializerError(var2);
|
||||
}
|
||||
}
|
||||
|
||||
public ModelEngineChannelHandler(Player player, PipelineWrapper pipeline) {
|
||||
this.player = player;
|
||||
this.serverPlayer = ((CraftPlayer)player).getHandle();
|
||||
this.updaters = ModelEngineAPI.getAPI().getModelUpdaters();
|
||||
this.entityHandler = ModelEngineAPI.getEntityHandler();
|
||||
this.desyncMonitor = pipeline.getDesyncMonitor();
|
||||
this.writeInterceptors = new PacketInterceptor();
|
||||
this.writeInterceptors
|
||||
.register(ClientboundAddEntityPacket.class, this::handleAddEntity)
|
||||
.register(ClientboundAddPlayerPacket.class, this::handleAddPlayer)
|
||||
.register(ClientboundRemoveEntitiesPacket.class, this::handleRemoveEntities)
|
||||
.register(Pos.class, packet -> this.handleEntityId(packet, MOVE_ENTITY_ID))
|
||||
.register(Rot.class, packet -> this.handleEntityId(packet, MOVE_ENTITY_ID))
|
||||
.register(PosRot.class, packet -> this.handleEntityId(packet, MOVE_ENTITY_ID))
|
||||
.register(ClientboundRotateHeadPacket.class, packet -> this.handleEntityId(packet, ROTATE_HEAD_ID))
|
||||
.register(ClientboundEntityEventPacket.class, this::handleEntityId)
|
||||
.register(ClientboundSetEntityMotionPacket.class, this::handleEntityMotion)
|
||||
.register(ClientboundTeleportEntityPacket.class, this::handleTeleportEntity)
|
||||
.register(ClientboundAnimatePacket.class, this::handleAnimate)
|
||||
.register(ClientboundSetEntityDataPacket.class, this::handleEntityData)
|
||||
.register(ClientboundSetEquipmentPacket.class, this::handleSetEquipment)
|
||||
.register(ClientboundRemoveMobEffectPacket.class, this::handleRemoveMobEffect)
|
||||
.register(ClientboundUpdateMobEffectPacket.class, this::handleUpdateMobEffect)
|
||||
.register(ClientboundKeepAlivePacket.class, this::handleKeepAlive)
|
||||
.register(ClientboundSetPassengersPacket.class, this::handlePassengers)
|
||||
.registerPost(ClientboundAddEntityPacket.class, this::handleAddEntityPost)
|
||||
.registerPost(ClientboundAddPlayerPacket.class, this::handleAddPlayerPost);
|
||||
this.readInterceptors = new PacketInterceptor();
|
||||
this.readInterceptors
|
||||
.register(ServerboundInteractPacket.class, this::handleInteract)
|
||||
.register(ServerboundPlayerInputPacket.class, this::handlePlayerInput)
|
||||
.register(ServerboundPongPacket.class, this::handlePong)
|
||||
.register(net.minecraft.network.protocol.game.ServerboundMovePlayerPacket.Pos.class, this::handlePlayerMove)
|
||||
.register(net.minecraft.network.protocol.game.ServerboundMovePlayerPacket.PosRot.class, this::handlePlayerMove);
|
||||
}
|
||||
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
if (!(msg instanceof Packet packet)) {
|
||||
super.write(ctx, msg, promise);
|
||||
} else {
|
||||
try {
|
||||
if (packet instanceof ClientboundBundlePacket bundle) {
|
||||
ArrayList<Packet<ClientGamePacketListener>> list = new ArrayList<>();
|
||||
|
||||
for (Packet<ClientGamePacketListener> subPacket : bundle.subPackets()) {
|
||||
Packet result = this.writeInterceptors.accept(subPacket);
|
||||
if (result != null) {
|
||||
list.add(result);
|
||||
|
||||
for (Packet p : this.writeInterceptors.acceptPost(result)) {
|
||||
list.add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!list.isEmpty()) {
|
||||
Packet var13 = new ClientboundBundlePacket(list);
|
||||
super.write(ctx, var13, promise);
|
||||
}
|
||||
} else {
|
||||
Packet var14 = this.writeInterceptors.accept(packet);
|
||||
if (var14 == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Collection<Packet> post = this.writeInterceptors.acceptPost(var14);
|
||||
if (post.isEmpty()) {
|
||||
super.write(ctx, var14, promise);
|
||||
} else {
|
||||
ArrayList<Packet<ClientGamePacketListener>> list = new ArrayList<>(post.size() + 1);
|
||||
list.add(var14);
|
||||
|
||||
for (Packet p : post) {
|
||||
list.add(p);
|
||||
}
|
||||
|
||||
Packet var15 = new ClientboundBundlePacket(list);
|
||||
super.write(ctx, var15, promise);
|
||||
}
|
||||
}
|
||||
} catch (Throwable var12) {
|
||||
var12.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void channelRead(@NotNull ChannelHandlerContext ctx, @NotNull Object msg) throws Exception {
|
||||
if (!(msg instanceof Packet)) {
|
||||
super.channelRead(ctx, msg);
|
||||
} else {
|
||||
Packet packet = this.readInterceptors.accept((Packet)msg);
|
||||
if (packet != null) {
|
||||
super.channelRead(ctx, packet);
|
||||
this.readInterceptors.acceptPost(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket handleAddEntity(ClientboundAddEntityPacket packet) {
|
||||
int id = packet.getId();
|
||||
this.markViewersDirty(id);
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private void markViewersDirty(int id) {
|
||||
ModeledEntity model = this.updaters.getModeledEntity(id);
|
||||
if (model != null) {
|
||||
model.getBase().getData().markViewersDirty();
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddPlayerPacket handleAddPlayer(ClientboundAddPlayerPacket packet) {
|
||||
int id = packet.getEntityId();
|
||||
this.markViewersDirty(id);
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private List<Packet> handleAddEntityPost(ClientboundAddEntityPacket packet) {
|
||||
return this.handleMount(packet.getUUID());
|
||||
}
|
||||
|
||||
private List<Packet> handleAddPlayerPost(ClientboundAddPlayerPacket packet) {
|
||||
return this.handleMount(packet.getPlayerId());
|
||||
}
|
||||
|
||||
private List<Packet> handleMount(UUID uuid) {
|
||||
Pair<ActiveModel, MountController> pair = ModelEngineAPI.getMountPairManager().get(uuid);
|
||||
if (pair == null) {
|
||||
return null;
|
||||
} else {
|
||||
ActiveModel model = (ActiveModel)pair.left();
|
||||
ArrayList<Packet> list = new ArrayList<>();
|
||||
model.getBehaviorRenderer(BoneBehaviorTypes.MOUNT).ifPresent(behaviorRenderer -> {
|
||||
if (behaviorRenderer instanceof MountRenderer renderer) {
|
||||
MountController controller = (MountController)pair.right();
|
||||
if (controller.getMount() instanceof BoneBehavior behavior) {
|
||||
Mount mount = renderer.getRendered().get(behavior.getBone().getBoneId());
|
||||
CollectionDataTracker<Integer> ids = mount.getPassengers();
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(mount.getMountId());
|
||||
buf.writeVarInt(ids.size());
|
||||
ids.forEach(buf::writeVarInt);
|
||||
list.add(new ClientboundSetPassengersPacket(buf));
|
||||
}
|
||||
}
|
||||
});
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundRemoveEntitiesPacket handleRemoveEntities(ClientboundRemoveEntitiesPacket packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int size = buf.readVarInt();
|
||||
HashSet<Integer> set = new HashSet<>(size);
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
int id = buf.readVarInt();
|
||||
this.markViewersDirty(id);
|
||||
if (ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE) {
|
||||
set.add(id);
|
||||
}
|
||||
}
|
||||
|
||||
if (set.size() == size) {
|
||||
return packet;
|
||||
} else {
|
||||
buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(set.size());
|
||||
|
||||
for (int id : set) {
|
||||
buf.writeVarInt(id);
|
||||
}
|
||||
|
||||
return new ClientboundRemoveEntitiesPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private <U extends PacketListener, T extends Packet<U>> Packet<U> handleEntityId(T packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int id = buf.readVarInt();
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private <U extends PacketListener, T extends Packet<U>> Packet<U> handleEntityId(T packet, MethodHandle idGetter) {
|
||||
int id;
|
||||
try {
|
||||
id = (int)idGetter.invoke((Packet)packet);
|
||||
} catch (Throwable var5) {
|
||||
throw new IllegalStateException(var5);
|
||||
}
|
||||
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundSetEntityMotionPacket handleEntityMotion(ClientboundSetEntityMotionPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundTeleportEntityPacket handleTeleportEntity(ClientboundTeleportEntityPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundAnimatePacket handleAnimate(ClientboundAnimatePacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket handleEntityData(ClientboundSetEntityDataPacket packet) {
|
||||
CancelType type = ModelEngineAPI.shouldShow(this.player, packet.id());
|
||||
if (type == CancelType.HIDE) {
|
||||
return null;
|
||||
} else if (packet.id() != this.player.getEntityId() && type == CancelType.SHOW) {
|
||||
return packet;
|
||||
} else {
|
||||
ModeledEntity modeledEntity = this.updaters.getModeledEntity(packet.id());
|
||||
if (modeledEntity == null) {
|
||||
return packet;
|
||||
} else {
|
||||
if (this.entityHandler.isForcedInvisible(modeledEntity.getBase().getUUID())) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(packet.id());
|
||||
|
||||
for (DataValue<?> item : packet.packedItems()) {
|
||||
if (item.id() == 0) {
|
||||
byte data = (Byte)item.value();
|
||||
data = TMath.setBit(data, 5, true);
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, data);
|
||||
} else {
|
||||
EntityUtils.writeDataUnsafe(buf, item.id(), item.serializer(), item.value());
|
||||
}
|
||||
}
|
||||
|
||||
buf.writeByte(255);
|
||||
packet = new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEquipmentPacket handleSetEquipment(ClientboundSetEquipmentPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getEntity()) == CancelType.SHOW ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundRemoveMobEffectPacket handleRemoveMobEffect(ClientboundRemoveMobEffectPacket packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int id = buf.readVarInt();
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundUpdateMobEffectPacket handleUpdateMobEffect(ClientboundUpdateMobEffectPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getEntityId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundKeepAlivePacket handleKeepAlive(ClientboundKeepAlivePacket packet) {
|
||||
if (this.desyncMonitor.clientTickShifted() || this.desyncMonitor.shouldRetest()) {
|
||||
this.desyncMonitor.startTest();
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket handlePassengers(ClientboundSetPassengersPacket packet) {
|
||||
PivotOverrideRegistry registry = ModelEngineAPI.getPivotOverrideRegistry();
|
||||
Optional<PivotOverride> maybeOverride = registry.get(packet.getVehicle());
|
||||
if (maybeOverride.isEmpty()) {
|
||||
return packet;
|
||||
} else {
|
||||
PivotOverride override = maybeOverride.get();
|
||||
Collection<Integer> extra = override.getPassengers().get();
|
||||
if (extra.isEmpty()) {
|
||||
return packet;
|
||||
} else {
|
||||
int[] existing = packet.getPassengers();
|
||||
IntLinkedOpenHashSet merged = new IntLinkedOpenHashSet(existing.length + extra.size());
|
||||
|
||||
for (int id : existing) {
|
||||
merged.add(id);
|
||||
}
|
||||
|
||||
boolean changed = false;
|
||||
|
||||
for (int id : extra) {
|
||||
if (merged.add(id)) {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!changed) {
|
||||
return packet;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(packet.getVehicle());
|
||||
buf.writeVarInt(merged.size());
|
||||
IntListIterator var16 = merged.iterator();
|
||||
|
||||
while (var16.hasNext()) {
|
||||
int idx = (Integer)var16.next();
|
||||
buf.writeVarInt(idx);
|
||||
}
|
||||
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ServerboundInteractPacket handleInteract(ServerboundInteractPacket packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int entityId = buf.readVarInt();
|
||||
if (entityId == DynamicHitbox.getHitboxId()) {
|
||||
DynamicHitbox hitbox = ModelEngineAPI.getInteractionTracker().getDynamicHitbox(this.player.getUniqueId());
|
||||
if (hitbox != null) {
|
||||
return new ServerboundInteractPacketWrapper(entityId, hitbox.getTarget(), packet);
|
||||
}
|
||||
}
|
||||
|
||||
ActiveModel activeModel = ModelEngineAPI.getInteractionTracker().getModelRelay(entityId);
|
||||
if (activeModel != null) {
|
||||
ModeledEntity modeledEntity = activeModel.getModeledEntity();
|
||||
if (modeledEntity == null) {
|
||||
return packet;
|
||||
} else {
|
||||
int id = modeledEntity.getBase().getEntityId();
|
||||
return new ServerboundInteractPacketWrapper(entityId, id, packet);
|
||||
}
|
||||
} else {
|
||||
Integer relayed = ModelEngineAPI.getInteractionTracker().getEntityRelay(entityId);
|
||||
return relayed != null ? new ServerboundInteractPacketWrapper(entityId, relayed, packet) : packet;
|
||||
}
|
||||
}
|
||||
|
||||
private ServerboundPlayerInputPacket handlePlayerInput(ServerboundPlayerInputPacket inputPacket) {
|
||||
MountController controller = ModelEngineAPI.getMountPairManager().getController(this.player.getUniqueId());
|
||||
if (controller != null) {
|
||||
MountInput input = controller.getInput();
|
||||
if (input == null) {
|
||||
controller.setInput(new MountInput(inputPacket.getXxa(), inputPacket.getZza(), inputPacket.isJumping(), inputPacket.isShiftKeyDown()));
|
||||
} else {
|
||||
input.setSide(inputPacket.getXxa());
|
||||
input.setFront(inputPacket.getZza());
|
||||
input.setJump(inputPacket.isJumping());
|
||||
input.setSneak(inputPacket.isShiftKeyDown());
|
||||
}
|
||||
}
|
||||
|
||||
return inputPacket;
|
||||
}
|
||||
|
||||
private ServerboundPongPacket handlePong(ServerboundPongPacket packet) {
|
||||
this.desyncMonitor.recordPongTime(System.currentTimeMillis());
|
||||
return null;
|
||||
}
|
||||
|
||||
private ServerboundMovePlayerPacket handlePlayerMove(ServerboundMovePlayerPacket packet) {
|
||||
this.desyncMonitor.recordClientSyncTime(System.currentTimeMillis());
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.network;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ticxo.modelengine.api.nms.network.ClientDesyncMonitor;
|
||||
import com.ticxo.modelengine.api.nms.network.NetworkHandler;
|
||||
import com.ticxo.modelengine.api.nms.network.PipelineWrapper;
|
||||
import com.ticxo.modelengine.api.nms.network.ProtectedPacketUnpacker;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.NMSFields;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.Bundler;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.Map.Entry;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientboundPingPacket;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class NetworkHandlerImpl implements NetworkHandler {
|
||||
public static NetworkHandlerImpl instance;
|
||||
private final Map<UUID, PipelineWrapper> pipelines = Maps.newConcurrentMap();
|
||||
private final Map<UUID, Bundler> bundles = Maps.newConcurrentMap();
|
||||
private volatile boolean isBatching;
|
||||
|
||||
public NetworkHandlerImpl() {
|
||||
if (instance != null) {
|
||||
throw new IllegalStateException("Network handler already initialized");
|
||||
} else {
|
||||
instance = this;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getProtocolVersion() {
|
||||
return SharedConstants.getProtocolVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<PipelineWrapper> getPipeline(UUID uuid) {
|
||||
return Optional.ofNullable(this.pipelines.get(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePipeline(UUID uuid) {
|
||||
this.pipelines.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectChannel(Player player) {
|
||||
ServerGamePacketListenerImpl listener = ((CraftPlayer)player).getHandle().connection;
|
||||
Connection connection = ReflectionUtils.get(listener, NMSFields.SERVER_GAME_PACKET_LISTENER_IMPL_connection);
|
||||
ChannelPipeline pipeline = connection.channel.pipeline();
|
||||
PipelineWrapper wrapper = new PipelineWrapper(player, connection.channel);
|
||||
ModelEngineChannelHandler handler = new ModelEngineChannelHandler(player, wrapper);
|
||||
this.pipelines.put(player.getUniqueId(), wrapper);
|
||||
this.bundles.put(player.getUniqueId(), new Bundler());
|
||||
|
||||
for (String name : pipeline.toMap().keySet()) {
|
||||
if (pipeline.get(name) instanceof Connection) {
|
||||
pipeline.addBefore(name, "protected_packet_unpacker", new ProtectedPacketUnpacker());
|
||||
pipeline.addBefore(name, "model_engine_packet_handler", handler);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ejectChannel(Player player) {
|
||||
ServerGamePacketListenerImpl listener = ((CraftPlayer)player).getHandle().connection;
|
||||
Connection connection = ReflectionUtils.get(listener, NMSFields.SERVER_GAME_PACKET_LISTENER_IMPL_connection);
|
||||
Channel channel = connection.channel;
|
||||
channel.eventLoop().submit(() -> {
|
||||
channel.pipeline().remove("protected_packet_unpacker");
|
||||
channel.pipeline().remove("model_engine_packet_handler");
|
||||
return null;
|
||||
});
|
||||
this.removePipeline(player.getUniqueId());
|
||||
this.bundles.remove(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ping(UUID uuid) {
|
||||
this.getPipeline(uuid).ifPresent(wrapper -> {
|
||||
ClientDesyncMonitor monitor = wrapper.getDesyncMonitor();
|
||||
if (monitor.shouldPing()) {
|
||||
monitor.markPingSent();
|
||||
int id = monitor.getPingCounter().getAndIncrement();
|
||||
NetworkUtils.sendNow(uuid, new ClientboundPingPacket(id));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startBatch() {
|
||||
this.isBatching = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endBatch() {
|
||||
this.isBatching = false;
|
||||
|
||||
for (Entry<UUID, Bundler> entry : this.bundles.entrySet()) {
|
||||
PipelineWrapper pipeline = this.pipelines.get(entry.getKey());
|
||||
if (pipeline != null) {
|
||||
Bundler bundler = entry.getValue();
|
||||
synchronized (bundler) {
|
||||
bundler.bundle(pipeline::writeAndFlush);
|
||||
bundler.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void appendPacket(UUID uuid, Packet packet) {
|
||||
Bundler bundler = this.bundles.get(uuid);
|
||||
if (bundler != null) {
|
||||
bundler.appendPacket(packet);
|
||||
}
|
||||
}
|
||||
|
||||
public void appendPackets(UUID uuid, Collection<? extends Packet> collection) {
|
||||
Bundler bundler = this.bundles.get(uuid);
|
||||
if (bundler != null) {
|
||||
bundler.appendPacket(collection);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBatching() {
|
||||
return this.isBatching;
|
||||
}
|
||||
}
|
||||
+399
@@ -0,0 +1,399 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.network.patch;
|
||||
|
||||
import com.destroystokyo.paper.event.player.PlayerUseUnknownEntityEvent;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import com.mojang.logging.LogUtils;
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.ServerInfo;
|
||||
import com.ticxo.modelengine.api.entity.BaseEntity;
|
||||
import com.ticxo.modelengine.api.events.BaseEntityInteractEvent;
|
||||
import com.ticxo.modelengine.api.events.BaseEntityInteractEvent.Action;
|
||||
import com.ticxo.modelengine.api.model.ActiveModel;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.NMSMethods;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import net.minecraft.advancements.CriteriaTriggers;
|
||||
import net.minecraft.network.protocol.PacketUtils;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
|
||||
import net.minecraft.network.protocol.game.ServerGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundUseItemPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket.ActionType;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket.Handler;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.ExperienceOrb;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.animal.Bucketable;
|
||||
import net.minecraft.world.entity.animal.allay.Allay;
|
||||
import net.minecraft.world.entity.item.ItemEntity;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.entity.projectile.AbstractArrow;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.level.ClipContext;
|
||||
import net.minecraft.world.level.GameType;
|
||||
import net.minecraft.world.level.ClipContext.Block;
|
||||
import net.minecraft.world.level.ClipContext.Fluid;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.HitResult;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraft.world.phys.HitResult.Type;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.CraftServer;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftPlayer;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.event.CraftEventFactory;
|
||||
import org.bukkit.event.Event.Result;
|
||||
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.util.Consumer;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class PatchedServerGamePacketListener {
|
||||
private static final Logger LOGGER = LogUtils.getLogger();
|
||||
|
||||
public static void handleInteract(ServerboundInteractPacketWrapper interactPacket, ServerGamePacketListener listener) {
|
||||
if (listener instanceof ServerGamePacketListenerImpl connection) {
|
||||
PacketUtils.ensureRunningOnSameThread(interactPacket, connection, connection.getPlayer().getLevel());
|
||||
final ServerPlayer player = connection.getPlayer();
|
||||
if (!player.isImmobile()) {
|
||||
final CraftPlayer craftPlayer = player.getBukkitEntity();
|
||||
final ServerLevel serverLevel = player.getLevel();
|
||||
final CraftServer craftServer = serverLevel.getServer().server;
|
||||
final Entity entity = interactPacket.getTarget(serverLevel);
|
||||
interactPacket.dispatch(new Handler() {
|
||||
public void onInteraction(InteractionHand hand) {
|
||||
if (interactPacket.isFakeInteraction()) {
|
||||
EntityHandler entityHandler = ModelEngineAPI.getNMSHandler().getEntityHandler();
|
||||
entityHandler.forceUseItem(craftPlayer, EquipmentSlot.HAND);
|
||||
entityHandler.forceUseItem(craftPlayer, EquipmentSlot.OFF_HAND);
|
||||
}
|
||||
}
|
||||
|
||||
public void onInteraction(InteractionHand hand, Vec3 pos) {
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
}
|
||||
});
|
||||
if (entity != player || player.isSpectator()) {
|
||||
player.resetLastActionTime();
|
||||
player.setShiftKeyDown(interactPacket.isUsingSecondaryAction());
|
||||
final ActiveModel activeModel = ModelEngineAPI.getInteractionTracker().getModelRelay(interactPacket.getOriginalId());
|
||||
if (activeModel != null) {
|
||||
final BaseEntity<?> base = activeModel.getModeledEntity().getBase();
|
||||
interactPacket.dispatch(
|
||||
new Handler() {
|
||||
public void onInteraction(InteractionHand hand) {
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT,
|
||||
EquipmentSlot.HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.HAND),
|
||||
null
|
||||
)
|
||||
);
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT,
|
||||
EquipmentSlot.OFF_HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.OFF_HAND),
|
||||
null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onInteraction(InteractionHand hand, Vec3 pos) {
|
||||
Vector position = new Vector(pos.x, pos.y, pos.z);
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT_ON,
|
||||
EquipmentSlot.HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.HAND),
|
||||
position
|
||||
)
|
||||
);
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT_ON,
|
||||
EquipmentSlot.OFF_HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.OFF_HAND),
|
||||
position
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.ATTACK,
|
||||
EquipmentSlot.HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.HAND),
|
||||
null
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (entity == null) {
|
||||
if (ServerInfo.IS_PAPER) {
|
||||
interactPacket.dispatch(new Handler() {
|
||||
public void onInteraction(@NotNull InteractionHand hand) {
|
||||
PatchedServerGamePacketListener.callPlayerUseUnknownEntityEvent(craftServer, craftPlayer, interactPacket, hand);
|
||||
}
|
||||
|
||||
public void onInteraction(@NotNull InteractionHand hand, @NotNull Vec3 pos) {
|
||||
PatchedServerGamePacketListener.callPlayerUseUnknownEntityEvent(craftServer, craftPlayer, interactPacket, hand);
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
PatchedServerGamePacketListener.callPlayerUseUnknownEntityEvent(craftServer, craftPlayer, interactPacket, InteractionHand.MAIN_HAND);
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (serverLevel.getWorldBorder().isWithinBounds(entity.blockPosition())) {
|
||||
AABB boundingBox = entity.getBoundingBox();
|
||||
if (!(boundingBox.distanceToSqr(player.getEyePosition()) >= ServerGamePacketListenerImpl.MAX_INTERACTION_DISTANCE)) {
|
||||
interactPacket.dispatch(
|
||||
new Handler() {
|
||||
private void performInteraction(
|
||||
InteractionHand enumhand, PatchedServerGamePacketListener.EntityInteraction entityInteraction, PlayerInteractEntityEvent event
|
||||
) {
|
||||
ItemStack itemstack = player.getItemInHand(enumhand);
|
||||
if (itemstack.isItemEnabled(serverLevel.enabledFeatures())) {
|
||||
ItemStack itemstack1 = itemstack.copy();
|
||||
ItemStack itemInHand = player.getItemInHand(enumhand);
|
||||
boolean triggerLeashUpdate = itemInHand.getItem() == Items.LEAD && entity instanceof Mob;
|
||||
Item origItem = player.getInventory().getSelected().getItem();
|
||||
craftServer.getPluginManager().callEvent(event);
|
||||
if (entity instanceof Bucketable
|
||||
&& entity instanceof LivingEntity
|
||||
&& origItem.asItem() == Items.WATER_BUCKET
|
||||
&& (event.isCancelled() || player.getInventory().getSelected().getItem() != origItem)) {
|
||||
entity.getEntityData().resendPossiblyDesyncedEntity(player);
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
|
||||
if (triggerLeashUpdate && (event.isCancelled() || player.getInventory().getSelected().getItem() != origItem)) {
|
||||
connection.send(new ClientboundSetEntityLinkPacket(entity, ((Mob)entity).getLeashHolder()));
|
||||
}
|
||||
|
||||
if (event.isCancelled() || player.getInventory().getSelected().getItem() != origItem) {
|
||||
entity.getEntityData().refresh(player);
|
||||
if (entity instanceof Allay allay) {
|
||||
connection.send(
|
||||
new ClientboundSetEquipmentPacket(
|
||||
entity.getId(),
|
||||
Arrays.stream(net.minecraft.world.entity.EquipmentSlot.values())
|
||||
.map(slot -> Pair.of(slot, allay.stripMeta(allay.getItemBySlot(slot), true)))
|
||||
.collect(Collectors.toList())
|
||||
)
|
||||
);
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
}
|
||||
|
||||
if (event.isCancelled()) {
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
return;
|
||||
}
|
||||
|
||||
InteractionResult enuminteractionresult = entityInteraction.run(player, entity, enumhand);
|
||||
if (!itemInHand.isEmpty() && itemInHand.getCount() <= -1) {
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
|
||||
if (enuminteractionresult.consumesAction()) {
|
||||
CriteriaTriggers.PLAYER_INTERACTED_WITH_ENTITY.trigger(player, itemstack1, entity);
|
||||
if (enuminteractionresult.shouldSwing()) {
|
||||
player.swing(enumhand, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onInteraction(@NotNull InteractionHand hand) {
|
||||
this.performInteraction(
|
||||
hand,
|
||||
Player::interactOn,
|
||||
new PlayerInteractEntityEvent(
|
||||
connection.getCraftPlayer(),
|
||||
entity.getBukkitEntity(),
|
||||
hand == InteractionHand.OFF_HAND ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onInteraction(@NotNull InteractionHand hand, @NotNull Vec3 pos) {
|
||||
this.performInteraction(
|
||||
hand,
|
||||
(entityplayer, entity1, enumhand1) -> entity1.interactAt(entityplayer, pos, enumhand1),
|
||||
new PlayerInteractAtEntityEvent(
|
||||
connection.getCraftPlayer(),
|
||||
entity.getBukkitEntity(),
|
||||
new Vector(pos.x, pos.y, pos.z),
|
||||
hand == InteractionHand.OFF_HAND ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
if (!(entity instanceof ItemEntity)
|
||||
&& !(entity instanceof ExperienceOrb)
|
||||
&& !(entity instanceof AbstractArrow)
|
||||
&& (entity != player || player.isSpectator())) {
|
||||
ItemStack itemstack = player.getItemInHand(InteractionHand.MAIN_HAND);
|
||||
if (itemstack.isItemEnabled(serverLevel.enabledFeatures())) {
|
||||
player.attack(entity);
|
||||
if (!itemstack.isEmpty() && itemstack.getCount() <= -1) {
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void handleUseItem(ServerboundUseItemPacket packet, ServerGamePacketListener listener, Consumer<InteractionResult> afterUse) {
|
||||
if (listener instanceof ServerGamePacketListenerImpl connection) {
|
||||
ServerPlayer player = connection.getPlayer();
|
||||
PacketUtils.ensureRunningOnSameThread(packet, connection, player.getLevel());
|
||||
if (!player.isImmobile()) {
|
||||
boolean shouldLimit = Boolean.TRUE
|
||||
.equals(ReflectionUtils.call(connection, NMSMethods.SERVER_GAME_PACKET_LISTENER_IMPL_checkLimit, packet.timestamp));
|
||||
if (shouldLimit) {
|
||||
connection.ackBlockChangesUpTo(packet.getSequence());
|
||||
ServerLevel worldserver = player.getLevel();
|
||||
InteractionHand enumhand = packet.getHand();
|
||||
ItemStack itemstack = player.getItemInHand(enumhand);
|
||||
player.resetLastActionTime();
|
||||
if (!itemstack.isEmpty() && itemstack.isItemEnabled(worldserver.enabledFeatures())) {
|
||||
float f1 = player.getXRot();
|
||||
float f2 = player.getYRot();
|
||||
double d0 = player.getX();
|
||||
double d1 = player.getY() + player.getEyeHeight();
|
||||
double d2 = player.getZ();
|
||||
Vec3 vec3d = new Vec3(d0, d1, d2);
|
||||
float f3 = Mth.cos(-f2 * (float) (Math.PI / 180.0) - (float) Math.PI);
|
||||
float f4 = Mth.sin(-f2 * (float) (Math.PI / 180.0) - (float) Math.PI);
|
||||
float f5 = -Mth.cos(-f1 * (float) (Math.PI / 180.0));
|
||||
float f6 = Mth.sin(-f1 * (float) (Math.PI / 180.0));
|
||||
float f7 = f4 * f5;
|
||||
float f8 = f3 * f5;
|
||||
double d3 = player.gameMode.getGameModeForPlayer() == GameType.CREATIVE ? 5.0 : 4.5;
|
||||
Vec3 vec3d1 = vec3d.add(f7 * d3, f6 * d3, f8 * d3);
|
||||
HitResult movingobjectposition = player.level.clip(new ClipContext(vec3d, vec3d1, Block.OUTLINE, Fluid.NONE, player));
|
||||
boolean cancelled;
|
||||
if (movingobjectposition != null && movingobjectposition.getType() == Type.BLOCK) {
|
||||
BlockHitResult movingobjectpositionblock = (BlockHitResult)movingobjectposition;
|
||||
if (player.gameMode.firedInteract
|
||||
&& player.gameMode.interactPosition.equals(movingobjectpositionblock.getBlockPos())
|
||||
&& player.gameMode.interactHand == enumhand
|
||||
&& ItemStack.tagMatches(player.gameMode.interactItemStack, itemstack)) {
|
||||
cancelled = player.gameMode.interactResult;
|
||||
} else {
|
||||
PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(
|
||||
player,
|
||||
org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK,
|
||||
movingobjectpositionblock.getBlockPos(),
|
||||
movingobjectpositionblock.getDirection(),
|
||||
itemstack,
|
||||
true,
|
||||
enumhand
|
||||
);
|
||||
cancelled = event.useItemInHand() == Result.DENY;
|
||||
}
|
||||
|
||||
player.gameMode.firedInteract = false;
|
||||
} else {
|
||||
PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(
|
||||
player, org.bukkit.event.block.Action.RIGHT_CLICK_AIR, itemstack, enumhand
|
||||
);
|
||||
cancelled = event.useItemInHand() == Result.DENY;
|
||||
}
|
||||
|
||||
if (cancelled) {
|
||||
player.getBukkitEntity().updateInventory();
|
||||
return;
|
||||
}
|
||||
|
||||
itemstack = player.getItemInHand(enumhand);
|
||||
if (itemstack.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
InteractionResult enuminteractionresult = player.gameMode.useItem(player, worldserver, itemstack, enumhand);
|
||||
afterUse.accept(enuminteractionresult);
|
||||
if (enuminteractionresult.shouldSwing()) {
|
||||
player.swing(enumhand, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void callPlayerUseUnknownEntityEvent(CraftServer cserver, CraftPlayer craftPlayer, ServerboundInteractPacket packet, InteractionHand hand) {
|
||||
cserver.getPluginManager()
|
||||
.callEvent(
|
||||
new PlayerUseUnknownEntityEvent(
|
||||
craftPlayer,
|
||||
packet.getEntityId(),
|
||||
packet.getActionType() == ActionType.ATTACK,
|
||||
hand == InteractionHand.MAIN_HAND ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface EntityInteraction {
|
||||
InteractionResult run(ServerPlayer var1, Entity var2, InteractionHand var3);
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.network.patch;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.interaction.InteractionTracker;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.game.ServerGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket.Handler;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ServerboundInteractPacketWrapper extends ServerboundInteractPacket {
|
||||
private final int originalId;
|
||||
private final int relayedId;
|
||||
private final ServerboundInteractPacket original;
|
||||
|
||||
public ServerboundInteractPacketWrapper(int originalId, int relayedId, ServerboundInteractPacket original) {
|
||||
super(NetworkUtils.readData(original));
|
||||
this.originalId = originalId;
|
||||
this.relayedId = relayedId;
|
||||
this.original = original;
|
||||
}
|
||||
|
||||
public void write(FriendlyByteBuf var0) {
|
||||
this.original.write(var0);
|
||||
}
|
||||
|
||||
public void handle(ServerGamePacketListener var0) {
|
||||
PatchedServerGamePacketListener.handleInteract(this, var0);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Entity getTarget(ServerLevel var0) {
|
||||
return var0.getEntityOrPart(this.relayedId);
|
||||
}
|
||||
|
||||
public boolean isFakeInteraction() {
|
||||
InteractionTracker tracker = ModelEngineAPI.getInteractionTracker();
|
||||
return tracker.getModelRelay(this.originalId) != null || tracker.getEntityRelay(this.originalId) != null;
|
||||
}
|
||||
|
||||
public boolean isUsingSecondaryAction() {
|
||||
return this.original.isUsingSecondaryAction();
|
||||
}
|
||||
|
||||
public void dispatch(Handler var0) {
|
||||
this.original.dispatch(var0);
|
||||
}
|
||||
|
||||
public int getOriginalId() {
|
||||
return this.originalId;
|
||||
}
|
||||
|
||||
public int getRelayedId() {
|
||||
return this.relayedId;
|
||||
}
|
||||
|
||||
public ServerboundInteractPacket getOriginal() {
|
||||
return this.original;
|
||||
}
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.network.utils;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class BulkEntityDataEncoder {
|
||||
private static final byte PACKET_TYPE_BULK_DATA = 0;
|
||||
public static final int FIELD_TRANSLATION = 0;
|
||||
public static final int FIELD_LEFT_ROTATION = 1;
|
||||
public static final int FIELD_SCALE = 2;
|
||||
public static final int FIELD_RIGHT_ROTATION = 3;
|
||||
public static final int FIELD_TRANSFORM_DURATION = 4;
|
||||
public static final int FIELD_GLOW_DATA = 5;
|
||||
public static final int FIELD_BRIGHTNESS = 6;
|
||||
public static final int FIELD_RENDER_DATA = 7;
|
||||
private final FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer(256));
|
||||
private int entryCount;
|
||||
private final int entryCountIndex;
|
||||
|
||||
public BulkEntityDataEncoder() {
|
||||
this.buf.writeByte(0);
|
||||
this.entryCountIndex = this.buf.writerIndex();
|
||||
this.buf.writeVarInt(0);
|
||||
this.entryCount = 0;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter startEntry(int entityId) {
|
||||
this.entryCount++;
|
||||
this.buf.writeVarInt(entityId);
|
||||
return new BulkEntityDataEncoder.EntryWriter(this.buf);
|
||||
}
|
||||
|
||||
public byte[] build() {
|
||||
int endIndex = this.buf.writerIndex();
|
||||
this.buf.writerIndex(this.entryCountIndex);
|
||||
this.buf.writeVarInt(this.entryCount);
|
||||
this.buf.writerIndex(endIndex);
|
||||
byte[] data = new byte[this.buf.readableBytes()];
|
||||
this.buf.getBytes(this.buf.readerIndex(), data);
|
||||
this.buf.release();
|
||||
return data;
|
||||
}
|
||||
|
||||
public int getEntryCount() {
|
||||
return this.entryCount;
|
||||
}
|
||||
|
||||
public static short floatToHalf(float f) {
|
||||
int bits = Float.floatToRawIntBits(f);
|
||||
int sign = bits >>> 16 & 32768;
|
||||
int exp = (bits >>> 23 & 0xFF) - 127;
|
||||
int mantissa = bits & 8388607;
|
||||
if (exp >= 16) {
|
||||
return (short)(sign | 31744);
|
||||
} else if (exp >= -14) {
|
||||
return (short)(sign | exp + 15 << 10 | mantissa >>> 13);
|
||||
} else if (exp >= -24) {
|
||||
int shift = -14 - exp;
|
||||
return (short)(sign | (8388608 | mantissa) >>> 13 + shift);
|
||||
} else {
|
||||
return (short)sign;
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeHalf(FriendlyByteBuf buf, float f) {
|
||||
buf.writeShort(floatToHalf(f));
|
||||
}
|
||||
|
||||
public static class EntryWriter {
|
||||
private final FriendlyByteBuf buf;
|
||||
private final int bitmaskIndex;
|
||||
private byte bitmask;
|
||||
|
||||
EntryWriter(FriendlyByteBuf buf) {
|
||||
this.buf = buf;
|
||||
this.bitmaskIndex = buf.writerIndex();
|
||||
buf.writeByte(0);
|
||||
this.bitmask = 0;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter translation(Vector3f pos) {
|
||||
this.bitmask = (byte)(this.bitmask | 1);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, pos.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, pos.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, pos.z);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter leftRotation(Quaternionf rot) {
|
||||
this.bitmask = (byte)(this.bitmask | 2);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.z);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.w);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter scale(Vector3f scale) {
|
||||
this.bitmask = (byte)(this.bitmask | 4);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, scale.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, scale.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, scale.z);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter rightRotation(Quaternionf rot) {
|
||||
this.bitmask = (byte)(this.bitmask | 8);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.z);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.w);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter transformDuration(int duration) {
|
||||
this.bitmask = (byte)(this.bitmask | 16);
|
||||
this.buf.writeVarInt(duration);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter glowData(byte sharedData, int glowColor) {
|
||||
this.bitmask = (byte)(this.bitmask | 32);
|
||||
this.buf.writeByte(sharedData);
|
||||
this.buf.writeInt(glowColor);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter brightness(int brightness) {
|
||||
this.bitmask = (byte)(this.bitmask | 64);
|
||||
this.buf.writeInt(brightness);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter renderData(byte billboard, float viewRange, byte displayType) {
|
||||
this.bitmask = (byte)(this.bitmask | 128);
|
||||
this.buf.writeByte(billboard);
|
||||
this.buf.writeFloat(viewRange);
|
||||
this.buf.writeByte(displayType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void finish() {
|
||||
this.buf.setByte(this.bitmaskIndex, this.bitmask);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.network.utils;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.nms.network.ProtectedPacket;
|
||||
import com.ticxo.modelengine.api.utils.config.ConfigProperty;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
|
||||
|
||||
public class Bundler {
|
||||
private static int BUNDLE_SIZE;
|
||||
private final List<List<Packet<ClientGamePacketListener>>> bundles = new ArrayList<>();
|
||||
|
||||
public synchronized void appendPacket(Packet<ClientGamePacketListener> packet) {
|
||||
if (this.bundles.isEmpty()) {
|
||||
this.bundles.add(new ArrayList<>());
|
||||
}
|
||||
|
||||
List<Packet<ClientGamePacketListener>> bundleList = this.bundles.get(this.bundles.size() - 1);
|
||||
if (bundleList.size() == BUNDLE_SIZE) {
|
||||
bundleList = new ArrayList<>();
|
||||
this.bundles.add(bundleList);
|
||||
}
|
||||
|
||||
bundleList.add(packet);
|
||||
}
|
||||
|
||||
public synchronized void appendPacket(Collection<? extends Packet> packets) {
|
||||
if (!packets.isEmpty()) {
|
||||
List<Packet<ClientGamePacketListener>> bundleList = this.bundles.isEmpty() ? null : this.bundles.get(this.bundles.size() - 1);
|
||||
if (bundleList == null || bundleList.size() + packets.size() > BUNDLE_SIZE) {
|
||||
bundleList = new ArrayList<>();
|
||||
this.bundles.add(bundleList);
|
||||
}
|
||||
|
||||
for (Packet p : packets) {
|
||||
if (bundleList.size() == BUNDLE_SIZE) {
|
||||
bundleList = new ArrayList<>();
|
||||
this.bundles.add(bundleList);
|
||||
}
|
||||
|
||||
bundleList.add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.bundles.clear();
|
||||
}
|
||||
|
||||
public void bundle(Consumer<Object> send) {
|
||||
for (List<Packet<ClientGamePacketListener>> list : this.bundles) {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(new ClientboundBundlePacket(list));
|
||||
send.accept(wrapped);
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
ModelEngineAPI.getAPI().getConfigManager().registerReferenceUpdate(() -> BUNDLE_SIZE = Math.min(ConfigProperty.BUNDLE_SIZE.getInt(), 4096));
|
||||
}
|
||||
}
|
||||
+271
@@ -0,0 +1,271 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.network.utils;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.lod.AnimationLODHandler.LODTracker;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.nms.network.ProtectedPacket;
|
||||
import com.ticxo.modelengine.api.utils.data.PositionCodec;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.NetworkHandlerImpl;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Pos;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class NetworkUtils {
|
||||
public static FriendlyByteBuf createByteBuf() {
|
||||
return new FriendlyByteBuf(Unpooled.buffer());
|
||||
}
|
||||
|
||||
public static FriendlyByteBuf createByteBuf(Consumer<FriendlyByteBuf> consumer) {
|
||||
FriendlyByteBuf buf = createByteBuf();
|
||||
consumer.accept(buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
public static FriendlyByteBuf readData(Packet<?> packet) {
|
||||
FriendlyByteBuf buf = createByteBuf();
|
||||
packet.write(buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
public static Packets.PacketSupplier createPivotSpawn(int id, UUID uuid, Vector3f pos) {
|
||||
return playerUUID -> ModelEngineAPI.getPlayerProtocolVersion(playerUUID) >= 764
|
||||
? new ClientboundAddEntityPacket(id, uuid, pos.x, pos.y - 0.5, pos.z, 0.0F, 0.0F, EntityType.AREA_EFFECT_CLOUD, 0, Vec3.ZERO, 0.0)
|
||||
: new ClientboundAddEntityPacket(id, uuid, pos.x, pos.y - 0.375, pos.z, 0.0F, 0.0F, EntityType.AREA_EFFECT_CLOUD, 0, Vec3.ZERO, 0.0);
|
||||
}
|
||||
|
||||
public static Packets.PacketSupplier createPivotTeleport(int id, Vector3f pos) {
|
||||
FriendlyByteBuf buf = createByteBuf();
|
||||
buf.writeVarInt(id);
|
||||
buf.writeDouble(pos.x);
|
||||
buf.writeDouble(pos.y - 0.375);
|
||||
buf.writeDouble(pos.z);
|
||||
buf.writeByte(0);
|
||||
buf.writeByte(0);
|
||||
buf.writeBoolean(false);
|
||||
ClientboundTeleportEntityPacket tpHigher = new ClientboundTeleportEntityPacket(buf);
|
||||
buf = createByteBuf();
|
||||
buf.writeVarInt(id);
|
||||
buf.writeDouble(pos.x);
|
||||
buf.writeDouble(pos.y - 0.5);
|
||||
buf.writeDouble(pos.z);
|
||||
buf.writeByte(0);
|
||||
buf.writeByte(0);
|
||||
buf.writeBoolean(false);
|
||||
ClientboundTeleportEntityPacket tpLower = new ClientboundTeleportEntityPacket(buf);
|
||||
return playerUUID -> ModelEngineAPI.getPlayerProtocolVersion(playerUUID) >= 764 ? tpLower : tpHigher;
|
||||
}
|
||||
|
||||
public static Object createPivotMove(int id, @Nullable PositionCodec codec, Vector3f pos) {
|
||||
if (codec == null) {
|
||||
return createPivotTeleport(id, pos);
|
||||
} else {
|
||||
return switch (codec.update(pos.x, pos.y, pos.z)) {
|
||||
case UNCHANGED -> null;
|
||||
case MOVE -> new Pos(id, codec.getDeltaX(), codec.getDeltaY(), codec.getDeltaZ(), false);
|
||||
case ABSOLUTE -> createPivotTeleport(id, pos);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static Packets.PacketSupplier lodWrapper(ModeledEntity modeledEntity, BiFunction<UUID, LODTracker, Packet<ClientGamePacketListener>> packetFunction) {
|
||||
return uuid -> {
|
||||
LODTracker tracker = modeledEntity.getAnimationLodHandler().tick(uuid);
|
||||
return tracker.isCanSkip() ? null : packetFunction.apply(uuid, tracker);
|
||||
};
|
||||
}
|
||||
|
||||
public static void send(UUID target, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
handler.appendPacket(target, packet);
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void send(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
handler.appendPacket(player, packet);
|
||||
}
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void send(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet, Predicate<Player> predicate) {
|
||||
if (packet != null) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
handler.appendPacket(player, packet);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
|
||||
for (UUID playerx : targets) {
|
||||
Player p = Bukkit.getPlayer(playerx);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(playerx).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRaw(UUID target, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRaw(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRaw(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet, Predicate<Player> predicate) {
|
||||
if (packet != null) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendNow(UUID target, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlushNow(wrapped));
|
||||
}
|
||||
}
|
||||
|
||||
private static Packet<ClientGamePacketListener> bundle(Collection<Packet<ClientGamePacketListener>> packets) {
|
||||
return (Packet<ClientGamePacketListener>)(packets.size() == 1 ? packets.iterator().next() : new ClientboundBundlePacket(packets));
|
||||
}
|
||||
|
||||
public static void sendBundled(UUID target, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
collection.compile(target, packets -> {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
handler.appendPackets(target, packets);
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(bundle(packets));
|
||||
handler.getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundled(Set<UUID> targets, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
collection.compile(player, packets -> handler.appendPackets(player, packets));
|
||||
}
|
||||
} else {
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> collection.compile(player, packets -> {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(bundle(packets));
|
||||
pipeline.writeAndFlush(wrapped);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundled(Set<UUID> targets, Packets collection, Predicate<Player> predicate) {
|
||||
if (!collection.isEmpty()) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
collection.compile(player, packets -> handler.appendPackets(player, packets));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (UUID playerx : targets) {
|
||||
Player p = Bukkit.getPlayer(playerx);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(playerx).ifPresent(pipeline -> collection.compile(playerx, packets -> {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(bundle(packets));
|
||||
pipeline.writeAndFlush(wrapped);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundledRaw(UUID target, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
collection.compile(target, packets -> {
|
||||
Packet<ClientGamePacketListener> packet = bundle(packets);
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundledRaw(Set<UUID> targets, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> collection.compile(player, packets -> {
|
||||
Packet<ClientGamePacketListener> packet = bundle(packets);
|
||||
pipeline.writeAndFlush(packet);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundledRaw(Set<UUID> targets, Packets collection, Predicate<Player> predicate) {
|
||||
if (!collection.isEmpty()) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> collection.compile(player, packets -> {
|
||||
Packet<ClientGamePacketListener> packet = bundle(packets);
|
||||
pipeline.writeAndFlush(packet);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.network.utils;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ticxo.modelengine.api.utils.logger.TLogger;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
|
||||
public class PacketInterceptor {
|
||||
private final Map<Class<? extends Packet>, PacketInterceptor.Modifier<? extends Packet>> registry = Maps.newConcurrentMap();
|
||||
private final Map<Class<? extends Packet>, PacketInterceptor.Listener<? extends Packet>> postRegistry = Maps.newConcurrentMap();
|
||||
|
||||
public <T extends Packet> PacketInterceptor register(Class<T> clazz, Function<T, Packet> function) {
|
||||
this.registry.put(clazz, new Modifier<>(clazz, function));
|
||||
return this;
|
||||
}
|
||||
|
||||
public <T extends Packet> PacketInterceptor registerPost(Class<T> clazz, Function<T, Collection<Packet>> consumer) {
|
||||
this.postRegistry.put(clazz, new Listener<>(clazz, consumer));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packet accept(Packet original) {
|
||||
if (original == null) {
|
||||
return null;
|
||||
} else {
|
||||
PacketInterceptor.Modifier<?> modifier = this.registry.get(original.getClass());
|
||||
return modifier == null ? original : modifier.modify(original);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Packet> acceptPost(Packet original) {
|
||||
if (original == null) {
|
||||
return List.of();
|
||||
} else {
|
||||
PacketInterceptor.Listener<?> listener = this.postRegistry.get(original.getClass());
|
||||
return (Collection<Packet>)(listener != null ? listener.listen(original) : List.of());
|
||||
}
|
||||
}
|
||||
|
||||
static class Listener<T extends Packet> {
|
||||
private final Class<T> clazz;
|
||||
private final Function<T, Collection<Packet>> function;
|
||||
|
||||
public Collection<Packet> listen(Packet original) {
|
||||
try {
|
||||
Collection<Packet> collection = this.function.apply(this.clazz.cast(original));
|
||||
return (Collection<Packet>)(collection == null ? List.of() : collection);
|
||||
} catch (Throwable var3) {
|
||||
TLogger.error("An error had occurred while intercepting the packet " + this.clazz.getSimpleName());
|
||||
var3.printStackTrace();
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
public Listener(Class<T> clazz, Function<T, Collection<Packet>> function) {
|
||||
this.clazz = clazz;
|
||||
this.function = function;
|
||||
}
|
||||
}
|
||||
|
||||
static class Modifier<T extends Packet> {
|
||||
private final Class<T> clazz;
|
||||
private final Function<T, Packet> function;
|
||||
|
||||
public Packet modify(Packet original) {
|
||||
try {
|
||||
return this.function.apply(this.clazz.cast(original));
|
||||
} catch (Throwable var3) {
|
||||
TLogger.error("An error had occurred while modifying the packet " + this.clazz.getSimpleName());
|
||||
var3.printStackTrace();
|
||||
return original;
|
||||
}
|
||||
}
|
||||
|
||||
public Modifier(Class<T> clazz, Function<T, Packet> function) {
|
||||
this.clazz = clazz;
|
||||
this.function = function;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.network.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class Packets extends ArrayList<Object> {
|
||||
public Packets(int initialCapacity, float loadFactor) {
|
||||
super(initialCapacity);
|
||||
}
|
||||
|
||||
public Packets(int initialCapacity) {
|
||||
super(initialCapacity);
|
||||
}
|
||||
|
||||
public Packets() {
|
||||
}
|
||||
|
||||
public Packets(@NotNull Collection<?> c) {
|
||||
super((Collection<? extends Object>)c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Object obj) {
|
||||
return obj == null ? false : super.add(obj);
|
||||
}
|
||||
|
||||
public boolean add(Packet<ClientGamePacketListener> packet) {
|
||||
return packet == null ? false : super.add(packet);
|
||||
}
|
||||
|
||||
private Collection<Packet<ClientGamePacketListener>> compile(UUID player) {
|
||||
Collection<Packet<ClientGamePacketListener>> list = new ArrayList<>();
|
||||
|
||||
for (Object obj : this) {
|
||||
if (obj instanceof Packet) {
|
||||
list.add((Packet<ClientGamePacketListener>)obj);
|
||||
} else if (obj instanceof Packets.PacketSupplier supplier) {
|
||||
Packet<ClientGamePacketListener> packet = supplier.supply(player);
|
||||
if (packet != null) {
|
||||
list.add(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public void compile(UUID player, Consumer<Collection<Packet<ClientGamePacketListener>>> ifNotEmpty) {
|
||||
Collection<Packet<ClientGamePacketListener>> set = this.compile(player);
|
||||
if (!set.isEmpty()) {
|
||||
ifNotEmpty.accept(set);
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PacketSupplier {
|
||||
Packet<ClientGamePacketListener> supply(UUID var1);
|
||||
}
|
||||
}
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.HeldItemRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.HeldItemRenderer.Item;
|
||||
import com.ticxo.modelengine.api.utils.data.PositionCodec;
|
||||
import com.ticxo.modelengine.api.utils.data.tracker.CollectionDataTracker;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.Packets;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Pos;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.inventory.CraftItemStack;
|
||||
import org.joml.Quaternionf;
|
||||
|
||||
public class HeldItemParser implements BehaviorRendererParser<HeldItemRenderer> {
|
||||
private float viewRange = 4096.0F;
|
||||
|
||||
public void sendToClients(HeldItemRenderer renderer) {
|
||||
this.viewRange = renderer.getActiveModel().getViewRange();
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
public void destroy(HeldItemRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, HeldItemRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
Location location = renderer.getActiveModel().getModeledEntity().getBase().getLocation();
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotSpawn(location, renderer));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(renderer));
|
||||
|
||||
for (Item item : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemSpawn(location, item));
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, true));
|
||||
}
|
||||
|
||||
for (Item item : renderer.getSpawnQueue().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemSpawn(location, item));
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, true));
|
||||
}
|
||||
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(renderer));
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, HeldItemRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
set.add(this.teleport(renderer));
|
||||
|
||||
for (Item item : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, false));
|
||||
}
|
||||
|
||||
Location location = renderer.getActiveModel().getModeledEntity().getBase().getLocation();
|
||||
|
||||
for (Item item : renderer.getSpawnQueue().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemSpawn(location, item));
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, true));
|
||||
}
|
||||
|
||||
Map<String, Item> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size());
|
||||
destroy.forEach((s, item) -> buf.writeVarInt(item.getId()));
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
if (renderer.getPassengers().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(renderer));
|
||||
renderer.getPassengers().clearDirty();
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, HeldItemRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Map<String, Item> items = renderer.getRendered();
|
||||
buf.writeVarInt(1 + items.size());
|
||||
buf.writeVarInt(renderer.getId());
|
||||
items.forEach((s, item) -> buf.writeVarInt(item.getId()));
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket pivotSpawn(Location location, HeldItemRenderer renderer) {
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getId(), renderer.getUuid(), location.getX(), location.getY(), location.getZ(), 0.0F, 0.0F, EntityType.ARMOR_STAND, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(HeldItemRenderer renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 15, EntityDataSerializers.BYTE, (byte)16);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket itemSpawn(Location location, Item renderer) {
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getId(), renderer.getUuid(), location.getX(), location.getY(), location.getZ(), 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket itemData(Item renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
if (spawn) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
EntityUtils.writeData(buf, 9, EntityDataSerializers.INT, 1);
|
||||
EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, this.viewRange);
|
||||
} else if (renderer.isTransformDirty()) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
}
|
||||
|
||||
renderer.getGlowing().ifDirty(flag -> EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)(flag ? 96 : 32)), spawn);
|
||||
renderer.getGlowColor().ifDirty(color -> EntityUtils.writeData(buf, 21, EntityDataSerializers.INT, color), spawn);
|
||||
renderer.getPosition().ifDirty(vector3f -> EntityUtils.writeData(buf, 10, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
renderer.getScale().ifDirty(vector3f -> EntityUtils.writeData(buf, 11, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
renderer.getRotation()
|
||||
.ifDirty(
|
||||
quaternionf -> EntityUtils.writeData(buf, 12, EntityDataSerializers.QUATERNION, quaternionf.rotateY((float) Math.PI, new Quaternionf())), spawn
|
||||
);
|
||||
renderer.getModel().ifDirty(itemStack -> EntityUtils.writeData(buf, 22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(itemStack)), spawn);
|
||||
renderer.getDisplay()
|
||||
.ifDirty(display -> EntityUtils.writeData(buf, 23, EntityDataSerializers.BYTE, display == null ? 0 : (byte)display.ordinal()), spawn);
|
||||
buf.writeByte(255);
|
||||
renderer.clearDirty();
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private Object teleport(HeldItemRenderer renderer) {
|
||||
Location location = renderer.getActiveModel().getModeledEntity().getBase().getLocation();
|
||||
PositionCodec codec = renderer.getPositionCodec();
|
||||
if (codec != null) {
|
||||
switch (codec.update(location.getX(), location.getY(), location.getZ())) {
|
||||
case UNCHANGED:
|
||||
return null;
|
||||
case MOVE:
|
||||
return new Pos(renderer.getId(), codec.getDeltaX(), codec.getDeltaY(), codec.getDeltaZ(), false);
|
||||
case ABSOLUTE:
|
||||
}
|
||||
}
|
||||
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
buf.writeDouble(location.getX());
|
||||
buf.writeDouble(location.getY());
|
||||
buf.writeDouble(location.getZ());
|
||||
buf.writeByte(0);
|
||||
buf.writeByte(0);
|
||||
buf.writeBoolean(false);
|
||||
return new ClientboundTeleportEntityPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket mount(HeldItemRenderer renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
CollectionDataTracker<Integer> set = renderer.getPassengers();
|
||||
buf.writeVarInt(set.size());
|
||||
set.forEach(buf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.LeashRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.LeashRenderer.Leash;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.Packets;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class LeashParser implements BehaviorRendererParser<LeashRenderer> {
|
||||
public void sendToClients(LeashRenderer renderer) {
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
HashSet<UUID> stop = new HashSet<>(data.getStopTracking());
|
||||
stop.removeAll(data.getTracking().keySet());
|
||||
this.remove(stop, renderer);
|
||||
}
|
||||
|
||||
public void destroy(LeashRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, LeashRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Leash leash : renderer.getRendered().values()) {
|
||||
set.add(this.pivotSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, true));
|
||||
}
|
||||
|
||||
for (Leash leash : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, true));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, LeashRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Leash leash : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, false));
|
||||
set.add(this.move(leash));
|
||||
}
|
||||
|
||||
for (Leash leash : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, true));
|
||||
}
|
||||
|
||||
Map<String, Leash> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
set.add(
|
||||
(Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(
|
||||
IntArrayList.toList(destroy.values().stream().mapMultiToInt((leashx, intConsumer) -> {
|
||||
intConsumer.accept(leashx.getLeashId());
|
||||
intConsumer.accept(leashx.getPivotId());
|
||||
}))
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, LeashRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Map<String, Leash> leashes = renderer.getRendered();
|
||||
buf.writeVarInt(leashes.size());
|
||||
leashes.forEach((s, leash) -> buf.writeVarInt(leash.getLeashId()));
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(Leash renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return NetworkUtils.createPivotSpawn(renderer.getPivotId(), renderer.getPivotUUID(), pos);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket leashSpawn(Leash renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getLeashId(), renderer.getLeastUUID(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.SLIME, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(Leash renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getLeashId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket leashData(Leash renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getLeashId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 16, EntityDataSerializers.INT, 0);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket mount(Leash renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(renderer.getLeashId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private Object move(Leash renderer) {
|
||||
if (!renderer.getPosition().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return NetworkUtils.createPivotMove(renderer.getPivotId(), renderer.getPositionCodec(), pos);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEntityLinkPacket link(Leash renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.getConnected().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeInt(renderer.getLeashId());
|
||||
buf.writeInt(renderer.getConnected().get());
|
||||
renderer.getConnected().clearDirty();
|
||||
return new ClientboundSetEntityLinkPacket(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.parser.behavior;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer.Mount;
|
||||
import com.ticxo.modelengine.api.utils.data.tracker.CollectionDataTracker;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.Packets;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Rot;
|
||||
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket.AttributeSnapshot;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.ai.attributes.Attributes;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class MountParser implements BehaviorRendererParser<MountRenderer> {
|
||||
public void sendToClients(MountRenderer renderer) {
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
public void destroy(MountRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, MountRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Mount mount : renderer.getRendered().values()) {
|
||||
set.add(this.pivotSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountHealth(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountMaxHealth(mount));
|
||||
}
|
||||
|
||||
for (Mount mount : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountHealth(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountMaxHealth(mount));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, MountRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Mount mount : renderer.getRendered().values()) {
|
||||
if (mount.getPosition().isDirty()) {
|
||||
set.add(this.pivotMove(mount));
|
||||
mount.getPosition().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getYaw().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountRotate(mount));
|
||||
mount.getYaw().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getHealth().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountHealth(mount));
|
||||
mount.getHealth().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getMaxHealth().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountMaxHealth(mount));
|
||||
mount.getMaxHealth().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getPassengers().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
mount.getPassengers().clearDirty();
|
||||
}
|
||||
}
|
||||
|
||||
for (Mount mount : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
}
|
||||
|
||||
Map<String, Mount> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size() * 2);
|
||||
destroy.forEach((s, mount) -> {
|
||||
buf.writeVarInt(mount.getPivotId());
|
||||
buf.writeVarInt(mount.getMountId());
|
||||
});
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, MountRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Collection<Mount> mounts = renderer.getRendered().values();
|
||||
buf.writeVarInt(mounts.size() * 2);
|
||||
mounts.forEach(mount -> {
|
||||
buf.writeVarInt(mount.getPivotId());
|
||||
buf.writeVarInt(mount.getMountId());
|
||||
});
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(Mount renderer) {
|
||||
return NetworkUtils.createPivotSpawn(renderer.getPivotId(), renderer.getPivotUuid(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket mountSpawn(Mount renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getMountId(),
|
||||
renderer.getMountUuid(),
|
||||
pos.x,
|
||||
pos.y,
|
||||
pos.z,
|
||||
0.0F,
|
||||
TMath.byteToRot(renderer.getYaw().get()),
|
||||
EntityType.ARMOR_STAND,
|
||||
0,
|
||||
Vec3.ZERO,
|
||||
0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket mountData(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 15, EntityDataSerializers.BYTE, (byte)16);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket pivotMount(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket mount(Mount renderer) {
|
||||
CollectionDataTracker<Integer> ids = renderer.getPassengers();
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
buf.writeVarInt(ids.size());
|
||||
ids.forEach(buf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotMove(Mount renderer) {
|
||||
return NetworkUtils.createPivotTeleport(renderer.getPivotId(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private Rot mountRotate(Mount renderer) {
|
||||
FriendlyByteBuf rot = NetworkUtils.createByteBuf();
|
||||
rot.writeVarInt(renderer.getMountId());
|
||||
rot.writeByte(renderer.getYaw().get());
|
||||
rot.writeByte(0);
|
||||
rot.writeBoolean(false);
|
||||
return Rot.read(rot);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket mountHealth(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
EntityUtils.writeData(buf, 9, EntityDataSerializers.FLOAT, renderer.getHealth().get());
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundUpdateAttributesPacket mountMaxHealth(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Collection<AttributeSnapshot> attributes = Lists.newArrayList();
|
||||
attributes.add(new AttributeSnapshot(Attributes.MAX_HEALTH, renderer.getMaxHealth().get().floatValue(), Lists.newArrayList()));
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
buf.writeCollection(attributes, (buf2, attribute) -> {
|
||||
buf2.writeResourceLocation(BuiltInRegistries.ATTRIBUTE.getKey(attribute.getAttribute()));
|
||||
buf2.writeDouble(attribute.getBase());
|
||||
buf2.writeCollection(attribute.getModifiers(), (buf3, modifier) -> {
|
||||
buf3.writeUUID(modifier.getId());
|
||||
buf3.writeDouble(modifier.getAmount());
|
||||
buf3.writeByte(modifier.getOperation().toValue());
|
||||
});
|
||||
});
|
||||
return new ClientboundUpdateAttributesPacket(buf);
|
||||
}
|
||||
}
|
||||
+196
@@ -0,0 +1,196 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.NameTagRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.NameTagRenderer.NameTag;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.Packets;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.Component.Serializer;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class NameTagParser implements BehaviorRendererParser<NameTagRenderer> {
|
||||
private float viewRange = 4096.0F;
|
||||
|
||||
public void sendToClients(NameTagRenderer renderer) {
|
||||
this.viewRange = renderer.getActiveModel().getViewRange();
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
public void destroy(NameTagRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, NameTagRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (NameTag nameTag : renderer.getRendered().values()) {
|
||||
set.add(this.pivotSpawn(nameTag));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(nameTag, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagSpawn(nameTag));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagData(nameTag, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(nameTag));
|
||||
}
|
||||
|
||||
for (NameTag nameTag : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(nameTag));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(nameTag, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagSpawn(nameTag));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagData(nameTag, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(nameTag));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, NameTagRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (NameTag nameTag : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagData(nameTag, false));
|
||||
if (nameTag.getPosition().isDirty()) {
|
||||
set.add(this.pivotMove(nameTag));
|
||||
nameTag.getPosition().clearDirty();
|
||||
}
|
||||
}
|
||||
|
||||
for (NameTag nameTagx : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(nameTagx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(nameTagx, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagSpawn(nameTagx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagData(nameTagx, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(nameTagx));
|
||||
}
|
||||
|
||||
Map<String, NameTag> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size() * 2);
|
||||
destroy.forEach((s, mount) -> {
|
||||
buf.writeVarInt(mount.getPivotId());
|
||||
buf.writeVarInt(mount.getTagId());
|
||||
});
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, NameTagRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Collection<NameTag> nameTags = renderer.getRendered().values();
|
||||
buf.writeVarInt(nameTags.size() * 2);
|
||||
nameTags.forEach(nameTag -> {
|
||||
buf.writeVarInt(nameTag.getPivotId());
|
||||
buf.writeVarInt(nameTag.getTagId());
|
||||
});
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(NameTag renderer) {
|
||||
return NetworkUtils.createPivotSpawn(renderer.getPivotId(), renderer.getPivotUuid(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(NameTag renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
if (spawn) {
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
}
|
||||
|
||||
buf.writeByte(255);
|
||||
renderer.clearDirty();
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket tagSpawn(NameTag renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getTagId(), renderer.getTagUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.TEXT_DISPLAY, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket tagData(NameTag renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getTagId());
|
||||
renderer.getVisibility().ifDirty(flag -> EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, flag ? this.viewRange : 0.0F), spawn);
|
||||
renderer.getTranslate().ifDirty(vector3f -> EntityUtils.writeData(buf, 10, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
renderer.getScale().ifDirty(vector3f -> EntityUtils.writeData(buf, 11, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
renderer.getTextOpacity().ifDirty(flag -> EntityUtils.writeData(buf, 26, EntityDataSerializers.BYTE, flag), spawn);
|
||||
renderer.getBackgroundColor().ifDirty(flag -> EntityUtils.writeData(buf, 25, EntityDataSerializers.INT, flag), spawn);
|
||||
renderer.getLineWidth().ifDirty(flag -> EntityUtils.writeData(buf, 24, EntityDataSerializers.INT, flag), spawn);
|
||||
renderer.getStyle().ifDirty(flags -> EntityUtils.writeData(buf, 27, EntityDataSerializers.BYTE, flags), spawn);
|
||||
renderer.getBillboard().ifDirty(flag -> {
|
||||
byte value = switch (flag) {
|
||||
case FIXED -> 0;
|
||||
case VERTICAL -> 1;
|
||||
case HORIZONTAL -> 2;
|
||||
case CENTER -> 3;
|
||||
default -> throw new IncompatibleClassChangeError();
|
||||
};
|
||||
EntityUtils.writeData(buf, 14, EntityDataSerializers.BYTE, value);
|
||||
}, spawn);
|
||||
renderer.getJsonString().ifDirty(jsonString -> {
|
||||
if (jsonString == null) {
|
||||
EntityUtils.writeData(buf, 23, EntityDataSerializers.COMPONENT, Component.empty());
|
||||
} else {
|
||||
EntityUtils.writeData(buf, 23, EntityDataSerializers.COMPONENT, Serializer.fromJson(jsonString));
|
||||
}
|
||||
}, spawn);
|
||||
renderer.getBrightness().ifDirty(val -> EntityUtils.writeData(buf, 15, EntityDataSerializers.INT, val), spawn);
|
||||
buf.writeByte(255);
|
||||
renderer.clearDirty();
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private Object pivotMove(NameTag renderer) {
|
||||
return NetworkUtils.createPivotMove(renderer.getPivotId(), renderer.getPositionCodec(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket pivotMount(NameTag renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(renderer.getTagId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
+224
@@ -0,0 +1,224 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SegmentRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SegmentRenderer.Pivot;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SegmentRenderer.SegmentDisplayBone;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayBone;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayBone.BoneData;
|
||||
import com.ticxo.modelengine.api.utils.data.UpdateScheme;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.Packets;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.inventory.CraftItemStack;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class SegmentParser implements BehaviorRendererParser<SegmentRenderer> {
|
||||
private final List<Runnable> cleanupQueue = new ArrayList<>();
|
||||
private float viewRange = 4096.0F;
|
||||
|
||||
public void sendToClients(SegmentRenderer renderer) {
|
||||
this.cleanupQueue.clear();
|
||||
this.viewRange = renderer.getActiveModel().getViewRange();
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
this.cleanupQueue.forEach(Runnable::run);
|
||||
this.cleanupQueue.clear();
|
||||
}
|
||||
|
||||
public void destroy(SegmentRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Packets set, Map<String, Pivot> map) {
|
||||
for (Pivot pivot : map.values()) {
|
||||
set.add(this.pivotSpawn(pivot));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(pivot));
|
||||
|
||||
for (SegmentDisplayBone bone : pivot.getRendered().values()) {
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true));
|
||||
}
|
||||
}
|
||||
|
||||
for (SegmentDisplayBone bone : pivot.getSpawnQueue().values()) {
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true));
|
||||
}
|
||||
}
|
||||
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(pivot));
|
||||
this.cleanupQueue.add(pivot::clearDirty);
|
||||
}
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, SegmentRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
this.spawn(set, renderer.getRendered());
|
||||
this.spawn(set, renderer.getSpawnQueue());
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, SegmentRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
HashSet<Integer> destroy = new HashSet<>();
|
||||
|
||||
for (Pivot pivot : renderer.getRendered().values()) {
|
||||
set.add(this.pivotTeleport(pivot));
|
||||
pivot.getRendered().values().forEach(bone -> {
|
||||
UpdateScheme<BoneData> scheme = bone.getModelUpdateScheme();
|
||||
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
switch (scheme.getUpdateMode(boneData)) {
|
||||
case NONE:
|
||||
case UPDATE:
|
||||
set.add(this.displayData(boneData, false));
|
||||
}
|
||||
}
|
||||
|
||||
scheme.getAdded().forEach(boneDatax -> {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneDatax));
|
||||
set.add(this.displayData(boneDatax, true));
|
||||
});
|
||||
scheme.getRemoved().forEach(boneDatax -> destroy.add(boneDatax.getId()));
|
||||
});
|
||||
this.cleanupQueue.add(pivot::clearDirty);
|
||||
}
|
||||
|
||||
this.spawn(set, renderer.getSpawnQueue());
|
||||
renderer.getDestroyQueue().forEach((s, pivotx) -> {
|
||||
destroy.add(pivotx.getId());
|
||||
destroy.addAll(pivotx.getPassengers());
|
||||
});
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeCollection(destroy, FriendlyByteBuf::writeVarInt);
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, SegmentRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Collection<Pivot> pivots = renderer.getRendered().values();
|
||||
IntArrayList ids = new IntArrayList();
|
||||
pivots.forEach(pivot -> {
|
||||
ids.add(pivot.getId());
|
||||
ids.addAll(pivot.getPassengers());
|
||||
});
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(ids));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(Pivot pivot) {
|
||||
return NetworkUtils.createPivotSpawn(pivot.getId(), pivot.getUuid(), pivot.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(Pivot pivot) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(pivot.getId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket pivotMount(Pivot pivot) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(pivot.getId());
|
||||
buf.writeCollection(pivot.getPassengers(), FriendlyByteBuf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private Object pivotTeleport(Pivot pivot) {
|
||||
if (!pivot.getPosition().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
this.cleanupQueue.add(() -> pivot.getPosition().clearDirty());
|
||||
return NetworkUtils.createPivotMove(pivot.getId(), pivot.getPositionCodec(), pivot.getPosition().get());
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket displaySpawn(Pivot pivot, BoneData boneData) {
|
||||
Vector3f pos = pivot.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(boneData.getId(), boneData.getUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0);
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier displayData(BoneData boneData, boolean force) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
if (!force && !bone.isDirty() && !boneData.getModel().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
this.cleanupQueue.add(bone::clearDirty);
|
||||
this.cleanupQueue.add(() -> boneData.getModel().clearDirty());
|
||||
return uuid -> this.displayData(uuid, boneData, force);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket displayData(UUID uuid, BoneData boneData, boolean force) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(boneData.getId());
|
||||
if (force) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
} else if (bone.isTransformDirty()) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
}
|
||||
|
||||
byte update = bone.getSnapshotHandler().getUpdate(uuid);
|
||||
bone.getStep().ifDirty(flag -> EntityUtils.writeData(buf, 9, EntityDataSerializers.INT, flag ? 0 : 1), force);
|
||||
bone.getGlowing().ifDirty(flag -> EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)(flag ? 96 : 32)), force);
|
||||
bone.getGlowColor().ifDirty(color -> EntityUtils.writeData(buf, 21, EntityDataSerializers.INT, color), force);
|
||||
bone.getBrightness().ifDirty(val -> EntityUtils.writeData(buf, 15, EntityDataSerializers.INT, val), force);
|
||||
bone.getPosition().ifDirty(vector3f -> EntityUtils.writeData(buf, 10, EntityDataSerializers.VECTOR3, vector3f), force || TMath.getBit(update, 0));
|
||||
bone.getScale().ifDirty(vector3f -> EntityUtils.writeData(buf, 11, EntityDataSerializers.VECTOR3, vector3f), force || TMath.getBit(update, 2));
|
||||
bone.getLeftRotation()
|
||||
.ifDirty(
|
||||
quaternionf -> EntityUtils.writeData(buf, 12, EntityDataSerializers.QUATERNION, quaternionf.rotateY((float) Math.PI, new Quaternionf())),
|
||||
force || TMath.getBit(update, 1)
|
||||
);
|
||||
bone.getRightRotation()
|
||||
.ifDirty(quaternionf -> EntityUtils.writeData(buf, 13, EntityDataSerializers.QUATERNION, quaternionf), force || TMath.getBit(update, 3));
|
||||
bone.getBillboard().ifDirty(billboard -> EntityUtils.writeData(buf, 14, EntityDataSerializers.BYTE, (byte)billboard.ordinal()));
|
||||
bone.getVisibility().ifDirty(flag -> EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, flag ? this.viewRange : 0.0F), force);
|
||||
boneData.getModel().ifDirty(itemStack -> EntityUtils.writeData(buf, 22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(itemStack)), force);
|
||||
bone.getDisplay().ifDirty(display -> EntityUtils.writeData(buf, 23, EntityDataSerializers.BYTE, display == null ? 0 : (byte)display.ordinal()), force);
|
||||
buf.writeByte(255);
|
||||
this.cleanupQueue.add(bone::clearDirty);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.BaseEntity;
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SubHitboxRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SubHitboxRenderer.SubHitbox;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.Packets;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class SubHitboxParser implements BehaviorRendererParser<SubHitboxRenderer> {
|
||||
public void sendToClients(SubHitboxRenderer renderer) {
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
public void destroy(SubHitboxRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, SubHitboxRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (SubHitbox subHitbox : renderer.getRendered().values()) {
|
||||
set.add(this.pivotSpawn(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxSpawn(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxData(subHitbox, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(subHitbox));
|
||||
}
|
||||
|
||||
for (SubHitbox subHitbox : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxSpawn(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxData(subHitbox, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(subHitbox));
|
||||
}
|
||||
|
||||
BaseEntity<?> base = renderer.getActiveModel().getModeledEntity().getBase();
|
||||
NetworkUtils.sendBundled(targets, set, player -> player != base.getOriginal());
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, SubHitboxRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (SubHitbox subHitbox : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxData(subHitbox, false));
|
||||
if (subHitbox.getPosition().isDirty()) {
|
||||
set.add(this.pivotMove(subHitbox));
|
||||
subHitbox.getPosition().clearDirty();
|
||||
}
|
||||
}
|
||||
|
||||
for (SubHitbox subHitboxx : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(subHitboxx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(subHitboxx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxSpawn(subHitboxx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxData(subHitboxx, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(subHitboxx));
|
||||
}
|
||||
|
||||
Map<String, SubHitbox> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size() * 2);
|
||||
destroy.forEach((s, subHitboxx) -> {
|
||||
buf.writeVarInt(subHitboxx.getPivotId());
|
||||
buf.writeVarInt(subHitboxx.getHitboxId());
|
||||
});
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
BaseEntity<?> base = renderer.getActiveModel().getModeledEntity().getBase();
|
||||
NetworkUtils.sendBundled(targets, set, player -> player != base.getOriginal());
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, SubHitboxRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Collection<SubHitbox> subHitboxes = renderer.getRendered().values();
|
||||
buf.writeVarInt(subHitboxes.size() * 2);
|
||||
subHitboxes.forEach(subHitbox -> {
|
||||
buf.writeVarInt(subHitbox.getPivotId());
|
||||
buf.writeVarInt(subHitbox.getHitboxId());
|
||||
});
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(SubHitbox renderer) {
|
||||
return NetworkUtils.createPivotSpawn(renderer.getPivotId(), renderer.getPivotUuid(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(SubHitbox renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private Object pivotMove(SubHitbox renderer) {
|
||||
return NetworkUtils.createPivotMove(renderer.getPivotId(), renderer.getPositionCodec(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket pivotMount(SubHitbox renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(renderer.getHitboxId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket hitboxSpawn(SubHitbox renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getHitboxId(), renderer.getHitboxUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.INTERACTION, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket hitboxData(SubHitbox renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getHitboxId());
|
||||
renderer.getWidth().ifDirty(width -> EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, width), spawn);
|
||||
renderer.getHeight().ifDirty(height -> EntityUtils.writeData(buf, 9, EntityDataSerializers.FLOAT, height), spawn);
|
||||
buf.writeByte(255);
|
||||
renderer.clearDirty();
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
+735
@@ -0,0 +1,735 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.parser.model;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.BaseEntity;
|
||||
import com.ticxo.modelengine.api.entity.BukkitEntity;
|
||||
import com.ticxo.modelengine.api.entity.CullType;
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.lod.AnimationLODHandler.LODTracker;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayBone;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayFire;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayRenderer;
|
||||
import com.ticxo.modelengine.api.model.render.ModelRendererParser;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayBone.BoneData;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayRenderer.Hitbox;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayRenderer.Pivot;
|
||||
import com.ticxo.modelengine.api.nms.network.ModPlayerTracker;
|
||||
import com.ticxo.modelengine.api.utils.data.PooledCollection;
|
||||
import com.ticxo.modelengine.api.utils.data.UpdateScheme;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.BulkEntityDataEncoder;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.Packets;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.network.syncher.SynchedEntityData.DataValue;
|
||||
import net.minecraft.util.Brightness;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.Display.BillboardConstraints;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.inventory.CraftItemStack;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class DisplayParser implements ModelRendererParser<DisplayRenderer> {
|
||||
private static final Quaternionf IDENTITY_ROTATION = new Quaternionf();
|
||||
private final Set<UUID> noCullPlayers = new HashSet<>();
|
||||
private final Set<UUID> noCullForcePlayers = new HashSet<>();
|
||||
private final Set<UUID> movementOnlyPlayers = new HashSet<>();
|
||||
private final Set<UUID> culledPlayers = new HashSet<>();
|
||||
private final List<Runnable> cleanupQueue = new ArrayList<>();
|
||||
private ModeledEntity modeledEntity;
|
||||
private float viewRange = 4096.0F;
|
||||
private Object pivotMove;
|
||||
private Object hitboxMove;
|
||||
private static Constructor<?> CUSTOM_PAYLOAD_CONSTRUCTOR;
|
||||
|
||||
public void sendToClients(DisplayRenderer renderer) {
|
||||
this.noCullPlayers.clear();
|
||||
this.noCullForcePlayers.clear();
|
||||
this.movementOnlyPlayers.clear();
|
||||
this.culledPlayers.clear();
|
||||
this.cleanupQueue.clear();
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.modeledEntity = renderer.getActiveModel().getModeledEntity();
|
||||
this.viewRange = renderer.getActiveModel().getViewRange();
|
||||
this.pivotMove = this.pivotTeleport(renderer.getPivot());
|
||||
this.hitboxMove = this.hitboxTeleport(renderer.getHitbox());
|
||||
|
||||
for (Entry<UUID, CullType> entry : data.getTracking().entrySet()) {
|
||||
switch ((CullType)entry.getValue()) {
|
||||
case NO_CULL:
|
||||
if (renderer.pollFullUpdate(entry.getKey())) {
|
||||
this.noCullForcePlayers.add(entry.getKey());
|
||||
} else {
|
||||
this.noCullPlayers.add(entry.getKey());
|
||||
}
|
||||
break;
|
||||
case MOVEMENT_ONLY:
|
||||
renderer.pushFullUpdate(entry.getKey());
|
||||
this.movementOnlyPlayers.add(entry.getKey());
|
||||
break;
|
||||
case CULLED:
|
||||
this.culledPlayers.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
if (renderer.pollFirstSpawn()) {
|
||||
HashSet<UUID> set = new HashSet<>((int)((this.movementOnlyPlayers.size() + this.noCullPlayers.size() + this.noCullForcePlayers.size()) / 0.75F) + 1);
|
||||
set.addAll(this.movementOnlyPlayers);
|
||||
set.addAll(this.noCullPlayers);
|
||||
set.addAll(this.noCullForcePlayers);
|
||||
this.spawn(set, renderer);
|
||||
} else {
|
||||
Set<UUID> spawning = data.getStartTracking();
|
||||
this.spawn(spawning, renderer);
|
||||
this.updateRealtime(excluding(this.movementOnlyPlayers, spawning), renderer, true, false);
|
||||
this.updateRealtime(excluding(this.noCullPlayers, spawning), renderer, false, false);
|
||||
this.updateRealtime(excluding(this.noCullForcePlayers, spawning), renderer, false, true);
|
||||
this.updateCulled(this.culledPlayers, renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
this.noCullPlayers.clear();
|
||||
this.noCullForcePlayers.clear();
|
||||
this.movementOnlyPlayers.clear();
|
||||
this.culledPlayers.clear();
|
||||
renderer.getPivot().clearDirty();
|
||||
renderer.getHitbox().clearDirty();
|
||||
PooledCollection<DisplayFire> fireDisplay = renderer.getHitbox().getFireDisplay();
|
||||
if (!fireDisplay.getCreated().isEmpty()) {
|
||||
fireDisplay.getCreated().clear();
|
||||
}
|
||||
|
||||
if (!fireDisplay.getReleased().isEmpty()) {
|
||||
fireDisplay.getReleased().clear();
|
||||
}
|
||||
|
||||
this.cleanupQueue.forEach(Runnable::run);
|
||||
this.cleanupQueue.clear();
|
||||
this.pivotMove = null;
|
||||
this.hitboxMove = null;
|
||||
}
|
||||
|
||||
private static Set<UUID> excluding(Set<UUID> targets, Set<UUID> excluded) {
|
||||
if (!targets.isEmpty() && !excluded.isEmpty()) {
|
||||
HashSet<UUID> filtered = new HashSet<>(targets);
|
||||
filtered.removeAll(excluded);
|
||||
return filtered;
|
||||
} else {
|
||||
return targets;
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy(DisplayRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, DisplayRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
Pivot pivot = renderer.getPivot();
|
||||
set.add(this.pivotSpawn(pivot));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(pivot));
|
||||
|
||||
for (DisplayBone bone : renderer.getRendered().values()) {
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true, false));
|
||||
}
|
||||
}
|
||||
|
||||
for (DisplayBone bone : renderer.getSpawnQueue().values()) {
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true, false));
|
||||
}
|
||||
}
|
||||
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(pivot));
|
||||
BaseEntity<?> base = renderer.getActiveModel().getModeledEntity().getBase();
|
||||
if (base instanceof BukkitEntity bukkitEntity && bukkitEntity.getOriginal() instanceof Player owner && targets.contains(owner.getUniqueId())) {
|
||||
NetworkUtils.sendBundled(owner.getUniqueId(), set);
|
||||
}
|
||||
|
||||
Hitbox hitbox = renderer.getHitbox();
|
||||
set.add(this.hitboxSpawnPivot(hitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxDataPivot(hitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxSpawn(hitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxData(hitbox, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.shadowSpawn(hitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.shadowData(hitbox, true));
|
||||
if (hitbox.isFireVisible()) {
|
||||
hitbox.getFireDisplay().getInUse().forEach(displayFire -> {
|
||||
set.add((Packet<ClientGamePacketListener>)this.fireSpawn(hitbox, displayFire));
|
||||
set.add((Packet<ClientGamePacketListener>)this.fireData(displayFire, true));
|
||||
});
|
||||
}
|
||||
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxMount(hitbox));
|
||||
NetworkUtils.sendBundled(targets, set, player -> !player.getUniqueId().equals(base.getUUID()));
|
||||
}
|
||||
}
|
||||
|
||||
private void updateRealtime(Set<UUID> targets, DisplayRenderer renderer, boolean movementOnly, boolean transformOnly) {
|
||||
if (!targets.isEmpty()) {
|
||||
Set<UUID> modTargets = null;
|
||||
Set<UUID> vanillaTargets = targets;
|
||||
|
||||
for (UUID uuid : targets) {
|
||||
if (ModPlayerTracker.hasClientMod(uuid)) {
|
||||
if (modTargets == null) {
|
||||
modTargets = new HashSet<>();
|
||||
vanillaTargets = new HashSet<>(targets);
|
||||
}
|
||||
|
||||
modTargets.add(uuid);
|
||||
vanillaTargets.remove(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
Packets set = new Packets();
|
||||
Packets boneSet = modTargets != null ? new Packets() : set;
|
||||
HashSet<Integer> destroy = new HashSet<>();
|
||||
Pivot pivot = renderer.getPivot();
|
||||
set.add(this.pivotMove);
|
||||
if (!movementOnly) {
|
||||
for (DisplayBone bone : renderer.getRendered().values()) {
|
||||
UpdateScheme<BoneData> scheme = bone.getModelUpdateScheme();
|
||||
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
switch (scheme.getUpdateMode(boneData)) {
|
||||
case NONE:
|
||||
case UPDATE:
|
||||
boneSet.add(this.displayData(boneData, false, transformOnly));
|
||||
}
|
||||
}
|
||||
|
||||
if (scheme.hasUpdate()) {
|
||||
scheme.getAdded().forEach(boneData -> {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true, false));
|
||||
});
|
||||
scheme.getRemoved().forEach(boneData -> destroy.add(boneData.getId()));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (DisplayBone bone : renderer.getRendered().values()) {
|
||||
UpdateScheme<BoneData> scheme = bone.getModelUpdateScheme();
|
||||
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
switch (scheme.getUpdateMode(boneData)) {
|
||||
case NONE:
|
||||
case UPDATE:
|
||||
boneSet.add((Packet<ClientGamePacketListener>)this.displayVisibleData(boneData));
|
||||
}
|
||||
}
|
||||
|
||||
if (scheme.hasUpdate()) {
|
||||
scheme.getAdded().forEach(boneData -> {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true, false));
|
||||
});
|
||||
scheme.getRemoved().forEach(boneData -> destroy.add(boneData.getId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (DisplayBone bone : renderer.getSpawnQueue().values()) {
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true, false));
|
||||
}
|
||||
}
|
||||
|
||||
renderer.getDestroyQueue().forEach((s, bones) -> bones.getModel().forEach((uuidx, boneData) -> destroy.add(boneData.getId())));
|
||||
if (pivot.getPassengers().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(pivot));
|
||||
}
|
||||
|
||||
BaseEntity<?> base = renderer.getActiveModel().getModeledEntity().getBase();
|
||||
UUID owner = base instanceof BukkitEntity bukkitEntity && bukkitEntity.getOriginal() instanceof Player && targets.contains(base.getUUID())
|
||||
? base.getUUID()
|
||||
: null;
|
||||
Packets hitboxSet = owner == null ? set : new Packets();
|
||||
HashSet<Integer> hitboxDestroy = owner == null ? destroy : new HashSet<>();
|
||||
Hitbox hitbox = renderer.getHitbox();
|
||||
AtomicBoolean updateHitboxMount = new AtomicBoolean(false);
|
||||
hitboxSet.add(this.hitboxMove);
|
||||
if (hitbox.getHitboxVisible().isDirty()) {
|
||||
if (hitbox.isHitboxVisible()) {
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.hitboxSpawn(hitbox));
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.hitboxData(hitbox, true));
|
||||
updateHitboxMount.set(true);
|
||||
} else {
|
||||
hitboxDestroy.add(hitbox.getHitboxId());
|
||||
}
|
||||
} else {
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.hitboxData(hitbox, false));
|
||||
}
|
||||
|
||||
if (hitbox.getShadowVisible().isDirty()) {
|
||||
if (hitbox.isShadowVisible()) {
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.shadowSpawn(hitbox));
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.shadowData(hitbox, true));
|
||||
updateHitboxMount.set(true);
|
||||
} else {
|
||||
hitboxDestroy.add(hitbox.getShadowId());
|
||||
}
|
||||
} else {
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.shadowData(hitbox, false));
|
||||
}
|
||||
|
||||
PooledCollection<DisplayFire> fireDisplay = hitbox.getFireDisplay();
|
||||
if (hitbox.getFireVisible().isDirty()) {
|
||||
if (hitbox.isFireVisible()) {
|
||||
fireDisplay.getInUse().forEach(displayFire -> {
|
||||
updateHitboxMount.set(true);
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.fireSpawn(hitbox, displayFire));
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.fireData(displayFire, true));
|
||||
});
|
||||
} else {
|
||||
fireDisplay.getAll().forEach(displayFire -> hitboxDestroy.add(displayFire.getId()));
|
||||
}
|
||||
} else {
|
||||
fireDisplay.processAll(
|
||||
displayFire -> {
|
||||
updateHitboxMount.set(true);
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.fireSpawn(hitbox, displayFire));
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.fireData(displayFire, true));
|
||||
},
|
||||
displayFire -> hitboxSet.add((Packet<ClientGamePacketListener>)this.fireData(displayFire, false)),
|
||||
displayFire -> hitboxDestroy.add(displayFire.getId())
|
||||
);
|
||||
}
|
||||
|
||||
if (!fireDisplay.getCreated().isEmpty()) {
|
||||
fireDisplay.getCreated().clear();
|
||||
}
|
||||
|
||||
if (!fireDisplay.getReleased().isEmpty()) {
|
||||
fireDisplay.getReleased().clear();
|
||||
}
|
||||
|
||||
if (updateHitboxMount.get()) {
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.hitboxMount(hitbox));
|
||||
}
|
||||
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size());
|
||||
destroy.forEach(buf::writeVarInt);
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
if (hitboxDestroy != destroy && !hitboxDestroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(hitboxDestroy.size());
|
||||
hitboxDestroy.forEach(buf::writeVarInt);
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
if (modTargets != null) {
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
NetworkUtils.sendBundled(vanillaTargets, boneSet);
|
||||
this.sendBulkBoneData(modTargets, renderer, movementOnly, transformOnly);
|
||||
} else {
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
|
||||
if (hitboxSet != set) {
|
||||
NetworkUtils.sendBundled(targets, hitboxSet, player -> !player.getUniqueId().equals(owner));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sendBulkBoneData(Set<UUID> modTargets, DisplayRenderer renderer, boolean movementOnly, boolean transformOnly) {
|
||||
if (!movementOnly) {
|
||||
for (UUID uuid : modTargets) {
|
||||
BulkEntityDataEncoder encoder = new BulkEntityDataEncoder();
|
||||
Packets fallbackPackets = new Packets();
|
||||
|
||||
for (DisplayBone bone : renderer.getRendered().values()) {
|
||||
UpdateScheme<BoneData> scheme = bone.getModelUpdateScheme();
|
||||
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
switch (scheme.getUpdateMode(boneData)) {
|
||||
case NONE:
|
||||
case UPDATE:
|
||||
if (boneData.getModel().isDirty()) {
|
||||
fallbackPackets.add((Packets.PacketSupplier)u -> this.displayData(u, null, boneData, false, transformOnly));
|
||||
} else if (!boneData.getBone().isSkippable()) {
|
||||
this.encodeBoneData(encoder, uuid, null, boneData, false, transformOnly);
|
||||
} else {
|
||||
LODTracker tracker = this.modeledEntity.getAnimationLodHandler().tick(uuid);
|
||||
if (!tracker.isCanSkip()) {
|
||||
this.encodeBoneData(encoder, uuid, tracker, boneData, false, transformOnly);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (encoder.getEntryCount() > 0) {
|
||||
byte[] data = encoder.build();
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeUtf("modelengine:bulk_data");
|
||||
buf.writeBytes(data);
|
||||
Packet<? super ClientGamePacketListener> packet = createCustomPayload(buf);
|
||||
if (packet != null) {
|
||||
NetworkUtils.send(uuid, packet);
|
||||
}
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(uuid, fallbackPackets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Packet<? super ClientGamePacketListener> createCustomPayload(FriendlyByteBuf buf) {
|
||||
try {
|
||||
if (CUSTOM_PAYLOAD_CONSTRUCTOR == null) {
|
||||
CUSTOM_PAYLOAD_CONSTRUCTOR = ClientboundCustomPayloadPacket.class.getDeclaredConstructor(FriendlyByteBuf.class);
|
||||
CUSTOM_PAYLOAD_CONSTRUCTOR.setAccessible(true);
|
||||
}
|
||||
|
||||
return (Packet<? super ClientGamePacketListener>)CUSTOM_PAYLOAD_CONSTRUCTOR.newInstance(buf);
|
||||
} catch (Exception var2) {
|
||||
var2.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void encodeBoneData(BulkEntityDataEncoder encoder, UUID uuid, @Nullable LODTracker tracker, BoneData boneData, boolean force, boolean transformOnly) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
if (force || transformOnly || bone.isDirty() || boneData.getModel().isDirty()) {
|
||||
byte update = bone.getSnapshotHandler().getUpdate(uuid);
|
||||
boolean wouldWriteTransform = force || update != 0;
|
||||
boolean wouldWriteRender = force || bone.isRenderDirty();
|
||||
if (wouldWriteTransform || wouldWriteRender) {
|
||||
BulkEntityDataEncoder.EntryWriter entry = encoder.startEntry(boneData.getId());
|
||||
bone.getPosition().ifDirty(v -> entry.translation(new Vector3f(v)), force || TMath.getBit(update, 0));
|
||||
bone.getLeftRotation().ifDirty(q -> entry.leftRotation(new Quaternionf(q)), force || TMath.getBit(update, 1));
|
||||
bone.getScale().ifDirty(v -> entry.scale(new Vector3f(v)), force || TMath.getBit(update, 2));
|
||||
bone.getRightRotation().ifDirty(q -> entry.rightRotation(new Quaternionf(q)), force || TMath.getBit(update, 3));
|
||||
int lodTick = tracker == null ? 1 : tracker.getTickDuration().get();
|
||||
boolean lodDirty = tracker != null && tracker.getTickDuration().isDirty();
|
||||
bone.getStep().ifDirty(flag -> entry.transformDuration(flag ? 0 : lodTick), force || transformOnly || lodDirty);
|
||||
bone.getGlowing().ifDirty(flag -> bone.getGlowColor().ifDirty(color -> entry.glowData((byte)(flag ? 96 : 32), color), true), force);
|
||||
bone.getBrightness().ifDirty(entry::brightness, force);
|
||||
boolean hasBillboard = bone.getBillboard().isDirty() || force;
|
||||
boolean hasVisibility = bone.getVisibility().isDirty() || force;
|
||||
boolean hasDisplayType = bone.getDisplay().isDirty() || force;
|
||||
if (hasBillboard || hasVisibility || hasDisplayType) {
|
||||
entry.renderData(
|
||||
(byte)bone.getBillboard().get().ordinal(),
|
||||
bone.getVisibility().get() ? this.viewRange : 0.0F,
|
||||
bone.getDisplay().get() == null ? 0 : (byte)bone.getDisplay().get().ordinal()
|
||||
);
|
||||
}
|
||||
|
||||
entry.finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateCulled(Set<UUID> targets, DisplayRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
IntArrayList entityIds = new IntArrayList();
|
||||
Map<String, DisplayBone> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
destroy.values()
|
||||
.stream()
|
||||
.mapMultiToInt((bones, intConsumer) -> bones.getModel().forEach((uuid, boneData) -> intConsumer.accept(boneData.getId())))
|
||||
.forEach(entityIds::add);
|
||||
}
|
||||
|
||||
if (!entityIds.isEmpty()) {
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(entityIds));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, DisplayRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
IntArrayList entityIds = new IntArrayList();
|
||||
renderer.getRendered().forEach((s, bone) -> {
|
||||
targets.forEach(uuid -> bone.getSnapshotHandler().remove(uuid));
|
||||
bone.getModel().forEach((integer, boneData) -> entityIds.add(boneData.getId()));
|
||||
});
|
||||
entityIds.add(renderer.getPivot().getId());
|
||||
entityIds.add(renderer.getHitbox().getPivotId());
|
||||
entityIds.add(renderer.getHitbox().getHitboxId());
|
||||
entityIds.add(renderer.getHitbox().getShadowId());
|
||||
renderer.getHitbox().getFireDisplay().getAll().forEach(displayFire -> entityIds.add(displayFire.getId()));
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(entityIds));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(Pivot pivot) {
|
||||
return pivot.isOverridden() ? null : NetworkUtils.createPivotSpawn(pivot.getId(), pivot.getUuid(), pivot.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(Pivot pivot) {
|
||||
if (pivot.isOverridden()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(pivot.getId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket pivotMount(Pivot pivot) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(pivot.getId());
|
||||
buf.writeVarInt(pivot.getPassengers().size());
|
||||
pivot.getPassengers().forEach(buf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private Object pivotTeleport(Pivot pivot) {
|
||||
return !pivot.getPosition().isDirty() ? null : NetworkUtils.createPivotMove(pivot.getId(), pivot.getPositionCodec(), pivot.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket displaySpawn(Pivot pivot, BoneData boneData) {
|
||||
Vector3f pos = pivot.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(boneData.getId(), boneData.getUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0);
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier displayData(BoneData boneData, boolean force, boolean transformOnly) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
if (!force && !transformOnly && !bone.isDirty() && !boneData.getModel().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
this.cleanupQueue.add(bone::clearDirty);
|
||||
this.cleanupQueue.add(() -> boneData.getModel().clearDirty());
|
||||
if (force) {
|
||||
return uuid -> this.displayData(uuid, this.modeledEntity.getAnimationLodHandler().tick(uuid), boneData, true, transformOnly);
|
||||
} else {
|
||||
return !bone.isSkippable()
|
||||
? uuid -> this.displayData(uuid, null, boneData, false, transformOnly)
|
||||
: NetworkUtils.lodWrapper(this.modeledEntity, (uuid, tracker) -> this.displayData(uuid, tracker, boneData, false, transformOnly));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket displayData(UUID uuid, @Nullable LODTracker tracker, BoneData boneData, boolean force, boolean transformOnly) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
byte update = bone.getSnapshotHandler().getUpdate(uuid);
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(boneData.getId());
|
||||
if (!force && update != 0) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
}
|
||||
|
||||
int lodTick = tracker == null ? 1 : tracker.getTickDuration().get();
|
||||
boolean lodDirty = tracker != null && tracker.getTickDuration().isDirty();
|
||||
bone.getStep().ifDirty(flag -> EntityUtils.writeData(buf, 9, EntityDataSerializers.INT, flag ? 0 : lodTick), force || transformOnly || lodDirty);
|
||||
bone.getGlowing().ifDirty(flag -> EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)(flag ? 96 : 32)), force);
|
||||
bone.getGlowColor().ifDirty(color -> {
|
||||
if (!force || color != -1) {
|
||||
EntityUtils.writeData(buf, 21, EntityDataSerializers.INT, color);
|
||||
}
|
||||
}, force);
|
||||
bone.getBrightness().ifDirty(val -> {
|
||||
if (!force || val != -1) {
|
||||
EntityUtils.writeData(buf, 15, EntityDataSerializers.INT, val);
|
||||
}
|
||||
}, force);
|
||||
bone.getPosition().ifDirty(vector3f -> EntityUtils.writeData(buf, 10, EntityDataSerializers.VECTOR3, vector3f), force || TMath.getBit(update, 0));
|
||||
bone.getScale().ifDirty(vector3f -> EntityUtils.writeData(buf, 11, EntityDataSerializers.VECTOR3, vector3f), force || TMath.getBit(update, 2));
|
||||
bone.getLeftRotation()
|
||||
.ifDirty(
|
||||
quaternionf -> EntityUtils.writeData(buf, 12, EntityDataSerializers.QUATERNION, quaternionf.rotateY((float) Math.PI, new Quaternionf())),
|
||||
force || TMath.getBit(update, 1)
|
||||
);
|
||||
bone.getRightRotation().ifDirty(quaternionf -> {
|
||||
if (!force || !TMath.isSimilar(quaternionf, IDENTITY_ROTATION)) {
|
||||
EntityUtils.writeData(buf, 13, EntityDataSerializers.QUATERNION, quaternionf);
|
||||
}
|
||||
}, force || TMath.getBit(update, 3));
|
||||
bone.getBillboard().ifDirty(billboard -> {
|
||||
if (!force || billboard.ordinal() != 0) {
|
||||
EntityUtils.writeData(buf, 14, EntityDataSerializers.BYTE, (byte)billboard.ordinal());
|
||||
}
|
||||
}, force);
|
||||
bone.getVisibility().ifDirty(flag -> EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, flag ? this.viewRange : 0.0F), force);
|
||||
boneData.getModel().ifDirty(itemStack -> EntityUtils.writeData(buf, 22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(itemStack)), force);
|
||||
bone.getDisplay().ifDirty(display -> EntityUtils.writeData(buf, 23, EntityDataSerializers.BYTE, display == null ? 0 : (byte)display.ordinal()), force);
|
||||
buf.writeByte(255);
|
||||
this.cleanupQueue.add(bone::clearDirty);
|
||||
this.cleanupQueue.add(() -> boneData.getModel().clearDirty());
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket displayVisibleData(BoneData boneData) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
if (!bone.isRenderDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(boneData.getId());
|
||||
bone.getGlowing().ifDirty(flag -> EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)(flag ? 96 : 32)));
|
||||
bone.getGlowColor().ifDirty(color -> EntityUtils.writeData(buf, 21, EntityDataSerializers.INT, color));
|
||||
bone.getBrightness().ifDirty(val -> EntityUtils.writeData(buf, 15, EntityDataSerializers.INT, val));
|
||||
bone.getBillboard().ifDirty(billboard -> EntityUtils.writeData(buf, 14, EntityDataSerializers.BYTE, (byte)billboard.ordinal()));
|
||||
bone.getVisibility().ifDirty(flag -> EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, flag ? this.viewRange : 0.0F));
|
||||
boneData.getModel().ifDirty(itemStack -> EntityUtils.writeData(buf, 22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(itemStack)));
|
||||
bone.getDisplay().ifDirty(display -> EntityUtils.writeData(buf, 23, EntityDataSerializers.BYTE, display == null ? 0 : (byte)display.ordinal()));
|
||||
buf.writeByte(255);
|
||||
this.cleanupQueue.add(bone::clearDirty);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier hitboxSpawnPivot(Hitbox hitbox) {
|
||||
return hitbox.isPivotVisible() ? NetworkUtils.createPivotSpawn(hitbox.getPivotId(), hitbox.getPivotUuid(), hitbox.getPosition().get()) : null;
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket hitboxDataPivot(Hitbox hitbox) {
|
||||
if (!hitbox.isPivotVisible()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(hitbox.getPivotId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket hitboxSpawn(Hitbox hitbox) {
|
||||
if (!hitbox.isHitboxVisible()) {
|
||||
return null;
|
||||
} else {
|
||||
Vector3f pos = hitbox.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
hitbox.getHitboxId(), hitbox.getHitboxUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.INTERACTION, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket hitboxData(Hitbox hitbox, boolean spawn) {
|
||||
if (hitbox.isHitboxVisible() && (hitbox.isHitboxDirty() || spawn)) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(hitbox.getHitboxId());
|
||||
hitbox.getWidth().ifDirty(val -> EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, val), spawn);
|
||||
hitbox.getHeight().ifDirty(val -> EntityUtils.writeData(buf, 9, EntityDataSerializers.FLOAT, val), spawn);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket shadowSpawn(Hitbox hitbox) {
|
||||
if (!hitbox.isShadowVisible()) {
|
||||
return null;
|
||||
} else {
|
||||
Vector3f pos = hitbox.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
hitbox.getShadowId(), hitbox.getShadowUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket shadowData(Hitbox hitbox, boolean spawn) {
|
||||
if (hitbox.isShadowVisible() && (hitbox.getShadowRadius().isDirty() || spawn)) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(hitbox.getShadowId());
|
||||
hitbox.getShadowRadius().ifDirty(val -> EntityUtils.writeData(buf, 17, EntityDataSerializers.FLOAT, val), spawn);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket fireSpawn(Hitbox hitbox, DisplayFire fire) {
|
||||
Vector3f pos = hitbox.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(fire.getId(), fire.getUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket fireData(DisplayFire fire, boolean spawn) {
|
||||
if (!fire.isDirty() && !spawn) {
|
||||
return null;
|
||||
} else {
|
||||
ArrayList<DataValue<?>> entityData = new ArrayList<>(2);
|
||||
if (spawn) {
|
||||
entityData.add(new DataValue(14, EntityDataSerializers.BYTE, (byte)BillboardConstraints.VERTICAL.ordinal()));
|
||||
entityData.add(new DataValue(15, EntityDataSerializers.INT, Brightness.FULL_BRIGHT.pack()));
|
||||
}
|
||||
|
||||
fire.getFireModel().ifDirty(stack -> entityData.add(new DataValue(22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(stack))), spawn);
|
||||
fire.getPosition().ifDirty(vector3f -> entityData.add(new DataValue(10, EntityDataSerializers.VECTOR3, vector3f)), spawn);
|
||||
fire.getScale().ifDirty(vector3f -> entityData.add(new DataValue(11, EntityDataSerializers.VECTOR3, vector3f)), spawn);
|
||||
fire.getVisible().ifDirty(flag -> entityData.add(new DataValue(16, EntityDataSerializers.FLOAT, flag ? this.viewRange : 0.0F)), spawn);
|
||||
return new ClientboundSetEntityDataPacket(fire.getId(), entityData);
|
||||
}
|
||||
}
|
||||
|
||||
private Object hitboxTeleport(Hitbox hitbox) {
|
||||
if (hitbox.isPivotVisible() && hitbox.getPosition().isDirty()) {
|
||||
this.cleanupQueue.add(() -> hitbox.getPosition().clearDirty());
|
||||
return NetworkUtils.createPivotMove(hitbox.getPivotId(), hitbox.getPositionCodec(), hitbox.getPosition().get());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket hitboxMount(Hitbox hitbox) {
|
||||
if (!hitbox.isPivotVisible()) {
|
||||
return null;
|
||||
} else {
|
||||
boolean hitboxVisible = hitbox.isHitboxVisible();
|
||||
boolean shadowVisible = hitbox.isShadowVisible();
|
||||
ArrayList<Integer> passengers = new ArrayList<>(2 + hitbox.getFireDisplay().getInUse().size());
|
||||
if (hitboxVisible) {
|
||||
passengers.add(hitbox.getHitboxId());
|
||||
}
|
||||
|
||||
if (shadowVisible) {
|
||||
passengers.add(hitbox.getShadowId());
|
||||
}
|
||||
|
||||
if (hitbox.isFireVisible()) {
|
||||
hitbox.getFireDisplay().getInUse().forEach(displayFire -> passengers.add(displayFire.getId()));
|
||||
}
|
||||
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(hitbox.getPivotId());
|
||||
buf.writeVarInt(passengers.size());
|
||||
passengers.forEach(buf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package com.ticxo.modelengine.v1_19_R3.parser.vfx;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXDisplayRenderer;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXRendererParser;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXDisplayRenderer.VFXModel;
|
||||
import com.ticxo.modelengine.v1_19_R3.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_19_R3.network.utils.Packets;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.craftbukkit.v1_19_R3.inventory.CraftItemStack;
|
||||
import org.bukkit.entity.ItemDisplay.ItemDisplayTransform;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class VFXDisplayParser implements VFXRendererParser<VFXDisplayRenderer> {
|
||||
public void sendToClients(VFXDisplayRenderer renderer) {
|
||||
IEntityData data = renderer.getVFX().getBase().getData();
|
||||
if (renderer.isRespawnRequired()) {
|
||||
this.teleport(renderer.getVFXModel());
|
||||
this.spawn(data.getTracking().keySet(), renderer.getVFXModel());
|
||||
renderer.setRespawnRequired(false);
|
||||
} else {
|
||||
this.update(data.getTracking().keySet(), renderer.getVFXModel());
|
||||
this.spawn(data.getStartTracking(), renderer.getVFXModel());
|
||||
this.remove(data.getStopTracking(), renderer.getVFXModel());
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy(VFXDisplayRenderer renderer) {
|
||||
IEntityData data = renderer.getVFX().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer.getVFXModel());
|
||||
}
|
||||
|
||||
public void spawn(Set<UUID> targets, VFXModel vfx) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
set.add(this.pivotSpawn(vfx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(vfx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.vfxSpawn(vfx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.vfxData(vfx, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(vfx));
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
public void update(Set<UUID> targets, VFXModel vfx) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
set.add(this.teleport(vfx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.vfxData(vfx, false));
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Set<UUID> targets, VFXModel vfx) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(2);
|
||||
buf.writeVarInt(vfx.getPivotId());
|
||||
buf.writeVarInt(vfx.getModelId());
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(VFXModel vfx) {
|
||||
return NetworkUtils.createPivotSpawn(vfx.getPivotId(), vfx.getPivotUuid(), vfx.getOrigin().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(VFXModel vfx) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(vfx.getPivotId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket vfxSpawn(VFXModel vfx) {
|
||||
Vector3f location = vfx.getOrigin().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
vfx.getModelId(), vfx.getModelUuid(), location.x, location.y, location.z, 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket vfxData(VFXModel vfx, boolean spawn) {
|
||||
if (!spawn && !vfx.isModelDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(vfx.getModelId());
|
||||
if (spawn) {
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
EntityUtils.writeData(buf, 9, EntityDataSerializers.INT, 1);
|
||||
EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, 4096.0F);
|
||||
EntityUtils.writeData(buf, 23, EntityDataSerializers.BYTE, (byte)ItemDisplayTransform.HEAD.ordinal());
|
||||
} else if (vfx.isModelDirty()) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
}
|
||||
|
||||
vfx.getPosition().ifDirty(vector3f -> EntityUtils.writeData(buf, 10, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
vfx.getScale().ifDirty(vector3f -> EntityUtils.writeData(buf, 11, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
vfx.getLeftRotation()
|
||||
.ifDirty(
|
||||
quaternionf -> EntityUtils.writeData(buf, 12, EntityDataSerializers.QUATERNION, quaternionf.rotateY((float) Math.PI, new Quaternionf())), spawn
|
||||
);
|
||||
vfx.getModel().ifDirty(itemStack -> EntityUtils.writeData(buf, 22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(itemStack)), spawn);
|
||||
vfx.getBrightness().ifDirty(val -> EntityUtils.writeData(buf, 15, EntityDataSerializers.INT, val), spawn);
|
||||
buf.writeByte(255);
|
||||
vfx.clearModelDirty();
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private Object teleport(VFXModel vfx) {
|
||||
return !vfx.getOrigin().isDirty() ? null : NetworkUtils.createPivotMove(vfx.getPivotId(), vfx.getPositionCodec(), vfx.getOrigin().get());
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket mount(VFXModel vfx) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(vfx.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(vfx.getModelId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.ticxo.modelengine.v1_20_R1;
|
||||
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils.ReflectionEnum;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.control.MoveControl;
|
||||
import net.minecraft.world.level.Level;
|
||||
|
||||
public enum NMSFields implements ReflectionEnum {
|
||||
ENTITY_dimensions(Entity.class, "bh", "dimensions"),
|
||||
ENTITY_eyeHeight(Entity.class, "bi", "eyeHeight"),
|
||||
LIVING_ENTITY_noJumpDelay(LivingEntity.class, "cb", "noJumpDelay"),
|
||||
LIVING_ENTITY_jumping(LivingEntity.class, "bk", "jumping"),
|
||||
MOB_lookControl(Mob.class, "bK", "lookControl"),
|
||||
MOB_moveControl(Mob.class, "bL", "moveControl"),
|
||||
MOB_navigation(Mob.class, "bN", "navigation"),
|
||||
MOB_goalSelector(Mob.class, "bO", "goalSelector"),
|
||||
MOB_bodyRotationControl(Mob.class, "bT", "bodyRotationControl"),
|
||||
SERVER_GAME_PACKET_LISTENER_IMPL_clientIsFloating(ServerGamePacketListenerImpl.class, "G", "clientIsFloating"),
|
||||
SERVER_GAME_PACKET_LISTENER_IMPL_connection(ServerGamePacketListenerImpl.class, "h", "connection"),
|
||||
MOVE_CONTROL_operation(MoveControl.class, "k", "operation"),
|
||||
TRACKED_ENTITY_serverEntity(TrackedEntity.class, "b", "serverEntity"),
|
||||
TRACKED_ENTITY_range(TrackedEntity.class, "d", "range"),
|
||||
LEVEL_threadSafeRandom(Level.class, "f", "threadSafeRandom"),
|
||||
CLIENTBOUND_MOVE_ENTITY_PACKET_entityId(ClientboundMoveEntityPacket.class, "a", "entityId"),
|
||||
CLIENTBOUND_ROTATE_HEAD_PACKET_entityId(ClientboundRotateHeadPacket.class, "a", "entityId");
|
||||
|
||||
private final Class<?> target;
|
||||
private final String obfuscated;
|
||||
private final String mapped;
|
||||
|
||||
private NMSFields(Class<?> target, String obfuscated, String mapped) {
|
||||
this.target = target;
|
||||
this.obfuscated = obfuscated;
|
||||
this.mapped = mapped;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getTarget() {
|
||||
return this.target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getObfuscated() {
|
||||
return this.obfuscated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMapped() {
|
||||
return this.mapped;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package com.ticxo.modelengine.v1_20_R1;
|
||||
|
||||
import com.mojang.math.MatrixUtil;
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.ServerInfo;
|
||||
import com.ticxo.modelengine.api.entity.data.BukkitEntityData;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.PivotOverride;
|
||||
import com.ticxo.modelengine.api.model.PivotOverrideRegistry;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.LeashRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.NameTagRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SegmentRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SubHitboxRenderer;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayRenderer;
|
||||
import com.ticxo.modelengine.api.model.render.ModelRenderer;
|
||||
import com.ticxo.modelengine.api.model.render.ModelRendererParser;
|
||||
import com.ticxo.modelengine.api.nms.NMSHandler;
|
||||
import com.ticxo.modelengine.api.nms.RenderParsers;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.nms.network.NetworkHandler;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXDisplayRenderer;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXRenderer;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXRendererParser;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.EntityHandlerImpl;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.NetworkHandlerImpl;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.parser.behavior.LeashParser;
|
||||
import com.ticxo.modelengine.v1_20_R1.parser.behavior.MountParser;
|
||||
import com.ticxo.modelengine.v1_20_R1.parser.behavior.NameTagParser;
|
||||
import com.ticxo.modelengine.v1_20_R1.parser.behavior.SegmentParser;
|
||||
import com.ticxo.modelengine.v1_20_R1.parser.behavior.SubHitboxParser;
|
||||
import com.ticxo.modelengine.v1_20_R1.parser.model.DisplayParser;
|
||||
import com.ticxo.modelengine.v1_20_R1.parser.vfx.VFXDisplayParser;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import org.apache.commons.lang3.tuple.Triple;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.util.Transformation;
|
||||
import org.joml.Matrix3f;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Quaternionfc;
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector3fc;
|
||||
|
||||
public class NMSHandler_v1_20_R1 implements NMSHandler {
|
||||
private final EntityHandler entityHandler = new EntityHandlerImpl();
|
||||
private final NetworkHandler networkHandler = new NetworkHandlerImpl();
|
||||
private final RenderParsers globalParsers = this.createParsers();
|
||||
private final VFXDisplayParser vfxDisplayParser = new VFXDisplayParser();
|
||||
|
||||
@Override
|
||||
public RenderParsers createParsers() {
|
||||
RenderParsers parsers = new RenderParsers();
|
||||
parsers.registerModelParser(renderer -> renderer instanceof DisplayRenderer, DisplayParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof MountRenderer, MountParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof LeashRenderer, LeashParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof NameTagRenderer, NameTagParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof SubHitboxRenderer, SubHitboxParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof SegmentRenderer, SegmentParser::new);
|
||||
return parsers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPivotOverrides() {
|
||||
PivotOverrideRegistry registry = ModelEngineAPI.getPivotOverrideRegistry();
|
||||
|
||||
for (PivotOverride override : registry.getAll()) {
|
||||
ModeledEntity modeledEntity = ModelEngineAPI.getModeledEntity(override.getUuid());
|
||||
BukkitEntityData data = modeledEntity != null && modeledEntity.getBase().getData() instanceof BukkitEntityData d ? d : null;
|
||||
boolean passengersDirty = override.getPassengers().isDirty();
|
||||
Set<UUID> lateTrackers = data != null ? data.getStartTracking() : Set.of();
|
||||
if (passengersDirty || !lateTrackers.isEmpty()) {
|
||||
FriendlyByteBuf byteBuf = NetworkUtils.createByteBuf(buf -> {
|
||||
buf.writeVarInt(override.getId());
|
||||
Collection<Integer> list = override.getPassengers().get();
|
||||
ArrayList<Integer> allPassengers = new ArrayList<>(list);
|
||||
if (modeledEntity != null) {
|
||||
List<Entity> basePassengers = modeledEntity.getBase().getPassengers();
|
||||
int[] snapshot = new int[basePassengers.size()];
|
||||
int i = 0;
|
||||
|
||||
for (Entity passenger : basePassengers) {
|
||||
int pid = passenger.getEntityId();
|
||||
snapshot[i++] = pid;
|
||||
if (!allPassengers.contains(pid)) {
|
||||
allPassengers.add(pid);
|
||||
}
|
||||
}
|
||||
|
||||
override.setBasePassengerSnapshot(snapshot);
|
||||
} else {
|
||||
for (int pid : override.getBasePassengerSnapshot()) {
|
||||
if (!allPassengers.contains(pid)) {
|
||||
allPassengers.add(pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buf.writeVarInt(allPassengers.size());
|
||||
allPassengers.forEach(buf::writeVarInt);
|
||||
});
|
||||
Set<UUID> recipients;
|
||||
if (data == null) {
|
||||
recipients = ServerInfo.getOnlinePlayers();
|
||||
} else if (passengersDirty) {
|
||||
recipients = new HashSet<>(data.getMutableTracking().keySet());
|
||||
recipients.addAll(lateTrackers);
|
||||
} else {
|
||||
recipients = lateTrackers;
|
||||
}
|
||||
|
||||
NetworkUtils.send(recipients, new ClientboundSetPassengersPacket(byteBuf));
|
||||
override.getPassengers().clearDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends ModelRenderer> ModelRendererParser<T> getModelRendererParser(T renderer) {
|
||||
return this.globalParsers.getModelParser(renderer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends BehaviorRenderer> BehaviorRendererParser<T> getBehaviorRendererParser(T renderer) {
|
||||
return this.globalParsers.getBehaviorParser(renderer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends VFXRenderer> VFXRendererParser<T> getVFXRendererParser(T renderer) {
|
||||
return renderer instanceof VFXDisplayRenderer ? (VFXRendererParser<T>)(Object)this.vfxDisplayParser : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMountPassengerUpdate(Set<UUID> targets, int mountId, Collection<Integer> passengerIds) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(mountId);
|
||||
buf.writeVarInt(passengerIds.size());
|
||||
passengerIds.forEach(buf::writeVarInt);
|
||||
ClientboundSetPassengersPacket packet = new ClientboundSetPassengersPacket(buf);
|
||||
|
||||
for (UUID target : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlushNow(packet));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Transformation decompose(Matrix4f transform) {
|
||||
float f = 1.0F / transform.m33();
|
||||
Triple<Quaternionf, Vector3f, Quaternionf> triple = MatrixUtil.svdDecompose(new Matrix3f(transform).scale(f));
|
||||
return new Transformation(
|
||||
transform.getTranslation(new Vector3f()).mul(f),
|
||||
new Quaternionf((Quaternionfc)triple.getLeft()),
|
||||
new Vector3f((Vector3fc)triple.getMiddle()),
|
||||
new Quaternionf((Quaternionfc)triple.getRight())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityHandler getEntityHandler() {
|
||||
return this.entityHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkHandler getNetworkHandler() {
|
||||
return this.networkHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RenderParsers getGlobalParsers() {
|
||||
return this.globalParsers;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.ticxo.modelengine.v1_20_R1;
|
||||
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils.MethodEnum;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.world.item.InstrumentItem;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public enum NMSMethods implements MethodEnum {
|
||||
TRACKED_ENTITY_getEffectiveRange(TrackedEntity.class, "b", "getEffectiveRange"),
|
||||
SERVER_GAME_PACKET_LISTENER_IMPL_checkLimit(ServerGamePacketListenerImpl.class, "checkLimit", "checkLimit", long.class),
|
||||
INSTRUMENT_ITEM_getInstrument(InstrumentItem.class, "d", "getInstrument", ItemStack.class);
|
||||
|
||||
private final Class<?> target;
|
||||
private final String obfuscated;
|
||||
private final String mapped;
|
||||
private final Class<?>[] parameterClasses;
|
||||
|
||||
private NMSMethods(Class<?> target, String obfuscated, String mapped, Class<?>... parameterClasses) {
|
||||
this.target = target;
|
||||
this.obfuscated = obfuscated;
|
||||
this.mapped = mapped;
|
||||
this.parameterClasses = parameterClasses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getTarget() {
|
||||
return this.target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getObfuscated() {
|
||||
return this.obfuscated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMapped() {
|
||||
return this.mapped;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?>[] getParameterClasses() {
|
||||
return this.parameterClasses;
|
||||
}
|
||||
}
|
||||
+687
@@ -0,0 +1,687 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.ServerInfo;
|
||||
import com.ticxo.modelengine.api.entity.BaseEntity;
|
||||
import com.ticxo.modelengine.api.entity.Hitbox;
|
||||
import com.ticxo.modelengine.api.entity.data.BukkitEntityData;
|
||||
import com.ticxo.modelengine.api.interaction.DynamicHitbox;
|
||||
import com.ticxo.modelengine.api.model.bone.ModelBone;
|
||||
import com.ticxo.modelengine.api.model.bone.type.SubHitbox;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityState;
|
||||
import com.ticxo.modelengine.api.nms.entity.HitboxEntity;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler.BoxRelToCam;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler.InteractionResult;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.BodyRotationController;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.LookController;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.MoveController;
|
||||
import com.ticxo.modelengine.api.nms.impl.TempTrackedEntity;
|
||||
import com.ticxo.modelengine.api.utils.RaceConditionUtil;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.api.utils.config.ConfigProperty;
|
||||
import com.ticxo.modelengine.api.utils.config.DebugToggle;
|
||||
import com.ticxo.modelengine.api.utils.logger.TLogger;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.api.utils.promise.Promise;
|
||||
import com.ticxo.modelengine.v1_20_R1.NMSFields;
|
||||
import com.ticxo.modelengine.v1_20_R1.NMSMethods;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.controller.BodyRotationControlWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.controller.LookControlWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.controller.MoveControlWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.hitbox.HitboxEntityImpl;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.navigation.AmphibiousNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.navigation.FlyingNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.navigation.GroundNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.navigation.WallClimberNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.navigation.WaterBoundNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.patch.PatchedServerGamePacketListener;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.Packets;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSoundEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundUseItemPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.damagesource.DamageSource;
|
||||
import net.minecraft.world.entity.EntityDimensions;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.MoverType;
|
||||
import net.minecraft.world.entity.ai.goal.Goal;
|
||||
import net.minecraft.world.entity.ai.goal.GoalSelector;
|
||||
import net.minecraft.world.entity.ai.goal.WrappedGoal;
|
||||
import net.minecraft.world.entity.ai.navigation.AmphibiousPathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.WallClimberNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.WaterBoundPathNavigation;
|
||||
import net.minecraft.world.item.Instrument;
|
||||
import net.minecraft.world.item.InstrumentItem;
|
||||
import net.minecraft.world.item.UseAnim;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Particle;
|
||||
import org.bukkit.Particle.DustOptions;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.util.CraftLocation;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.util.CraftMagicNumbers;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.BoundingBox;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class EntityHandlerImpl implements EntityHandler {
|
||||
private static final MethodHandle JUMPING_GETTER = createJumpingGetter();
|
||||
private final Set<UUID> forceInvisible = ConcurrentHashMap.newKeySet();
|
||||
private double forceRenderWidth;
|
||||
private double forceRenderHeight;
|
||||
|
||||
private static MethodHandle createJumpingGetter() {
|
||||
Field field = ReflectionUtils.getField(NMSFields.LIVING_ENTITY_jumping);
|
||||
if (field == null) {
|
||||
return null;
|
||||
} else {
|
||||
try {
|
||||
return MethodHandles.lookup().unreflectGetter(field);
|
||||
} catch (IllegalAccessException var2) {
|
||||
var2.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public EntityHandlerImpl() {
|
||||
ModelEngineAPI.getAPI().getConfigManager().registerReferenceUpdate(this::updateConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateConfig() {
|
||||
this.forceRenderWidth = ConfigProperty.BLOCK_CULL_IGNORE_SIZE_WIDTH.getDouble();
|
||||
this.forceRenderHeight = ConfigProperty.BLOCK_CULL_IGNORE_SIZE_HEIGHT.getDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextEntityId() {
|
||||
return CraftMagicNumbers.INSTANCE.nextEntityId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHitbox(Entity entity, @NotNull Hitbox hitbox) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
EntityDimensions box = new EntityDimensions((float)hitbox.getMaxWidth(), (float)hitbox.getHeight(), true);
|
||||
ReflectionUtils.set(nms, NMSFields.ENTITY_dimensions, box);
|
||||
ReflectionUtils.set(nms, NMSFields.ENTITY_eyeHeight, (float)hitbox.getEyeHeight());
|
||||
nms.setBoundingBox(box.makeBoundingBox(nms.position()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStepHeight(Entity entity, double height) {
|
||||
EntityUtils.nms(entity).setMaxUpStep((float)height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getStepHeight(Entity entity) {
|
||||
return EntityUtils.nms(entity).maxUpStep();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Entity entity, double x, double y, double z) {
|
||||
EntityUtils.nms(entity).setPos(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void movePassenger(Entity entity, double x, double y, double z) {
|
||||
net.minecraft.world.entity.Entity nmsEntity = EntityUtils.nms(entity);
|
||||
double seatY = y + nmsEntity.getMyRidingOffset();
|
||||
nmsEntity.setPos(x, seatY, z);
|
||||
nmsEntity.setDeltaMovement(Vec3.ZERO);
|
||||
nmsEntity.resetFallDistance();
|
||||
if (nmsEntity instanceof ServerPlayer player) {
|
||||
ReflectionUtils.set(player.connection, NMSFields.SERVER_GAME_PACKET_LISTENER_IMPL_clientIsFloating, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forceSpawn(BaseEntity<?> entity, Player player) {
|
||||
if (player != null) {
|
||||
if (entity.getData() instanceof BukkitEntityData bukkitEntityData) {
|
||||
bukkitEntityData.getTracked().sendPairingData(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forceDespawn(BaseEntity<?> entity, Player player) {
|
||||
if (player != null) {
|
||||
NetworkUtils.send(player.getUniqueId(), new ClientboundRemoveEntitiesPacket(new int[]{entity.getEntityId()}));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setForcedInvisible(Entity entity, boolean flag) {
|
||||
if (this.isForcedInvisible(entity) != flag) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
byte data = 0;
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
data = TMath.setBit(data, i, nms.getSharedFlag(i));
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
this.forceInvisible.add(entity.getUniqueId());
|
||||
data = TMath.setBit(data, 5, true);
|
||||
} else {
|
||||
this.forceInvisible.remove(entity.getUniqueId());
|
||||
}
|
||||
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(entity.getEntityId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, data);
|
||||
buf.writeByte(255);
|
||||
ClientboundSetEntityDataPacket packet = new ClientboundSetEntityDataPacket(buf);
|
||||
NetworkUtils.send(ServerInfo.getOnlinePlayers(), packet);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isForcedInvisible(UUID uuid) {
|
||||
return this.forceInvisible.contains(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearForcedInvisible(UUID uuid) {
|
||||
this.forceInvisible.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyRotationController wrapBodyRotationControl(Entity entity, Supplier<BodyRotationController> def) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
BodyRotationControlWrapper var5 = new BodyRotationControlWrapper(mob);
|
||||
return (BodyRotationController)(ReflectionUtils.set(mob, NMSFields.MOB_bodyRotationControl, var5) ? var5 : def.get());
|
||||
} else {
|
||||
return def.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MoveController wrapMoveController(Entity entity, Supplier<MoveController> def) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
if (mob.getMoveControl() instanceof MoveController controller) {
|
||||
return controller;
|
||||
} else {
|
||||
MoveControlWrapper controller = new MoveControlWrapper(mob, mob.getMoveControl());
|
||||
return (MoveController)(ReflectionUtils.set(mob, NMSFields.MOB_moveControl, controller) ? controller : def.get());
|
||||
}
|
||||
} else {
|
||||
return def.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LookController wrapLookController(Entity entity, Supplier<LookController> def) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
if (mob.getLookControl() instanceof LookController controller) {
|
||||
return controller;
|
||||
} else {
|
||||
LookControlWrapper controller = new LookControlWrapper(mob, mob.getLookControl());
|
||||
return (LookController)(ReflectionUtils.set(mob, NMSFields.MOB_lookControl, controller) ? controller : def.get());
|
||||
}
|
||||
} else {
|
||||
return def.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wrapNavigation(Entity entity) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
try {
|
||||
Field navField = ReflectionUtils.getField(NMSFields.MOB_navigation);
|
||||
PathNavigation oldNav = mob.getNavigation();
|
||||
PathNavigation newNav;
|
||||
if (oldNav instanceof WallClimberNavigation wallClimberNavigation) {
|
||||
newNav = new WallClimberNavigationWrapper(mob, wallClimberNavigation);
|
||||
} else if (oldNav instanceof GroundPathNavigation groundPathNavigation) {
|
||||
newNav = new GroundNavigationWrapper(mob, groundPathNavigation);
|
||||
} else if (oldNav instanceof FlyingPathNavigation flyingPathNavigation) {
|
||||
newNav = new FlyingNavigationWrapper(mob, flyingPathNavigation);
|
||||
} else if (oldNav instanceof WaterBoundPathNavigation) {
|
||||
newNav = new WaterBoundNavigationWrapper(mob);
|
||||
} else {
|
||||
if (!(oldNav instanceof AmphibiousPathNavigation amphibiousPathNavigation)) {
|
||||
TLogger.warn("Failed to create custom navigation for " + mob.getType() + ": " + mob.getUUID());
|
||||
TLogger.warn("Reason: Navigation class type is " + oldNav.getClass().getSimpleName() + ".");
|
||||
return;
|
||||
}
|
||||
|
||||
newNav = new AmphibiousNavigationWrapper(mob, amphibiousPathNavigation);
|
||||
}
|
||||
|
||||
navField.set(mob, newNav);
|
||||
GoalSelector goalSelector = (GoalSelector)ReflectionUtils.getField(NMSFields.MOB_goalSelector).get(mob);
|
||||
RaceConditionUtil.wrapConmod(() -> this.replaceNavigation(goalSelector, newNav));
|
||||
} catch (IllegalAccessException var10) {
|
||||
var10.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void replaceNavigation(GoalSelector goalSelector, PathNavigation newNav) {
|
||||
try {
|
||||
for (WrappedGoal wrappedGoal : goalSelector.getAvailableGoals()) {
|
||||
Goal goal = wrappedGoal.getGoal();
|
||||
|
||||
for (Field field : goal.getClass().getDeclaredFields()) {
|
||||
field = ReflectionUtils.getField(goal.getClass(), field.getName());
|
||||
Object f = field.get(goal);
|
||||
if (f instanceof PathNavigation) {
|
||||
field.set(goal, newNav);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException var11) {
|
||||
var11.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public HitboxEntity createHitbox(Location location, ModelBone bone, SubHitbox subHitbox) {
|
||||
ServerLevel level = ((CraftWorld)location.getWorld()).getHandle();
|
||||
HitboxEntityImpl entity = new HitboxEntityImpl(level, bone, subHitbox);
|
||||
entity.queueLocation(new Vector3f().set(location.getX(), location.getY(), location.getZ()));
|
||||
entity.setPos(location.getX(), location.getY(), location.getZ());
|
||||
ModelEngineAPI.setRenderCanceled(entity.getId(), true);
|
||||
Promise.start(entity.getBukkitEntity()).thenRunSync(() -> {
|
||||
level.addFreshEntity(entity);
|
||||
ModelEngineAPI.getInteractionTracker().addHitbox(entity);
|
||||
});
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public HitboxEntity castHitbox(Entity entity) {
|
||||
return EntityUtils.nms(entity) instanceof HitboxEntity hitbox ? hitbox : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hurt(Entity entity, Object source, float amount) {
|
||||
if (source instanceof DamageSource damageSource) {
|
||||
return EntityUtils.nms(entity).hurt(damageSource, amount);
|
||||
} else {
|
||||
throw new RuntimeException("Passed in source is not an NMS DamageSource.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InteractionResult interact(Entity entity, HumanEntity player, EquipmentSlot hand) {
|
||||
if (EntityUtils.nms(entity) instanceof LivingEntity livingEntity) {
|
||||
net.minecraft.world.InteractionResult result = livingEntity.interact(
|
||||
((CraftPlayer)player).getHandle(), hand == EquipmentSlot.HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND
|
||||
);
|
||||
|
||||
return switch (result) {
|
||||
case SUCCESS -> InteractionResult.SUCCESS;
|
||||
case CONSUME -> InteractionResult.CONSUME;
|
||||
case CONSUME_PARTIAL -> InteractionResult.CONSUME_PARTIAL;
|
||||
case PASS -> InteractionResult.PASS;
|
||||
case FAIL -> InteractionResult.FAIL;
|
||||
default -> throw new IncompatibleClassChangeError();
|
||||
};
|
||||
} else {
|
||||
return InteractionResult.FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spawnDynamicHitbox(DynamicHitbox hitbox) {
|
||||
Vector location = hitbox.getPositionTracker().get();
|
||||
final Packets.PacketSupplier pivotSpawn = NetworkUtils.createPivotSpawn(
|
||||
DynamicHitbox.getPivotId(), DynamicHitbox.getPivotUUID(), location.toVector3f().add(0.0F, -0.5202F, 0.0F)
|
||||
);
|
||||
FriendlyByteBuf pivotDataBuf = NetworkUtils.createByteBuf();
|
||||
pivotDataBuf.writeVarInt(DynamicHitbox.getPivotId());
|
||||
EntityUtils.writeData(pivotDataBuf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(pivotDataBuf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
pivotDataBuf.writeByte(255);
|
||||
final ClientboundSetEntityDataPacket pivotData = new ClientboundSetEntityDataPacket(pivotDataBuf);
|
||||
final ClientboundAddEntityPacket hitboxSpawn = new ClientboundAddEntityPacket(
|
||||
DynamicHitbox.getHitboxId(),
|
||||
DynamicHitbox.getHitboxUUID(),
|
||||
location.getX(),
|
||||
location.getY() - 0.5202,
|
||||
location.getZ(),
|
||||
0.0F,
|
||||
0.0F,
|
||||
EntityType.SLIME,
|
||||
0,
|
||||
Vec3.ZERO,
|
||||
0.0
|
||||
);
|
||||
FriendlyByteBuf hitboxDataBuf = NetworkUtils.createByteBuf();
|
||||
hitboxDataBuf.writeVarInt(DynamicHitbox.getHitboxId());
|
||||
EntityUtils.writeData(hitboxDataBuf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(hitboxDataBuf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(hitboxDataBuf, 16, EntityDataSerializers.INT, 2);
|
||||
hitboxDataBuf.writeByte(255);
|
||||
final ClientboundSetEntityDataPacket hitboxData = new ClientboundSetEntityDataPacket(hitboxDataBuf);
|
||||
FriendlyByteBuf mountBuf = NetworkUtils.createByteBuf();
|
||||
mountBuf.writeVarInt(DynamicHitbox.getPivotId());
|
||||
mountBuf.writeVarInt(1);
|
||||
mountBuf.writeVarInt(DynamicHitbox.getHitboxId());
|
||||
final ClientboundSetPassengersPacket mount = new ClientboundSetPassengersPacket(mountBuf);
|
||||
NetworkUtils.sendBundled(Set.of(hitbox.getPlayer().getUniqueId()), new Packets() {
|
||||
{
|
||||
this.add(pivotSpawn);
|
||||
this.add(pivotData);
|
||||
this.add(hitboxSpawn);
|
||||
this.add(hitboxData);
|
||||
this.add(mount);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDynamicHitbox(DynamicHitbox hitbox) {
|
||||
Vector3f vector = hitbox.getPositionTracker().get().toVector3f().add(0.0F, -0.5202F, 0.0F);
|
||||
NetworkUtils.send(
|
||||
hitbox.getPlayer().getUniqueId(), NetworkUtils.createPivotTeleport(DynamicHitbox.getPivotId(), vector).supply(hitbox.getPlayer().getUniqueId())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyDynamicHitbox(DynamicHitbox hitbox) {
|
||||
ClientboundRemoveEntitiesPacket destroy = new ClientboundRemoveEntitiesPacket(new int[]{DynamicHitbox.getHitboxId(), DynamicHitbox.getPivotId()});
|
||||
NetworkUtils.send(hitbox.getPlayer().getUniqueId(), destroy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forceUseItem(Player player, EquipmentSlot hand) {
|
||||
ItemStack stack = player.getEquipment().getItem(hand);
|
||||
net.minecraft.world.item.ItemStack nmsStack = ((CraftItemStack)stack).handle;
|
||||
ServerPlayer nmsPlayer = (ServerPlayer)EntityUtils.nms(player);
|
||||
ServerGamePacketListenerImpl connection = nmsPlayer.connection;
|
||||
ServerboundUseItemPacket useItemPacket = new ServerboundUseItemPacket(
|
||||
hand == EquipmentSlot.HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND, 0
|
||||
);
|
||||
useItemPacket.timestamp = System.currentTimeMillis();
|
||||
PatchedServerGamePacketListener.handleUseItem(
|
||||
useItemPacket,
|
||||
connection,
|
||||
interactionResult -> {
|
||||
if (nmsStack.getUseAnimation() != UseAnim.NONE && interactionResult == net.minecraft.world.InteractionResult.CONSUME) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(player.getEntityId());
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.BYTE, (byte)(hand == EquipmentSlot.HAND ? 1 : 3));
|
||||
buf.writeByte(255);
|
||||
ClientboundSetEntityDataPacket packet = new ClientboundSetEntityDataPacket(buf);
|
||||
NetworkUtils.send(player.getUniqueId(), packet);
|
||||
if (nmsStack.getItem() instanceof InstrumentItem instrumentItem) {
|
||||
Optional<? extends Holder<Instrument>> optional = ReflectionUtils.call(instrumentItem, NMSMethods.INSTRUMENT_ITEM_getInstrument, nmsStack);
|
||||
optional.ifPresent(
|
||||
instrumentHolder -> {
|
||||
Instrument instrument = (Instrument)instrumentHolder.value();
|
||||
Holder<SoundEvent> soundEvent = instrument.soundEvent();
|
||||
float f = instrument.range() / 16.0F;
|
||||
RandomSource random = ReflectionUtils.get(nmsPlayer.level(), NMSFields.LEVEL_threadSafeRandom);
|
||||
ClientboundSoundEntityPacket soundPacket = new ClientboundSoundEntityPacket(
|
||||
soundEvent, SoundSource.RECORDS, nmsPlayer, f, 1.0F, random.nextLong()
|
||||
);
|
||||
NetworkUtils.send(player.getUniqueId(), soundPacket);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYRot(Entity entity) {
|
||||
return EntityUtils.nms(entity).getYRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYHeadRot(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
return nms instanceof LivingEntity livingEntity ? livingEntity.getYHeadRot() : nms.getYRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getXHeadRot(Entity entity) {
|
||||
return EntityUtils.nms(entity).getXRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYBodyRot(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
return nms instanceof LivingEntity livingEntity ? livingEntity.yBodyRot : nms.getYRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYRot(Entity entity, float angle) {
|
||||
EntityUtils.nms(entity).setYRot(angle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYHeadRot(Entity entity, float angle) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
if (nms instanceof LivingEntity livingEntity) {
|
||||
livingEntity.setYHeadRot(angle);
|
||||
} else {
|
||||
nms.setYRot(angle);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setXHeadRot(Entity entity, float angle) {
|
||||
EntityUtils.nms(entity).setXRot(angle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYBodyRot(Entity entity, float angle) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
if (nms instanceof LivingEntity livingEntity) {
|
||||
livingEntity.setYBodyRot(angle);
|
||||
} else {
|
||||
nms.setYRot(angle);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(Entity entity, double x, double y, double z) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
nms.move(MoverType.SELF, new Vec3(x, y, z));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWalking(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
if (nms.tickCount < 1) {
|
||||
return false;
|
||||
} else {
|
||||
double dX = nms.getX() - nms.xOld;
|
||||
double dZ = nms.getZ() - nms.zOld;
|
||||
return dX * dX + dZ * dZ > 2.5000003E-7F;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStrafing(Entity entity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isJumping(Entity entity) {
|
||||
if (JUMPING_GETTER != null && EntityUtils.nms(entity) instanceof LivingEntity livingEntity) {
|
||||
try {
|
||||
return (boolean)JUMPING_GETTER.invokeExact((LivingEntity)livingEntity);
|
||||
} catch (Throwable var4) {
|
||||
var4.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFlying(Entity entity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getHealth(Entity entity) {
|
||||
return EntityUtils.nms(entity) instanceof LivingEntity livingEntity ? livingEntity.getHealth() : 20.0F;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMaxHealth(Entity entity) {
|
||||
return EntityUtils.nms(entity) instanceof LivingEntity livingEntity ? livingEntity.getMaxHealth() : 20.0F;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector3f getPivotOffset(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nmsEntity = EntityUtils.nms(entity);
|
||||
return new Vector3f(0.0F, (float)nmsEntity.getPassengersRidingOffset(), 0.0F);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRemoved(Entity entity) {
|
||||
return EntityUtils.nms(entity).isRemoved();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBoundingBox(Entity entity, BoundingBox dest) {
|
||||
AABB nms = EntityUtils.nms(entity).getBoundingBox();
|
||||
dest.resize(nms.minX, nms.minY, nms.minZ, nms.maxX, nms.maxY, nms.maxZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getGlowColor(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
return nms.getTeamColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDeathTick(Entity entity, int tick) {
|
||||
if (EntityUtils.nms(entity) instanceof LivingEntity livingEntity) {
|
||||
livingEntity.deathTime = tick;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public com.ticxo.modelengine.api.nms.entity.wrapper.TrackedEntity wrapTrackedEntity(Entity entity) {
|
||||
ServerLevel level = ((CraftWorld)entity.getWorld()).getHandle();
|
||||
Int2ObjectMap<TrackedEntity> map = level.getChunkSource().chunkMap.entityMap;
|
||||
int entityId = entity.getEntityId();
|
||||
TrackedEntity tracker = (TrackedEntity)map.get(entityId);
|
||||
return (com.ticxo.modelengine.api.nms.entity.wrapper.TrackedEntity)(tracker == null
|
||||
? new TempTrackedEntity(entity)
|
||||
: new TrackedEntityImpl(entity, () -> {
|
||||
ServerLevel lvl = ((CraftWorld)entity.getWorld()).getHandle();
|
||||
Int2ObjectMap<TrackedEntity> m = lvl.getChunkSource().chunkMap.entityMap;
|
||||
return m == null ? null : (TrackedEntity)m.get(entity.getEntityId());
|
||||
}, tracker));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldCull(Player player, Location eyePosition, BoundingBox box) {
|
||||
CraftWorld world = (CraftWorld)player.getWorld();
|
||||
Vec3 start = CraftLocation.toVec3D(eyePosition);
|
||||
if (!(box.getWidthX() >= this.forceRenderWidth) && !(box.getWidthZ() >= this.forceRenderWidth) && !(box.getHeight() >= this.forceRenderHeight)) {
|
||||
int minX = Mth.floor(box.getMinX());
|
||||
int minY = Mth.floor(box.getMinY());
|
||||
int minZ = Mth.floor(box.getMinZ());
|
||||
int maxX = Mth.ceil(box.getMaxX()) - 1;
|
||||
int maxY = Mth.ceil(box.getMaxY()) - 1;
|
||||
int maxZ = Mth.ceil(box.getMaxZ()) - 1;
|
||||
BoxRelToCam relX = BoxRelToCam.from(minX, maxX, Mth.floor(start.x));
|
||||
BoxRelToCam relY = BoxRelToCam.from(minY, maxY, Mth.floor(start.y));
|
||||
BoxRelToCam relZ = BoxRelToCam.from(minZ, maxZ, Mth.floor(start.z));
|
||||
if (relX == BoxRelToCam.INSIDE && relY == BoxRelToCam.INSIDE && relZ == BoxRelToCam.INSIDE) {
|
||||
return false;
|
||||
} else {
|
||||
if (DebugToggle.isDebugging(DebugToggle.SHOW_CULL_POINTS)) {
|
||||
EntityHandler.sweepCullPoints(minX, minY, minZ, maxX, maxY, maxZ, relX, relY, relZ, (x, y, z) -> {
|
||||
world.spawnParticle(Particle.REDSTONE, x, y, z, 1, new DustOptions(Color.RED, 0.2F));
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
return !EntityHandler.sweepCullPoints(minX, minY, minZ, maxX, maxY, maxZ, relX, relY, relZ, (x, y, z) -> isVisible(world, start, new Vec3(x, y, z)));
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isVisible(CraftWorld world, Vec3 startPos, Vec3 endPos) {
|
||||
return OcclusionClipContext.isClear(world.getHandle(), startPos, endPos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readEntityState(Entity entity, EntityState state) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
state.yRot = nms.getYRot();
|
||||
state.xHeadRot = nms.getXRot();
|
||||
if (nms instanceof LivingEntity living) {
|
||||
state.yHeadRot = living.getYHeadRot();
|
||||
state.yBodyRot = living.yBodyRot;
|
||||
state.health = living.getHealth();
|
||||
} else {
|
||||
state.yHeadRot = state.yRot;
|
||||
state.yBodyRot = state.yRot;
|
||||
state.health = 20.0F;
|
||||
}
|
||||
|
||||
boolean walking = false;
|
||||
if (nms.tickCount >= 1) {
|
||||
double dX = nms.getX() - nms.xOld;
|
||||
double dZ = nms.getZ() - nms.zOld;
|
||||
walking = dX * dX + dZ * dZ > 2.5000003E-7F;
|
||||
}
|
||||
|
||||
state.walking = walking;
|
||||
state.strafing = false;
|
||||
state.flying = false;
|
||||
state.removed = nms.isRemoved();
|
||||
state.jumping = this.isJumping(entity);
|
||||
state.glowing = this.isGlowing(entity);
|
||||
state.onFire = this.isVisualFire(entity);
|
||||
this.getBoundingBox(entity, state.boundingBox);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity;
|
||||
|
||||
public class EntityMetadataID {
|
||||
public static final int ENTITY_SHARED_DATA = 0;
|
||||
public static final int ENTITY_AIR = 1;
|
||||
public static final int ENTITY_CUSTOM_NAME = 2;
|
||||
public static final int ENTITY_CUSTOM_NAME_VISIBLE = 3;
|
||||
public static final int INTERACTION_WIDTH = 8;
|
||||
public static final int INTERACTION_HEIGHT = 9;
|
||||
public static final int INTERACTION_RESPONSE = 10;
|
||||
public static final int DISPLAY_INTERPOLATION_DELAY = 8;
|
||||
public static final int DISPLAY_TRANSFORM_DURATION = 9;
|
||||
public static final int DISPLAY_TRANSLATION = 10;
|
||||
public static final int DISPLAY_SCALE = 11;
|
||||
public static final int DISPLAY_ROTATION_LEFT = 12;
|
||||
public static final int DISPLAY_ROTATION_RIGHT = 13;
|
||||
public static final int DISPLAY_BILLBOARD = 14;
|
||||
public static final int DISPLAY_BRIGHTNESS = 15;
|
||||
public static final int DISPLAY_VIEW_RANGE = 16;
|
||||
public static final int DISPLAY_SHADOW_RADIUS = 17;
|
||||
public static final int DISPLAY_WIDTH = 19;
|
||||
public static final int DISPLAY_HEIGHT = 20;
|
||||
public static final int DISPLAY_GLOW_COLOR = 21;
|
||||
public static final int ITEM_DISPLAY_ITEM = 22;
|
||||
public static final int ITEM_DISPLAY_TYPE = 23;
|
||||
public static final int TEXT_DISPLAY_TEXT = 23;
|
||||
public static final int TEXT_LINE_WIDTH = 24;
|
||||
public static final int TEXT_DISPLAY_BACKGROUND = 25;
|
||||
public static final int TEXT_DISPLAY_OPACITY = 26;
|
||||
public static final int TEXT_DISPLAY_OPTIONS = 27;
|
||||
public static final int AREA_EFFECT_CLOUD_RADIUS = 8;
|
||||
public static final int LIVING_ENTITY_HAND_DATA = 8;
|
||||
public static final int LIVING_ENTITY_HEALTH = 9;
|
||||
public static final int ARMOR_STAND_SHARED_DATA = 15;
|
||||
public static final int SLIME_SIZE = 16;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity;
|
||||
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.syncher.EntityDataSerializer;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftEntity;
|
||||
|
||||
public class EntityUtils {
|
||||
public static Entity nms(org.bukkit.entity.Entity entity) {
|
||||
return ((CraftEntity)entity).getHandleRaw();
|
||||
}
|
||||
|
||||
public static <T> void writeData(FriendlyByteBuf byteBuf, int id, EntityDataSerializer<T> serializer, T val) {
|
||||
int serializedId = EntityDataSerializers.getSerializedId(serializer);
|
||||
byteBuf.writeByte(id);
|
||||
byteBuf.writeVarInt(serializedId);
|
||||
serializer.write(byteBuf, val);
|
||||
}
|
||||
|
||||
public static void writeDataUnsafe(FriendlyByteBuf byteBuf, int id, EntityDataSerializer serializer, Object val) {
|
||||
int serializedId = EntityDataSerializers.getSerializedId(serializer);
|
||||
byteBuf.writeByte(id);
|
||||
byteBuf.writeVarInt(serializedId);
|
||||
serializer.write(byteBuf, val);
|
||||
}
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.BlockPos.MutableBlockPos;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.ClipContext;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.ClipContext.Block;
|
||||
import net.minecraft.world.level.ClipContext.Fluid;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.ChunkAccess;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraft.world.phys.shapes.Shapes;
|
||||
import net.minecraft.world.phys.shapes.VoxelShape;
|
||||
|
||||
public class OcclusionClipContext extends ClipContext {
|
||||
public OcclusionClipContext(Vec3 start, Vec3 end) {
|
||||
super(start, end, Block.VISUAL, Fluid.NONE, null);
|
||||
}
|
||||
|
||||
public VoxelShape getBlockShape(BlockState state, BlockGetter world, BlockPos pos) {
|
||||
return occlusionShape(state, world, pos);
|
||||
}
|
||||
|
||||
private static VoxelShape occlusionShape(BlockState state, BlockGetter world, BlockPos pos) {
|
||||
return state.isOpaque() && state.getOcclusionShape(world, pos) == Shapes.block() ? Shapes.block() : Shapes.empty();
|
||||
}
|
||||
|
||||
static boolean isClear(Level level, Vec3 from, Vec3 to) {
|
||||
if (from.equals(to)) {
|
||||
return true;
|
||||
} else {
|
||||
double toX = Mth.lerp(-1.0E-7, to.x, from.x);
|
||||
double toY = Mth.lerp(-1.0E-7, to.y, from.y);
|
||||
double toZ = Mth.lerp(-1.0E-7, to.z, from.z);
|
||||
double fromX = Mth.lerp(-1.0E-7, from.x, to.x);
|
||||
double fromY = Mth.lerp(-1.0E-7, from.y, to.y);
|
||||
double fromZ = Mth.lerp(-1.0E-7, from.z, to.z);
|
||||
int x = Mth.floor(fromX);
|
||||
int y = Mth.floor(fromY);
|
||||
int z = Mth.floor(fromZ);
|
||||
double dx = toX - fromX;
|
||||
double dy = toY - fromY;
|
||||
double dz = toZ - fromZ;
|
||||
int signX = Mth.sign(dx);
|
||||
int signY = Mth.sign(dy);
|
||||
int signZ = Mth.sign(dz);
|
||||
double tDeltaX = signX == 0 ? Double.MAX_VALUE : signX / dx;
|
||||
double tDeltaY = signY == 0 ? Double.MAX_VALUE : signY / dy;
|
||||
double tDeltaZ = signZ == 0 ? Double.MAX_VALUE : signZ / dz;
|
||||
double tX = tDeltaX * (signX > 0 ? 1.0 - Mth.frac(fromX) : Mth.frac(fromX));
|
||||
double tY = tDeltaY * (signY > 0 ? 1.0 - Mth.frac(fromY) : Mth.frac(fromY));
|
||||
double tZ = tDeltaZ * (signZ > 0 ? 1.0 - Mth.frac(fromZ) : Mth.frac(fromZ));
|
||||
MutableBlockPos pos = new MutableBlockPos(x, y, z);
|
||||
ChunkAccess chunk = null;
|
||||
int chunkX = 0;
|
||||
int chunkZ = 0;
|
||||
|
||||
while (true) {
|
||||
pos.set(x, y, z);
|
||||
BlockState state;
|
||||
if (level.captureTreeGeneration) {
|
||||
state = level.getBlockStateIfLoaded(pos);
|
||||
} else if (level.isOutsideBuildHeight(pos)) {
|
||||
state = Blocks.VOID_AIR.defaultBlockState();
|
||||
} else {
|
||||
if (chunk == null || x >> 4 != chunkX || z >> 4 != chunkZ) {
|
||||
chunkX = x >> 4;
|
||||
chunkZ = z >> 4;
|
||||
chunk = level.getChunkIfLoadedImmediately(chunkX, chunkZ);
|
||||
}
|
||||
|
||||
state = chunk == null ? null : chunk.getBlockState(pos);
|
||||
}
|
||||
|
||||
if (state == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!state.isAir()) {
|
||||
VoxelShape shape = occlusionShape(state, level, pos);
|
||||
if (!shape.isEmpty()) {
|
||||
BlockHitResult hit = level.clipWithInteractionOverride(from, to, pos, shape, state);
|
||||
if (hit != null && from.distanceToSqr(hit.getLocation()) <= Double.MAX_VALUE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (tX > 1.0 && tY > 1.0 && tZ > 1.0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (tX < tY) {
|
||||
if (tX < tZ) {
|
||||
x += signX;
|
||||
tX += tDeltaX;
|
||||
} else {
|
||||
z += signZ;
|
||||
tZ += tDeltaZ;
|
||||
}
|
||||
} else if (tY < tZ) {
|
||||
y += signY;
|
||||
tY += tDeltaY;
|
||||
} else {
|
||||
z += signZ;
|
||||
tZ += tDeltaZ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+286
@@ -0,0 +1,286 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.NMSFields;
|
||||
import com.ticxo.modelengine.v1_20_R1.NMSMethods;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerPlayerConnection;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TrackedEntityImpl implements com.ticxo.modelengine.api.nms.entity.wrapper.TrackedEntity {
|
||||
private final Entity entity;
|
||||
private final Supplier<TrackedEntity> trackedEntitySupplier;
|
||||
private final Set<UUID> forcedPairing = Sets.newConcurrentHashSet();
|
||||
private final Set<UUID> forcedRemove = Sets.newConcurrentHashSet();
|
||||
private Predicate<Player> playerPredicate = DEFAULT_PREDICATE;
|
||||
@NotNull
|
||||
private TrackedEntity lastTrackedEntity;
|
||||
private static final int RECONCILE_INTERVAL = 20;
|
||||
private Object[] lastSeenBy;
|
||||
private volatile boolean viewersDirty = true;
|
||||
private volatile boolean mayHaveForced;
|
||||
private int ticksSinceReconcile;
|
||||
private final int initialBaseRange;
|
||||
|
||||
public TrackedEntityImpl(Entity entity, Supplier<TrackedEntity> trackedEntitySupplier, @NotNull TrackedEntity lastTrackedEntity) {
|
||||
this.entity = entity;
|
||||
this.trackedEntitySupplier = trackedEntitySupplier;
|
||||
this.lastTrackedEntity = lastTrackedEntity;
|
||||
this.initialBaseRange = this.getBaseRange();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInitialBaseRange() {
|
||||
return this.initialBaseRange;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBaseRange() {
|
||||
Integer range = ReflectionUtils.get(this.getTrackedEntity(), NMSFields.TRACKED_ENTITY_range);
|
||||
if (range == null) {
|
||||
throw new NullPointerException(String.format("Unable to retrieve base range of entity with UUID %s.", this.entity.getUniqueId()));
|
||||
} else {
|
||||
return range;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBaseRange(int range) {
|
||||
ReflectionUtils.set(this.getTrackedEntity(), NMSFields.TRACKED_ENTITY_range, range);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEffectiveRange() {
|
||||
Integer range = ReflectionUtils.call(this.getTrackedEntity(), NMSMethods.TRACKED_ENTITY_getEffectiveRange);
|
||||
if (range == null) {
|
||||
throw new NullPointerException(String.format("Unable to retrieve range of entity with UUID %s.", this.entity.getUniqueId()));
|
||||
} else {
|
||||
return range;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getTrackedPlayer() {
|
||||
boolean hasForced = !this.forcedPairing.isEmpty();
|
||||
boolean hasRemoved = !this.forcedRemove.isEmpty();
|
||||
Set<ServerPlayerConnection> seenBy = this.getTrackedEntity().seenBy;
|
||||
HashSet<UUID> set = new HashSet<>(Math.max(8, (int)((seenBy.size() + (hasForced ? this.forcedPairing.size() : 0)) / 0.75F) + 1));
|
||||
if (hasForced) {
|
||||
set.addAll(this.forcedPairing);
|
||||
}
|
||||
|
||||
for (ServerPlayerConnection connection : seenBy) {
|
||||
Player player = connection.getPlayer().getBukkitEntity();
|
||||
if (player != null && this.playerPredicate.test(player) && (!hasRemoved || !this.forcedRemove.contains(player.getUniqueId()))) {
|
||||
set.add(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getTrackedPlayer(Predicate<Player> predicate) {
|
||||
boolean hasForced = !this.forcedPairing.isEmpty();
|
||||
boolean hasRemoved = !this.forcedRemove.isEmpty();
|
||||
Set<ServerPlayerConnection> seenBy = this.getTrackedEntity().seenBy;
|
||||
HashSet<UUID> set = new HashSet<>(Math.max(8, (int)((seenBy.size() + (hasForced ? this.forcedPairing.size() : 0)) / 0.75F) + 1));
|
||||
if (hasForced) {
|
||||
set.addAll(this.forcedPairing);
|
||||
}
|
||||
|
||||
for (ServerPlayerConnection connection : seenBy) {
|
||||
Player player = connection.getPlayer().getBukkitEntity();
|
||||
if (player != null
|
||||
&& predicate.test(player)
|
||||
&& this.playerPredicate.test(player)
|
||||
&& (!hasRemoved || !this.forcedRemove.contains(player.getUniqueId()))) {
|
||||
set.add(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getTrackedPlayer(Predicate<Player> predicate, Set<UUID> dest) {
|
||||
dest.clear();
|
||||
boolean hasForced = !this.forcedPairing.isEmpty();
|
||||
boolean hasRemoved = !this.forcedRemove.isEmpty();
|
||||
Set<ServerPlayerConnection> seenBy = this.getTrackedEntity().seenBy;
|
||||
if (hasForced) {
|
||||
dest.addAll(this.forcedPairing);
|
||||
}
|
||||
|
||||
for (ServerPlayerConnection connection : seenBy) {
|
||||
Player player = connection.getPlayer().getBukkitEntity();
|
||||
if (player != null
|
||||
&& predicate.test(player)
|
||||
&& this.playerPredicate.test(player)
|
||||
&& (!hasRemoved || !this.forcedRemove.contains(player.getUniqueId()))) {
|
||||
dest.add(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isViewerSetUnchanged() {
|
||||
if ((!this.mayHaveForced || this.forcedPairing.isEmpty() && this.forcedRemove.isEmpty()) && this.playerPredicate == DEFAULT_PREDICATE) {
|
||||
if (!this.viewersDirty && ++this.ticksSinceReconcile < 20) {
|
||||
return true;
|
||||
} else {
|
||||
this.viewersDirty = false;
|
||||
this.ticksSinceReconcile = 0;
|
||||
return this.snapshotUnchanged(this.getTrackedEntity().seenBy);
|
||||
}
|
||||
} else {
|
||||
this.lastSeenBy = null;
|
||||
this.viewersDirty = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markViewersDirty() {
|
||||
this.viewersDirty = true;
|
||||
}
|
||||
|
||||
private boolean snapshotUnchanged(Collection<?> viewers) {
|
||||
int size = viewers.size();
|
||||
Object[] previous = this.lastSeenBy;
|
||||
if (previous != null && previous.length == size) {
|
||||
int index = 0;
|
||||
boolean same = true;
|
||||
|
||||
for (Object viewer : viewers) {
|
||||
if (index >= size || previous[index] != viewer) {
|
||||
same = false;
|
||||
break;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
if (same && index == size) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Object[] snapshot = new Object[size];
|
||||
int index = 0;
|
||||
|
||||
for (Object viewer : viewers) {
|
||||
if (index >= size) {
|
||||
break;
|
||||
}
|
||||
|
||||
snapshot[index++] = viewer;
|
||||
}
|
||||
|
||||
this.lastSeenBy = index == size ? snapshot : Arrays.copyOf(snapshot, index);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPairingData(Player player) {
|
||||
if (player != null) {
|
||||
ServerPlayer nms = ((CraftPlayer)player).getHandle();
|
||||
this.getTrackedEntity().serverEntity.sendPairingData(nms, packet -> NetworkUtils.send(player.getUniqueId(), packet));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcastSpawn() {
|
||||
for (UUID player : this.getTrackedPlayer()) {
|
||||
this.sendPairingData(Bukkit.getPlayer(player));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcastRemove() {
|
||||
this.getTrackedEntity().broadcastRemoved();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addForcedPairing(UUID uuid) {
|
||||
this.mayHaveForced = true;
|
||||
this.forcedPairing.add(uuid);
|
||||
this.removeForcedHidden(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeForcedPairing(UUID uuid) {
|
||||
this.forcedPairing.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearForcedPairing() {
|
||||
this.forcedPairing.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addForcedHidden(UUID uuid) {
|
||||
this.mayHaveForced = true;
|
||||
this.forcedRemove.add(uuid);
|
||||
this.removeForcedPairing(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeForcedHidden(UUID uuid) {
|
||||
this.forcedRemove.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearForcedHidden() {
|
||||
this.forcedRemove.clear();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private TrackedEntity getTrackedEntity() {
|
||||
TrackedEntity trackedEntity = this.trackedEntitySupplier.get();
|
||||
if (trackedEntity != null && this.lastTrackedEntity != trackedEntity) {
|
||||
this.syncInstance(trackedEntity);
|
||||
this.lastTrackedEntity = trackedEntity;
|
||||
}
|
||||
|
||||
return this.lastTrackedEntity;
|
||||
}
|
||||
|
||||
private void syncInstance(TrackedEntity target) {
|
||||
Integer range = ReflectionUtils.get(this.lastTrackedEntity, NMSFields.TRACKED_ENTITY_range);
|
||||
if (range != null) {
|
||||
ReflectionUtils.set(target, NMSFields.TRACKED_ENTITY_range, range);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity getEntity() {
|
||||
return this.entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<Player> getPlayerPredicate() {
|
||||
return this.playerPredicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayerPredicate(Predicate<Player> playerPredicate) {
|
||||
this.playerPredicate = playerPredicate;
|
||||
}
|
||||
}
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity.controller;
|
||||
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.BodyRotationController;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.control.BodyRotationControl;
|
||||
|
||||
public class BodyRotationControlWrapper extends BodyRotationControl implements BodyRotationController {
|
||||
private final Mob mob;
|
||||
private boolean isHeadClampUneven;
|
||||
private boolean isBodyClampUneven;
|
||||
private float maxHeadAngle;
|
||||
private float maxBodyAngle;
|
||||
private float minHeadAngle;
|
||||
private float minBodyAngle;
|
||||
private boolean playerMode;
|
||||
private float stableAngle = 15.0F;
|
||||
private int rotationDelay = 10;
|
||||
private int rotationDuration = 10;
|
||||
private int headStableTime;
|
||||
private float lastStableYHeadRot;
|
||||
|
||||
public BodyRotationControlWrapper(Mob mob) {
|
||||
super(mob);
|
||||
this.mob = mob;
|
||||
this.maxHeadAngle = mob.getMaxHeadYRot();
|
||||
this.maxBodyAngle = mob.getMaxHeadYRot();
|
||||
this.minHeadAngle = -this.maxHeadAngle;
|
||||
this.minBodyAngle = -this.maxBodyAngle;
|
||||
}
|
||||
|
||||
public void clientTick() {
|
||||
if (!this.getMob().isDeadOrDying()) {
|
||||
if (this.isMoving()) {
|
||||
this.mob.yBodyRot = this.mob.getYRot();
|
||||
this.rotateHeadIfNecessary();
|
||||
this.lastStableYHeadRot = this.mob.yHeadRot;
|
||||
this.headStableTime = 0;
|
||||
} else if (this.notCarryingMobPassengers()) {
|
||||
if (Math.abs(this.mob.yHeadRot - this.lastStableYHeadRot) > this.stableAngle) {
|
||||
this.headStableTime = 0;
|
||||
this.lastStableYHeadRot = this.mob.yHeadRot;
|
||||
this.rotateBodyIfNecessary();
|
||||
} else if (!this.playerMode) {
|
||||
this.headStableTime++;
|
||||
if (this.headStableTime > this.rotationDelay) {
|
||||
this.rotateHeadTowardsFront();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYHeadRot() {
|
||||
return this.mob.yHeadRot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYHeadRot(float rot) {
|
||||
this.mob.yHeadRot = rot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getXHeadRot() {
|
||||
return this.mob.getXRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setXHeadRot(float rot) {
|
||||
this.mob.setXRot(rot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYBodyRot() {
|
||||
return this.mob.yBodyRot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYBodyRot(float rot) {
|
||||
this.mob.yBodyRot = rot;
|
||||
}
|
||||
|
||||
private void rotateBodyIfNecessary() {
|
||||
this.mob.yBodyRot = TMath.rotateIfNecessary(
|
||||
this.mob.yBodyRot, this.mob.yHeadRot, this.isBodyClampUneven ? this.minBodyAngle : -this.maxBodyAngle, this.maxBodyAngle
|
||||
);
|
||||
}
|
||||
|
||||
private void rotateHeadIfNecessary() {
|
||||
this.mob.yHeadRot = TMath.rotateIfNecessary(
|
||||
this.mob.yHeadRot, this.mob.yBodyRot, this.isHeadClampUneven ? this.minHeadAngle : -this.maxHeadAngle, this.maxHeadAngle
|
||||
);
|
||||
}
|
||||
|
||||
private void rotateHeadTowardsFront() {
|
||||
float ratio = (float)(this.headStableTime - this.rotationDelay) / this.rotationDuration;
|
||||
float clampedRatio = TMath.clamp(ratio, 0.0F, 1.0F);
|
||||
float maxClamp = this.maxHeadAngle * (1.0F - clampedRatio);
|
||||
float minClamp = (this.isHeadClampUneven ? this.minHeadAngle : -this.maxHeadAngle) * (1.0F - clampedRatio);
|
||||
this.mob.yBodyRot = TMath.rotateIfNecessary(this.mob.yBodyRot, this.mob.yHeadRot, minClamp, maxClamp);
|
||||
}
|
||||
|
||||
private boolean notCarryingMobPassengers() {
|
||||
return !(this.mob.getFirstPassenger() instanceof Mob);
|
||||
}
|
||||
|
||||
private boolean isMoving() {
|
||||
double dX = this.mob.getX() - this.mob.xo;
|
||||
double dZ = this.mob.getZ() - this.mob.zo;
|
||||
return dX * dX + dZ * dZ > 2.5000003E-7F;
|
||||
}
|
||||
|
||||
public Mob getMob() {
|
||||
return this.mob;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isHeadClampUneven() {
|
||||
return this.isHeadClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBodyClampUneven() {
|
||||
return this.isBodyClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMaxHeadAngle() {
|
||||
return this.maxHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMaxBodyAngle() {
|
||||
return this.maxBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMinHeadAngle() {
|
||||
return this.minHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMinBodyAngle() {
|
||||
return this.minBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlayerMode() {
|
||||
return this.playerMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getStableAngle() {
|
||||
return this.stableAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRotationDelay() {
|
||||
return this.rotationDelay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRotationDuration() {
|
||||
return this.rotationDuration;
|
||||
}
|
||||
|
||||
public int getHeadStableTime() {
|
||||
return this.headStableTime;
|
||||
}
|
||||
|
||||
public float getLastStableYHeadRot() {
|
||||
return this.lastStableYHeadRot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeadClampUneven(boolean isHeadClampUneven) {
|
||||
this.isHeadClampUneven = isHeadClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBodyClampUneven(boolean isBodyClampUneven) {
|
||||
this.isBodyClampUneven = isBodyClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxHeadAngle(float maxHeadAngle) {
|
||||
this.maxHeadAngle = maxHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxBodyAngle(float maxBodyAngle) {
|
||||
this.maxBodyAngle = maxBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMinHeadAngle(float minHeadAngle) {
|
||||
this.minHeadAngle = minHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMinBodyAngle(float minBodyAngle) {
|
||||
this.minBodyAngle = minBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayerMode(boolean playerMode) {
|
||||
this.playerMode = playerMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStableAngle(float stableAngle) {
|
||||
this.stableAngle = stableAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRotationDelay(int rotationDelay) {
|
||||
this.rotationDelay = rotationDelay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRotationDuration(int rotationDuration) {
|
||||
this.rotationDuration = rotationDuration;
|
||||
}
|
||||
|
||||
public void setHeadStableTime(int headStableTime) {
|
||||
this.headStableTime = headStableTime;
|
||||
}
|
||||
|
||||
public void setLastStableYHeadRot(float lastStableYHeadRot) {
|
||||
this.lastStableYHeadRot = lastStableYHeadRot;
|
||||
}
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity.controller;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.bone.BoneBehaviorTypes;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.BoneBehaviorType;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.BehaviorManager;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountData;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountManager;
|
||||
import com.ticxo.modelengine.api.model.bone.type.Mount;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.LookController;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.control.LookControl;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.spigotmc.ActivationRange;
|
||||
|
||||
public class LookControlWrapper extends LookControl implements LookController {
|
||||
private final LookControl original;
|
||||
private ModeledEntity modeledEntity;
|
||||
|
||||
public LookControlWrapper(Mob mob, LookControl control) {
|
||||
super(mob);
|
||||
this.original = control;
|
||||
}
|
||||
|
||||
public void setLookAt(Vec3 var0) {
|
||||
this.original.setLookAt(var0);
|
||||
}
|
||||
|
||||
public void setLookAt(Entity var0) {
|
||||
this.original.setLookAt(var0);
|
||||
}
|
||||
|
||||
public void setLookAt(Entity var0, float var1, float var2) {
|
||||
this.original.setLookAt(var0, var1, var2);
|
||||
}
|
||||
|
||||
public void setLookAt(double var0, double var2, double var4) {
|
||||
this.original.setLookAt(var0, var2, var4);
|
||||
}
|
||||
|
||||
public void setLookAt(double var0, double var2, double var4, float var6, float var7) {
|
||||
this.original.setLookAt(var0, var2, var4, var6, var7);
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
if (this.modeledEntity == null) {
|
||||
this.modeledEntity = ModelEngineAPI.getModeledEntity(this.mob.getUUID());
|
||||
}
|
||||
|
||||
BehaviorManager mountManager = this.getMainManager();
|
||||
if (mountManager != null && ((MountManager)mountManager).isControlled()) {
|
||||
this.controlledTick(asMountManager(mountManager));
|
||||
} else {
|
||||
this.defaultTick();
|
||||
}
|
||||
}
|
||||
|
||||
protected <T extends BehaviorManager<? extends Mount> & MountManager> void controlledTick(T manager) {
|
||||
org.bukkit.entity.Entity driver = manager.getDriver();
|
||||
if (driver != null) {
|
||||
MountController controller = ModelEngineAPI.getMountPairManager().getController(driver.getUniqueId());
|
||||
if (controller != null) {
|
||||
controller.updateDirection(this, manager.getActiveModel());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.defaultTick();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defaultTick() {
|
||||
if (this.isActive()) {
|
||||
this.original.tick();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLookingAtTarget() {
|
||||
return this.original.isLookingAtTarget();
|
||||
}
|
||||
|
||||
public double getWantedX() {
|
||||
return this.original.getWantedX();
|
||||
}
|
||||
|
||||
public double getWantedY() {
|
||||
return this.original.getWantedY();
|
||||
}
|
||||
|
||||
public double getWantedZ() {
|
||||
return this.original.getWantedZ();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lookAt(double x, double y, double z) {
|
||||
double dX = x - this.mob.getX();
|
||||
double dY = y - this.mob.getEyeY();
|
||||
double dZ = z - this.mob.getZ();
|
||||
double hMagnitude = Math.sqrt(dX * dX + dZ * dZ);
|
||||
float pitch = (float)Math.toDegrees(Math.atan2(-dY, hMagnitude));
|
||||
float yaw = (float)Math.toDegrees(Math.atan2(-dX, dZ));
|
||||
this.setPitch(pitch);
|
||||
this.setHeadYaw(yaw);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPitch(float pitch) {
|
||||
this.mob.setXRot(pitch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeadYaw(float yaw) {
|
||||
this.mob.setYRot(yaw);
|
||||
this.mob.yHeadRot = yaw;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBodyYaw(float yaw) {
|
||||
this.mob.yBodyRot = yaw;
|
||||
}
|
||||
|
||||
private <T extends BehaviorManager<? extends Mount> & MountManager> T getMainManager() {
|
||||
if (this.modeledEntity == null) {
|
||||
return null;
|
||||
} else {
|
||||
return this.modeledEntity.getGlobalBehaviorData(BoneBehaviorTypes.MOUNT) instanceof MountData mountData
|
||||
? mountData.getMainMountManager()
|
||||
: null;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isActive() {
|
||||
return ActivationRange.checkIfActive(this.mob);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <M extends BehaviorManager<? extends Mount> & MountManager> M asMountManager(Object manager) {
|
||||
return (M)manager;
|
||||
}
|
||||
}
|
||||
+247
@@ -0,0 +1,247 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity.controller;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.animation.rootmotion.RootMotionDelta;
|
||||
import com.ticxo.modelengine.api.model.ActiveModel;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.BoneBehavior;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.GlobalBehaviorData;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.BehaviorManager;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountData;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountManager;
|
||||
import com.ticxo.modelengine.api.model.bone.type.Mount;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController.MountInput;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.MoveController;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_20_R1.NMSFields;
|
||||
import java.util.Queue;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.MoverType;
|
||||
import net.minecraft.world.entity.ai.attributes.Attributes;
|
||||
import net.minecraft.world.entity.ai.control.MoveControl;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.apache.logging.log4j.util.TriConsumer;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.util.CraftVector;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
public class MoveControlWrapper extends MoveControl implements MoveController {
|
||||
protected final MoveControl original;
|
||||
protected final Queue<Runnable> runnables = new ConcurrentLinkedQueue<>();
|
||||
protected boolean isOnGround;
|
||||
|
||||
public MoveControlWrapper(Mob mob, MoveControl control) {
|
||||
super(mob);
|
||||
this.original = control;
|
||||
}
|
||||
|
||||
public boolean hasWanted() {
|
||||
return this.original.hasWanted();
|
||||
}
|
||||
|
||||
public double getSpeedModifier() {
|
||||
return this.original.getSpeedModifier();
|
||||
}
|
||||
|
||||
public void setWantedPosition(double x, double y, double z, double speed) {
|
||||
this.original.setWantedPosition(x, y, z, speed);
|
||||
}
|
||||
|
||||
public void strafe(float forward, float sideways) {
|
||||
this.original.strafe(forward, sideways);
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
this.isOnGround = this.mob.onGround();
|
||||
ModeledEntity modeledEntity = ModelEngineAPI.getModeledEntity(this.mob.getUUID());
|
||||
if (modeledEntity == null) {
|
||||
this.defaultTick();
|
||||
} else {
|
||||
GlobalBehaviorData mountData = modeledEntity.getMountData();
|
||||
BehaviorManager mainMountManager = mountData == null ? null : ((MountData)mountData).getMainMountManager();
|
||||
if (mainMountManager != null && ((MountManager)mainMountManager).isControlled()) {
|
||||
this.mob.setOnGround(true);
|
||||
this.disableWaterJumping();
|
||||
this.driverTick(asMountManager(mainMountManager));
|
||||
} else {
|
||||
this.defaultTick();
|
||||
}
|
||||
|
||||
this.passengerTick(modeledEntity, asMountManager(mainMountManager));
|
||||
|
||||
while (!this.runnables.isEmpty()) {
|
||||
this.runnables.poll().run();
|
||||
}
|
||||
|
||||
Vector mobMotion = this.toVector();
|
||||
RootMotionDelta rootMotion = modeledEntity.getRootMotionHandler().calculateRootMotion(mobMotion);
|
||||
this.fromVector(mobMotion);
|
||||
if (rootMotion != null) {
|
||||
Vector velocity = rootMotion.delta();
|
||||
if (!velocity.isZero() || !rootMotion.onGround()) {
|
||||
this.nullifyFallDistance();
|
||||
if (!TMath.isSimilar((float)velocity.getY(), 0.0F) || !rootMotion.onGround()) {
|
||||
this.mob.setDeltaMovement(this.mob.getDeltaMovement().multiply(1.0, 0.0, 1.0));
|
||||
}
|
||||
|
||||
this.mob.move(MoverType.SELF, new Vec3(velocity.getX(), velocity.getY(), velocity.getZ()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public double getWantedX() {
|
||||
return this.original.getWantedX();
|
||||
}
|
||||
|
||||
public double getWantedY() {
|
||||
return this.original.getWantedY();
|
||||
}
|
||||
|
||||
public double getWantedZ() {
|
||||
return this.original.getWantedZ();
|
||||
}
|
||||
|
||||
protected <T extends BehaviorManager<? extends Mount> & MountManager> void driverTick(T manager) {
|
||||
this.mob.setZza(0.0F);
|
||||
this.mob.setXxa(0.0F);
|
||||
this.updateRider(manager.getDriver(), manager.getActiveModel(), manager.getDriverBone(), MountController::updateDriverMovement);
|
||||
}
|
||||
|
||||
protected <T extends BehaviorManager<? extends Mount> & MountManager> void passengerTick(ModeledEntity modeledEntity, T manager) {
|
||||
for (ActiveModel activeModel : modeledEntity.getModels().values()) {
|
||||
activeModel.getMountManager()
|
||||
.ifPresent(
|
||||
mountManager -> {
|
||||
for (BoneBehavior seat : ((MountManager)mountManager).getSeats().values()) {
|
||||
for (Entity passenger : ((Mount)seat).getPassengers()) {
|
||||
this.updateRider(passenger, activeModel, (Mount)seat, MountController::updatePassengerMovement);
|
||||
}
|
||||
}
|
||||
|
||||
if (mountManager != manager && ((MountManager)mountManager).isControlled()) {
|
||||
this.updateRider(
|
||||
((MountManager)mountManager).getDriver(),
|
||||
activeModel,
|
||||
((MountManager)mountManager).getDriverBone(),
|
||||
MountController::updatePassengerMovement
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateRider(Entity entity, ActiveModel activeModel, Mount mountBone, TriConsumer<MountController, MoveController, ActiveModel> updateMethod) {
|
||||
MountController controller = this.getController(entity.getUniqueId());
|
||||
if (controller != null) {
|
||||
if (controller.getInput() == null) {
|
||||
controller.setInput(new MountInput());
|
||||
}
|
||||
|
||||
updateMethod.accept(controller, this, activeModel);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defaultTick() {
|
||||
this.original.tick();
|
||||
}
|
||||
|
||||
private void disableWaterJumping() {
|
||||
if (this.mob.isInWater()) {
|
||||
ReflectionUtils.set(this.mob, NMSFields.LIVING_ENTITY_noJumpDelay, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(float side, float up, float front, float speedModifier) {
|
||||
float speed = this.getSpeed();
|
||||
this.mob.setSpeed(speed * speedModifier);
|
||||
this.mob.setZza(front);
|
||||
this.mob.setYya(up);
|
||||
this.mob.setXxa(side);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void globalMove(float x, float y, float z, float speedModifier) {
|
||||
float speed = this.getSpeed();
|
||||
this.mob.setSpeed(speed * speedModifier);
|
||||
Vec3 vec = new Vec3(x, y, z).yRot(-this.mob.getYRot() * (float) (Math.PI / 180.0));
|
||||
this.mob.setXxa((float)vec.x);
|
||||
this.mob.setYya((float)vec.y);
|
||||
this.mob.setZza((float)vec.z);
|
||||
}
|
||||
|
||||
private void fromVector(Vector vector) {
|
||||
Vec3 vec = new Vec3(vector.getX(), vector.getY(), vector.getZ()).yRot(-this.mob.getYRot() * (float) (Math.PI / 180.0));
|
||||
this.mob.setXxa((float)vec.x);
|
||||
this.mob.setYya((float)vec.y);
|
||||
this.mob.setZza((float)vec.z);
|
||||
}
|
||||
|
||||
private Vector toVector() {
|
||||
return new Vector(
|
||||
Float.isNaN(this.mob.xxa) ? 0.0F : this.mob.xxa, Float.isNaN(this.mob.yya) ? 0.0F : this.mob.yya, Float.isNaN(this.mob.zza) ? 0.0F : this.mob.zza
|
||||
)
|
||||
.rotateAroundY(this.mob.getYRot() * (float) (Math.PI / 180.0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jump() {
|
||||
this.mob.getJumpControl().jump();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVelocity(double x, double y, double z) {
|
||||
this.mob.setDeltaMovement(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addVelocity(double x, double y, double z) {
|
||||
this.mob.setDeltaMovement(this.mob.getDeltaMovement().add(x, y, z));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullifyFallDistance() {
|
||||
this.mob.resetFallDistance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnGround() {
|
||||
return this.isOnGround;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInWater() {
|
||||
return this.mob.isInWater();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getSpeed() {
|
||||
return (float)this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector getVelocity() {
|
||||
return CraftVector.toBukkit(this.mob.getDeltaMovement());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queuePostTick(Runnable runnable) {
|
||||
this.runnables.add(runnable);
|
||||
}
|
||||
|
||||
private MountController getController(UUID uuid) {
|
||||
return ModelEngineAPI.getMountPairManager().getController(uuid);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <M extends BehaviorManager<? extends Mount> & MountManager> M asMountManager(Object manager) {
|
||||
return (M)manager;
|
||||
}
|
||||
}
|
||||
+269
@@ -0,0 +1,269 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity.hitbox;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.model.bone.ModelBone;
|
||||
import com.ticxo.modelengine.api.model.bone.type.SubHitbox;
|
||||
import com.ticxo.modelengine.api.nms.entity.HitboxEntity;
|
||||
import com.ticxo.modelengine.api.utils.math.OrientedBoundingBox;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.EntityUtils;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.core.NonNullList;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.damagesource.DamageSource;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.EquipmentSlot;
|
||||
import net.minecraft.world.entity.HumanoidArm;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Entity.RemovalReason;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftHumanEntity;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.util.CraftLocation;
|
||||
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class HitboxEntityImpl extends LivingEntity implements HitboxEntity {
|
||||
private final NonNullList<ItemStack> handItems = NonNullList.withSize(2, ItemStack.EMPTY);
|
||||
private final NonNullList<ItemStack> armorItems = NonNullList.withSize(4, ItemStack.EMPTY);
|
||||
private final ModelBone bone;
|
||||
private final SubHitbox subHitbox;
|
||||
private OBB obb;
|
||||
private volatile Vector3f location;
|
||||
private boolean markRemoved;
|
||||
|
||||
public HitboxEntityImpl(Level world, @NotNull ModelBone bone, @NotNull SubHitbox subHitbox) {
|
||||
super(EntityType.SILVERFISH, world);
|
||||
this.bone = bone;
|
||||
this.subHitbox = subHitbox;
|
||||
this.setInvulnerable(true);
|
||||
this.setNoGravity(true);
|
||||
this.noPhysics = true;
|
||||
}
|
||||
|
||||
public HumanoidArm getMainArm() {
|
||||
return HumanoidArm.RIGHT;
|
||||
}
|
||||
|
||||
public Iterable<ItemStack> getArmorSlots() {
|
||||
return this.armorItems;
|
||||
}
|
||||
|
||||
public ItemStack getItemBySlot(EquipmentSlot slot) {
|
||||
return switch (slot.getType()) {
|
||||
case HAND -> (ItemStack)this.handItems.get(slot.getIndex());
|
||||
case ARMOR -> (ItemStack)this.armorItems.get(slot.getIndex());
|
||||
default -> throw new IncompatibleClassChangeError();
|
||||
};
|
||||
}
|
||||
|
||||
public void setItemSlot(EquipmentSlot slot, ItemStack stack) {
|
||||
this.verifyEquippedItem(stack);
|
||||
switch (slot.getType()) {
|
||||
case HAND:
|
||||
this.onEquipItem(slot, this.handItems.set(slot.getIndex(), stack), stack);
|
||||
break;
|
||||
case ARMOR:
|
||||
this.onEquipItem(slot, this.armorItems.set(slot.getIndex(), stack), stack);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInvisible() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void push(Entity entity) {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected AABB makeBoundingBox() {
|
||||
if (this.subHitbox == null) {
|
||||
return super.makeBoundingBox();
|
||||
} else {
|
||||
Vector3f pos = this.subHitbox.getLocation();
|
||||
Vector3f dim = this.subHitbox.getDimension();
|
||||
if (this.subHitbox.isOBB()) {
|
||||
float halfX = dim.x * 0.5F;
|
||||
float halfY = dim.y * 0.5F;
|
||||
float halfZ = dim.z * 0.5F;
|
||||
Quaternionf rot = this.subHitbox.getRotation();
|
||||
float yaw = this.subHitbox.getYaw();
|
||||
this.obb = new OBB(pos.x - halfX, pos.y - halfY, pos.z - halfZ, pos.x + halfX, pos.y + halfY, pos.z + halfZ, rot, yaw);
|
||||
return this.obb;
|
||||
} else {
|
||||
float halfX = dim.x * 0.5F;
|
||||
float halfZ = dim.z * 0.5F;
|
||||
return new AABB(pos.x - halfX, pos.y, pos.z - halfZ, pos.x + halfX, pos.y + dim.y, pos.z + halfZ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
if (this.markRemoved) {
|
||||
this.discard();
|
||||
} else {
|
||||
super.tick();
|
||||
if (this.bone == null || this.subHitbox == null) {
|
||||
this.discard();
|
||||
} else if (!this.bone.getActiveModel().getModeledEntity().getBase().isAlive()) {
|
||||
this.discard();
|
||||
} else {
|
||||
Vector3f loc = this.location;
|
||||
if (loc != null && loc.isFinite()) {
|
||||
Vec3 vec = new Vec3(loc);
|
||||
this.setPos(vec);
|
||||
|
||||
for (org.bukkit.entity.Entity entity : this.subHitbox.getBoundEntities().values()) {
|
||||
EntityUtils.nms(entity).setPos(vec);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean fireImmune() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void pushEntities() {
|
||||
}
|
||||
|
||||
protected void doPush(Entity entity) {
|
||||
}
|
||||
|
||||
public void aiStep() {
|
||||
}
|
||||
|
||||
public boolean isPushable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hurt(DamageSource source, float amount) {
|
||||
for (org.bukkit.entity.Entity entity : this.subHitbox.getBoundEntities().values()) {
|
||||
EntityUtils.nms(entity).hurt(source, amount);
|
||||
}
|
||||
|
||||
if (this.subHitbox.getDamageMultiplier() <= 1.0E-5F) {
|
||||
return false;
|
||||
} else {
|
||||
CraftHumanEntity cause = source.getEntity() instanceof Player player ? player.getBukkitEntity() : null;
|
||||
return this.bone.getActiveModel().getModeledEntity().getBase().hurt(this, cause, source, amount * this.subHitbox.getDamageMultiplier());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public InteractionResult interact(Player player, InteractionHand hand) {
|
||||
if (player.getBukkitEntity() instanceof org.bukkit.entity.Player craftPlayer) {
|
||||
for (org.bukkit.entity.Entity entity : this.subHitbox.getBoundEntities().values()) {
|
||||
PlayerInteractAtEntityEvent event = new PlayerInteractAtEntityEvent(
|
||||
craftPlayer,
|
||||
entity,
|
||||
new Vector(0, 0, 0),
|
||||
hand == InteractionHand.OFF_HAND ? org.bukkit.inventory.EquipmentSlot.OFF_HAND : org.bukkit.inventory.EquipmentSlot.HAND
|
||||
);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if (!event.isCancelled()) {
|
||||
EntityUtils.nms(entity).interact(player, hand);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.subHitbox.getDamageMultiplier() <= 1.0E-5F) {
|
||||
return InteractionResult.PASS;
|
||||
} else {
|
||||
com.ticxo.modelengine.api.nms.entity.EntityHandler.InteractionResult result = this.bone
|
||||
.getActiveModel()
|
||||
.getModeledEntity()
|
||||
.getBase()
|
||||
.interact(
|
||||
this,
|
||||
player.getBukkitEntity(),
|
||||
hand == InteractionHand.MAIN_HAND ? org.bukkit.inventory.EquipmentSlot.HAND : org.bukkit.inventory.EquipmentSlot.OFF_HAND
|
||||
);
|
||||
|
||||
return switch (result) {
|
||||
case SUCCESS, SUCCESS_NO_ITEM_USED -> InteractionResult.SUCCESS;
|
||||
case CONSUME -> InteractionResult.CONSUME;
|
||||
case CONSUME_PARTIAL -> InteractionResult.CONSUME_PARTIAL;
|
||||
case PASS -> InteractionResult.PASS;
|
||||
case FAIL -> InteractionResult.FAIL;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public boolean shouldBeSaved() {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected int decreaseAirSupply(int air) {
|
||||
return air;
|
||||
}
|
||||
|
||||
protected int increaseAirSupply(int air) {
|
||||
return air;
|
||||
}
|
||||
|
||||
public void remove(RemovalReason reason) {
|
||||
super.remove(reason);
|
||||
ModelEngineAPI.setRenderCanceled(this.getId(), false);
|
||||
ModelEngineAPI.getInteractionTracker().removeHitbox(this.getEntityId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEntityId() {
|
||||
return this.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return this.getUUID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queueLocation(Vector3f location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getLocation() {
|
||||
return CraftLocation.toBukkit(this.position(), this.level().getWorld(), this.getBukkitYaw(), this.getXRot());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public OrientedBoundingBox getOrientedBoundingBox() {
|
||||
return this.obb == null ? null : this.obb.getBukkitOBB();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return !this.isRemoved() && !this.markRemoved;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markRemoved() {
|
||||
this.markRemoved = true;
|
||||
ModelEngineAPI.setRenderCanceled(this.getId(), false);
|
||||
ModelEngineAPI.getInteractionTracker().removeHitbox(this.getEntityId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModelBone getBone() {
|
||||
return this.bone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SubHitbox getSubHitbox() {
|
||||
return this.subHitbox;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity.hitbox;
|
||||
|
||||
import com.ticxo.modelengine.api.utils.math.OrientedBoundingBox;
|
||||
import java.util.Optional;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class OBB extends AABB {
|
||||
private final Quaternionf rotation;
|
||||
private final float yaw;
|
||||
private final OrientedBoundingBox bukkitOBB;
|
||||
|
||||
public OBB(Vec3 cornerA, Vec3 cornerB, Quaternionf rotation, float yaw) {
|
||||
this(cornerA.x, cornerA.y, cornerA.z, cornerB.x, cornerB.y, cornerB.z, rotation, yaw);
|
||||
}
|
||||
|
||||
public OBB(double minX, double minY, double minZ, double maxX, double maxY, double maxZ, Quaternionf rotation, float yaw) {
|
||||
super(minX, minY, minZ, maxX, maxY, maxZ);
|
||||
this.rotation = rotation;
|
||||
this.yaw = yaw;
|
||||
this.bukkitOBB = new OrientedBoundingBox(
|
||||
this.getCenter().toVector3f(), new Vector3f((float)this.getXsize(), (float)this.getYsize(), (float)this.getZsize()), rotation, yaw
|
||||
);
|
||||
}
|
||||
|
||||
public OBB makeOBBInstance(Vec3 position, Quaternionf rotation, float yaw) {
|
||||
return new OBB(position.add(this.minX, this.minY, this.minZ), position.add(this.maxX, this.maxY, this.maxZ), rotation, yaw);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public AABB inflate(double xInflate, double yInflate, double zInflate) {
|
||||
double minX = this.minX - xInflate;
|
||||
double minY = this.minY - yInflate;
|
||||
double minZ = this.minZ - zInflate;
|
||||
double maxX = this.maxX + xInflate;
|
||||
double maxY = this.maxY + yInflate;
|
||||
double maxZ = this.maxZ + zInflate;
|
||||
return new OBB(minX, minY, minZ, maxX, maxY, maxZ, this.rotation, this.yaw);
|
||||
}
|
||||
|
||||
public boolean intersects(@NotNull AABB aabb) {
|
||||
return aabb instanceof OBB obb ? this.intersects(obb) : super.intersects(aabb);
|
||||
}
|
||||
|
||||
public boolean intersects(OBB obb) {
|
||||
return this.bukkitOBB.intersects(obb.bukkitOBB);
|
||||
}
|
||||
|
||||
public boolean intersects(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
|
||||
OrientedBoundingBox obbB = new OrientedBoundingBox((float)minX, (float)minY, (float)minZ, (float)maxX, (float)maxY, (float)maxZ);
|
||||
return this.bukkitOBB.intersects(obbB);
|
||||
}
|
||||
|
||||
public Optional<Vec3> clip(Vec3 from, Vec3 to) {
|
||||
Quaternionf inverse = this.rotation.conjugate(new Quaternionf());
|
||||
Vec3 center = this.getCenter();
|
||||
float yaw = this.yaw * (float) (Math.PI / 180.0);
|
||||
Vec3 rFrom = new Vec3(from.subtract(center).toVector3f().rotateY(-yaw).rotate(inverse)).add(center);
|
||||
Vec3 rTo = new Vec3(to.subtract(center).toVector3f().rotateY(-yaw).rotate(inverse)).add(center);
|
||||
Optional<Vec3> result = super.clip(rFrom, rTo);
|
||||
if (result.isEmpty()) {
|
||||
return result;
|
||||
} else {
|
||||
Vec3 clip = new Vec3(result.get().subtract(center).toVector3f().rotate(this.rotation).rotateY(yaw)).add(center);
|
||||
return Optional.of(clip);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "OBB(rotation=" + this.getRotation() + ", yaw=" + this.getYaw() + ", bukkitOBB=" + this.getBukkitOBB() + ")";
|
||||
}
|
||||
|
||||
public Quaternionf getRotation() {
|
||||
return this.rotation;
|
||||
}
|
||||
|
||||
public float getYaw() {
|
||||
return this.yaw;
|
||||
}
|
||||
|
||||
public OrientedBoundingBox getBukkitOBB() {
|
||||
return this.bukkitOBB;
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.AmphibiousPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class AmphibiousNavigationWrapper extends AmphibiousPathNavigation {
|
||||
private final AmphibiousPathNavigation oldNav;
|
||||
|
||||
public AmphibiousNavigationWrapper(Mob mob, AmphibiousPathNavigation oldNav) {
|
||||
super(mob, mob.level());
|
||||
this.oldNav = oldNav;
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isStableDestination(BlockPos var0) {
|
||||
return this.oldNav.isStableDestination(var0);
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class FlyingNavigationWrapper extends FlyingPathNavigation {
|
||||
public FlyingNavigationWrapper(Mob mob, FlyingPathNavigation oldNav) {
|
||||
super(mob, mob.level());
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
this.setCanOpenDoors(oldNav.canOpenDoors());
|
||||
this.setCanPassDoors(oldNav.canPassDoors());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class GroundNavigationWrapper extends GroundPathNavigation {
|
||||
public GroundNavigationWrapper(Mob mob, GroundPathNavigation oldNav) {
|
||||
super(mob, mob.level());
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
this.setCanOpenDoors(oldNav.canOpenDoors());
|
||||
this.setCanPassDoors(oldNav.canPassDoors());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.WallClimberNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class WallClimberNavigationWrapper extends WallClimberNavigation {
|
||||
public WallClimberNavigationWrapper(Mob mob, WallClimberNavigation oldNav) {
|
||||
super(mob, mob.level());
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
this.setCanOpenDoors(oldNav.canOpenDoors());
|
||||
this.setCanPassDoors(oldNav.canPassDoors());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.WaterBoundPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class WaterBoundNavigationWrapper extends WaterBoundPathNavigation {
|
||||
public WaterBoundNavigationWrapper(Mob mob) {
|
||||
super(mob, mob.level());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+452
@@ -0,0 +1,452 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.network;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.interaction.DynamicHitbox;
|
||||
import com.ticxo.modelengine.api.model.ActiveModel;
|
||||
import com.ticxo.modelengine.api.model.ModelUpdaters;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.PivotOverride;
|
||||
import com.ticxo.modelengine.api.model.PivotOverrideRegistry;
|
||||
import com.ticxo.modelengine.api.model.bone.BoneBehaviorTypes;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.BoneBehavior;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer.Mount;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController.MountInput;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.nms.network.CancelType;
|
||||
import com.ticxo.modelengine.api.nms.network.ClientDesyncMonitor;
|
||||
import com.ticxo.modelengine.api.nms.network.PipelineWrapper;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.api.utils.data.tracker.CollectionDataTracker;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_20_R1.NMSFields;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.patch.ServerboundInteractPacketWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.PacketInterceptor;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
import it.unimi.dsi.fastutil.Pair;
|
||||
import it.unimi.dsi.fastutil.ints.IntLinkedOpenHashSet;
|
||||
import it.unimi.dsi.fastutil.ints.IntListIterator;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.PacketListener;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddPlayerPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundAnimatePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundEntityEventPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundKeepAlivePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveMobEffectPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundPlayerInputPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundPongPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Pos;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.PosRot;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Rot;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.network.syncher.SynchedEntityData.DataValue;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ModelEngineChannelHandler extends ChannelDuplexHandler {
|
||||
private static final MethodHandle MOVE_ENTITY_ID = entityIdGetter(NMSFields.CLIENTBOUND_MOVE_ENTITY_PACKET_entityId);
|
||||
private static final MethodHandle ROTATE_HEAD_ID = entityIdGetter(NMSFields.CLIENTBOUND_ROTATE_HEAD_PACKET_entityId);
|
||||
private final Player player;
|
||||
private final ServerPlayer serverPlayer;
|
||||
private final ModelUpdaters updaters;
|
||||
private final EntityHandler entityHandler;
|
||||
private final ClientDesyncMonitor desyncMonitor;
|
||||
private final PacketInterceptor writeInterceptors;
|
||||
private final PacketInterceptor readInterceptors;
|
||||
|
||||
private static MethodHandle entityIdGetter(NMSFields field) {
|
||||
try {
|
||||
return MethodHandles.lookup().unreflectGetter(ReflectionUtils.getField(field));
|
||||
} catch (IllegalAccessException var2) {
|
||||
throw new ExceptionInInitializerError(var2);
|
||||
}
|
||||
}
|
||||
|
||||
public ModelEngineChannelHandler(Player player, PipelineWrapper pipeline) {
|
||||
this.player = player;
|
||||
this.serverPlayer = ((CraftPlayer)player).getHandle();
|
||||
this.updaters = ModelEngineAPI.getAPI().getModelUpdaters();
|
||||
this.entityHandler = ModelEngineAPI.getEntityHandler();
|
||||
this.desyncMonitor = pipeline.getDesyncMonitor();
|
||||
this.writeInterceptors = new PacketInterceptor();
|
||||
this.writeInterceptors
|
||||
.register(ClientboundAddEntityPacket.class, this::handleAddEntity)
|
||||
.register(ClientboundAddPlayerPacket.class, this::handleAddPlayer)
|
||||
.register(ClientboundRemoveEntitiesPacket.class, this::handleRemoveEntities)
|
||||
.register(Pos.class, packet -> this.handleEntityId(packet, MOVE_ENTITY_ID))
|
||||
.register(Rot.class, packet -> this.handleEntityId(packet, MOVE_ENTITY_ID))
|
||||
.register(PosRot.class, packet -> this.handleEntityId(packet, MOVE_ENTITY_ID))
|
||||
.register(ClientboundRotateHeadPacket.class, packet -> this.handleEntityId(packet, ROTATE_HEAD_ID))
|
||||
.register(ClientboundEntityEventPacket.class, this::handleEntityId)
|
||||
.register(ClientboundSetEntityMotionPacket.class, this::handleEntityMotion)
|
||||
.register(ClientboundTeleportEntityPacket.class, this::handleTeleportEntity)
|
||||
.register(ClientboundAnimatePacket.class, this::handleAnimate)
|
||||
.register(ClientboundSetEntityDataPacket.class, this::handleEntityData)
|
||||
.register(ClientboundSetEquipmentPacket.class, this::handleSetEquipment)
|
||||
.register(ClientboundRemoveMobEffectPacket.class, this::handleRemoveMobEffect)
|
||||
.register(ClientboundUpdateMobEffectPacket.class, this::handleUpdateMobEffect)
|
||||
.register(ClientboundKeepAlivePacket.class, this::handleKeepAlive)
|
||||
.register(ClientboundSetPassengersPacket.class, this::handlePassengers)
|
||||
.registerPost(ClientboundAddEntityPacket.class, this::handleAddEntityPost)
|
||||
.registerPost(ClientboundAddPlayerPacket.class, this::handleAddPlayerPost);
|
||||
this.readInterceptors = new PacketInterceptor();
|
||||
this.readInterceptors
|
||||
.register(ServerboundInteractPacket.class, this::handleInteract)
|
||||
.register(ServerboundPlayerInputPacket.class, this::handlePlayerInput)
|
||||
.register(ServerboundPongPacket.class, this::handlePong)
|
||||
.register(net.minecraft.network.protocol.game.ServerboundMovePlayerPacket.Pos.class, this::handlePlayerMove)
|
||||
.register(net.minecraft.network.protocol.game.ServerboundMovePlayerPacket.PosRot.class, this::handlePlayerMove);
|
||||
}
|
||||
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
if (!(msg instanceof Packet packet)) {
|
||||
super.write(ctx, msg, promise);
|
||||
} else {
|
||||
try {
|
||||
if (packet instanceof ClientboundBundlePacket bundle) {
|
||||
ArrayList<Packet<ClientGamePacketListener>> list = new ArrayList<>();
|
||||
|
||||
for (Packet<ClientGamePacketListener> subPacket : bundle.subPackets()) {
|
||||
Packet result = this.writeInterceptors.accept(subPacket);
|
||||
if (result != null) {
|
||||
list.add(result);
|
||||
|
||||
for (Packet p : this.writeInterceptors.acceptPost(result)) {
|
||||
list.add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!list.isEmpty()) {
|
||||
Packet var13 = new ClientboundBundlePacket(list);
|
||||
super.write(ctx, var13, promise);
|
||||
}
|
||||
} else {
|
||||
Packet var14 = this.writeInterceptors.accept(packet);
|
||||
if (var14 == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Collection<Packet> post = this.writeInterceptors.acceptPost(var14);
|
||||
if (post.isEmpty()) {
|
||||
super.write(ctx, var14, promise);
|
||||
} else {
|
||||
ArrayList<Packet<ClientGamePacketListener>> list = new ArrayList<>(post.size() + 1);
|
||||
list.add(var14);
|
||||
|
||||
for (Packet p : post) {
|
||||
list.add(p);
|
||||
}
|
||||
|
||||
Packet var15 = new ClientboundBundlePacket(list);
|
||||
super.write(ctx, var15, promise);
|
||||
}
|
||||
}
|
||||
} catch (Throwable var12) {
|
||||
var12.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void channelRead(@NotNull ChannelHandlerContext ctx, @NotNull Object msg) throws Exception {
|
||||
if (!(msg instanceof Packet)) {
|
||||
super.channelRead(ctx, msg);
|
||||
} else {
|
||||
Packet packet = this.readInterceptors.accept((Packet)msg);
|
||||
if (packet != null) {
|
||||
super.channelRead(ctx, packet);
|
||||
this.readInterceptors.acceptPost(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket handleAddEntity(ClientboundAddEntityPacket packet) {
|
||||
int id = packet.getId();
|
||||
this.markViewersDirty(id);
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private void markViewersDirty(int id) {
|
||||
ModeledEntity model = this.updaters.getModeledEntity(id);
|
||||
if (model != null) {
|
||||
model.getBase().getData().markViewersDirty();
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddPlayerPacket handleAddPlayer(ClientboundAddPlayerPacket packet) {
|
||||
int id = packet.getEntityId();
|
||||
this.markViewersDirty(id);
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private List<Packet> handleAddEntityPost(ClientboundAddEntityPacket packet) {
|
||||
return this.handleMount(packet.getUUID());
|
||||
}
|
||||
|
||||
private List<Packet> handleAddPlayerPost(ClientboundAddPlayerPacket packet) {
|
||||
return this.handleMount(packet.getPlayerId());
|
||||
}
|
||||
|
||||
private List<Packet> handleMount(UUID uuid) {
|
||||
Pair<ActiveModel, MountController> pair = ModelEngineAPI.getMountPairManager().get(uuid);
|
||||
if (pair == null) {
|
||||
return null;
|
||||
} else {
|
||||
ActiveModel model = (ActiveModel)pair.left();
|
||||
ArrayList<Packet> list = new ArrayList<>();
|
||||
model.getBehaviorRenderer(BoneBehaviorTypes.MOUNT).ifPresent(behaviorRenderer -> {
|
||||
if (behaviorRenderer instanceof MountRenderer renderer) {
|
||||
MountController controller = (MountController)pair.right();
|
||||
if (controller.getMount() instanceof BoneBehavior behavior) {
|
||||
Mount mount = renderer.getRendered().get(behavior.getBone().getBoneId());
|
||||
CollectionDataTracker<Integer> ids = mount.getPassengers();
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(mount.getMountId());
|
||||
buf.writeVarInt(ids.size());
|
||||
ids.forEach(buf::writeVarInt);
|
||||
list.add(new ClientboundSetPassengersPacket(buf));
|
||||
}
|
||||
}
|
||||
});
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundRemoveEntitiesPacket handleRemoveEntities(ClientboundRemoveEntitiesPacket packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int size = buf.readVarInt();
|
||||
HashSet<Integer> set = new HashSet<>(size);
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
int id = buf.readVarInt();
|
||||
this.markViewersDirty(id);
|
||||
if (ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE) {
|
||||
set.add(id);
|
||||
}
|
||||
}
|
||||
|
||||
if (set.size() == size) {
|
||||
return packet;
|
||||
} else {
|
||||
buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(set.size());
|
||||
|
||||
for (int id : set) {
|
||||
buf.writeVarInt(id);
|
||||
}
|
||||
|
||||
return new ClientboundRemoveEntitiesPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private <U extends PacketListener, T extends Packet<U>> Packet<U> handleEntityId(T packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int id = buf.readVarInt();
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private <U extends PacketListener, T extends Packet<U>> Packet<U> handleEntityId(T packet, MethodHandle idGetter) {
|
||||
int id;
|
||||
try {
|
||||
id = (int)idGetter.invoke((Packet)packet);
|
||||
} catch (Throwable var5) {
|
||||
throw new IllegalStateException(var5);
|
||||
}
|
||||
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundSetEntityMotionPacket handleEntityMotion(ClientboundSetEntityMotionPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundTeleportEntityPacket handleTeleportEntity(ClientboundTeleportEntityPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundAnimatePacket handleAnimate(ClientboundAnimatePacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket handleEntityData(ClientboundSetEntityDataPacket packet) {
|
||||
CancelType type = ModelEngineAPI.shouldShow(this.player, packet.id());
|
||||
if (type == CancelType.HIDE) {
|
||||
return null;
|
||||
} else if (packet.id() != this.player.getEntityId() && type == CancelType.SHOW) {
|
||||
return packet;
|
||||
} else {
|
||||
ModeledEntity modeledEntity = this.updaters.getModeledEntity(packet.id());
|
||||
if (modeledEntity == null) {
|
||||
return packet;
|
||||
} else {
|
||||
if (this.entityHandler.isForcedInvisible(modeledEntity.getBase().getUUID())) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(packet.id());
|
||||
|
||||
for (DataValue<?> item : packet.packedItems()) {
|
||||
if (item.id() == 0) {
|
||||
byte data = (Byte)item.value();
|
||||
data = TMath.setBit(data, 5, true);
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, data);
|
||||
} else {
|
||||
EntityUtils.writeDataUnsafe(buf, item.id(), item.serializer(), item.value());
|
||||
}
|
||||
}
|
||||
|
||||
buf.writeByte(255);
|
||||
packet = new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEquipmentPacket handleSetEquipment(ClientboundSetEquipmentPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getEntity()) == CancelType.SHOW ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundRemoveMobEffectPacket handleRemoveMobEffect(ClientboundRemoveMobEffectPacket packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int id = buf.readVarInt();
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundUpdateMobEffectPacket handleUpdateMobEffect(ClientboundUpdateMobEffectPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getEntityId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundKeepAlivePacket handleKeepAlive(ClientboundKeepAlivePacket packet) {
|
||||
if (this.desyncMonitor.clientTickShifted() || this.desyncMonitor.shouldRetest()) {
|
||||
this.desyncMonitor.startTest();
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket handlePassengers(ClientboundSetPassengersPacket packet) {
|
||||
PivotOverrideRegistry registry = ModelEngineAPI.getPivotOverrideRegistry();
|
||||
Optional<PivotOverride> maybeOverride = registry.get(packet.getVehicle());
|
||||
if (maybeOverride.isEmpty()) {
|
||||
return packet;
|
||||
} else {
|
||||
PivotOverride override = maybeOverride.get();
|
||||
Collection<Integer> extra = override.getPassengers().get();
|
||||
if (extra.isEmpty()) {
|
||||
return packet;
|
||||
} else {
|
||||
int[] existing = packet.getPassengers();
|
||||
IntLinkedOpenHashSet merged = new IntLinkedOpenHashSet(existing.length + extra.size());
|
||||
|
||||
for (int id : existing) {
|
||||
merged.add(id);
|
||||
}
|
||||
|
||||
boolean changed = false;
|
||||
|
||||
for (int id : extra) {
|
||||
if (merged.add(id)) {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!changed) {
|
||||
return packet;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(packet.getVehicle());
|
||||
buf.writeVarInt(merged.size());
|
||||
IntListIterator var16 = merged.iterator();
|
||||
|
||||
while (var16.hasNext()) {
|
||||
int idx = (Integer)var16.next();
|
||||
buf.writeVarInt(idx);
|
||||
}
|
||||
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ServerboundInteractPacket handleInteract(ServerboundInteractPacket packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int entityId = buf.readVarInt();
|
||||
if (entityId == DynamicHitbox.getHitboxId()) {
|
||||
DynamicHitbox hitbox = ModelEngineAPI.getInteractionTracker().getDynamicHitbox(this.player.getUniqueId());
|
||||
if (hitbox != null) {
|
||||
return new ServerboundInteractPacketWrapper(entityId, hitbox.getTarget(), packet);
|
||||
}
|
||||
}
|
||||
|
||||
ActiveModel activeModel = ModelEngineAPI.getInteractionTracker().getModelRelay(entityId);
|
||||
if (activeModel != null) {
|
||||
ModeledEntity modeledEntity = activeModel.getModeledEntity();
|
||||
if (modeledEntity == null) {
|
||||
return packet;
|
||||
} else {
|
||||
int id = modeledEntity.getBase().getEntityId();
|
||||
return new ServerboundInteractPacketWrapper(entityId, id, packet);
|
||||
}
|
||||
} else {
|
||||
Integer relayed = ModelEngineAPI.getInteractionTracker().getEntityRelay(entityId);
|
||||
return relayed != null ? new ServerboundInteractPacketWrapper(entityId, relayed, packet) : packet;
|
||||
}
|
||||
}
|
||||
|
||||
private ServerboundPlayerInputPacket handlePlayerInput(ServerboundPlayerInputPacket inputPacket) {
|
||||
MountController controller = ModelEngineAPI.getMountPairManager().getController(this.player.getUniqueId());
|
||||
if (controller != null) {
|
||||
MountInput input = controller.getInput();
|
||||
if (input == null) {
|
||||
controller.setInput(new MountInput(inputPacket.getXxa(), inputPacket.getZza(), inputPacket.isJumping(), inputPacket.isShiftKeyDown()));
|
||||
} else {
|
||||
input.setSide(inputPacket.getXxa());
|
||||
input.setFront(inputPacket.getZza());
|
||||
input.setJump(inputPacket.isJumping());
|
||||
input.setSneak(inputPacket.isShiftKeyDown());
|
||||
}
|
||||
}
|
||||
|
||||
return inputPacket;
|
||||
}
|
||||
|
||||
private ServerboundPongPacket handlePong(ServerboundPongPacket packet) {
|
||||
this.desyncMonitor.recordPongTime(System.currentTimeMillis());
|
||||
return null;
|
||||
}
|
||||
|
||||
private ServerboundMovePlayerPacket handlePlayerMove(ServerboundMovePlayerPacket packet) {
|
||||
this.desyncMonitor.recordClientSyncTime(System.currentTimeMillis());
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.network;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ticxo.modelengine.api.nms.network.ClientDesyncMonitor;
|
||||
import com.ticxo.modelengine.api.nms.network.NetworkHandler;
|
||||
import com.ticxo.modelengine.api.nms.network.PipelineWrapper;
|
||||
import com.ticxo.modelengine.api.nms.network.ProtectedPacketUnpacker;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.NMSFields;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.Bundler;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.Map.Entry;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientboundPingPacket;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class NetworkHandlerImpl implements NetworkHandler {
|
||||
public static NetworkHandlerImpl instance;
|
||||
private final Map<UUID, PipelineWrapper> pipelines = Maps.newConcurrentMap();
|
||||
private final Map<UUID, Bundler> bundles = Maps.newConcurrentMap();
|
||||
private volatile boolean isBatching;
|
||||
|
||||
public NetworkHandlerImpl() {
|
||||
if (instance != null) {
|
||||
throw new IllegalStateException("Network handler already initialized");
|
||||
} else {
|
||||
instance = this;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getProtocolVersion() {
|
||||
return SharedConstants.getProtocolVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<PipelineWrapper> getPipeline(UUID uuid) {
|
||||
return Optional.ofNullable(this.pipelines.get(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePipeline(UUID uuid) {
|
||||
this.pipelines.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectChannel(Player player) {
|
||||
ServerGamePacketListenerImpl listener = ((CraftPlayer)player).getHandle().connection;
|
||||
Connection connection = ReflectionUtils.get(listener, NMSFields.SERVER_GAME_PACKET_LISTENER_IMPL_connection);
|
||||
ChannelPipeline pipeline = connection.channel.pipeline();
|
||||
PipelineWrapper wrapper = new PipelineWrapper(player, connection.channel);
|
||||
ModelEngineChannelHandler handler = new ModelEngineChannelHandler(player, wrapper);
|
||||
this.pipelines.put(player.getUniqueId(), wrapper);
|
||||
this.bundles.put(player.getUniqueId(), new Bundler());
|
||||
|
||||
for (String name : pipeline.toMap().keySet()) {
|
||||
if (pipeline.get(name) instanceof Connection) {
|
||||
pipeline.addBefore(name, "protected_packet_unpacker", new ProtectedPacketUnpacker());
|
||||
pipeline.addBefore(name, "model_engine_packet_handler", handler);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ejectChannel(Player player) {
|
||||
ServerGamePacketListenerImpl listener = ((CraftPlayer)player).getHandle().connection;
|
||||
Connection connection = ReflectionUtils.get(listener, NMSFields.SERVER_GAME_PACKET_LISTENER_IMPL_connection);
|
||||
Channel channel = connection.channel;
|
||||
channel.eventLoop().submit(() -> {
|
||||
channel.pipeline().remove("protected_packet_unpacker");
|
||||
channel.pipeline().remove("model_engine_packet_handler");
|
||||
return null;
|
||||
});
|
||||
this.removePipeline(player.getUniqueId());
|
||||
this.bundles.remove(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ping(UUID uuid) {
|
||||
this.getPipeline(uuid).ifPresent(wrapper -> {
|
||||
ClientDesyncMonitor monitor = wrapper.getDesyncMonitor();
|
||||
if (monitor.shouldPing()) {
|
||||
monitor.markPingSent();
|
||||
int id = monitor.getPingCounter().getAndIncrement();
|
||||
NetworkUtils.sendNow(uuid, new ClientboundPingPacket(id));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startBatch() {
|
||||
this.isBatching = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endBatch() {
|
||||
this.isBatching = false;
|
||||
|
||||
for (Entry<UUID, Bundler> entry : this.bundles.entrySet()) {
|
||||
PipelineWrapper pipeline = this.pipelines.get(entry.getKey());
|
||||
if (pipeline != null) {
|
||||
Bundler bundler = entry.getValue();
|
||||
synchronized (bundler) {
|
||||
bundler.bundle(pipeline::writeAndFlush);
|
||||
bundler.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void appendPacket(UUID uuid, Packet packet) {
|
||||
Bundler bundler = this.bundles.get(uuid);
|
||||
if (bundler != null) {
|
||||
bundler.appendPacket(packet);
|
||||
}
|
||||
}
|
||||
|
||||
public void appendPackets(UUID uuid, Collection<? extends Packet> collection) {
|
||||
Bundler bundler = this.bundles.get(uuid);
|
||||
if (bundler != null) {
|
||||
bundler.appendPacket(collection);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBatching() {
|
||||
return this.isBatching;
|
||||
}
|
||||
}
|
||||
+408
@@ -0,0 +1,408 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.network.patch;
|
||||
|
||||
import com.destroystokyo.paper.event.player.PlayerUseUnknownEntityEvent;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import com.mojang.logging.LogUtils;
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.ServerInfo;
|
||||
import com.ticxo.modelengine.api.entity.BaseEntity;
|
||||
import com.ticxo.modelengine.api.events.BaseEntityInteractEvent;
|
||||
import com.ticxo.modelengine.api.events.BaseEntityInteractEvent.Action;
|
||||
import com.ticxo.modelengine.api.model.ActiveModel;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.NMSMethods;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Nullable;
|
||||
import net.minecraft.advancements.CriteriaTriggers;
|
||||
import net.minecraft.network.protocol.PacketUtils;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
|
||||
import net.minecraft.network.protocol.game.ServerGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundUseItemPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket.Handler;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.ExperienceOrb;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.animal.Bucketable;
|
||||
import net.minecraft.world.entity.animal.allay.Allay;
|
||||
import net.minecraft.world.entity.item.ItemEntity;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.entity.projectile.AbstractArrow;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.level.ClipContext;
|
||||
import net.minecraft.world.level.GameType;
|
||||
import net.minecraft.world.level.ClipContext.Block;
|
||||
import net.minecraft.world.level.ClipContext.Fluid;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.HitResult;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraft.world.phys.HitResult.Type;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.CraftServer;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.event.CraftEventFactory;
|
||||
import org.bukkit.event.Event.Result;
|
||||
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.util.Consumer;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class PatchedServerGamePacketListener {
|
||||
private static final Logger LOGGER = LogUtils.getLogger();
|
||||
|
||||
public static void handleInteract(ServerboundInteractPacketWrapper interactPacket, ServerGamePacketListener listener) {
|
||||
if (listener instanceof ServerGamePacketListenerImpl connection) {
|
||||
PacketUtils.ensureRunningOnSameThread(interactPacket, connection, connection.getPlayer().serverLevel());
|
||||
final ServerPlayer player = connection.getPlayer();
|
||||
if (!player.isImmobile()) {
|
||||
final CraftPlayer craftPlayer = player.getBukkitEntity();
|
||||
final ServerLevel serverLevel = player.serverLevel();
|
||||
final CraftServer craftServer = serverLevel.getServer().server;
|
||||
final Entity entity = interactPacket.getTarget(serverLevel);
|
||||
interactPacket.dispatch(new Handler() {
|
||||
public void onInteraction(InteractionHand hand) {
|
||||
if (interactPacket.isFakeInteraction()) {
|
||||
EntityHandler entityHandler = ModelEngineAPI.getNMSHandler().getEntityHandler();
|
||||
entityHandler.forceUseItem(craftPlayer, EquipmentSlot.HAND);
|
||||
entityHandler.forceUseItem(craftPlayer, EquipmentSlot.OFF_HAND);
|
||||
}
|
||||
}
|
||||
|
||||
public void onInteraction(InteractionHand hand, Vec3 pos) {
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
}
|
||||
});
|
||||
if (entity != player || player.isSpectator()) {
|
||||
player.resetLastActionTime();
|
||||
player.setShiftKeyDown(interactPacket.isUsingSecondaryAction());
|
||||
final ActiveModel activeModel = ModelEngineAPI.getInteractionTracker().getModelRelay(interactPacket.getOriginalId());
|
||||
if (activeModel != null) {
|
||||
final BaseEntity<?> base = activeModel.getModeledEntity().getBase();
|
||||
interactPacket.dispatch(
|
||||
new Handler() {
|
||||
public void onInteraction(InteractionHand hand) {
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT,
|
||||
EquipmentSlot.HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.HAND),
|
||||
null
|
||||
)
|
||||
);
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT,
|
||||
EquipmentSlot.OFF_HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.OFF_HAND),
|
||||
null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onInteraction(InteractionHand hand, Vec3 pos) {
|
||||
Vector position = new Vector(pos.x, pos.y, pos.z);
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT_ON,
|
||||
EquipmentSlot.HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.HAND),
|
||||
position
|
||||
)
|
||||
);
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT_ON,
|
||||
EquipmentSlot.OFF_HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.OFF_HAND),
|
||||
position
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.ATTACK,
|
||||
EquipmentSlot.HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.HAND),
|
||||
null
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (entity == null) {
|
||||
if (ServerInfo.IS_PAPER) {
|
||||
interactPacket.dispatch(
|
||||
new Handler() {
|
||||
public void onInteraction(InteractionHand hand) {
|
||||
PatchedServerGamePacketListener.callPlayerUseUnknownEntityEvent(craftServer, craftPlayer, interactPacket, hand, null);
|
||||
}
|
||||
|
||||
public void onInteraction(InteractionHand hand, Vec3 pos) {
|
||||
PatchedServerGamePacketListener.callPlayerUseUnknownEntityEvent(craftServer, craftPlayer, interactPacket, hand, pos);
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
PatchedServerGamePacketListener.callPlayerUseUnknownEntityEvent(
|
||||
craftServer, craftPlayer, interactPacket, InteractionHand.MAIN_HAND, null
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
} else if (serverLevel.getWorldBorder().isWithinBounds(entity.blockPosition())) {
|
||||
AABB boundingBox = entity.getBoundingBox();
|
||||
if (!(boundingBox.distanceToSqr(player.getEyePosition()) >= ServerGamePacketListenerImpl.MAX_INTERACTION_DISTANCE)) {
|
||||
interactPacket.dispatch(
|
||||
new Handler() {
|
||||
private void performInteraction(
|
||||
InteractionHand enumhand, PatchedServerGamePacketListener.EntityInteraction entityInteraction, PlayerInteractEntityEvent event
|
||||
) {
|
||||
ItemStack itemstack = player.getItemInHand(enumhand);
|
||||
if (itemstack.isItemEnabled(serverLevel.enabledFeatures())) {
|
||||
ItemStack itemstack1 = itemstack.copy();
|
||||
ItemStack itemInHand = player.getItemInHand(enumhand);
|
||||
boolean triggerLeashUpdate = itemInHand.getItem() == Items.LEAD && entity instanceof Mob;
|
||||
Item origItem = player.getInventory().getSelected().getItem();
|
||||
craftServer.getPluginManager().callEvent(event);
|
||||
if (entity instanceof Bucketable
|
||||
&& entity instanceof LivingEntity
|
||||
&& origItem.asItem() == Items.WATER_BUCKET
|
||||
&& (event.isCancelled() || player.getInventory().getSelected().getItem() != origItem)) {
|
||||
entity.getEntityData().resendPossiblyDesyncedEntity(player);
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
|
||||
if (triggerLeashUpdate && (event.isCancelled() || player.getInventory().getSelected().getItem() != origItem)) {
|
||||
connection.send(new ClientboundSetEntityLinkPacket(entity, ((Mob)entity).getLeashHolder()));
|
||||
}
|
||||
|
||||
if (event.isCancelled() || player.getInventory().getSelected().getItem() != origItem) {
|
||||
entity.getEntityData().refresh(player);
|
||||
if (entity instanceof Allay allay) {
|
||||
connection.send(
|
||||
new ClientboundSetEquipmentPacket(
|
||||
entity.getId(),
|
||||
Arrays.stream(net.minecraft.world.entity.EquipmentSlot.values())
|
||||
.map(slot -> Pair.of(slot, allay.stripMeta(allay.getItemBySlot(slot), true)))
|
||||
.collect(Collectors.toList())
|
||||
)
|
||||
);
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
}
|
||||
|
||||
if (event.isCancelled()) {
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
return;
|
||||
}
|
||||
|
||||
InteractionResult enuminteractionresult = entityInteraction.run(player, entity, enumhand);
|
||||
if (!itemInHand.isEmpty() && itemInHand.getCount() <= -1) {
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
|
||||
if (enuminteractionresult.consumesAction()) {
|
||||
CriteriaTriggers.PLAYER_INTERACTED_WITH_ENTITY.trigger(player, itemstack1, entity);
|
||||
if (enuminteractionresult.shouldSwing()) {
|
||||
player.swing(enumhand, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onInteraction(@NotNull InteractionHand hand) {
|
||||
this.performInteraction(
|
||||
hand,
|
||||
Player::interactOn,
|
||||
new PlayerInteractEntityEvent(
|
||||
connection.getCraftPlayer(),
|
||||
entity.getBukkitEntity(),
|
||||
hand == InteractionHand.OFF_HAND ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onInteraction(@NotNull InteractionHand hand, @NotNull Vec3 pos) {
|
||||
this.performInteraction(
|
||||
hand,
|
||||
(entityplayer, entity1, enumhand1) -> entity1.interactAt(entityplayer, pos, enumhand1),
|
||||
new PlayerInteractAtEntityEvent(
|
||||
connection.getCraftPlayer(),
|
||||
entity.getBukkitEntity(),
|
||||
new Vector(pos.x, pos.y, pos.z),
|
||||
hand == InteractionHand.OFF_HAND ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
if (!(entity instanceof ItemEntity)
|
||||
&& !(entity instanceof ExperienceOrb)
|
||||
&& !(entity instanceof AbstractArrow)
|
||||
&& (entity != player || player.isSpectator())) {
|
||||
ItemStack itemstack = player.getItemInHand(InteractionHand.MAIN_HAND);
|
||||
if (itemstack.isItemEnabled(serverLevel.enabledFeatures())) {
|
||||
player.attack(entity);
|
||||
if (!itemstack.isEmpty() && itemstack.getCount() <= -1) {
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void handleUseItem(ServerboundUseItemPacket packet, ServerGamePacketListener listener, Consumer<InteractionResult> afterUse) {
|
||||
if (listener instanceof ServerGamePacketListenerImpl connection) {
|
||||
ServerPlayer player = connection.getPlayer();
|
||||
PacketUtils.ensureRunningOnSameThread(packet, connection, player.serverLevel());
|
||||
if (!player.isImmobile()) {
|
||||
boolean shouldLimit = Boolean.TRUE
|
||||
.equals(ReflectionUtils.call(connection, NMSMethods.SERVER_GAME_PACKET_LISTENER_IMPL_checkLimit, packet.timestamp));
|
||||
if (shouldLimit) {
|
||||
connection.ackBlockChangesUpTo(packet.getSequence());
|
||||
ServerLevel worldserver = player.serverLevel();
|
||||
InteractionHand enumhand = packet.getHand();
|
||||
ItemStack itemstack = player.getItemInHand(enumhand);
|
||||
player.resetLastActionTime();
|
||||
if (!itemstack.isEmpty() && itemstack.isItemEnabled(worldserver.enabledFeatures())) {
|
||||
float f1 = player.getXRot();
|
||||
float f2 = player.getYRot();
|
||||
double d0 = player.getX();
|
||||
double d1 = player.getY() + player.getEyeHeight();
|
||||
double d2 = player.getZ();
|
||||
Vec3 vec3d = new Vec3(d0, d1, d2);
|
||||
float f3 = Mth.cos(-f2 * (float) (Math.PI / 180.0) - (float) Math.PI);
|
||||
float f4 = Mth.sin(-f2 * (float) (Math.PI / 180.0) - (float) Math.PI);
|
||||
float f5 = -Mth.cos(-f1 * (float) (Math.PI / 180.0));
|
||||
float f6 = Mth.sin(-f1 * (float) (Math.PI / 180.0));
|
||||
float f7 = f4 * f5;
|
||||
float f8 = f3 * f5;
|
||||
double d3 = player.gameMode.getGameModeForPlayer() == GameType.CREATIVE ? 5.0 : 4.5;
|
||||
Vec3 vec3d1 = vec3d.add(f7 * d3, f6 * d3, f8 * d3);
|
||||
HitResult movingobjectposition = player.level().clip(new ClipContext(vec3d, vec3d1, Block.OUTLINE, Fluid.NONE, player));
|
||||
boolean cancelled;
|
||||
if (movingobjectposition != null && movingobjectposition.getType() == Type.BLOCK) {
|
||||
BlockHitResult movingobjectpositionblock = (BlockHitResult)movingobjectposition;
|
||||
if (player.gameMode.firedInteract
|
||||
&& player.gameMode.interactPosition.equals(movingobjectpositionblock.getBlockPos())
|
||||
&& player.gameMode.interactHand == enumhand
|
||||
&& ItemStack.isSameItemSameTags(player.gameMode.interactItemStack, itemstack)) {
|
||||
cancelled = player.gameMode.interactResult;
|
||||
} else {
|
||||
PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(
|
||||
player,
|
||||
org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK,
|
||||
movingobjectpositionblock.getBlockPos(),
|
||||
movingobjectpositionblock.getDirection(),
|
||||
itemstack,
|
||||
true,
|
||||
enumhand,
|
||||
movingobjectpositionblock.getLocation()
|
||||
);
|
||||
cancelled = event.useItemInHand() == Result.DENY;
|
||||
}
|
||||
|
||||
player.gameMode.firedInteract = false;
|
||||
} else {
|
||||
PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(
|
||||
player, org.bukkit.event.block.Action.RIGHT_CLICK_AIR, itemstack, enumhand
|
||||
);
|
||||
cancelled = event.useItemInHand() == Result.DENY;
|
||||
}
|
||||
|
||||
if (cancelled) {
|
||||
player.resyncUsingItem(player);
|
||||
player.getBukkitEntity().updateInventory();
|
||||
return;
|
||||
}
|
||||
|
||||
itemstack = player.getItemInHand(enumhand);
|
||||
if (itemstack.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
InteractionResult enuminteractionresult = player.gameMode.useItem(player, worldserver, itemstack, enumhand);
|
||||
afterUse.accept(enuminteractionresult);
|
||||
if (enuminteractionresult.shouldSwing()) {
|
||||
player.swing(enumhand, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void callPlayerUseUnknownEntityEvent(
|
||||
CraftServer cserver, CraftPlayer craftPlayer, ServerboundInteractPacket packet, InteractionHand hand, @Nullable Vec3 vector
|
||||
) {
|
||||
cserver.getPluginManager()
|
||||
.callEvent(
|
||||
new PlayerUseUnknownEntityEvent(
|
||||
craftPlayer,
|
||||
packet.getEntityId(),
|
||||
packet.isAttack(),
|
||||
hand == InteractionHand.MAIN_HAND ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND,
|
||||
vector != null ? new Vector(vector.x, vector.y, vector.z) : null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface EntityInteraction {
|
||||
InteractionResult run(ServerPlayer var1, Entity var2, InteractionHand var3);
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.network.patch;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.interaction.InteractionTracker;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.game.ServerGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket.Handler;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ServerboundInteractPacketWrapper extends ServerboundInteractPacket {
|
||||
private final int originalId;
|
||||
private final int relayedId;
|
||||
private final ServerboundInteractPacket original;
|
||||
|
||||
public ServerboundInteractPacketWrapper(int originalId, int relayedId, ServerboundInteractPacket original) {
|
||||
super(NetworkUtils.readData(original));
|
||||
this.originalId = originalId;
|
||||
this.relayedId = relayedId;
|
||||
this.original = original;
|
||||
}
|
||||
|
||||
public void write(FriendlyByteBuf var0) {
|
||||
this.original.write(var0);
|
||||
}
|
||||
|
||||
public void handle(ServerGamePacketListener var0) {
|
||||
PatchedServerGamePacketListener.handleInteract(this, var0);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Entity getTarget(ServerLevel var0) {
|
||||
return var0.getEntityOrPart(this.relayedId);
|
||||
}
|
||||
|
||||
public boolean isFakeInteraction() {
|
||||
InteractionTracker tracker = ModelEngineAPI.getInteractionTracker();
|
||||
return tracker.getModelRelay(this.originalId) != null || tracker.getEntityRelay(this.originalId) != null;
|
||||
}
|
||||
|
||||
public boolean isUsingSecondaryAction() {
|
||||
return this.original.isUsingSecondaryAction();
|
||||
}
|
||||
|
||||
public void dispatch(Handler var0) {
|
||||
this.original.dispatch(var0);
|
||||
}
|
||||
|
||||
public int getOriginalId() {
|
||||
return this.originalId;
|
||||
}
|
||||
|
||||
public int getRelayedId() {
|
||||
return this.relayedId;
|
||||
}
|
||||
|
||||
public ServerboundInteractPacket getOriginal() {
|
||||
return this.original;
|
||||
}
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.network.utils;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class BulkEntityDataEncoder {
|
||||
private static final byte PACKET_TYPE_BULK_DATA = 0;
|
||||
public static final int FIELD_TRANSLATION = 0;
|
||||
public static final int FIELD_LEFT_ROTATION = 1;
|
||||
public static final int FIELD_SCALE = 2;
|
||||
public static final int FIELD_RIGHT_ROTATION = 3;
|
||||
public static final int FIELD_TRANSFORM_DURATION = 4;
|
||||
public static final int FIELD_GLOW_DATA = 5;
|
||||
public static final int FIELD_BRIGHTNESS = 6;
|
||||
public static final int FIELD_RENDER_DATA = 7;
|
||||
private final FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer(256));
|
||||
private int entryCount;
|
||||
private final int entryCountIndex;
|
||||
|
||||
public BulkEntityDataEncoder() {
|
||||
this.buf.writeByte(0);
|
||||
this.entryCountIndex = this.buf.writerIndex();
|
||||
this.buf.writeVarInt(0);
|
||||
this.entryCount = 0;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter startEntry(int entityId) {
|
||||
this.entryCount++;
|
||||
this.buf.writeVarInt(entityId);
|
||||
return new BulkEntityDataEncoder.EntryWriter(this.buf);
|
||||
}
|
||||
|
||||
public byte[] build() {
|
||||
int endIndex = this.buf.writerIndex();
|
||||
this.buf.writerIndex(this.entryCountIndex);
|
||||
this.buf.writeVarInt(this.entryCount);
|
||||
this.buf.writerIndex(endIndex);
|
||||
byte[] data = new byte[this.buf.readableBytes()];
|
||||
this.buf.getBytes(this.buf.readerIndex(), data);
|
||||
this.buf.release();
|
||||
return data;
|
||||
}
|
||||
|
||||
public int getEntryCount() {
|
||||
return this.entryCount;
|
||||
}
|
||||
|
||||
public static short floatToHalf(float f) {
|
||||
int bits = Float.floatToRawIntBits(f);
|
||||
int sign = bits >>> 16 & 32768;
|
||||
int exp = (bits >>> 23 & 0xFF) - 127;
|
||||
int mantissa = bits & 8388607;
|
||||
if (exp >= 16) {
|
||||
return (short)(sign | 31744);
|
||||
} else if (exp >= -14) {
|
||||
return (short)(sign | exp + 15 << 10 | mantissa >>> 13);
|
||||
} else if (exp >= -24) {
|
||||
int shift = -14 - exp;
|
||||
return (short)(sign | (8388608 | mantissa) >>> 13 + shift);
|
||||
} else {
|
||||
return (short)sign;
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeHalf(FriendlyByteBuf buf, float f) {
|
||||
buf.writeShort(floatToHalf(f));
|
||||
}
|
||||
|
||||
public static class EntryWriter {
|
||||
private final FriendlyByteBuf buf;
|
||||
private final int bitmaskIndex;
|
||||
private byte bitmask;
|
||||
|
||||
EntryWriter(FriendlyByteBuf buf) {
|
||||
this.buf = buf;
|
||||
this.bitmaskIndex = buf.writerIndex();
|
||||
buf.writeByte(0);
|
||||
this.bitmask = 0;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter translation(Vector3f pos) {
|
||||
this.bitmask = (byte)(this.bitmask | 1);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, pos.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, pos.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, pos.z);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter leftRotation(Quaternionf rot) {
|
||||
this.bitmask = (byte)(this.bitmask | 2);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.z);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.w);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter scale(Vector3f scale) {
|
||||
this.bitmask = (byte)(this.bitmask | 4);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, scale.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, scale.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, scale.z);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter rightRotation(Quaternionf rot) {
|
||||
this.bitmask = (byte)(this.bitmask | 8);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.z);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.w);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter transformDuration(int duration) {
|
||||
this.bitmask = (byte)(this.bitmask | 16);
|
||||
this.buf.writeVarInt(duration);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter glowData(byte sharedData, int glowColor) {
|
||||
this.bitmask = (byte)(this.bitmask | 32);
|
||||
this.buf.writeByte(sharedData);
|
||||
this.buf.writeInt(glowColor);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter brightness(int brightness) {
|
||||
this.bitmask = (byte)(this.bitmask | 64);
|
||||
this.buf.writeInt(brightness);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter renderData(byte billboard, float viewRange, byte displayType) {
|
||||
this.bitmask = (byte)(this.bitmask | 128);
|
||||
this.buf.writeByte(billboard);
|
||||
this.buf.writeFloat(viewRange);
|
||||
this.buf.writeByte(displayType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void finish() {
|
||||
this.buf.setByte(this.bitmaskIndex, this.bitmask);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.network.utils;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.nms.network.ProtectedPacket;
|
||||
import com.ticxo.modelengine.api.utils.config.ConfigProperty;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
|
||||
|
||||
public class Bundler {
|
||||
private static int BUNDLE_SIZE;
|
||||
private final List<List<Packet<ClientGamePacketListener>>> bundles = new ArrayList<>();
|
||||
|
||||
public synchronized void appendPacket(Packet<ClientGamePacketListener> packet) {
|
||||
if (this.bundles.isEmpty()) {
|
||||
this.bundles.add(new ArrayList<>());
|
||||
}
|
||||
|
||||
List<Packet<ClientGamePacketListener>> bundleList = this.bundles.get(this.bundles.size() - 1);
|
||||
if (bundleList.size() == BUNDLE_SIZE) {
|
||||
bundleList = new ArrayList<>();
|
||||
this.bundles.add(bundleList);
|
||||
}
|
||||
|
||||
bundleList.add(packet);
|
||||
}
|
||||
|
||||
public synchronized void appendPacket(Collection<? extends Packet> packets) {
|
||||
if (!packets.isEmpty()) {
|
||||
List<Packet<ClientGamePacketListener>> bundleList = this.bundles.isEmpty() ? null : this.bundles.get(this.bundles.size() - 1);
|
||||
if (bundleList == null || bundleList.size() + packets.size() > BUNDLE_SIZE) {
|
||||
bundleList = new ArrayList<>();
|
||||
this.bundles.add(bundleList);
|
||||
}
|
||||
|
||||
for (Packet p : packets) {
|
||||
if (bundleList.size() == BUNDLE_SIZE) {
|
||||
bundleList = new ArrayList<>();
|
||||
this.bundles.add(bundleList);
|
||||
}
|
||||
|
||||
bundleList.add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.bundles.clear();
|
||||
}
|
||||
|
||||
public void bundle(Consumer<Object> send) {
|
||||
for (List<Packet<ClientGamePacketListener>> list : this.bundles) {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(new ClientboundBundlePacket(list));
|
||||
send.accept(wrapped);
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
ModelEngineAPI.getAPI().getConfigManager().registerReferenceUpdate(() -> BUNDLE_SIZE = Math.min(ConfigProperty.BUNDLE_SIZE.getInt(), 4096));
|
||||
}
|
||||
}
|
||||
+271
@@ -0,0 +1,271 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.network.utils;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.lod.AnimationLODHandler.LODTracker;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.nms.network.ProtectedPacket;
|
||||
import com.ticxo.modelengine.api.utils.data.PositionCodec;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.NetworkHandlerImpl;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Pos;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class NetworkUtils {
|
||||
public static FriendlyByteBuf createByteBuf() {
|
||||
return new FriendlyByteBuf(Unpooled.buffer());
|
||||
}
|
||||
|
||||
public static FriendlyByteBuf createByteBuf(Consumer<FriendlyByteBuf> consumer) {
|
||||
FriendlyByteBuf buf = createByteBuf();
|
||||
consumer.accept(buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
public static FriendlyByteBuf readData(Packet<?> packet) {
|
||||
FriendlyByteBuf buf = createByteBuf();
|
||||
packet.write(buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
public static Packets.PacketSupplier createPivotSpawn(int id, UUID uuid, Vector3f pos) {
|
||||
return playerUUID -> ModelEngineAPI.getPlayerProtocolVersion(playerUUID) >= 764
|
||||
? new ClientboundAddEntityPacket(id, uuid, pos.x, pos.y - 0.5, pos.z, 0.0F, 0.0F, EntityType.AREA_EFFECT_CLOUD, 0, Vec3.ZERO, 0.0)
|
||||
: new ClientboundAddEntityPacket(id, uuid, pos.x, pos.y - 0.375, pos.z, 0.0F, 0.0F, EntityType.AREA_EFFECT_CLOUD, 0, Vec3.ZERO, 0.0);
|
||||
}
|
||||
|
||||
public static Packets.PacketSupplier createPivotTeleport(int id, Vector3f pos) {
|
||||
FriendlyByteBuf buf = createByteBuf();
|
||||
buf.writeVarInt(id);
|
||||
buf.writeDouble(pos.x);
|
||||
buf.writeDouble(pos.y - 0.375);
|
||||
buf.writeDouble(pos.z);
|
||||
buf.writeByte(0);
|
||||
buf.writeByte(0);
|
||||
buf.writeBoolean(false);
|
||||
ClientboundTeleportEntityPacket tpHigher = new ClientboundTeleportEntityPacket(buf);
|
||||
buf = createByteBuf();
|
||||
buf.writeVarInt(id);
|
||||
buf.writeDouble(pos.x);
|
||||
buf.writeDouble(pos.y - 0.5);
|
||||
buf.writeDouble(pos.z);
|
||||
buf.writeByte(0);
|
||||
buf.writeByte(0);
|
||||
buf.writeBoolean(false);
|
||||
ClientboundTeleportEntityPacket tpLower = new ClientboundTeleportEntityPacket(buf);
|
||||
return playerUUID -> ModelEngineAPI.getPlayerProtocolVersion(playerUUID) >= 764 ? tpLower : tpHigher;
|
||||
}
|
||||
|
||||
public static Object createPivotMove(int id, @Nullable PositionCodec codec, Vector3f pos) {
|
||||
if (codec == null) {
|
||||
return createPivotTeleport(id, pos);
|
||||
} else {
|
||||
return switch (codec.update(pos.x, pos.y, pos.z)) {
|
||||
case UNCHANGED -> null;
|
||||
case MOVE -> new Pos(id, codec.getDeltaX(), codec.getDeltaY(), codec.getDeltaZ(), false);
|
||||
case ABSOLUTE -> createPivotTeleport(id, pos);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static Packets.PacketSupplier lodWrapper(ModeledEntity modeledEntity, BiFunction<UUID, LODTracker, Packet<ClientGamePacketListener>> packetFunction) {
|
||||
return uuid -> {
|
||||
LODTracker tracker = modeledEntity.getAnimationLodHandler().tick(uuid);
|
||||
return tracker.isCanSkip() ? null : packetFunction.apply(uuid, tracker);
|
||||
};
|
||||
}
|
||||
|
||||
public static void send(UUID target, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
handler.appendPacket(target, packet);
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void send(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
handler.appendPacket(player, packet);
|
||||
}
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void send(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet, Predicate<Player> predicate) {
|
||||
if (packet != null) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
handler.appendPacket(player, packet);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
|
||||
for (UUID playerx : targets) {
|
||||
Player p = Bukkit.getPlayer(playerx);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(playerx).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRaw(UUID target, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRaw(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRaw(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet, Predicate<Player> predicate) {
|
||||
if (packet != null) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendNow(UUID target, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlushNow(wrapped));
|
||||
}
|
||||
}
|
||||
|
||||
private static Packet<ClientGamePacketListener> bundle(Collection<Packet<ClientGamePacketListener>> packets) {
|
||||
return (Packet<ClientGamePacketListener>)(packets.size() == 1 ? packets.iterator().next() : new ClientboundBundlePacket(packets));
|
||||
}
|
||||
|
||||
public static void sendBundled(UUID target, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
collection.compile(target, packets -> {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
handler.appendPackets(target, packets);
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(bundle(packets));
|
||||
handler.getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundled(Set<UUID> targets, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
collection.compile(player, packets -> handler.appendPackets(player, packets));
|
||||
}
|
||||
} else {
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> collection.compile(player, packets -> {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(bundle(packets));
|
||||
pipeline.writeAndFlush(wrapped);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundled(Set<UUID> targets, Packets collection, Predicate<Player> predicate) {
|
||||
if (!collection.isEmpty()) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
collection.compile(player, packets -> handler.appendPackets(player, packets));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (UUID playerx : targets) {
|
||||
Player p = Bukkit.getPlayer(playerx);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(playerx).ifPresent(pipeline -> collection.compile(playerx, packets -> {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(bundle(packets));
|
||||
pipeline.writeAndFlush(wrapped);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundledRaw(UUID target, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
collection.compile(target, packets -> {
|
||||
Packet<ClientGamePacketListener> packet = bundle(packets);
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundledRaw(Set<UUID> targets, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> collection.compile(player, packets -> {
|
||||
Packet<ClientGamePacketListener> packet = bundle(packets);
|
||||
pipeline.writeAndFlush(packet);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundledRaw(Set<UUID> targets, Packets collection, Predicate<Player> predicate) {
|
||||
if (!collection.isEmpty()) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> collection.compile(player, packets -> {
|
||||
Packet<ClientGamePacketListener> packet = bundle(packets);
|
||||
pipeline.writeAndFlush(packet);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.network.utils;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ticxo.modelengine.api.utils.logger.TLogger;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
|
||||
public class PacketInterceptor {
|
||||
private final Map<Class<? extends Packet>, PacketInterceptor.Modifier<? extends Packet>> registry = Maps.newConcurrentMap();
|
||||
private final Map<Class<? extends Packet>, PacketInterceptor.Listener<? extends Packet>> postRegistry = Maps.newConcurrentMap();
|
||||
|
||||
public <T extends Packet> PacketInterceptor register(Class<T> clazz, Function<T, Packet> function) {
|
||||
this.registry.put(clazz, new Modifier<>(clazz, function));
|
||||
return this;
|
||||
}
|
||||
|
||||
public <T extends Packet> PacketInterceptor registerPost(Class<T> clazz, Function<T, Collection<Packet>> consumer) {
|
||||
this.postRegistry.put(clazz, new Listener<>(clazz, consumer));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packet accept(Packet original) {
|
||||
if (original == null) {
|
||||
return null;
|
||||
} else {
|
||||
PacketInterceptor.Modifier<?> modifier = this.registry.get(original.getClass());
|
||||
return modifier == null ? original : modifier.modify(original);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Packet> acceptPost(Packet original) {
|
||||
if (original == null) {
|
||||
return List.of();
|
||||
} else {
|
||||
PacketInterceptor.Listener<?> listener = this.postRegistry.get(original.getClass());
|
||||
return (Collection<Packet>)(listener != null ? listener.listen(original) : List.of());
|
||||
}
|
||||
}
|
||||
|
||||
static class Listener<T extends Packet> {
|
||||
private final Class<T> clazz;
|
||||
private final Function<T, Collection<Packet>> function;
|
||||
|
||||
public Collection<Packet> listen(Packet original) {
|
||||
try {
|
||||
Collection<Packet> collection = this.function.apply(this.clazz.cast(original));
|
||||
return (Collection<Packet>)(collection == null ? List.of() : collection);
|
||||
} catch (Throwable var3) {
|
||||
TLogger.error("An error had occurred while intercepting the packet " + this.clazz.getSimpleName());
|
||||
var3.printStackTrace();
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
public Listener(Class<T> clazz, Function<T, Collection<Packet>> function) {
|
||||
this.clazz = clazz;
|
||||
this.function = function;
|
||||
}
|
||||
}
|
||||
|
||||
static class Modifier<T extends Packet> {
|
||||
private final Class<T> clazz;
|
||||
private final Function<T, Packet> function;
|
||||
|
||||
public Packet modify(Packet original) {
|
||||
try {
|
||||
return this.function.apply(this.clazz.cast(original));
|
||||
} catch (Throwable var3) {
|
||||
TLogger.error("An error had occurred while modifying the packet " + this.clazz.getSimpleName());
|
||||
var3.printStackTrace();
|
||||
return original;
|
||||
}
|
||||
}
|
||||
|
||||
public Modifier(Class<T> clazz, Function<T, Packet> function) {
|
||||
this.clazz = clazz;
|
||||
this.function = function;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.network.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class Packets extends ArrayList<Object> {
|
||||
public Packets(int initialCapacity, float loadFactor) {
|
||||
super(initialCapacity);
|
||||
}
|
||||
|
||||
public Packets(int initialCapacity) {
|
||||
super(initialCapacity);
|
||||
}
|
||||
|
||||
public Packets() {
|
||||
}
|
||||
|
||||
public Packets(@NotNull Collection<?> c) {
|
||||
super((Collection<? extends Object>)c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Object obj) {
|
||||
return obj == null ? false : super.add(obj);
|
||||
}
|
||||
|
||||
public boolean add(Packet<ClientGamePacketListener> packet) {
|
||||
return packet == null ? false : super.add(packet);
|
||||
}
|
||||
|
||||
private Collection<Packet<ClientGamePacketListener>> compile(UUID player) {
|
||||
Collection<Packet<ClientGamePacketListener>> list = new ArrayList<>();
|
||||
|
||||
for (Object obj : this) {
|
||||
if (obj instanceof Packet) {
|
||||
list.add((Packet<ClientGamePacketListener>)obj);
|
||||
} else if (obj instanceof Packets.PacketSupplier supplier) {
|
||||
Packet<ClientGamePacketListener> packet = supplier.supply(player);
|
||||
if (packet != null) {
|
||||
list.add(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public void compile(UUID player, Consumer<Collection<Packet<ClientGamePacketListener>>> ifNotEmpty) {
|
||||
Collection<Packet<ClientGamePacketListener>> set = this.compile(player);
|
||||
if (!set.isEmpty()) {
|
||||
ifNotEmpty.accept(set);
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PacketSupplier {
|
||||
Packet<ClientGamePacketListener> supply(UUID var1);
|
||||
}
|
||||
}
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.HeldItemRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.HeldItemRenderer.Item;
|
||||
import com.ticxo.modelengine.api.utils.data.PositionCodec;
|
||||
import com.ticxo.modelengine.api.utils.data.tracker.CollectionDataTracker;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.Packets;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Pos;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.inventory.CraftItemStack;
|
||||
|
||||
public class HeldItemParser implements BehaviorRendererParser<HeldItemRenderer> {
|
||||
private float viewRange = 4096.0F;
|
||||
|
||||
public void sendToClients(HeldItemRenderer renderer) {
|
||||
this.viewRange = renderer.getActiveModel().getViewRange();
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
public void destroy(HeldItemRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, HeldItemRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
Location location = renderer.getActiveModel().getModeledEntity().getBase().getLocation();
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotSpawn(location, renderer));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(renderer));
|
||||
|
||||
for (Item item : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemSpawn(location, item));
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, true));
|
||||
}
|
||||
|
||||
for (Item item : renderer.getSpawnQueue().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemSpawn(location, item));
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, true));
|
||||
}
|
||||
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(renderer));
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, HeldItemRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
set.add(this.teleport(renderer));
|
||||
|
||||
for (Item item : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, false));
|
||||
}
|
||||
|
||||
Location location = renderer.getActiveModel().getModeledEntity().getBase().getLocation();
|
||||
|
||||
for (Item item : renderer.getSpawnQueue().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemSpawn(location, item));
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, true));
|
||||
}
|
||||
|
||||
Map<String, Item> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size());
|
||||
destroy.forEach((s, item) -> buf.writeVarInt(item.getId()));
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
if (renderer.getPassengers().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(renderer));
|
||||
renderer.getPassengers().clearDirty();
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, HeldItemRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Map<String, Item> items = renderer.getRendered();
|
||||
buf.writeVarInt(1 + items.size());
|
||||
buf.writeVarInt(renderer.getId());
|
||||
items.forEach((s, item) -> buf.writeVarInt(item.getId()));
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket pivotSpawn(Location location, HeldItemRenderer renderer) {
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getId(), renderer.getUuid(), location.getX(), location.getY(), location.getZ(), 0.0F, 0.0F, EntityType.ARMOR_STAND, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(HeldItemRenderer renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 15, EntityDataSerializers.BYTE, (byte)16);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket itemSpawn(Location location, Item renderer) {
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getId(), renderer.getUuid(), location.getX(), location.getY(), location.getZ(), 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket itemData(Item renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
if (spawn) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
EntityUtils.writeData(buf, 9, EntityDataSerializers.INT, 1);
|
||||
EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, this.viewRange);
|
||||
} else if (renderer.isTransformDirty()) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
}
|
||||
|
||||
renderer.getGlowing().ifDirty(flag -> EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)(flag ? 96 : 32)), spawn);
|
||||
renderer.getGlowColor().ifDirty(color -> EntityUtils.writeData(buf, 21, EntityDataSerializers.INT, color), spawn);
|
||||
renderer.getPosition().ifDirty(vector3f -> EntityUtils.writeData(buf, 10, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
renderer.getScale().ifDirty(vector3f -> EntityUtils.writeData(buf, 11, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
renderer.getRotation().ifDirty(quaternionf -> EntityUtils.writeData(buf, 12, EntityDataSerializers.QUATERNION, quaternionf), spawn);
|
||||
renderer.getModel().ifDirty(itemStack -> EntityUtils.writeData(buf, 22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(itemStack)), spawn);
|
||||
renderer.getDisplay()
|
||||
.ifDirty(display -> EntityUtils.writeData(buf, 23, EntityDataSerializers.BYTE, display == null ? 0 : (byte)display.ordinal()), spawn);
|
||||
buf.writeByte(255);
|
||||
renderer.clearDirty();
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private Object teleport(HeldItemRenderer renderer) {
|
||||
Location location = renderer.getActiveModel().getModeledEntity().getBase().getLocation();
|
||||
PositionCodec codec = renderer.getPositionCodec();
|
||||
if (codec != null) {
|
||||
switch (codec.update(location.getX(), location.getY(), location.getZ())) {
|
||||
case UNCHANGED:
|
||||
return null;
|
||||
case MOVE:
|
||||
return new Pos(renderer.getId(), codec.getDeltaX(), codec.getDeltaY(), codec.getDeltaZ(), false);
|
||||
case ABSOLUTE:
|
||||
}
|
||||
}
|
||||
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
buf.writeDouble(location.getX());
|
||||
buf.writeDouble(location.getY());
|
||||
buf.writeDouble(location.getZ());
|
||||
buf.writeByte(0);
|
||||
buf.writeByte(0);
|
||||
buf.writeBoolean(false);
|
||||
return new ClientboundTeleportEntityPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket mount(HeldItemRenderer renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
CollectionDataTracker<Integer> set = renderer.getPassengers();
|
||||
buf.writeVarInt(set.size());
|
||||
set.forEach(buf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.LeashRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.LeashRenderer.Leash;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.Packets;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class LeashParser implements BehaviorRendererParser<LeashRenderer> {
|
||||
public void sendToClients(LeashRenderer renderer) {
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
HashSet<UUID> stop = new HashSet<>(data.getStopTracking());
|
||||
stop.removeAll(data.getTracking().keySet());
|
||||
this.remove(stop, renderer);
|
||||
}
|
||||
|
||||
public void destroy(LeashRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, LeashRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Leash leash : renderer.getRendered().values()) {
|
||||
set.add(this.pivotSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, true));
|
||||
}
|
||||
|
||||
for (Leash leash : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, true));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, LeashRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Leash leash : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, false));
|
||||
set.add(this.move(leash));
|
||||
}
|
||||
|
||||
for (Leash leash : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, true));
|
||||
}
|
||||
|
||||
Map<String, Leash> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
set.add(
|
||||
(Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(
|
||||
IntArrayList.toList(destroy.values().stream().mapMultiToInt((leashx, intConsumer) -> {
|
||||
intConsumer.accept(leashx.getLeashId());
|
||||
intConsumer.accept(leashx.getPivotId());
|
||||
}))
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, LeashRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Map<String, Leash> leashes = renderer.getRendered();
|
||||
buf.writeVarInt(leashes.size());
|
||||
leashes.forEach((s, leash) -> buf.writeVarInt(leash.getLeashId()));
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(Leash renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return NetworkUtils.createPivotSpawn(renderer.getPivotId(), renderer.getPivotUUID(), pos);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket leashSpawn(Leash renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getLeashId(), renderer.getLeastUUID(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.SLIME, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(Leash renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getLeashId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket leashData(Leash renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getLeashId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 16, EntityDataSerializers.INT, 0);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket mount(Leash renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(renderer.getLeashId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private Object move(Leash renderer) {
|
||||
if (!renderer.getPosition().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return NetworkUtils.createPivotMove(renderer.getPivotId(), renderer.getPositionCodec(), pos);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEntityLinkPacket link(Leash renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.getConnected().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeInt(renderer.getLeashId());
|
||||
buf.writeInt(renderer.getConnected().get());
|
||||
renderer.getConnected().clearDirty();
|
||||
return new ClientboundSetEntityLinkPacket(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.parser.behavior;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer.Mount;
|
||||
import com.ticxo.modelengine.api.utils.data.tracker.CollectionDataTracker;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.Packets;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Rot;
|
||||
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket.AttributeSnapshot;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.ai.attributes.Attributes;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class MountParser implements BehaviorRendererParser<MountRenderer> {
|
||||
public void sendToClients(MountRenderer renderer) {
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
public void destroy(MountRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, MountRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Mount mount : renderer.getRendered().values()) {
|
||||
set.add(this.pivotSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountHealth(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountMaxHealth(mount));
|
||||
}
|
||||
|
||||
for (Mount mount : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountHealth(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountMaxHealth(mount));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, MountRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Mount mount : renderer.getRendered().values()) {
|
||||
if (mount.getPosition().isDirty()) {
|
||||
set.add(this.pivotMove(mount));
|
||||
mount.getPosition().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getYaw().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountRotate(mount));
|
||||
mount.getYaw().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getHealth().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountHealth(mount));
|
||||
mount.getHealth().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getMaxHealth().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountMaxHealth(mount));
|
||||
mount.getMaxHealth().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getPassengers().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
mount.getPassengers().clearDirty();
|
||||
}
|
||||
}
|
||||
|
||||
for (Mount mount : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
}
|
||||
|
||||
Map<String, Mount> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size() * 2);
|
||||
destroy.forEach((s, mount) -> {
|
||||
buf.writeVarInt(mount.getPivotId());
|
||||
buf.writeVarInt(mount.getMountId());
|
||||
});
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, MountRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Collection<Mount> mounts = renderer.getRendered().values();
|
||||
buf.writeVarInt(mounts.size() * 2);
|
||||
mounts.forEach(mount -> {
|
||||
buf.writeVarInt(mount.getPivotId());
|
||||
buf.writeVarInt(mount.getMountId());
|
||||
});
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(Mount renderer) {
|
||||
return NetworkUtils.createPivotSpawn(renderer.getPivotId(), renderer.getPivotUuid(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket mountSpawn(Mount renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getMountId(),
|
||||
renderer.getMountUuid(),
|
||||
pos.x,
|
||||
pos.y,
|
||||
pos.z,
|
||||
0.0F,
|
||||
TMath.byteToRot(renderer.getYaw().get()),
|
||||
EntityType.ARMOR_STAND,
|
||||
0,
|
||||
Vec3.ZERO,
|
||||
0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket mountData(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 15, EntityDataSerializers.BYTE, (byte)16);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket pivotMount(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket mount(Mount renderer) {
|
||||
CollectionDataTracker<Integer> ids = renderer.getPassengers();
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
buf.writeVarInt(ids.size());
|
||||
ids.forEach(buf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotMove(Mount renderer) {
|
||||
return NetworkUtils.createPivotTeleport(renderer.getPivotId(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private Rot mountRotate(Mount renderer) {
|
||||
FriendlyByteBuf rot = NetworkUtils.createByteBuf();
|
||||
rot.writeVarInt(renderer.getMountId());
|
||||
rot.writeByte(renderer.getYaw().get());
|
||||
rot.writeByte(0);
|
||||
rot.writeBoolean(false);
|
||||
return Rot.read(rot);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket mountHealth(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
EntityUtils.writeData(buf, 9, EntityDataSerializers.FLOAT, renderer.getHealth().get());
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundUpdateAttributesPacket mountMaxHealth(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Collection<AttributeSnapshot> attributes = Lists.newArrayList();
|
||||
attributes.add(new AttributeSnapshot(Attributes.MAX_HEALTH, renderer.getMaxHealth().get().floatValue(), Lists.newArrayList()));
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
buf.writeCollection(attributes, (buf2, attribute) -> {
|
||||
buf2.writeResourceLocation(BuiltInRegistries.ATTRIBUTE.getKey(attribute.getAttribute()));
|
||||
buf2.writeDouble(attribute.getBase());
|
||||
buf2.writeCollection(attribute.getModifiers(), (buf3, modifier) -> {
|
||||
buf3.writeUUID(modifier.getId());
|
||||
buf3.writeDouble(modifier.getAmount());
|
||||
buf3.writeByte(modifier.getOperation().toValue());
|
||||
});
|
||||
});
|
||||
return new ClientboundUpdateAttributesPacket(buf);
|
||||
}
|
||||
}
|
||||
+196
@@ -0,0 +1,196 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.NameTagRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.NameTagRenderer.NameTag;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.Packets;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.Component.Serializer;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class NameTagParser implements BehaviorRendererParser<NameTagRenderer> {
|
||||
private float viewRange = 4096.0F;
|
||||
|
||||
public void sendToClients(NameTagRenderer renderer) {
|
||||
this.viewRange = renderer.getActiveModel().getViewRange();
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
public void destroy(NameTagRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, NameTagRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (NameTag nameTag : renderer.getRendered().values()) {
|
||||
set.add(this.pivotSpawn(nameTag));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(nameTag, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagSpawn(nameTag));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagData(nameTag, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(nameTag));
|
||||
}
|
||||
|
||||
for (NameTag nameTag : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(nameTag));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(nameTag, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagSpawn(nameTag));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagData(nameTag, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(nameTag));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, NameTagRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (NameTag nameTag : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagData(nameTag, false));
|
||||
if (nameTag.getPosition().isDirty()) {
|
||||
set.add(this.pivotMove(nameTag));
|
||||
nameTag.getPosition().clearDirty();
|
||||
}
|
||||
}
|
||||
|
||||
for (NameTag nameTagx : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(nameTagx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(nameTagx, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagSpawn(nameTagx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.tagData(nameTagx, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(nameTagx));
|
||||
}
|
||||
|
||||
Map<String, NameTag> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size() * 2);
|
||||
destroy.forEach((s, mount) -> {
|
||||
buf.writeVarInt(mount.getPivotId());
|
||||
buf.writeVarInt(mount.getTagId());
|
||||
});
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, NameTagRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Collection<NameTag> nameTags = renderer.getRendered().values();
|
||||
buf.writeVarInt(nameTags.size() * 2);
|
||||
nameTags.forEach(nameTag -> {
|
||||
buf.writeVarInt(nameTag.getPivotId());
|
||||
buf.writeVarInt(nameTag.getTagId());
|
||||
});
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(NameTag renderer) {
|
||||
return NetworkUtils.createPivotSpawn(renderer.getPivotId(), renderer.getPivotUuid(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(NameTag renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
if (spawn) {
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
}
|
||||
|
||||
buf.writeByte(255);
|
||||
renderer.clearDirty();
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket tagSpawn(NameTag renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getTagId(), renderer.getTagUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.TEXT_DISPLAY, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket tagData(NameTag renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getTagId());
|
||||
renderer.getVisibility().ifDirty(flag -> EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, flag ? this.viewRange : 0.0F), spawn);
|
||||
renderer.getTranslate().ifDirty(vector3f -> EntityUtils.writeData(buf, 10, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
renderer.getScale().ifDirty(vector3f -> EntityUtils.writeData(buf, 11, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
renderer.getTextOpacity().ifDirty(flag -> EntityUtils.writeData(buf, 26, EntityDataSerializers.BYTE, flag), spawn);
|
||||
renderer.getLineWidth().ifDirty(flag -> EntityUtils.writeData(buf, 24, EntityDataSerializers.INT, flag), spawn);
|
||||
renderer.getBackgroundColor().ifDirty(flag -> EntityUtils.writeData(buf, 25, EntityDataSerializers.INT, flag), spawn);
|
||||
renderer.getStyle().ifDirty(flags -> EntityUtils.writeData(buf, 27, EntityDataSerializers.BYTE, flags), spawn);
|
||||
renderer.getBillboard().ifDirty(flag -> {
|
||||
byte value = switch (flag) {
|
||||
case FIXED -> 0;
|
||||
case VERTICAL -> 1;
|
||||
case HORIZONTAL -> 2;
|
||||
case CENTER -> 3;
|
||||
default -> throw new IncompatibleClassChangeError();
|
||||
};
|
||||
EntityUtils.writeData(buf, 14, EntityDataSerializers.BYTE, value);
|
||||
}, spawn);
|
||||
renderer.getJsonString().ifDirty(jsonString -> {
|
||||
if (jsonString == null) {
|
||||
EntityUtils.writeData(buf, 23, EntityDataSerializers.COMPONENT, Component.empty());
|
||||
} else {
|
||||
EntityUtils.writeData(buf, 23, EntityDataSerializers.COMPONENT, Serializer.fromJson(jsonString));
|
||||
}
|
||||
}, spawn);
|
||||
renderer.getBrightness().ifDirty(val -> EntityUtils.writeData(buf, 15, EntityDataSerializers.INT, val), spawn);
|
||||
buf.writeByte(255);
|
||||
renderer.clearDirty();
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private Object pivotMove(NameTag renderer) {
|
||||
return NetworkUtils.createPivotMove(renderer.getPivotId(), renderer.getPositionCodec(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket pivotMount(NameTag renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(renderer.getTagId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
+220
@@ -0,0 +1,220 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SegmentRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SegmentRenderer.Pivot;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SegmentRenderer.SegmentDisplayBone;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayBone;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayBone.BoneData;
|
||||
import com.ticxo.modelengine.api.utils.data.UpdateScheme;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.Packets;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.inventory.CraftItemStack;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class SegmentParser implements BehaviorRendererParser<SegmentRenderer> {
|
||||
private final List<Runnable> cleanupQueue = new ArrayList<>();
|
||||
private float viewRange = 4096.0F;
|
||||
|
||||
public void sendToClients(SegmentRenderer renderer) {
|
||||
this.cleanupQueue.clear();
|
||||
this.viewRange = renderer.getActiveModel().getViewRange();
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
this.cleanupQueue.forEach(Runnable::run);
|
||||
this.cleanupQueue.clear();
|
||||
}
|
||||
|
||||
public void destroy(SegmentRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Packets set, Map<String, Pivot> map) {
|
||||
for (Pivot pivot : map.values()) {
|
||||
set.add(this.pivotSpawn(pivot));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(pivot));
|
||||
|
||||
for (SegmentDisplayBone bone : pivot.getRendered().values()) {
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true));
|
||||
}
|
||||
}
|
||||
|
||||
for (SegmentDisplayBone bone : pivot.getSpawnQueue().values()) {
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true));
|
||||
}
|
||||
}
|
||||
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(pivot));
|
||||
this.cleanupQueue.add(pivot::clearDirty);
|
||||
}
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, SegmentRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
this.spawn(set, renderer.getRendered());
|
||||
this.spawn(set, renderer.getSpawnQueue());
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, SegmentRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
HashSet<Integer> destroy = new HashSet<>();
|
||||
|
||||
for (Pivot pivot : renderer.getRendered().values()) {
|
||||
set.add(this.pivotTeleport(pivot));
|
||||
pivot.getRendered().values().forEach(bone -> {
|
||||
UpdateScheme<BoneData> scheme = bone.getModelUpdateScheme();
|
||||
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
switch (scheme.getUpdateMode(boneData)) {
|
||||
case NONE:
|
||||
case UPDATE:
|
||||
set.add(this.displayData(boneData, false));
|
||||
}
|
||||
}
|
||||
|
||||
scheme.getAdded().forEach(boneDatax -> {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneDatax));
|
||||
set.add(this.displayData(boneDatax, true));
|
||||
});
|
||||
scheme.getRemoved().forEach(boneDatax -> destroy.add(boneDatax.getId()));
|
||||
});
|
||||
this.cleanupQueue.add(pivot::clearDirty);
|
||||
}
|
||||
|
||||
this.spawn(set, renderer.getSpawnQueue());
|
||||
renderer.getDestroyQueue().forEach((s, pivotx) -> {
|
||||
destroy.add(pivotx.getId());
|
||||
destroy.addAll(pivotx.getPassengers());
|
||||
});
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeCollection(destroy, FriendlyByteBuf::writeVarInt);
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, SegmentRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Collection<Pivot> pivots = renderer.getRendered().values();
|
||||
IntArrayList ids = new IntArrayList();
|
||||
pivots.forEach(pivot -> {
|
||||
ids.add(pivot.getId());
|
||||
ids.addAll(pivot.getPassengers());
|
||||
});
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(ids));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(Pivot pivot) {
|
||||
return NetworkUtils.createPivotSpawn(pivot.getId(), pivot.getUuid(), pivot.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(Pivot pivot) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(pivot.getId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket pivotMount(Pivot pivot) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(pivot.getId());
|
||||
buf.writeCollection(pivot.getPassengers(), FriendlyByteBuf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private Object pivotTeleport(Pivot pivot) {
|
||||
if (!pivot.getPosition().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
this.cleanupQueue.add(() -> pivot.getPosition().clearDirty());
|
||||
return NetworkUtils.createPivotMove(pivot.getId(), pivot.getPositionCodec(), pivot.getPosition().get());
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket displaySpawn(Pivot pivot, BoneData boneData) {
|
||||
Vector3f pos = pivot.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(boneData.getId(), boneData.getUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0);
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier displayData(BoneData boneData, boolean force) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
if (!force && !bone.isDirty() && !boneData.getModel().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
this.cleanupQueue.add(bone::clearDirty);
|
||||
this.cleanupQueue.add(() -> boneData.getModel().clearDirty());
|
||||
return uuid -> this.displayData(uuid, boneData, force);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket displayData(UUID uuid, BoneData boneData, boolean force) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(boneData.getId());
|
||||
if (force) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
} else if (bone.isTransformDirty()) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
}
|
||||
|
||||
byte update = bone.getSnapshotHandler().getUpdate(uuid);
|
||||
bone.getStep().ifDirty(flag -> EntityUtils.writeData(buf, 9, EntityDataSerializers.INT, flag ? 0 : 1), force);
|
||||
bone.getGlowing().ifDirty(flag -> EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)(flag ? 96 : 32)), force);
|
||||
bone.getGlowColor().ifDirty(color -> EntityUtils.writeData(buf, 21, EntityDataSerializers.INT, color), force);
|
||||
bone.getBrightness().ifDirty(val -> EntityUtils.writeData(buf, 15, EntityDataSerializers.INT, val), force);
|
||||
bone.getPosition().ifDirty(vector3f -> EntityUtils.writeData(buf, 10, EntityDataSerializers.VECTOR3, vector3f), force || TMath.getBit(update, 0));
|
||||
bone.getScale().ifDirty(vector3f -> EntityUtils.writeData(buf, 11, EntityDataSerializers.VECTOR3, vector3f), force || TMath.getBit(update, 2));
|
||||
bone.getLeftRotation()
|
||||
.ifDirty(quaternionf -> EntityUtils.writeData(buf, 12, EntityDataSerializers.QUATERNION, quaternionf), force || TMath.getBit(update, 1));
|
||||
bone.getRightRotation()
|
||||
.ifDirty(quaternionf -> EntityUtils.writeData(buf, 13, EntityDataSerializers.QUATERNION, quaternionf), force || TMath.getBit(update, 3));
|
||||
bone.getBillboard().ifDirty(billboard -> EntityUtils.writeData(buf, 14, EntityDataSerializers.BYTE, (byte)billboard.ordinal()));
|
||||
bone.getVisibility().ifDirty(flag -> EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, flag ? this.viewRange : 0.0F), force);
|
||||
boneData.getModel().ifDirty(itemStack -> EntityUtils.writeData(buf, 22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(itemStack)), force);
|
||||
bone.getDisplay().ifDirty(display -> EntityUtils.writeData(buf, 23, EntityDataSerializers.BYTE, display == null ? 0 : (byte)display.ordinal()), force);
|
||||
buf.writeByte(255);
|
||||
this.cleanupQueue.add(bone::clearDirty);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.BaseEntity;
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SubHitboxRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SubHitboxRenderer.SubHitbox;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.Packets;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class SubHitboxParser implements BehaviorRendererParser<SubHitboxRenderer> {
|
||||
public void sendToClients(SubHitboxRenderer renderer) {
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
public void destroy(SubHitboxRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, SubHitboxRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (SubHitbox subHitbox : renderer.getRendered().values()) {
|
||||
set.add(this.pivotSpawn(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxSpawn(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxData(subHitbox, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(subHitbox));
|
||||
}
|
||||
|
||||
for (SubHitbox subHitbox : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxSpawn(subHitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxData(subHitbox, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(subHitbox));
|
||||
}
|
||||
|
||||
BaseEntity<?> base = renderer.getActiveModel().getModeledEntity().getBase();
|
||||
NetworkUtils.sendBundled(targets, set, player -> player != base.getOriginal());
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, SubHitboxRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (SubHitbox subHitbox : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxData(subHitbox, false));
|
||||
if (subHitbox.getPosition().isDirty()) {
|
||||
set.add(this.pivotMove(subHitbox));
|
||||
subHitbox.getPosition().clearDirty();
|
||||
}
|
||||
}
|
||||
|
||||
for (SubHitbox subHitboxx : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(subHitboxx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(subHitboxx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxSpawn(subHitboxx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxData(subHitboxx, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(subHitboxx));
|
||||
}
|
||||
|
||||
Map<String, SubHitbox> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size() * 2);
|
||||
destroy.forEach((s, subHitboxx) -> {
|
||||
buf.writeVarInt(subHitboxx.getPivotId());
|
||||
buf.writeVarInt(subHitboxx.getHitboxId());
|
||||
});
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
BaseEntity<?> base = renderer.getActiveModel().getModeledEntity().getBase();
|
||||
NetworkUtils.sendBundled(targets, set, player -> player != base.getOriginal());
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, SubHitboxRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Collection<SubHitbox> subHitboxes = renderer.getRendered().values();
|
||||
buf.writeVarInt(subHitboxes.size() * 2);
|
||||
subHitboxes.forEach(subHitbox -> {
|
||||
buf.writeVarInt(subHitbox.getPivotId());
|
||||
buf.writeVarInt(subHitbox.getHitboxId());
|
||||
});
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(SubHitbox renderer) {
|
||||
return NetworkUtils.createPivotSpawn(renderer.getPivotId(), renderer.getPivotUuid(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(SubHitbox renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private Object pivotMove(SubHitbox renderer) {
|
||||
return NetworkUtils.createPivotMove(renderer.getPivotId(), renderer.getPositionCodec(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket pivotMount(SubHitbox renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(renderer.getHitboxId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket hitboxSpawn(SubHitbox renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getHitboxId(), renderer.getHitboxUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.INTERACTION, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket hitboxData(SubHitbox renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getHitboxId());
|
||||
renderer.getWidth().ifDirty(width -> EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, width), spawn);
|
||||
renderer.getHeight().ifDirty(height -> EntityUtils.writeData(buf, 9, EntityDataSerializers.FLOAT, height), spawn);
|
||||
buf.writeByte(255);
|
||||
renderer.clearDirty();
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
+732
@@ -0,0 +1,732 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.parser.model;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.BaseEntity;
|
||||
import com.ticxo.modelengine.api.entity.BukkitEntity;
|
||||
import com.ticxo.modelengine.api.entity.CullType;
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.lod.AnimationLODHandler.LODTracker;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayBone;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayFire;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayRenderer;
|
||||
import com.ticxo.modelengine.api.model.render.ModelRendererParser;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayBone.BoneData;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayRenderer.Hitbox;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayRenderer.Pivot;
|
||||
import com.ticxo.modelengine.api.nms.network.ModPlayerTracker;
|
||||
import com.ticxo.modelengine.api.utils.data.PooledCollection;
|
||||
import com.ticxo.modelengine.api.utils.data.UpdateScheme;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.BulkEntityDataEncoder;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.Packets;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.network.syncher.SynchedEntityData.DataValue;
|
||||
import net.minecraft.util.Brightness;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.Display.BillboardConstraints;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class DisplayParser implements ModelRendererParser<DisplayRenderer> {
|
||||
private static final Quaternionf IDENTITY_ROTATION = new Quaternionf();
|
||||
private final Set<UUID> noCullPlayers = new HashSet<>();
|
||||
private final Set<UUID> noCullForcePlayers = new HashSet<>();
|
||||
private final Set<UUID> movementOnlyPlayers = new HashSet<>();
|
||||
private final Set<UUID> culledPlayers = new HashSet<>();
|
||||
private final List<Runnable> cleanupQueue = new ArrayList<>();
|
||||
private ModeledEntity modeledEntity;
|
||||
private float viewRange = 4096.0F;
|
||||
private Object pivotMove;
|
||||
private Object hitboxMove;
|
||||
private static Constructor<?> CUSTOM_PAYLOAD_CONSTRUCTOR;
|
||||
|
||||
public void sendToClients(DisplayRenderer renderer) {
|
||||
this.noCullPlayers.clear();
|
||||
this.noCullForcePlayers.clear();
|
||||
this.movementOnlyPlayers.clear();
|
||||
this.culledPlayers.clear();
|
||||
this.cleanupQueue.clear();
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.modeledEntity = renderer.getActiveModel().getModeledEntity();
|
||||
this.viewRange = renderer.getActiveModel().getViewRange();
|
||||
this.pivotMove = this.pivotTeleport(renderer.getPivot());
|
||||
this.hitboxMove = this.hitboxTeleport(renderer.getHitbox());
|
||||
|
||||
for (Entry<UUID, CullType> entry : data.getTracking().entrySet()) {
|
||||
switch ((CullType)entry.getValue()) {
|
||||
case NO_CULL:
|
||||
if (renderer.pollFullUpdate(entry.getKey())) {
|
||||
this.noCullForcePlayers.add(entry.getKey());
|
||||
} else {
|
||||
this.noCullPlayers.add(entry.getKey());
|
||||
}
|
||||
break;
|
||||
case MOVEMENT_ONLY:
|
||||
renderer.pushFullUpdate(entry.getKey());
|
||||
this.movementOnlyPlayers.add(entry.getKey());
|
||||
break;
|
||||
case CULLED:
|
||||
this.culledPlayers.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
if (renderer.pollFirstSpawn()) {
|
||||
HashSet<UUID> set = new HashSet<>((int)((this.movementOnlyPlayers.size() + this.noCullPlayers.size() + this.noCullForcePlayers.size()) / 0.75F) + 1);
|
||||
set.addAll(this.movementOnlyPlayers);
|
||||
set.addAll(this.noCullPlayers);
|
||||
set.addAll(this.noCullForcePlayers);
|
||||
this.spawn(set, renderer);
|
||||
} else {
|
||||
Set<UUID> spawning = data.getStartTracking();
|
||||
this.spawn(spawning, renderer);
|
||||
this.updateRealtime(excluding(this.movementOnlyPlayers, spawning), renderer, true, false);
|
||||
this.updateRealtime(excluding(this.noCullPlayers, spawning), renderer, false, false);
|
||||
this.updateRealtime(excluding(this.noCullForcePlayers, spawning), renderer, false, true);
|
||||
this.updateCulled(this.culledPlayers, renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
this.noCullPlayers.clear();
|
||||
this.noCullForcePlayers.clear();
|
||||
this.movementOnlyPlayers.clear();
|
||||
this.culledPlayers.clear();
|
||||
renderer.getPivot().clearDirty();
|
||||
renderer.getHitbox().clearDirty();
|
||||
PooledCollection<DisplayFire> fireDisplay = renderer.getHitbox().getFireDisplay();
|
||||
if (!fireDisplay.getCreated().isEmpty()) {
|
||||
fireDisplay.getCreated().clear();
|
||||
}
|
||||
|
||||
if (!fireDisplay.getReleased().isEmpty()) {
|
||||
fireDisplay.getReleased().clear();
|
||||
}
|
||||
|
||||
this.cleanupQueue.forEach(Runnable::run);
|
||||
this.cleanupQueue.clear();
|
||||
this.pivotMove = null;
|
||||
this.hitboxMove = null;
|
||||
}
|
||||
|
||||
private static Set<UUID> excluding(Set<UUID> targets, Set<UUID> excluded) {
|
||||
if (!targets.isEmpty() && !excluded.isEmpty()) {
|
||||
HashSet<UUID> filtered = new HashSet<>(targets);
|
||||
filtered.removeAll(excluded);
|
||||
return filtered;
|
||||
} else {
|
||||
return targets;
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy(DisplayRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, DisplayRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
Pivot pivot = renderer.getPivot();
|
||||
set.add(this.pivotSpawn(pivot));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(pivot));
|
||||
|
||||
for (DisplayBone bone : renderer.getRendered().values()) {
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true, false));
|
||||
}
|
||||
}
|
||||
|
||||
for (DisplayBone bone : renderer.getSpawnQueue().values()) {
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true, false));
|
||||
}
|
||||
}
|
||||
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(pivot));
|
||||
BaseEntity<?> base = renderer.getActiveModel().getModeledEntity().getBase();
|
||||
if (base instanceof BukkitEntity bukkitEntity && bukkitEntity.getOriginal() instanceof Player owner && targets.contains(owner.getUniqueId())) {
|
||||
NetworkUtils.sendBundled(owner.getUniqueId(), set);
|
||||
}
|
||||
|
||||
Hitbox hitbox = renderer.getHitbox();
|
||||
set.add(this.hitboxSpawnPivot(hitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxDataPivot(hitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxSpawn(hitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxData(hitbox, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.shadowSpawn(hitbox));
|
||||
set.add((Packet<ClientGamePacketListener>)this.shadowData(hitbox, true));
|
||||
if (hitbox.isFireVisible()) {
|
||||
hitbox.getFireDisplay().getInUse().forEach(displayFire -> {
|
||||
set.add((Packet<ClientGamePacketListener>)this.fireSpawn(hitbox, displayFire));
|
||||
set.add((Packet<ClientGamePacketListener>)this.fireData(displayFire, true));
|
||||
});
|
||||
}
|
||||
|
||||
set.add((Packet<ClientGamePacketListener>)this.hitboxMount(hitbox));
|
||||
NetworkUtils.sendBundled(targets, set, player -> !player.getUniqueId().equals(base.getUUID()));
|
||||
}
|
||||
}
|
||||
|
||||
private void updateRealtime(Set<UUID> targets, DisplayRenderer renderer, boolean movementOnly, boolean transformOnly) {
|
||||
if (!targets.isEmpty()) {
|
||||
Set<UUID> modTargets = null;
|
||||
Set<UUID> vanillaTargets = targets;
|
||||
|
||||
for (UUID uuid : targets) {
|
||||
if (ModPlayerTracker.hasClientMod(uuid)) {
|
||||
if (modTargets == null) {
|
||||
modTargets = new HashSet<>();
|
||||
vanillaTargets = new HashSet<>(targets);
|
||||
}
|
||||
|
||||
modTargets.add(uuid);
|
||||
vanillaTargets.remove(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
Packets set = new Packets();
|
||||
Packets boneSet = modTargets != null ? new Packets() : set;
|
||||
HashSet<Integer> destroy = new HashSet<>();
|
||||
Pivot pivot = renderer.getPivot();
|
||||
set.add(this.pivotMove);
|
||||
if (!movementOnly) {
|
||||
for (DisplayBone bone : renderer.getRendered().values()) {
|
||||
UpdateScheme<BoneData> scheme = bone.getModelUpdateScheme();
|
||||
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
switch (scheme.getUpdateMode(boneData)) {
|
||||
case NONE:
|
||||
case UPDATE:
|
||||
boneSet.add(this.displayData(boneData, false, transformOnly));
|
||||
}
|
||||
}
|
||||
|
||||
if (scheme.hasUpdate()) {
|
||||
scheme.getAdded().forEach(boneData -> {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true, false));
|
||||
});
|
||||
scheme.getRemoved().forEach(boneData -> destroy.add(boneData.getId()));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (DisplayBone bone : renderer.getRendered().values()) {
|
||||
UpdateScheme<BoneData> scheme = bone.getModelUpdateScheme();
|
||||
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
switch (scheme.getUpdateMode(boneData)) {
|
||||
case NONE:
|
||||
case UPDATE:
|
||||
boneSet.add((Packet<ClientGamePacketListener>)this.displayVisibleData(boneData));
|
||||
}
|
||||
}
|
||||
|
||||
if (scheme.hasUpdate()) {
|
||||
scheme.getAdded().forEach(boneData -> {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true, false));
|
||||
});
|
||||
scheme.getRemoved().forEach(boneData -> destroy.add(boneData.getId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (DisplayBone bone : renderer.getSpawnQueue().values()) {
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.displaySpawn(pivot, boneData));
|
||||
set.add(this.displayData(boneData, true, false));
|
||||
}
|
||||
}
|
||||
|
||||
renderer.getDestroyQueue().forEach((s, bones) -> bones.getModel().forEach((uuidx, boneData) -> destroy.add(boneData.getId())));
|
||||
if (pivot.getPassengers().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(pivot));
|
||||
}
|
||||
|
||||
BaseEntity<?> base = renderer.getActiveModel().getModeledEntity().getBase();
|
||||
UUID owner = base instanceof BukkitEntity bukkitEntity && bukkitEntity.getOriginal() instanceof Player && targets.contains(base.getUUID())
|
||||
? base.getUUID()
|
||||
: null;
|
||||
Packets hitboxSet = owner == null ? set : new Packets();
|
||||
HashSet<Integer> hitboxDestroy = owner == null ? destroy : new HashSet<>();
|
||||
Hitbox hitbox = renderer.getHitbox();
|
||||
AtomicBoolean updateHitboxMount = new AtomicBoolean(false);
|
||||
hitboxSet.add(this.hitboxMove);
|
||||
if (hitbox.getHitboxVisible().isDirty()) {
|
||||
if (hitbox.isHitboxVisible()) {
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.hitboxSpawn(hitbox));
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.hitboxData(hitbox, true));
|
||||
updateHitboxMount.set(true);
|
||||
} else {
|
||||
hitboxDestroy.add(hitbox.getHitboxId());
|
||||
}
|
||||
} else {
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.hitboxData(hitbox, false));
|
||||
}
|
||||
|
||||
if (hitbox.getShadowVisible().isDirty()) {
|
||||
if (hitbox.isShadowVisible()) {
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.shadowSpawn(hitbox));
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.shadowData(hitbox, true));
|
||||
updateHitboxMount.set(true);
|
||||
} else {
|
||||
hitboxDestroy.add(hitbox.getShadowId());
|
||||
}
|
||||
} else {
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.shadowData(hitbox, false));
|
||||
}
|
||||
|
||||
PooledCollection<DisplayFire> fireDisplay = hitbox.getFireDisplay();
|
||||
if (hitbox.getFireVisible().isDirty()) {
|
||||
if (hitbox.isFireVisible()) {
|
||||
fireDisplay.getInUse().forEach(displayFire -> {
|
||||
updateHitboxMount.set(true);
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.fireSpawn(hitbox, displayFire));
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.fireData(displayFire, true));
|
||||
});
|
||||
} else {
|
||||
fireDisplay.getAll().forEach(displayFire -> hitboxDestroy.add(displayFire.getId()));
|
||||
}
|
||||
} else {
|
||||
fireDisplay.processAll(
|
||||
displayFire -> {
|
||||
updateHitboxMount.set(true);
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.fireSpawn(hitbox, displayFire));
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.fireData(displayFire, true));
|
||||
},
|
||||
displayFire -> hitboxSet.add((Packet<ClientGamePacketListener>)this.fireData(displayFire, false)),
|
||||
displayFire -> hitboxDestroy.add(displayFire.getId())
|
||||
);
|
||||
}
|
||||
|
||||
if (!fireDisplay.getCreated().isEmpty()) {
|
||||
fireDisplay.getCreated().clear();
|
||||
}
|
||||
|
||||
if (!fireDisplay.getReleased().isEmpty()) {
|
||||
fireDisplay.getReleased().clear();
|
||||
}
|
||||
|
||||
if (updateHitboxMount.get()) {
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)this.hitboxMount(hitbox));
|
||||
}
|
||||
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size());
|
||||
destroy.forEach(buf::writeVarInt);
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
if (hitboxDestroy != destroy && !hitboxDestroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(hitboxDestroy.size());
|
||||
hitboxDestroy.forEach(buf::writeVarInt);
|
||||
hitboxSet.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
if (modTargets != null) {
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
NetworkUtils.sendBundled(vanillaTargets, boneSet);
|
||||
this.sendBulkBoneData(modTargets, renderer, movementOnly, transformOnly);
|
||||
} else {
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
|
||||
if (hitboxSet != set) {
|
||||
NetworkUtils.sendBundled(targets, hitboxSet, player -> !player.getUniqueId().equals(owner));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sendBulkBoneData(Set<UUID> modTargets, DisplayRenderer renderer, boolean movementOnly, boolean transformOnly) {
|
||||
if (!movementOnly) {
|
||||
for (UUID uuid : modTargets) {
|
||||
BulkEntityDataEncoder encoder = new BulkEntityDataEncoder();
|
||||
Packets fallbackPackets = new Packets();
|
||||
|
||||
for (DisplayBone bone : renderer.getRendered().values()) {
|
||||
UpdateScheme<BoneData> scheme = bone.getModelUpdateScheme();
|
||||
|
||||
for (BoneData boneData : bone.getModel().values()) {
|
||||
switch (scheme.getUpdateMode(boneData)) {
|
||||
case NONE:
|
||||
case UPDATE:
|
||||
if (boneData.getModel().isDirty()) {
|
||||
fallbackPackets.add((Packets.PacketSupplier)u -> this.displayData(u, null, boneData, false, transformOnly));
|
||||
} else if (!boneData.getBone().isSkippable()) {
|
||||
this.encodeBoneData(encoder, uuid, null, boneData, false, transformOnly);
|
||||
} else {
|
||||
LODTracker tracker = this.modeledEntity.getAnimationLodHandler().tick(uuid);
|
||||
if (!tracker.isCanSkip()) {
|
||||
this.encodeBoneData(encoder, uuid, tracker, boneData, false, transformOnly);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (encoder.getEntryCount() > 0) {
|
||||
byte[] data = encoder.build();
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeUtf("modelengine:bulk_data");
|
||||
buf.writeBytes(data);
|
||||
Packet<? super ClientGamePacketListener> packet = createCustomPayload(buf);
|
||||
if (packet != null) {
|
||||
NetworkUtils.send(uuid, packet);
|
||||
}
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(uuid, fallbackPackets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Packet<? super ClientGamePacketListener> createCustomPayload(FriendlyByteBuf buf) {
|
||||
try {
|
||||
if (CUSTOM_PAYLOAD_CONSTRUCTOR == null) {
|
||||
CUSTOM_PAYLOAD_CONSTRUCTOR = ClientboundCustomPayloadPacket.class.getDeclaredConstructor(FriendlyByteBuf.class);
|
||||
CUSTOM_PAYLOAD_CONSTRUCTOR.setAccessible(true);
|
||||
}
|
||||
|
||||
return (Packet<? super ClientGamePacketListener>)CUSTOM_PAYLOAD_CONSTRUCTOR.newInstance(buf);
|
||||
} catch (Exception var2) {
|
||||
var2.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void encodeBoneData(BulkEntityDataEncoder encoder, UUID uuid, @Nullable LODTracker tracker, BoneData boneData, boolean force, boolean transformOnly) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
if (force || transformOnly || bone.isDirty() || boneData.getModel().isDirty()) {
|
||||
byte update = bone.getSnapshotHandler().getUpdate(uuid);
|
||||
boolean wouldWriteTransform = force || update != 0;
|
||||
boolean wouldWriteRender = force || bone.isRenderDirty();
|
||||
if (wouldWriteTransform || wouldWriteRender) {
|
||||
BulkEntityDataEncoder.EntryWriter entry = encoder.startEntry(boneData.getId());
|
||||
bone.getPosition().ifDirty(v -> entry.translation(new Vector3f(v)), force || TMath.getBit(update, 0));
|
||||
bone.getLeftRotation().ifDirty(q -> entry.leftRotation(new Quaternionf(q)), force || TMath.getBit(update, 1));
|
||||
bone.getScale().ifDirty(v -> entry.scale(new Vector3f(v)), force || TMath.getBit(update, 2));
|
||||
bone.getRightRotation().ifDirty(q -> entry.rightRotation(new Quaternionf(q)), force || TMath.getBit(update, 3));
|
||||
int lodTick = tracker == null ? 1 : tracker.getTickDuration().get();
|
||||
boolean lodDirty = tracker != null && tracker.getTickDuration().isDirty();
|
||||
bone.getStep().ifDirty(flag -> entry.transformDuration(flag ? 0 : lodTick), force || transformOnly || lodDirty);
|
||||
bone.getGlowing().ifDirty(flag -> bone.getGlowColor().ifDirty(color -> entry.glowData((byte)(flag ? 96 : 32), color), true), force);
|
||||
bone.getBrightness().ifDirty(entry::brightness, force);
|
||||
boolean hasBillboard = bone.getBillboard().isDirty() || force;
|
||||
boolean hasVisibility = bone.getVisibility().isDirty() || force;
|
||||
boolean hasDisplayType = bone.getDisplay().isDirty() || force;
|
||||
if (hasBillboard || hasVisibility || hasDisplayType) {
|
||||
entry.renderData(
|
||||
(byte)bone.getBillboard().get().ordinal(),
|
||||
bone.getVisibility().get() ? this.viewRange : 0.0F,
|
||||
bone.getDisplay().get() == null ? 0 : (byte)bone.getDisplay().get().ordinal()
|
||||
);
|
||||
}
|
||||
|
||||
entry.finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateCulled(Set<UUID> targets, DisplayRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
IntArrayList entityIds = new IntArrayList();
|
||||
Map<String, DisplayBone> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
destroy.values()
|
||||
.stream()
|
||||
.mapMultiToInt((bones, intConsumer) -> bones.getModel().forEach((uuid, boneData) -> intConsumer.accept(boneData.getId())))
|
||||
.forEach(entityIds::add);
|
||||
}
|
||||
|
||||
if (!entityIds.isEmpty()) {
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(entityIds));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, DisplayRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
IntArrayList entityIds = new IntArrayList();
|
||||
renderer.getRendered().forEach((s, bone) -> {
|
||||
targets.forEach(uuid -> bone.getSnapshotHandler().remove(uuid));
|
||||
bone.getModel().forEach((integer, boneData) -> entityIds.add(boneData.getId()));
|
||||
});
|
||||
entityIds.add(renderer.getPivot().getId());
|
||||
entityIds.add(renderer.getHitbox().getPivotId());
|
||||
entityIds.add(renderer.getHitbox().getHitboxId());
|
||||
entityIds.add(renderer.getHitbox().getShadowId());
|
||||
renderer.getHitbox().getFireDisplay().getAll().forEach(displayFire -> entityIds.add(displayFire.getId()));
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(entityIds));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(Pivot pivot) {
|
||||
return pivot.isOverridden() ? null : NetworkUtils.createPivotSpawn(pivot.getId(), pivot.getUuid(), pivot.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(Pivot pivot) {
|
||||
if (pivot.isOverridden()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(pivot.getId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket pivotMount(Pivot pivot) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(pivot.getId());
|
||||
buf.writeVarInt(pivot.getPassengers().size());
|
||||
pivot.getPassengers().forEach(buf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private Object pivotTeleport(Pivot pivot) {
|
||||
return !pivot.getPosition().isDirty() ? null : NetworkUtils.createPivotMove(pivot.getId(), pivot.getPositionCodec(), pivot.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket displaySpawn(Pivot pivot, BoneData boneData) {
|
||||
Vector3f pos = pivot.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(boneData.getId(), boneData.getUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0);
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier displayData(BoneData boneData, boolean force, boolean transformOnly) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
if (!force && !transformOnly && !bone.isDirty() && !boneData.getModel().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
this.cleanupQueue.add(bone::clearDirty);
|
||||
this.cleanupQueue.add(() -> boneData.getModel().clearDirty());
|
||||
if (force) {
|
||||
return uuid -> this.displayData(uuid, this.modeledEntity.getAnimationLodHandler().tick(uuid), boneData, true, transformOnly);
|
||||
} else {
|
||||
return !bone.isSkippable()
|
||||
? uuid -> this.displayData(uuid, null, boneData, false, transformOnly)
|
||||
: NetworkUtils.lodWrapper(this.modeledEntity, (uuid, tracker) -> this.displayData(uuid, tracker, boneData, false, transformOnly));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket displayData(UUID uuid, @Nullable LODTracker tracker, BoneData boneData, boolean force, boolean transformOnly) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
byte update = bone.getSnapshotHandler().getUpdate(uuid);
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(boneData.getId());
|
||||
if (!force && update != 0) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
}
|
||||
|
||||
int lodTick = tracker == null ? 1 : tracker.getTickDuration().get();
|
||||
boolean lodDirty = tracker != null && tracker.getTickDuration().isDirty();
|
||||
bone.getStep().ifDirty(flag -> EntityUtils.writeData(buf, 9, EntityDataSerializers.INT, flag ? 0 : lodTick), force || transformOnly || lodDirty);
|
||||
bone.getGlowing().ifDirty(flag -> EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)(flag ? 96 : 32)), force);
|
||||
bone.getGlowColor().ifDirty(color -> {
|
||||
if (!force || color != -1) {
|
||||
EntityUtils.writeData(buf, 21, EntityDataSerializers.INT, color);
|
||||
}
|
||||
}, force);
|
||||
bone.getBrightness().ifDirty(val -> {
|
||||
if (!force || val != -1) {
|
||||
EntityUtils.writeData(buf, 15, EntityDataSerializers.INT, val);
|
||||
}
|
||||
}, force);
|
||||
bone.getPosition().ifDirty(vector3f -> EntityUtils.writeData(buf, 10, EntityDataSerializers.VECTOR3, vector3f), force || TMath.getBit(update, 0));
|
||||
bone.getScale().ifDirty(vector3f -> EntityUtils.writeData(buf, 11, EntityDataSerializers.VECTOR3, vector3f), force || TMath.getBit(update, 2));
|
||||
bone.getLeftRotation()
|
||||
.ifDirty(quaternionf -> EntityUtils.writeData(buf, 12, EntityDataSerializers.QUATERNION, quaternionf), force || TMath.getBit(update, 1));
|
||||
bone.getRightRotation().ifDirty(quaternionf -> {
|
||||
if (!force || !TMath.isSimilar(quaternionf, IDENTITY_ROTATION)) {
|
||||
EntityUtils.writeData(buf, 13, EntityDataSerializers.QUATERNION, quaternionf);
|
||||
}
|
||||
}, force || TMath.getBit(update, 3));
|
||||
bone.getBillboard().ifDirty(billboard -> {
|
||||
if (!force || billboard.ordinal() != 0) {
|
||||
EntityUtils.writeData(buf, 14, EntityDataSerializers.BYTE, (byte)billboard.ordinal());
|
||||
}
|
||||
}, force);
|
||||
bone.getVisibility().ifDirty(flag -> EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, flag ? this.viewRange : 0.0F), force);
|
||||
boneData.getModel().ifDirty(itemStack -> EntityUtils.writeData(buf, 22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(itemStack)), force);
|
||||
bone.getDisplay().ifDirty(display -> EntityUtils.writeData(buf, 23, EntityDataSerializers.BYTE, display == null ? 0 : (byte)display.ordinal()), force);
|
||||
buf.writeByte(255);
|
||||
this.cleanupQueue.add(bone::clearDirty);
|
||||
this.cleanupQueue.add(() -> boneData.getModel().clearDirty());
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket displayVisibleData(BoneData boneData) {
|
||||
DisplayBone bone = boneData.getBone();
|
||||
if (!bone.isRenderDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(boneData.getId());
|
||||
bone.getGlowing().ifDirty(flag -> EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)(flag ? 96 : 32)));
|
||||
bone.getGlowColor().ifDirty(color -> EntityUtils.writeData(buf, 21, EntityDataSerializers.INT, color));
|
||||
bone.getBrightness().ifDirty(val -> EntityUtils.writeData(buf, 15, EntityDataSerializers.INT, val));
|
||||
bone.getBillboard().ifDirty(billboard -> EntityUtils.writeData(buf, 14, EntityDataSerializers.BYTE, (byte)billboard.ordinal()));
|
||||
bone.getVisibility().ifDirty(flag -> EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, flag ? this.viewRange : 0.0F));
|
||||
boneData.getModel().ifDirty(itemStack -> EntityUtils.writeData(buf, 22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(itemStack)));
|
||||
bone.getDisplay().ifDirty(display -> EntityUtils.writeData(buf, 23, EntityDataSerializers.BYTE, display == null ? 0 : (byte)display.ordinal()));
|
||||
buf.writeByte(255);
|
||||
this.cleanupQueue.add(bone::clearDirty);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier hitboxSpawnPivot(Hitbox hitbox) {
|
||||
return hitbox.isPivotVisible() ? NetworkUtils.createPivotSpawn(hitbox.getPivotId(), hitbox.getPivotUuid(), hitbox.getPosition().get()) : null;
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket hitboxDataPivot(Hitbox hitbox) {
|
||||
if (!hitbox.isPivotVisible()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(hitbox.getPivotId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket hitboxSpawn(Hitbox hitbox) {
|
||||
if (!hitbox.isHitboxVisible()) {
|
||||
return null;
|
||||
} else {
|
||||
Vector3f pos = hitbox.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
hitbox.getHitboxId(), hitbox.getHitboxUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.INTERACTION, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket hitboxData(Hitbox hitbox, boolean spawn) {
|
||||
if (hitbox.isHitboxVisible() && (hitbox.isHitboxDirty() || spawn)) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(hitbox.getHitboxId());
|
||||
hitbox.getWidth().ifDirty(val -> EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, val), spawn);
|
||||
hitbox.getHeight().ifDirty(val -> EntityUtils.writeData(buf, 9, EntityDataSerializers.FLOAT, val), spawn);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket shadowSpawn(Hitbox hitbox) {
|
||||
if (!hitbox.isShadowVisible()) {
|
||||
return null;
|
||||
} else {
|
||||
Vector3f pos = hitbox.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
hitbox.getShadowId(), hitbox.getShadowUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket shadowData(Hitbox hitbox, boolean spawn) {
|
||||
if (hitbox.isShadowVisible() && (hitbox.getShadowRadius().isDirty() || spawn)) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(hitbox.getShadowId());
|
||||
hitbox.getShadowRadius().ifDirty(val -> EntityUtils.writeData(buf, 17, EntityDataSerializers.FLOAT, val), spawn);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket fireSpawn(Hitbox hitbox, DisplayFire fire) {
|
||||
Vector3f pos = hitbox.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(fire.getId(), fire.getUuid(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket fireData(DisplayFire fire, boolean spawn) {
|
||||
if (!fire.isDirty() && !spawn) {
|
||||
return null;
|
||||
} else {
|
||||
ArrayList<DataValue<?>> entityData = new ArrayList<>(2);
|
||||
if (spawn) {
|
||||
entityData.add(new DataValue(14, EntityDataSerializers.BYTE, (byte)BillboardConstraints.VERTICAL.ordinal()));
|
||||
entityData.add(new DataValue(15, EntityDataSerializers.INT, Brightness.FULL_BRIGHT.pack()));
|
||||
}
|
||||
|
||||
fire.getFireModel().ifDirty(stack -> entityData.add(new DataValue(22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(stack))), spawn);
|
||||
fire.getPosition().ifDirty(vector3f -> entityData.add(new DataValue(10, EntityDataSerializers.VECTOR3, vector3f)), spawn);
|
||||
fire.getScale().ifDirty(vector3f -> entityData.add(new DataValue(11, EntityDataSerializers.VECTOR3, vector3f)), spawn);
|
||||
fire.getVisible().ifDirty(flag -> entityData.add(new DataValue(16, EntityDataSerializers.FLOAT, flag ? this.viewRange : 0.0F)), spawn);
|
||||
return new ClientboundSetEntityDataPacket(fire.getId(), entityData);
|
||||
}
|
||||
}
|
||||
|
||||
private Object hitboxTeleport(Hitbox hitbox) {
|
||||
if (hitbox.isPivotVisible() && hitbox.getPosition().isDirty()) {
|
||||
this.cleanupQueue.add(() -> hitbox.getPosition().clearDirty());
|
||||
return NetworkUtils.createPivotMove(hitbox.getPivotId(), hitbox.getPositionCodec(), hitbox.getPosition().get());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket hitboxMount(Hitbox hitbox) {
|
||||
if (!hitbox.isPivotVisible()) {
|
||||
return null;
|
||||
} else {
|
||||
boolean hitboxVisible = hitbox.isHitboxVisible();
|
||||
boolean shadowVisible = hitbox.isShadowVisible();
|
||||
ArrayList<Integer> passengers = new ArrayList<>(2 + hitbox.getFireDisplay().getInUse().size());
|
||||
if (hitboxVisible) {
|
||||
passengers.add(hitbox.getHitboxId());
|
||||
}
|
||||
|
||||
if (shadowVisible) {
|
||||
passengers.add(hitbox.getShadowId());
|
||||
}
|
||||
|
||||
if (hitbox.isFireVisible()) {
|
||||
hitbox.getFireDisplay().getInUse().forEach(displayFire -> passengers.add(displayFire.getId()));
|
||||
}
|
||||
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(hitbox.getPivotId());
|
||||
buf.writeVarInt(passengers.size());
|
||||
passengers.forEach(buf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package com.ticxo.modelengine.v1_20_R1.parser.vfx;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXDisplayRenderer;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXRendererParser;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXDisplayRenderer.VFXModel;
|
||||
import com.ticxo.modelengine.v1_20_R1.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R1.network.utils.Packets;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.entity.ItemDisplay.ItemDisplayTransform;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class VFXDisplayParser implements VFXRendererParser<VFXDisplayRenderer> {
|
||||
public void sendToClients(VFXDisplayRenderer renderer) {
|
||||
IEntityData data = renderer.getVFX().getBase().getData();
|
||||
if (renderer.isRespawnRequired()) {
|
||||
this.teleport(renderer.getVFXModel());
|
||||
this.spawn(data.getTracking().keySet(), renderer.getVFXModel());
|
||||
renderer.setRespawnRequired(false);
|
||||
} else {
|
||||
this.update(data.getTracking().keySet(), renderer.getVFXModel());
|
||||
this.spawn(data.getStartTracking(), renderer.getVFXModel());
|
||||
this.remove(data.getStopTracking(), renderer.getVFXModel());
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy(VFXDisplayRenderer renderer) {
|
||||
IEntityData data = renderer.getVFX().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer.getVFXModel());
|
||||
}
|
||||
|
||||
public void spawn(Set<UUID> targets, VFXModel vfx) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
set.add(this.pivotSpawn(vfx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(vfx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.vfxSpawn(vfx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.vfxData(vfx, true));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(vfx));
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
public void update(Set<UUID> targets, VFXModel vfx) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
set.add(this.teleport(vfx));
|
||||
set.add((Packet<ClientGamePacketListener>)this.vfxData(vfx, false));
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Set<UUID> targets, VFXModel vfx) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(2);
|
||||
buf.writeVarInt(vfx.getPivotId());
|
||||
buf.writeVarInt(vfx.getModelId());
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(VFXModel vfx) {
|
||||
return NetworkUtils.createPivotSpawn(vfx.getPivotId(), vfx.getPivotUuid(), vfx.getOrigin().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(VFXModel vfx) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(vfx.getPivotId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket vfxSpawn(VFXModel vfx) {
|
||||
Vector3f location = vfx.getOrigin().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
vfx.getModelId(), vfx.getModelUuid(), location.x, location.y, location.z, 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket vfxData(VFXModel vfx, boolean spawn) {
|
||||
if (!spawn && !vfx.isModelDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(vfx.getModelId());
|
||||
if (spawn) {
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
EntityUtils.writeData(buf, 9, EntityDataSerializers.INT, 1);
|
||||
EntityUtils.writeData(buf, 16, EntityDataSerializers.FLOAT, 4096.0F);
|
||||
EntityUtils.writeData(buf, 23, EntityDataSerializers.BYTE, (byte)ItemDisplayTransform.HEAD.ordinal());
|
||||
} else if (vfx.isModelDirty()) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
}
|
||||
|
||||
vfx.getPosition().ifDirty(vector3f -> EntityUtils.writeData(buf, 10, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
vfx.getScale().ifDirty(vector3f -> EntityUtils.writeData(buf, 11, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
vfx.getLeftRotation().ifDirty(quaternionf -> EntityUtils.writeData(buf, 12, EntityDataSerializers.QUATERNION, quaternionf), spawn);
|
||||
vfx.getModel().ifDirty(itemStack -> EntityUtils.writeData(buf, 22, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(itemStack)), spawn);
|
||||
vfx.getBrightness().ifDirty(val -> EntityUtils.writeData(buf, 15, EntityDataSerializers.INT, val), spawn);
|
||||
buf.writeByte(255);
|
||||
vfx.clearModelDirty();
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private Object teleport(VFXModel vfx) {
|
||||
return !vfx.getOrigin().isDirty() ? null : NetworkUtils.createPivotMove(vfx.getPivotId(), vfx.getPositionCodec(), vfx.getOrigin().get());
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket mount(VFXModel vfx) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(vfx.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(vfx.getModelId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.ticxo.modelengine.v1_20_R2;
|
||||
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils.ReflectionEnum;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.control.MoveControl;
|
||||
import net.minecraft.world.level.Level;
|
||||
|
||||
public enum NMSFields implements ReflectionEnum {
|
||||
ENTITY_dimensions(Entity.class, "bh", "dimensions"),
|
||||
ENTITY_eyeHeight(Entity.class, "bi", "eyeHeight"),
|
||||
LIVING_ENTITY_noJumpDelay(LivingEntity.class, "ca", "noJumpDelay"),
|
||||
LIVING_ENTITY_jumping(LivingEntity.class, "bj", "jumping"),
|
||||
MOB_lookControl(Mob.class, "bK", "lookControl"),
|
||||
MOB_moveControl(Mob.class, "bL", "moveControl"),
|
||||
MOB_navigation(Mob.class, "bN", "navigation"),
|
||||
MOB_goalSelector(Mob.class, "bO", "goalSelector"),
|
||||
MOB_bodyRotationControl(Mob.class, "bU", "bodyRotationControl"),
|
||||
SERVER_GAME_PACKET_LISTENER_IMPL_clientIsFloating(ServerGamePacketListenerImpl.class, "E", "clientIsFloating"),
|
||||
SERVER_COMMON_PACKET_LISTENER_IMPL_connection(ServerCommonPacketListenerImpl.class, "c", "connection"),
|
||||
MOVE_CONTROL_operation(MoveControl.class, "k", "operation"),
|
||||
TRACKED_ENTITY_serverEntity(TrackedEntity.class, "b", "serverEntity"),
|
||||
TRACKED_ENTITY_range(TrackedEntity.class, "d", "range"),
|
||||
LEVEL_threadSafeRandom(Level.class, "f", "threadSafeRandom"),
|
||||
CLIENTBOUND_MOVE_ENTITY_PACKET_entityId(ClientboundMoveEntityPacket.class, "a", "entityId"),
|
||||
CLIENTBOUND_ROTATE_HEAD_PACKET_entityId(ClientboundRotateHeadPacket.class, "a", "entityId");
|
||||
|
||||
private final Class<?> target;
|
||||
private final String obfuscated;
|
||||
private final String mapped;
|
||||
|
||||
private NMSFields(Class<?> target, String obfuscated, String mapped) {
|
||||
this.target = target;
|
||||
this.obfuscated = obfuscated;
|
||||
this.mapped = mapped;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getTarget() {
|
||||
return this.target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getObfuscated() {
|
||||
return this.obfuscated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMapped() {
|
||||
return this.mapped;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package com.ticxo.modelengine.v1_20_R2;
|
||||
|
||||
import com.mojang.math.MatrixUtil;
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.ServerInfo;
|
||||
import com.ticxo.modelengine.api.entity.data.BukkitEntityData;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.PivotOverride;
|
||||
import com.ticxo.modelengine.api.model.PivotOverrideRegistry;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.LeashRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.NameTagRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SegmentRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.SubHitboxRenderer;
|
||||
import com.ticxo.modelengine.api.model.render.DisplayRenderer;
|
||||
import com.ticxo.modelengine.api.model.render.ModelRenderer;
|
||||
import com.ticxo.modelengine.api.model.render.ModelRendererParser;
|
||||
import com.ticxo.modelengine.api.nms.NMSHandler;
|
||||
import com.ticxo.modelengine.api.nms.RenderParsers;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.nms.network.NetworkHandler;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXDisplayRenderer;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXRenderer;
|
||||
import com.ticxo.modelengine.api.vfx.render.VFXRendererParser;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.EntityHandlerImpl;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.NetworkHandlerImpl;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.parser.behavior.LeashParser;
|
||||
import com.ticxo.modelengine.v1_20_R2.parser.behavior.MountParser;
|
||||
import com.ticxo.modelengine.v1_20_R2.parser.behavior.NameTagParser;
|
||||
import com.ticxo.modelengine.v1_20_R2.parser.behavior.SegmentParser;
|
||||
import com.ticxo.modelengine.v1_20_R2.parser.behavior.SubHitboxParser;
|
||||
import com.ticxo.modelengine.v1_20_R2.parser.model.DisplayParser;
|
||||
import com.ticxo.modelengine.v1_20_R2.parser.vfx.VFXDisplayParser;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import org.apache.commons.lang3.tuple.Triple;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.util.Transformation;
|
||||
import org.joml.Matrix3f;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Quaternionfc;
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector3fc;
|
||||
|
||||
public class NMSHandler_v1_20_R2 implements NMSHandler {
|
||||
private final EntityHandler entityHandler = new EntityHandlerImpl();
|
||||
private final NetworkHandler networkHandler = new NetworkHandlerImpl();
|
||||
private final RenderParsers globalParsers = this.createParsers();
|
||||
private final VFXDisplayParser vfxDisplayParser = new VFXDisplayParser();
|
||||
|
||||
@Override
|
||||
public RenderParsers createParsers() {
|
||||
RenderParsers parsers = new RenderParsers();
|
||||
parsers.registerModelParser(renderer -> renderer instanceof DisplayRenderer, DisplayParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof MountRenderer, MountParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof LeashRenderer, LeashParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof NameTagRenderer, NameTagParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof SubHitboxRenderer, SubHitboxParser::new);
|
||||
parsers.registerBehaviorParser(renderer -> renderer instanceof SegmentRenderer, SegmentParser::new);
|
||||
return parsers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPivotOverrides() {
|
||||
PivotOverrideRegistry registry = ModelEngineAPI.getPivotOverrideRegistry();
|
||||
|
||||
for (PivotOverride override : registry.getAll()) {
|
||||
ModeledEntity modeledEntity = ModelEngineAPI.getModeledEntity(override.getUuid());
|
||||
BukkitEntityData data = modeledEntity != null && modeledEntity.getBase().getData() instanceof BukkitEntityData d ? d : null;
|
||||
boolean passengersDirty = override.getPassengers().isDirty();
|
||||
Set<UUID> lateTrackers = data != null ? data.getStartTracking() : Set.of();
|
||||
if (passengersDirty || !lateTrackers.isEmpty()) {
|
||||
FriendlyByteBuf byteBuf = NetworkUtils.createByteBuf(buf -> {
|
||||
buf.writeVarInt(override.getId());
|
||||
Collection<Integer> list = override.getPassengers().get();
|
||||
ArrayList<Integer> allPassengers = new ArrayList<>(list);
|
||||
if (modeledEntity != null) {
|
||||
List<Entity> basePassengers = modeledEntity.getBase().getPassengers();
|
||||
int[] snapshot = new int[basePassengers.size()];
|
||||
int i = 0;
|
||||
|
||||
for (Entity passenger : basePassengers) {
|
||||
int pid = passenger.getEntityId();
|
||||
snapshot[i++] = pid;
|
||||
if (!allPassengers.contains(pid)) {
|
||||
allPassengers.add(pid);
|
||||
}
|
||||
}
|
||||
|
||||
override.setBasePassengerSnapshot(snapshot);
|
||||
} else {
|
||||
for (int pid : override.getBasePassengerSnapshot()) {
|
||||
if (!allPassengers.contains(pid)) {
|
||||
allPassengers.add(pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buf.writeVarInt(allPassengers.size());
|
||||
allPassengers.forEach(buf::writeVarInt);
|
||||
});
|
||||
Set<UUID> recipients;
|
||||
if (data == null) {
|
||||
recipients = ServerInfo.getOnlinePlayers();
|
||||
} else if (passengersDirty) {
|
||||
recipients = new HashSet<>(data.getMutableTracking().keySet());
|
||||
recipients.addAll(lateTrackers);
|
||||
} else {
|
||||
recipients = lateTrackers;
|
||||
}
|
||||
|
||||
NetworkUtils.send(recipients, new ClientboundSetPassengersPacket(byteBuf));
|
||||
override.getPassengers().clearDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends ModelRenderer> ModelRendererParser<T> getModelRendererParser(T renderer) {
|
||||
return this.globalParsers.getModelParser(renderer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends BehaviorRenderer> BehaviorRendererParser<T> getBehaviorRendererParser(T renderer) {
|
||||
return this.globalParsers.getBehaviorParser(renderer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends VFXRenderer> VFXRendererParser<T> getVFXRendererParser(T renderer) {
|
||||
return renderer instanceof VFXDisplayRenderer ? (VFXRendererParser<T>)(Object)this.vfxDisplayParser : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMountPassengerUpdate(Set<UUID> targets, int mountId, Collection<Integer> passengerIds) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(mountId);
|
||||
buf.writeVarInt(passengerIds.size());
|
||||
passengerIds.forEach(buf::writeVarInt);
|
||||
ClientboundSetPassengersPacket packet = new ClientboundSetPassengersPacket(buf);
|
||||
|
||||
for (UUID target : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlushNow(packet));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Transformation decompose(Matrix4f transform) {
|
||||
float f = 1.0F / transform.m33();
|
||||
Triple<Quaternionf, Vector3f, Quaternionf> triple = MatrixUtil.svdDecompose(new Matrix3f(transform).scale(f));
|
||||
return new Transformation(
|
||||
transform.getTranslation(new Vector3f()).mul(f),
|
||||
new Quaternionf((Quaternionfc)triple.getLeft()),
|
||||
new Vector3f((Vector3fc)triple.getMiddle()),
|
||||
new Quaternionf((Quaternionfc)triple.getRight())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityHandler getEntityHandler() {
|
||||
return this.entityHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkHandler getNetworkHandler() {
|
||||
return this.networkHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RenderParsers getGlobalParsers() {
|
||||
return this.globalParsers;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.ticxo.modelengine.v1_20_R2;
|
||||
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils.MethodEnum;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.EntityDimensions;
|
||||
import net.minecraft.world.item.InstrumentItem;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public enum NMSMethods implements MethodEnum {
|
||||
TRACKED_ENTITY_getEffectiveRange(TrackedEntity.class, "b", "getEffectiveRange"),
|
||||
SERVER_GAME_PACKET_LISTENER_IMPL_checkLimit(ServerGamePacketListenerImpl.class, "checkLimit", "checkLimit", long.class),
|
||||
INSTRUMENT_ITEM_getInstrument(InstrumentItem.class, "d", "getInstrument", ItemStack.class),
|
||||
ENTITY_getPassengerAttachmentPoint(Entity.class, "a", "getPassengerAttachmentPoint", Entity.class, EntityDimensions.class, float.class);
|
||||
|
||||
private final Class<?> target;
|
||||
private final String obfuscated;
|
||||
private final String mapped;
|
||||
private final Class<?>[] parameterClasses;
|
||||
|
||||
private NMSMethods(Class<?> target, String obfuscated, String mapped, Class<?>... parameterClasses) {
|
||||
this.target = target;
|
||||
this.obfuscated = obfuscated;
|
||||
this.mapped = mapped;
|
||||
this.parameterClasses = parameterClasses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getTarget() {
|
||||
return this.target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getObfuscated() {
|
||||
return this.obfuscated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMapped() {
|
||||
return this.mapped;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?>[] getParameterClasses() {
|
||||
return this.parameterClasses;
|
||||
}
|
||||
}
|
||||
+714
@@ -0,0 +1,714 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.ServerInfo;
|
||||
import com.ticxo.modelengine.api.entity.BaseEntity;
|
||||
import com.ticxo.modelengine.api.entity.Hitbox;
|
||||
import com.ticxo.modelengine.api.entity.data.BukkitEntityData;
|
||||
import com.ticxo.modelengine.api.interaction.DynamicHitbox;
|
||||
import com.ticxo.modelengine.api.model.bone.ModelBone;
|
||||
import com.ticxo.modelengine.api.model.bone.type.SubHitbox;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityState;
|
||||
import com.ticxo.modelengine.api.nms.entity.HitboxEntity;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler.BoxRelToCam;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler.InteractionResult;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.BodyRotationController;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.LookController;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.MoveController;
|
||||
import com.ticxo.modelengine.api.nms.impl.TempTrackedEntity;
|
||||
import com.ticxo.modelengine.api.utils.RaceConditionUtil;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.api.utils.config.ConfigProperty;
|
||||
import com.ticxo.modelengine.api.utils.config.DebugToggle;
|
||||
import com.ticxo.modelengine.api.utils.logger.TLogger;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.api.utils.promise.Promise;
|
||||
import com.ticxo.modelengine.v1_20_R2.NMSFields;
|
||||
import com.ticxo.modelengine.v1_20_R2.NMSMethods;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.controller.BodyRotationControlWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.controller.LookControlWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.controller.MoveControlWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.hitbox.HitboxEntityImpl;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.navigation.AmphibiousNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.navigation.FlyingNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.navigation.GroundNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.navigation.WallClimberNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.navigation.WaterBoundNavigationWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.patch.PatchedServerGamePacketListener;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.Packets;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSoundEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundUseItemPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.damagesource.DamageSource;
|
||||
import net.minecraft.world.entity.AreaEffectCloud;
|
||||
import net.minecraft.world.entity.EntityDimensions;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.MoverType;
|
||||
import net.minecraft.world.entity.Pose;
|
||||
import net.minecraft.world.entity.ai.goal.Goal;
|
||||
import net.minecraft.world.entity.ai.goal.GoalSelector;
|
||||
import net.minecraft.world.entity.ai.goal.WrappedGoal;
|
||||
import net.minecraft.world.entity.ai.navigation.AmphibiousPathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.WallClimberNavigation;
|
||||
import net.minecraft.world.entity.ai.navigation.WaterBoundPathNavigation;
|
||||
import net.minecraft.world.entity.decoration.ArmorStand;
|
||||
import net.minecraft.world.item.Instrument;
|
||||
import net.minecraft.world.item.InstrumentItem;
|
||||
import net.minecraft.world.item.UseAnim;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraft.world.phys.HitResult.Type;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Particle;
|
||||
import org.bukkit.Particle.DustOptions;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.CraftWorld;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.util.CraftLocation;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.util.CraftMagicNumbers;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.BoundingBox;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class EntityHandlerImpl implements EntityHandler {
|
||||
private static boolean usePaperClipMethod;
|
||||
private static final MethodHandle JUMPING_GETTER = createJumpingGetter();
|
||||
private final Set<UUID> forceInvisible = ConcurrentHashMap.newKeySet();
|
||||
private double forceRenderWidth;
|
||||
private double forceRenderHeight;
|
||||
private ArmorStand dummyArmorStand;
|
||||
private AreaEffectCloud dummyCloud;
|
||||
|
||||
private static MethodHandle createJumpingGetter() {
|
||||
Field field = ReflectionUtils.getField(NMSFields.LIVING_ENTITY_jumping);
|
||||
if (field == null) {
|
||||
return null;
|
||||
} else {
|
||||
try {
|
||||
return MethodHandles.lookup().unreflectGetter(field);
|
||||
} catch (IllegalAccessException var2) {
|
||||
var2.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public EntityHandlerImpl() {
|
||||
ModelEngineAPI.getAPI().getConfigManager().registerReferenceUpdate(this::updateConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateConfig() {
|
||||
this.forceRenderWidth = ConfigProperty.BLOCK_CULL_IGNORE_SIZE_WIDTH.getDouble();
|
||||
this.forceRenderHeight = ConfigProperty.BLOCK_CULL_IGNORE_SIZE_HEIGHT.getDouble();
|
||||
usePaperClipMethod = ConfigProperty.BLOCK_CULL_USE_PAPER_CLIP.getBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextEntityId() {
|
||||
return CraftMagicNumbers.INSTANCE.nextEntityId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHitbox(Entity entity, @NotNull Hitbox hitbox) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
EntityDimensions box = new EntityDimensions((float)hitbox.getMaxWidth(), (float)hitbox.getHeight(), true);
|
||||
ReflectionUtils.set(nms, NMSFields.ENTITY_dimensions, box);
|
||||
ReflectionUtils.set(nms, NMSFields.ENTITY_eyeHeight, (float)hitbox.getEyeHeight());
|
||||
nms.setBoundingBox(box.makeBoundingBox(nms.position()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStepHeight(Entity entity, double height) {
|
||||
EntityUtils.nms(entity).setMaxUpStep((float)height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getStepHeight(Entity entity) {
|
||||
return EntityUtils.nms(entity).maxUpStep();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Entity entity, double x, double y, double z) {
|
||||
EntityUtils.nms(entity).setPos(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void movePassenger(Entity entity, double x, double y, double z) {
|
||||
net.minecraft.world.entity.Entity nmsEntity = EntityUtils.nms(entity);
|
||||
if (this.dummyArmorStand == null) {
|
||||
this.dummyArmorStand = new ArmorStand(EntityType.ARMOR_STAND, nmsEntity.level());
|
||||
this.dummyArmorStand.setMarker(true);
|
||||
}
|
||||
|
||||
double seatY = y + nmsEntity.getMyRidingOffset(this.dummyArmorStand);
|
||||
nmsEntity.setPos(x, seatY, z);
|
||||
nmsEntity.setDeltaMovement(Vec3.ZERO);
|
||||
nmsEntity.resetFallDistance();
|
||||
if (nmsEntity instanceof ServerPlayer player) {
|
||||
ReflectionUtils.set(player.connection, NMSFields.SERVER_GAME_PACKET_LISTENER_IMPL_clientIsFloating, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forceSpawn(BaseEntity<?> entity, Player player) {
|
||||
if (player != null) {
|
||||
if (entity.getData() instanceof BukkitEntityData bukkitEntityData) {
|
||||
bukkitEntityData.getTracked().sendPairingData(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forceDespawn(BaseEntity<?> entity, Player player) {
|
||||
if (player != null) {
|
||||
NetworkUtils.send(player.getUniqueId(), new ClientboundRemoveEntitiesPacket(new int[]{entity.getEntityId()}));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setForcedInvisible(Entity entity, boolean flag) {
|
||||
if (this.isForcedInvisible(entity) != flag) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
byte data = 0;
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
data = TMath.setBit(data, i, nms.getSharedFlag(i));
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
this.forceInvisible.add(entity.getUniqueId());
|
||||
data = TMath.setBit(data, 5, true);
|
||||
} else {
|
||||
this.forceInvisible.remove(entity.getUniqueId());
|
||||
}
|
||||
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(entity.getEntityId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, data);
|
||||
buf.writeByte(255);
|
||||
ClientboundSetEntityDataPacket packet = new ClientboundSetEntityDataPacket(buf);
|
||||
NetworkUtils.send(ServerInfo.getOnlinePlayers(), packet);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isForcedInvisible(UUID uuid) {
|
||||
return this.forceInvisible.contains(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearForcedInvisible(UUID uuid) {
|
||||
this.forceInvisible.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyRotationController wrapBodyRotationControl(Entity entity, Supplier<BodyRotationController> def) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
BodyRotationControlWrapper var5 = new BodyRotationControlWrapper(mob);
|
||||
return (BodyRotationController)(ReflectionUtils.set(mob, NMSFields.MOB_bodyRotationControl, var5) ? var5 : def.get());
|
||||
} else {
|
||||
return def.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MoveController wrapMoveController(Entity entity, Supplier<MoveController> def) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
if (mob.getMoveControl() instanceof MoveController controller) {
|
||||
return controller;
|
||||
} else {
|
||||
MoveControlWrapper controller = new MoveControlWrapper(mob, mob.getMoveControl());
|
||||
return (MoveController)(ReflectionUtils.set(mob, NMSFields.MOB_moveControl, controller) ? controller : def.get());
|
||||
}
|
||||
} else {
|
||||
return def.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LookController wrapLookController(Entity entity, Supplier<LookController> def) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
if (mob.getLookControl() instanceof LookController controller) {
|
||||
return controller;
|
||||
} else {
|
||||
LookControlWrapper controller = new LookControlWrapper(mob, mob.getLookControl());
|
||||
return (LookController)(ReflectionUtils.set(mob, NMSFields.MOB_lookControl, controller) ? controller : def.get());
|
||||
}
|
||||
} else {
|
||||
return def.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wrapNavigation(Entity entity) {
|
||||
if (EntityUtils.nms(entity) instanceof Mob mob) {
|
||||
try {
|
||||
Field navField = ReflectionUtils.getField(NMSFields.MOB_navigation);
|
||||
PathNavigation oldNav = mob.getNavigation();
|
||||
PathNavigation newNav;
|
||||
if (oldNav instanceof WallClimberNavigation wallClimberNavigation) {
|
||||
newNav = new WallClimberNavigationWrapper(mob, wallClimberNavigation);
|
||||
} else if (oldNav instanceof GroundPathNavigation groundPathNavigation) {
|
||||
newNav = new GroundNavigationWrapper(mob, groundPathNavigation);
|
||||
} else if (oldNav instanceof FlyingPathNavigation flyingPathNavigation) {
|
||||
newNav = new FlyingNavigationWrapper(mob, flyingPathNavigation);
|
||||
} else if (oldNav instanceof WaterBoundPathNavigation) {
|
||||
newNav = new WaterBoundNavigationWrapper(mob);
|
||||
} else {
|
||||
if (!(oldNav instanceof AmphibiousPathNavigation amphibiousPathNavigation)) {
|
||||
TLogger.warn("Failed to create custom navigation for " + mob.getType() + ": " + mob.getUUID());
|
||||
TLogger.warn("Reason: Navigation class type is " + oldNav.getClass().getSimpleName() + ".");
|
||||
return;
|
||||
}
|
||||
|
||||
newNav = new AmphibiousNavigationWrapper(mob, amphibiousPathNavigation);
|
||||
}
|
||||
|
||||
navField.set(mob, newNav);
|
||||
GoalSelector goalSelector = (GoalSelector)ReflectionUtils.getField(NMSFields.MOB_goalSelector).get(mob);
|
||||
RaceConditionUtil.wrapConmod(() -> this.replaceNavigation(goalSelector, newNav));
|
||||
} catch (IllegalAccessException var10) {
|
||||
var10.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void replaceNavigation(GoalSelector goalSelector, PathNavigation newNav) {
|
||||
try {
|
||||
for (WrappedGoal wrappedGoal : goalSelector.getAvailableGoals()) {
|
||||
Goal goal = wrappedGoal.getGoal();
|
||||
|
||||
for (Field field : goal.getClass().getDeclaredFields()) {
|
||||
field = ReflectionUtils.getField(goal.getClass(), field.getName());
|
||||
Object f = field.get(goal);
|
||||
if (f instanceof PathNavigation) {
|
||||
field.set(goal, newNav);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException var11) {
|
||||
var11.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public HitboxEntity createHitbox(Location location, ModelBone bone, SubHitbox subHitbox) {
|
||||
ServerLevel level = ((CraftWorld)location.getWorld()).getHandle();
|
||||
HitboxEntityImpl entity = new HitboxEntityImpl(level, bone, subHitbox);
|
||||
entity.queueLocation(new Vector3f().set(location.getX(), location.getY(), location.getZ()));
|
||||
entity.setPos(location.getX(), location.getY(), location.getZ());
|
||||
ModelEngineAPI.setRenderCanceled(entity.getId(), true);
|
||||
Promise.start(entity.getBukkitEntity()).thenRunSync(() -> {
|
||||
level.addFreshEntity(entity);
|
||||
ModelEngineAPI.getInteractionTracker().addHitbox(entity);
|
||||
});
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public HitboxEntity castHitbox(Entity entity) {
|
||||
return EntityUtils.nms(entity) instanceof HitboxEntity hitbox ? hitbox : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hurt(Entity entity, Object source, float amount) {
|
||||
if (source instanceof DamageSource damageSource) {
|
||||
return EntityUtils.nms(entity).hurt(damageSource, amount);
|
||||
} else {
|
||||
throw new RuntimeException("Passed in source is not an NMS DamageSource.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InteractionResult interact(Entity entity, HumanEntity player, EquipmentSlot hand) {
|
||||
if (EntityUtils.nms(entity) instanceof LivingEntity livingEntity) {
|
||||
net.minecraft.world.InteractionResult result = livingEntity.interact(
|
||||
((CraftPlayer)player).getHandle(), hand == EquipmentSlot.HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND
|
||||
);
|
||||
|
||||
return switch (result) {
|
||||
case SUCCESS -> InteractionResult.SUCCESS;
|
||||
case CONSUME -> InteractionResult.CONSUME;
|
||||
case CONSUME_PARTIAL -> InteractionResult.CONSUME_PARTIAL;
|
||||
case PASS -> InteractionResult.PASS;
|
||||
case FAIL -> InteractionResult.FAIL;
|
||||
default -> throw new IncompatibleClassChangeError();
|
||||
};
|
||||
} else {
|
||||
return InteractionResult.FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spawnDynamicHitbox(DynamicHitbox hitbox) {
|
||||
Vector location = hitbox.getPositionTracker().get();
|
||||
final Packets.PacketSupplier pivotSpawn = NetworkUtils.createPivotSpawn(
|
||||
DynamicHitbox.getPivotId(), DynamicHitbox.getPivotUUID(), location.toVector3f().add(0.0F, -0.5202F, 0.0F)
|
||||
);
|
||||
FriendlyByteBuf pivotDataBuf = NetworkUtils.createByteBuf();
|
||||
pivotDataBuf.writeVarInt(DynamicHitbox.getPivotId());
|
||||
EntityUtils.writeData(pivotDataBuf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(pivotDataBuf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
pivotDataBuf.writeByte(255);
|
||||
final ClientboundSetEntityDataPacket pivotData = new ClientboundSetEntityDataPacket(pivotDataBuf);
|
||||
final ClientboundAddEntityPacket hitboxSpawn = new ClientboundAddEntityPacket(
|
||||
DynamicHitbox.getHitboxId(),
|
||||
DynamicHitbox.getHitboxUUID(),
|
||||
location.getX(),
|
||||
location.getY() - 0.5202,
|
||||
location.getZ(),
|
||||
0.0F,
|
||||
0.0F,
|
||||
EntityType.SLIME,
|
||||
0,
|
||||
Vec3.ZERO,
|
||||
0.0
|
||||
);
|
||||
FriendlyByteBuf hitboxDataBuf = NetworkUtils.createByteBuf();
|
||||
hitboxDataBuf.writeVarInt(DynamicHitbox.getHitboxId());
|
||||
EntityUtils.writeData(hitboxDataBuf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(hitboxDataBuf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(hitboxDataBuf, 16, EntityDataSerializers.INT, 2);
|
||||
hitboxDataBuf.writeByte(255);
|
||||
final ClientboundSetEntityDataPacket hitboxData = new ClientboundSetEntityDataPacket(hitboxDataBuf);
|
||||
FriendlyByteBuf mountBuf = NetworkUtils.createByteBuf();
|
||||
mountBuf.writeVarInt(DynamicHitbox.getPivotId());
|
||||
mountBuf.writeVarInt(1);
|
||||
mountBuf.writeVarInt(DynamicHitbox.getHitboxId());
|
||||
final ClientboundSetPassengersPacket mount = new ClientboundSetPassengersPacket(mountBuf);
|
||||
NetworkUtils.sendBundled(Set.of(hitbox.getPlayer().getUniqueId()), new Packets() {
|
||||
{
|
||||
this.add(pivotSpawn);
|
||||
this.add(pivotData);
|
||||
this.add(hitboxSpawn);
|
||||
this.add(hitboxData);
|
||||
this.add(mount);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDynamicHitbox(DynamicHitbox hitbox) {
|
||||
Vector3f vector = hitbox.getPositionTracker().get().toVector3f().add(0.0F, -0.5202F, 0.0F);
|
||||
NetworkUtils.send(
|
||||
hitbox.getPlayer().getUniqueId(), NetworkUtils.createPivotTeleport(DynamicHitbox.getPivotId(), vector).supply(hitbox.getPlayer().getUniqueId())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyDynamicHitbox(DynamicHitbox hitbox) {
|
||||
ClientboundRemoveEntitiesPacket destroy = new ClientboundRemoveEntitiesPacket(new int[]{DynamicHitbox.getHitboxId(), DynamicHitbox.getPivotId()});
|
||||
NetworkUtils.send(hitbox.getPlayer().getUniqueId(), destroy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forceUseItem(Player player, EquipmentSlot hand) {
|
||||
ItemStack stack = player.getEquipment().getItem(hand);
|
||||
net.minecraft.world.item.ItemStack nmsStack = ((CraftItemStack)stack).handle;
|
||||
ServerPlayer nmsPlayer = (ServerPlayer)EntityUtils.nms(player);
|
||||
ServerGamePacketListenerImpl connection = nmsPlayer.connection;
|
||||
ServerboundUseItemPacket useItemPacket = new ServerboundUseItemPacket(
|
||||
hand == EquipmentSlot.HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND, 0
|
||||
);
|
||||
useItemPacket.timestamp = System.currentTimeMillis();
|
||||
PatchedServerGamePacketListener.handleUseItem(
|
||||
useItemPacket,
|
||||
connection,
|
||||
interactionResult -> {
|
||||
if (nmsStack.getUseAnimation() != UseAnim.NONE && interactionResult == net.minecraft.world.InteractionResult.CONSUME) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(player.getEntityId());
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.BYTE, (byte)(hand == EquipmentSlot.HAND ? 1 : 3));
|
||||
buf.writeByte(255);
|
||||
ClientboundSetEntityDataPacket packet = new ClientboundSetEntityDataPacket(buf);
|
||||
NetworkUtils.send(player.getUniqueId(), packet);
|
||||
if (nmsStack.getItem() instanceof InstrumentItem instrumentItem) {
|
||||
Optional<? extends Holder<Instrument>> optional = ReflectionUtils.call(instrumentItem, NMSMethods.INSTRUMENT_ITEM_getInstrument, nmsStack);
|
||||
optional.ifPresent(
|
||||
instrumentHolder -> {
|
||||
Instrument instrument = (Instrument)instrumentHolder.value();
|
||||
Holder<SoundEvent> soundEvent = instrument.soundEvent();
|
||||
float f = instrument.range() / 16.0F;
|
||||
RandomSource random = ReflectionUtils.get(nmsPlayer.level(), NMSFields.LEVEL_threadSafeRandom);
|
||||
ClientboundSoundEntityPacket soundPacket = new ClientboundSoundEntityPacket(
|
||||
soundEvent, SoundSource.RECORDS, nmsPlayer, f, 1.0F, random.nextLong()
|
||||
);
|
||||
NetworkUtils.send(player.getUniqueId(), soundPacket);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYRot(Entity entity) {
|
||||
return EntityUtils.nms(entity).getYRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYHeadRot(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
return nms instanceof LivingEntity livingEntity ? livingEntity.getYHeadRot() : nms.getYRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getXHeadRot(Entity entity) {
|
||||
return EntityUtils.nms(entity).getXRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYBodyRot(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
return nms instanceof LivingEntity livingEntity ? livingEntity.yBodyRot : nms.getYRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYRot(Entity entity, float angle) {
|
||||
EntityUtils.nms(entity).setYRot(angle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYHeadRot(Entity entity, float angle) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
if (nms instanceof LivingEntity livingEntity) {
|
||||
livingEntity.setYHeadRot(angle);
|
||||
} else {
|
||||
nms.setYRot(angle);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setXHeadRot(Entity entity, float angle) {
|
||||
EntityUtils.nms(entity).setXRot(angle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYBodyRot(Entity entity, float angle) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
if (nms instanceof LivingEntity livingEntity) {
|
||||
livingEntity.setYBodyRot(angle);
|
||||
} else {
|
||||
nms.setYRot(angle);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(Entity entity, double x, double y, double z) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
nms.move(MoverType.SELF, new Vec3(x, y, z));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWalking(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
if (nms.tickCount < 1) {
|
||||
return false;
|
||||
} else {
|
||||
double dX = nms.getX() - nms.xOld;
|
||||
double dZ = nms.getZ() - nms.zOld;
|
||||
return dX * dX + dZ * dZ > 2.5000003E-7F;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStrafing(Entity entity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isJumping(Entity entity) {
|
||||
if (JUMPING_GETTER != null && EntityUtils.nms(entity) instanceof LivingEntity livingEntity) {
|
||||
try {
|
||||
return (boolean)JUMPING_GETTER.invokeExact((LivingEntity)livingEntity);
|
||||
} catch (Throwable var4) {
|
||||
var4.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFlying(Entity entity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getHealth(Entity entity) {
|
||||
return EntityUtils.nms(entity) instanceof LivingEntity livingEntity ? livingEntity.getHealth() : 20.0F;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMaxHealth(Entity entity) {
|
||||
return EntityUtils.nms(entity) instanceof LivingEntity livingEntity ? livingEntity.getMaxHealth() : 20.0F;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector3f getPivotOffset(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nmsEntity = EntityUtils.nms(entity);
|
||||
if (this.dummyCloud == null) {
|
||||
this.dummyCloud = new AreaEffectCloud(EntityType.AREA_EFFECT_CLOUD, nmsEntity.level());
|
||||
}
|
||||
|
||||
Vec3 offset = ReflectionUtils.call(
|
||||
nmsEntity, NMSMethods.ENTITY_getPassengerAttachmentPoint, this.dummyCloud, nmsEntity.getDimensions(Pose.STANDING), 1.0F
|
||||
);
|
||||
Vec3 offset2 = this.dummyCloud.getPassengerRidingPosition(this.dummyCloud);
|
||||
return offset == null ? new Vector3f() : offset.add(offset2).toVector3f();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRemoved(Entity entity) {
|
||||
return EntityUtils.nms(entity).isRemoved();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getGlowColor(Entity entity) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
return nms.getTeamColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDeathTick(Entity entity, int tick) {
|
||||
if (EntityUtils.nms(entity) instanceof LivingEntity livingEntity) {
|
||||
livingEntity.deathTime = tick;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBoundingBox(Entity entity, BoundingBox dest) {
|
||||
AABB nms = EntityUtils.nms(entity).getBoundingBox();
|
||||
dest.resize(nms.minX, nms.minY, nms.minZ, nms.maxX, nms.maxY, nms.maxZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public com.ticxo.modelengine.api.nms.entity.wrapper.TrackedEntity wrapTrackedEntity(Entity entity) {
|
||||
ServerLevel level = ((CraftWorld)entity.getWorld()).getHandle();
|
||||
Int2ObjectMap<TrackedEntity> map = level.getChunkSource().chunkMap.entityMap;
|
||||
int entityId = entity.getEntityId();
|
||||
TrackedEntity tracker = (TrackedEntity)map.get(entityId);
|
||||
return (com.ticxo.modelengine.api.nms.entity.wrapper.TrackedEntity)(tracker == null
|
||||
? new TempTrackedEntity(entity)
|
||||
: new TrackedEntityImpl(entity, () -> {
|
||||
ServerLevel lvl = ((CraftWorld)entity.getWorld()).getHandle();
|
||||
Int2ObjectMap<TrackedEntity> m = lvl.getChunkSource().chunkMap.entityMap;
|
||||
return m == null ? null : (TrackedEntity)m.get(entity.getEntityId());
|
||||
}, tracker));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldCull(Player player, Location eyePosition, BoundingBox box) {
|
||||
CraftWorld world = (CraftWorld)player.getWorld();
|
||||
Vec3 start = CraftLocation.toVec3D(eyePosition);
|
||||
if (!(box.getWidthX() >= this.forceRenderWidth) && !(box.getWidthZ() >= this.forceRenderWidth) && !(box.getHeight() >= this.forceRenderHeight)) {
|
||||
int minX = Mth.floor(box.getMinX());
|
||||
int minY = Mth.floor(box.getMinY());
|
||||
int minZ = Mth.floor(box.getMinZ());
|
||||
int maxX = Mth.ceil(box.getMaxX()) - 1;
|
||||
int maxY = Mth.ceil(box.getMaxY()) - 1;
|
||||
int maxZ = Mth.ceil(box.getMaxZ()) - 1;
|
||||
BoxRelToCam relX = BoxRelToCam.from(minX, maxX, Mth.floor(start.x));
|
||||
BoxRelToCam relY = BoxRelToCam.from(minY, maxY, Mth.floor(start.y));
|
||||
BoxRelToCam relZ = BoxRelToCam.from(minZ, maxZ, Mth.floor(start.z));
|
||||
if (relX == BoxRelToCam.INSIDE && relY == BoxRelToCam.INSIDE && relZ == BoxRelToCam.INSIDE) {
|
||||
return false;
|
||||
} else {
|
||||
if (DebugToggle.isDebugging(DebugToggle.SHOW_CULL_POINTS)) {
|
||||
EntityHandler.sweepCullPoints(minX, minY, minZ, maxX, maxY, maxZ, relX, relY, relZ, (x, y, z) -> {
|
||||
world.spawnParticle(Particle.REDSTONE, x, y, z, 1, new DustOptions(Color.RED, 0.2F));
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
return !EntityHandler.sweepCullPoints(minX, minY, minZ, maxX, maxY, maxZ, relX, relY, relZ, (x, y, z) -> isVisible(world, start, new Vec3(x, y, z)));
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isVisible(CraftWorld world, Vec3 startPos, Vec3 endPos) {
|
||||
if (!usePaperClipMethod) {
|
||||
return OcclusionClipContext.isClear(world.getHandle(), startPos, endPos);
|
||||
} else {
|
||||
BlockHitResult nmsHitResult = world.getHandle().clip(new OcclusionClipContext(startPos, endPos));
|
||||
return nmsHitResult.getType() == Type.MISS;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readEntityState(Entity entity, EntityState state) {
|
||||
net.minecraft.world.entity.Entity nms = EntityUtils.nms(entity);
|
||||
state.yRot = nms.getYRot();
|
||||
state.xHeadRot = nms.getXRot();
|
||||
if (nms instanceof LivingEntity living) {
|
||||
state.yHeadRot = living.getYHeadRot();
|
||||
state.yBodyRot = living.yBodyRot;
|
||||
state.health = living.getHealth();
|
||||
} else {
|
||||
state.yHeadRot = state.yRot;
|
||||
state.yBodyRot = state.yRot;
|
||||
state.health = 20.0F;
|
||||
}
|
||||
|
||||
boolean walking = false;
|
||||
if (nms.tickCount >= 1) {
|
||||
double dX = nms.getX() - nms.xOld;
|
||||
double dZ = nms.getZ() - nms.zOld;
|
||||
walking = dX * dX + dZ * dZ > 2.5000003E-7F;
|
||||
}
|
||||
|
||||
state.walking = walking;
|
||||
state.strafing = false;
|
||||
state.flying = false;
|
||||
state.removed = nms.isRemoved();
|
||||
state.jumping = this.isJumping(entity);
|
||||
state.glowing = this.isGlowing(entity);
|
||||
state.onFire = this.isVisualFire(entity);
|
||||
this.getBoundingBox(entity, state.boundingBox);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity;
|
||||
|
||||
public class EntityMetadataID {
|
||||
public static final int ENTITY_SHARED_DATA = 0;
|
||||
public static final int ENTITY_AIR = 1;
|
||||
public static final int ENTITY_CUSTOM_NAME = 2;
|
||||
public static final int ENTITY_CUSTOM_NAME_VISIBLE = 3;
|
||||
public static final int INTERACTION_WIDTH = 8;
|
||||
public static final int INTERACTION_HEIGHT = 9;
|
||||
public static final int INTERACTION_RESPONSE = 10;
|
||||
public static final int DISPLAY_INTERPOLATION_DELAY = 8;
|
||||
public static final int DISPLAY_TRANSFORM_DURATION = 9;
|
||||
public static final int DISPLAY_TRANSLATION = 11;
|
||||
public static final int DISPLAY_SCALE = 12;
|
||||
public static final int DISPLAY_ROTATION_LEFT = 13;
|
||||
public static final int DISPLAY_ROTATION_RIGHT = 14;
|
||||
public static final int DISPLAY_BILLBOARD = 15;
|
||||
public static final int DISPLAY_BRIGHTNESS = 16;
|
||||
public static final int DISPLAY_VIEW_RANGE = 17;
|
||||
public static final int DISPLAY_SHADOW_RADIUS = 18;
|
||||
public static final int DISPLAY_WIDTH = 20;
|
||||
public static final int DISPLAY_HEIGHT = 21;
|
||||
public static final int DISPLAY_GLOW_COLOR = 22;
|
||||
public static final int ITEM_DISPLAY_ITEM = 23;
|
||||
public static final int ITEM_DISPLAY_TYPE = 24;
|
||||
public static final int TEXT_DISPLAY_TEXT = 23;
|
||||
public static final int TEXT_LINE_WIDTH = 24;
|
||||
public static final int TEXT_DISPLAY_BACKGROUND = 25;
|
||||
public static final int TEXT_DISPLAY_OPACITY = 26;
|
||||
public static final int TEXT_DISPLAY_OPTIONS = 27;
|
||||
public static final int AREA_EFFECT_CLOUD_RADIUS = 8;
|
||||
public static final int LIVING_ENTITY_HAND_DATA = 8;
|
||||
public static final int LIVING_ENTITY_HEALTH = 9;
|
||||
public static final int ARMOR_STAND_SHARED_DATA = 15;
|
||||
public static final int SLIME_SIZE = 16;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity;
|
||||
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.syncher.EntityDataSerializer;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftEntity;
|
||||
|
||||
public class EntityUtils {
|
||||
public static Entity nms(org.bukkit.entity.Entity entity) {
|
||||
return ((CraftEntity)entity).getHandleRaw();
|
||||
}
|
||||
|
||||
public static <T> void writeData(FriendlyByteBuf byteBuf, int id, EntityDataSerializer<T> serializer, T val) {
|
||||
int serializedId = EntityDataSerializers.getSerializedId(serializer);
|
||||
byteBuf.writeByte(id);
|
||||
byteBuf.writeVarInt(serializedId);
|
||||
serializer.write(byteBuf, val);
|
||||
}
|
||||
|
||||
public static void writeDataUnsafe(FriendlyByteBuf byteBuf, int id, EntityDataSerializer serializer, Object val) {
|
||||
int serializedId = EntityDataSerializers.getSerializedId(serializer);
|
||||
byteBuf.writeByte(id);
|
||||
byteBuf.writeVarInt(serializedId);
|
||||
serializer.write(byteBuf, val);
|
||||
}
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.BlockPos.MutableBlockPos;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.ClipContext;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.ClipContext.Block;
|
||||
import net.minecraft.world.level.ClipContext.Fluid;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.ChunkAccess;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraft.world.phys.shapes.Shapes;
|
||||
import net.minecraft.world.phys.shapes.VoxelShape;
|
||||
|
||||
public class OcclusionClipContext extends ClipContext {
|
||||
public OcclusionClipContext(Vec3 start, Vec3 end) {
|
||||
super(start, end, Block.VISUAL, Fluid.NONE, null);
|
||||
}
|
||||
|
||||
public VoxelShape getBlockShape(BlockState state, BlockGetter world, BlockPos pos) {
|
||||
return occlusionShape(state, world, pos);
|
||||
}
|
||||
|
||||
private static VoxelShape occlusionShape(BlockState state, BlockGetter world, BlockPos pos) {
|
||||
return state.isOpaque() && state.getOcclusionShape(world, pos) == Shapes.block() ? Shapes.block() : Shapes.empty();
|
||||
}
|
||||
|
||||
static boolean isClear(Level level, Vec3 from, Vec3 to) {
|
||||
if (from.equals(to)) {
|
||||
return true;
|
||||
} else {
|
||||
double toX = Mth.lerp(-1.0E-7, to.x, from.x);
|
||||
double toY = Mth.lerp(-1.0E-7, to.y, from.y);
|
||||
double toZ = Mth.lerp(-1.0E-7, to.z, from.z);
|
||||
double fromX = Mth.lerp(-1.0E-7, from.x, to.x);
|
||||
double fromY = Mth.lerp(-1.0E-7, from.y, to.y);
|
||||
double fromZ = Mth.lerp(-1.0E-7, from.z, to.z);
|
||||
int x = Mth.floor(fromX);
|
||||
int y = Mth.floor(fromY);
|
||||
int z = Mth.floor(fromZ);
|
||||
double dx = toX - fromX;
|
||||
double dy = toY - fromY;
|
||||
double dz = toZ - fromZ;
|
||||
int signX = Mth.sign(dx);
|
||||
int signY = Mth.sign(dy);
|
||||
int signZ = Mth.sign(dz);
|
||||
double tDeltaX = signX == 0 ? Double.MAX_VALUE : signX / dx;
|
||||
double tDeltaY = signY == 0 ? Double.MAX_VALUE : signY / dy;
|
||||
double tDeltaZ = signZ == 0 ? Double.MAX_VALUE : signZ / dz;
|
||||
double tX = tDeltaX * (signX > 0 ? 1.0 - Mth.frac(fromX) : Mth.frac(fromX));
|
||||
double tY = tDeltaY * (signY > 0 ? 1.0 - Mth.frac(fromY) : Mth.frac(fromY));
|
||||
double tZ = tDeltaZ * (signZ > 0 ? 1.0 - Mth.frac(fromZ) : Mth.frac(fromZ));
|
||||
MutableBlockPos pos = new MutableBlockPos(x, y, z);
|
||||
ChunkAccess chunk = null;
|
||||
int chunkX = 0;
|
||||
int chunkZ = 0;
|
||||
|
||||
while (true) {
|
||||
pos.set(x, y, z);
|
||||
BlockState state;
|
||||
if (level.captureTreeGeneration) {
|
||||
state = level.getBlockStateIfLoaded(pos);
|
||||
} else if (level.isOutsideBuildHeight(pos)) {
|
||||
state = Blocks.VOID_AIR.defaultBlockState();
|
||||
} else {
|
||||
if (chunk == null || x >> 4 != chunkX || z >> 4 != chunkZ) {
|
||||
chunkX = x >> 4;
|
||||
chunkZ = z >> 4;
|
||||
chunk = level.getChunkIfLoadedImmediately(chunkX, chunkZ);
|
||||
}
|
||||
|
||||
state = chunk == null ? null : chunk.getBlockState(pos);
|
||||
}
|
||||
|
||||
if (state == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!state.isAir()) {
|
||||
VoxelShape shape = occlusionShape(state, level, pos);
|
||||
if (!shape.isEmpty()) {
|
||||
BlockHitResult hit = level.clipWithInteractionOverride(from, to, pos, shape, state);
|
||||
if (hit != null && from.distanceToSqr(hit.getLocation()) <= Double.MAX_VALUE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (tX > 1.0 && tY > 1.0 && tZ > 1.0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (tX < tY) {
|
||||
if (tX < tZ) {
|
||||
x += signX;
|
||||
tX += tDeltaX;
|
||||
} else {
|
||||
z += signZ;
|
||||
tZ += tDeltaZ;
|
||||
}
|
||||
} else if (tY < tZ) {
|
||||
y += signY;
|
||||
tY += tDeltaY;
|
||||
} else {
|
||||
z += signZ;
|
||||
tZ += tDeltaZ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+286
@@ -0,0 +1,286 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.NMSFields;
|
||||
import com.ticxo.modelengine.v1_20_R2.NMSMethods;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.NetworkUtils;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.level.ChunkMap.TrackedEntity;
|
||||
import net.minecraft.server.network.ServerPlayerConnection;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TrackedEntityImpl implements com.ticxo.modelengine.api.nms.entity.wrapper.TrackedEntity {
|
||||
private final Entity entity;
|
||||
private final Supplier<TrackedEntity> trackedEntitySupplier;
|
||||
private final Set<UUID> forcedPairing = Sets.newConcurrentHashSet();
|
||||
private final Set<UUID> forcedRemove = Sets.newConcurrentHashSet();
|
||||
private Predicate<Player> playerPredicate = DEFAULT_PREDICATE;
|
||||
@NotNull
|
||||
private TrackedEntity lastTrackedEntity;
|
||||
private static final int RECONCILE_INTERVAL = 20;
|
||||
private Object[] lastSeenBy;
|
||||
private volatile boolean viewersDirty = true;
|
||||
private volatile boolean mayHaveForced;
|
||||
private int ticksSinceReconcile;
|
||||
private final int initialBaseRange;
|
||||
|
||||
public TrackedEntityImpl(Entity entity, Supplier<TrackedEntity> trackedEntitySupplier, @NotNull TrackedEntity lastTrackedEntity) {
|
||||
this.entity = entity;
|
||||
this.trackedEntitySupplier = trackedEntitySupplier;
|
||||
this.lastTrackedEntity = lastTrackedEntity;
|
||||
this.initialBaseRange = this.getBaseRange();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInitialBaseRange() {
|
||||
return this.initialBaseRange;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBaseRange() {
|
||||
Integer range = ReflectionUtils.get(this.getTrackedEntity(), NMSFields.TRACKED_ENTITY_range);
|
||||
if (range == null) {
|
||||
throw new NullPointerException(String.format("Unable to retrieve base range of entity with UUID %s.", this.entity.getUniqueId()));
|
||||
} else {
|
||||
return range;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBaseRange(int range) {
|
||||
ReflectionUtils.set(this.getTrackedEntity(), NMSFields.TRACKED_ENTITY_range, range);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEffectiveRange() {
|
||||
Integer range = ReflectionUtils.call(this.getTrackedEntity(), NMSMethods.TRACKED_ENTITY_getEffectiveRange);
|
||||
if (range == null) {
|
||||
throw new NullPointerException(String.format("Unable to retrieve range of entity with UUID %s.", this.entity.getUniqueId()));
|
||||
} else {
|
||||
return range;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getTrackedPlayer() {
|
||||
boolean hasForced = !this.forcedPairing.isEmpty();
|
||||
boolean hasRemoved = !this.forcedRemove.isEmpty();
|
||||
Set<ServerPlayerConnection> seenBy = this.getTrackedEntity().seenBy;
|
||||
HashSet<UUID> set = new HashSet<>(Math.max(8, (int)((seenBy.size() + (hasForced ? this.forcedPairing.size() : 0)) / 0.75F) + 1));
|
||||
if (hasForced) {
|
||||
set.addAll(this.forcedPairing);
|
||||
}
|
||||
|
||||
for (ServerPlayerConnection connection : seenBy) {
|
||||
Player player = connection.getPlayer().getBukkitEntity();
|
||||
if (player != null && this.playerPredicate.test(player) && (!hasRemoved || !this.forcedRemove.contains(player.getUniqueId()))) {
|
||||
set.add(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getTrackedPlayer(Predicate<Player> predicate) {
|
||||
boolean hasForced = !this.forcedPairing.isEmpty();
|
||||
boolean hasRemoved = !this.forcedRemove.isEmpty();
|
||||
Set<ServerPlayerConnection> seenBy = this.getTrackedEntity().seenBy;
|
||||
HashSet<UUID> set = new HashSet<>(Math.max(8, (int)((seenBy.size() + (hasForced ? this.forcedPairing.size() : 0)) / 0.75F) + 1));
|
||||
if (hasForced) {
|
||||
set.addAll(this.forcedPairing);
|
||||
}
|
||||
|
||||
for (ServerPlayerConnection connection : seenBy) {
|
||||
Player player = connection.getPlayer().getBukkitEntity();
|
||||
if (player != null
|
||||
&& predicate.test(player)
|
||||
&& this.playerPredicate.test(player)
|
||||
&& (!hasRemoved || !this.forcedRemove.contains(player.getUniqueId()))) {
|
||||
set.add(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getTrackedPlayer(Predicate<Player> predicate, Set<UUID> dest) {
|
||||
dest.clear();
|
||||
boolean hasForced = !this.forcedPairing.isEmpty();
|
||||
boolean hasRemoved = !this.forcedRemove.isEmpty();
|
||||
Set<ServerPlayerConnection> seenBy = this.getTrackedEntity().seenBy;
|
||||
if (hasForced) {
|
||||
dest.addAll(this.forcedPairing);
|
||||
}
|
||||
|
||||
for (ServerPlayerConnection connection : seenBy) {
|
||||
Player player = connection.getPlayer().getBukkitEntity();
|
||||
if (player != null
|
||||
&& predicate.test(player)
|
||||
&& this.playerPredicate.test(player)
|
||||
&& (!hasRemoved || !this.forcedRemove.contains(player.getUniqueId()))) {
|
||||
dest.add(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isViewerSetUnchanged() {
|
||||
if ((!this.mayHaveForced || this.forcedPairing.isEmpty() && this.forcedRemove.isEmpty()) && this.playerPredicate == DEFAULT_PREDICATE) {
|
||||
if (!this.viewersDirty && ++this.ticksSinceReconcile < 20) {
|
||||
return true;
|
||||
} else {
|
||||
this.viewersDirty = false;
|
||||
this.ticksSinceReconcile = 0;
|
||||
return this.snapshotUnchanged(this.getTrackedEntity().seenBy);
|
||||
}
|
||||
} else {
|
||||
this.lastSeenBy = null;
|
||||
this.viewersDirty = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markViewersDirty() {
|
||||
this.viewersDirty = true;
|
||||
}
|
||||
|
||||
private boolean snapshotUnchanged(Collection<?> viewers) {
|
||||
int size = viewers.size();
|
||||
Object[] previous = this.lastSeenBy;
|
||||
if (previous != null && previous.length == size) {
|
||||
int index = 0;
|
||||
boolean same = true;
|
||||
|
||||
for (Object viewer : viewers) {
|
||||
if (index >= size || previous[index] != viewer) {
|
||||
same = false;
|
||||
break;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
if (same && index == size) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Object[] snapshot = new Object[size];
|
||||
int index = 0;
|
||||
|
||||
for (Object viewer : viewers) {
|
||||
if (index >= size) {
|
||||
break;
|
||||
}
|
||||
|
||||
snapshot[index++] = viewer;
|
||||
}
|
||||
|
||||
this.lastSeenBy = index == size ? snapshot : Arrays.copyOf(snapshot, index);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPairingData(Player player) {
|
||||
if (player != null) {
|
||||
ServerPlayer nms = ((CraftPlayer)player).getHandle();
|
||||
this.getTrackedEntity().serverEntity.sendPairingData(nms, packet -> NetworkUtils.send(player.getUniqueId(), packet));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcastSpawn() {
|
||||
for (UUID player : this.getTrackedPlayer()) {
|
||||
this.sendPairingData(Bukkit.getPlayer(player));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcastRemove() {
|
||||
this.getTrackedEntity().broadcastRemoved();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addForcedPairing(UUID uuid) {
|
||||
this.mayHaveForced = true;
|
||||
this.forcedPairing.add(uuid);
|
||||
this.removeForcedHidden(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeForcedPairing(UUID uuid) {
|
||||
this.forcedPairing.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearForcedPairing() {
|
||||
this.forcedPairing.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addForcedHidden(UUID uuid) {
|
||||
this.mayHaveForced = true;
|
||||
this.forcedRemove.add(uuid);
|
||||
this.removeForcedPairing(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeForcedHidden(UUID uuid) {
|
||||
this.forcedRemove.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearForcedHidden() {
|
||||
this.forcedRemove.clear();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private TrackedEntity getTrackedEntity() {
|
||||
TrackedEntity trackedEntity = this.trackedEntitySupplier.get();
|
||||
if (trackedEntity != null && this.lastTrackedEntity != trackedEntity) {
|
||||
this.syncInstance(trackedEntity);
|
||||
this.lastTrackedEntity = trackedEntity;
|
||||
}
|
||||
|
||||
return this.lastTrackedEntity;
|
||||
}
|
||||
|
||||
private void syncInstance(TrackedEntity target) {
|
||||
Integer range = ReflectionUtils.get(this.lastTrackedEntity, NMSFields.TRACKED_ENTITY_range);
|
||||
if (range != null) {
|
||||
ReflectionUtils.set(target, NMSFields.TRACKED_ENTITY_range, range);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity getEntity() {
|
||||
return this.entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<Player> getPlayerPredicate() {
|
||||
return this.playerPredicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayerPredicate(Predicate<Player> playerPredicate) {
|
||||
this.playerPredicate = playerPredicate;
|
||||
}
|
||||
}
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity.controller;
|
||||
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.BodyRotationController;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.control.BodyRotationControl;
|
||||
|
||||
public class BodyRotationControlWrapper extends BodyRotationControl implements BodyRotationController {
|
||||
private final Mob mob;
|
||||
private boolean isHeadClampUneven;
|
||||
private boolean isBodyClampUneven;
|
||||
private float maxHeadAngle;
|
||||
private float maxBodyAngle;
|
||||
private float minHeadAngle;
|
||||
private float minBodyAngle;
|
||||
private boolean playerMode;
|
||||
private float stableAngle = 15.0F;
|
||||
private int rotationDelay = 10;
|
||||
private int rotationDuration = 10;
|
||||
private int headStableTime;
|
||||
private float lastStableYHeadRot;
|
||||
|
||||
public BodyRotationControlWrapper(Mob mob) {
|
||||
super(mob);
|
||||
this.mob = mob;
|
||||
this.maxHeadAngle = mob.getMaxHeadYRot();
|
||||
this.maxBodyAngle = mob.getMaxHeadYRot();
|
||||
this.minHeadAngle = -this.maxHeadAngle;
|
||||
this.minBodyAngle = -this.maxBodyAngle;
|
||||
}
|
||||
|
||||
public void clientTick() {
|
||||
if (!this.getMob().isDeadOrDying()) {
|
||||
if (this.isMoving()) {
|
||||
this.mob.yBodyRot = this.mob.getYRot();
|
||||
this.rotateHeadIfNecessary();
|
||||
this.lastStableYHeadRot = this.mob.yHeadRot;
|
||||
this.headStableTime = 0;
|
||||
} else if (this.notCarryingMobPassengers()) {
|
||||
if (Math.abs(this.mob.yHeadRot - this.lastStableYHeadRot) > this.stableAngle) {
|
||||
this.headStableTime = 0;
|
||||
this.lastStableYHeadRot = this.mob.yHeadRot;
|
||||
this.rotateBodyIfNecessary();
|
||||
} else if (!this.playerMode) {
|
||||
this.headStableTime++;
|
||||
if (this.headStableTime > this.rotationDelay) {
|
||||
this.rotateHeadTowardsFront();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYHeadRot() {
|
||||
return this.mob.yHeadRot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYHeadRot(float rot) {
|
||||
this.mob.yHeadRot = rot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getXHeadRot() {
|
||||
return this.mob.getXRot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setXHeadRot(float rot) {
|
||||
this.mob.setXRot(rot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYBodyRot() {
|
||||
return this.mob.yBodyRot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setYBodyRot(float rot) {
|
||||
this.mob.yBodyRot = rot;
|
||||
}
|
||||
|
||||
private void rotateBodyIfNecessary() {
|
||||
this.mob.yBodyRot = TMath.rotateIfNecessary(
|
||||
this.mob.yBodyRot, this.mob.yHeadRot, this.isBodyClampUneven ? this.minBodyAngle : -this.maxBodyAngle, this.maxBodyAngle
|
||||
);
|
||||
}
|
||||
|
||||
private void rotateHeadIfNecessary() {
|
||||
this.mob.yHeadRot = TMath.rotateIfNecessary(
|
||||
this.mob.yHeadRot, this.mob.yBodyRot, this.isHeadClampUneven ? this.minHeadAngle : -this.maxHeadAngle, this.maxHeadAngle
|
||||
);
|
||||
}
|
||||
|
||||
private void rotateHeadTowardsFront() {
|
||||
float ratio = (float)(this.headStableTime - this.rotationDelay) / this.rotationDuration;
|
||||
float clampedRatio = TMath.clamp(ratio, 0.0F, 1.0F);
|
||||
float maxClamp = this.maxHeadAngle * (1.0F - clampedRatio);
|
||||
float minClamp = (this.isHeadClampUneven ? this.minHeadAngle : -this.maxHeadAngle) * (1.0F - clampedRatio);
|
||||
this.mob.yBodyRot = TMath.rotateIfNecessary(this.mob.yBodyRot, this.mob.yHeadRot, minClamp, maxClamp);
|
||||
}
|
||||
|
||||
private boolean notCarryingMobPassengers() {
|
||||
return !(this.mob.getFirstPassenger() instanceof Mob);
|
||||
}
|
||||
|
||||
private boolean isMoving() {
|
||||
double dX = this.mob.getX() - this.mob.xo;
|
||||
double dZ = this.mob.getZ() - this.mob.zo;
|
||||
return dX * dX + dZ * dZ > 2.5000003E-7F;
|
||||
}
|
||||
|
||||
public Mob getMob() {
|
||||
return this.mob;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isHeadClampUneven() {
|
||||
return this.isHeadClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBodyClampUneven() {
|
||||
return this.isBodyClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMaxHeadAngle() {
|
||||
return this.maxHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMaxBodyAngle() {
|
||||
return this.maxBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMinHeadAngle() {
|
||||
return this.minHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMinBodyAngle() {
|
||||
return this.minBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlayerMode() {
|
||||
return this.playerMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getStableAngle() {
|
||||
return this.stableAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRotationDelay() {
|
||||
return this.rotationDelay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRotationDuration() {
|
||||
return this.rotationDuration;
|
||||
}
|
||||
|
||||
public int getHeadStableTime() {
|
||||
return this.headStableTime;
|
||||
}
|
||||
|
||||
public float getLastStableYHeadRot() {
|
||||
return this.lastStableYHeadRot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeadClampUneven(boolean isHeadClampUneven) {
|
||||
this.isHeadClampUneven = isHeadClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBodyClampUneven(boolean isBodyClampUneven) {
|
||||
this.isBodyClampUneven = isBodyClampUneven;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxHeadAngle(float maxHeadAngle) {
|
||||
this.maxHeadAngle = maxHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxBodyAngle(float maxBodyAngle) {
|
||||
this.maxBodyAngle = maxBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMinHeadAngle(float minHeadAngle) {
|
||||
this.minHeadAngle = minHeadAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMinBodyAngle(float minBodyAngle) {
|
||||
this.minBodyAngle = minBodyAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayerMode(boolean playerMode) {
|
||||
this.playerMode = playerMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStableAngle(float stableAngle) {
|
||||
this.stableAngle = stableAngle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRotationDelay(int rotationDelay) {
|
||||
this.rotationDelay = rotationDelay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRotationDuration(int rotationDuration) {
|
||||
this.rotationDuration = rotationDuration;
|
||||
}
|
||||
|
||||
public void setHeadStableTime(int headStableTime) {
|
||||
this.headStableTime = headStableTime;
|
||||
}
|
||||
|
||||
public void setLastStableYHeadRot(float lastStableYHeadRot) {
|
||||
this.lastStableYHeadRot = lastStableYHeadRot;
|
||||
}
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity.controller;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.bone.BoneBehaviorTypes;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.BoneBehaviorType;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.BehaviorManager;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountData;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountManager;
|
||||
import com.ticxo.modelengine.api.model.bone.type.Mount;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.LookController;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.control.LookControl;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.spigotmc.ActivationRange;
|
||||
|
||||
public class LookControlWrapper extends LookControl implements LookController {
|
||||
private final LookControl original;
|
||||
private ModeledEntity modeledEntity;
|
||||
|
||||
public LookControlWrapper(Mob mob, LookControl control) {
|
||||
super(mob);
|
||||
this.original = control;
|
||||
}
|
||||
|
||||
public void setLookAt(Vec3 var0) {
|
||||
this.original.setLookAt(var0);
|
||||
}
|
||||
|
||||
public void setLookAt(Entity var0) {
|
||||
this.original.setLookAt(var0);
|
||||
}
|
||||
|
||||
public void setLookAt(Entity var0, float var1, float var2) {
|
||||
this.original.setLookAt(var0, var1, var2);
|
||||
}
|
||||
|
||||
public void setLookAt(double var0, double var2, double var4) {
|
||||
this.original.setLookAt(var0, var2, var4);
|
||||
}
|
||||
|
||||
public void setLookAt(double var0, double var2, double var4, float var6, float var7) {
|
||||
this.original.setLookAt(var0, var2, var4, var6, var7);
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
if (this.modeledEntity == null) {
|
||||
this.modeledEntity = ModelEngineAPI.getModeledEntity(this.mob.getUUID());
|
||||
}
|
||||
|
||||
BehaviorManager mountManager = this.getMainManager();
|
||||
if (mountManager != null && ((MountManager)mountManager).isControlled()) {
|
||||
this.controlledTick(asMountManager(mountManager));
|
||||
} else {
|
||||
this.defaultTick();
|
||||
}
|
||||
}
|
||||
|
||||
protected <T extends BehaviorManager<? extends Mount> & MountManager> void controlledTick(T manager) {
|
||||
org.bukkit.entity.Entity driver = manager.getDriver();
|
||||
if (driver != null) {
|
||||
MountController controller = ModelEngineAPI.getMountPairManager().getController(driver.getUniqueId());
|
||||
if (controller != null) {
|
||||
controller.updateDirection(this, manager.getActiveModel());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.defaultTick();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defaultTick() {
|
||||
if (this.isActive()) {
|
||||
this.original.tick();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLookingAtTarget() {
|
||||
return this.original.isLookingAtTarget();
|
||||
}
|
||||
|
||||
public double getWantedX() {
|
||||
return this.original.getWantedX();
|
||||
}
|
||||
|
||||
public double getWantedY() {
|
||||
return this.original.getWantedY();
|
||||
}
|
||||
|
||||
public double getWantedZ() {
|
||||
return this.original.getWantedZ();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lookAt(double x, double y, double z) {
|
||||
double dX = x - this.mob.getX();
|
||||
double dY = y - this.mob.getEyeY();
|
||||
double dZ = z - this.mob.getZ();
|
||||
double hMagnitude = Math.sqrt(dX * dX + dZ * dZ);
|
||||
float pitch = (float)Math.toDegrees(Math.atan2(-dY, hMagnitude));
|
||||
float yaw = (float)Math.toDegrees(Math.atan2(-dX, dZ));
|
||||
this.setPitch(pitch);
|
||||
this.setHeadYaw(yaw);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPitch(float pitch) {
|
||||
this.mob.setXRot(pitch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeadYaw(float yaw) {
|
||||
this.mob.setYRot(yaw);
|
||||
this.mob.yHeadRot = yaw;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBodyYaw(float yaw) {
|
||||
this.mob.yBodyRot = yaw;
|
||||
}
|
||||
|
||||
private <T extends BehaviorManager<? extends Mount> & MountManager> T getMainManager() {
|
||||
if (this.modeledEntity == null) {
|
||||
return null;
|
||||
} else {
|
||||
return this.modeledEntity.getGlobalBehaviorData(BoneBehaviorTypes.MOUNT) instanceof MountData mountData
|
||||
? mountData.getMainMountManager()
|
||||
: null;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isActive() {
|
||||
return ActivationRange.checkIfActive(this.mob);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <M extends BehaviorManager<? extends Mount> & MountManager> M asMountManager(Object manager) {
|
||||
return (M)manager;
|
||||
}
|
||||
}
|
||||
+247
@@ -0,0 +1,247 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity.controller;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.animation.rootmotion.RootMotionDelta;
|
||||
import com.ticxo.modelengine.api.model.ActiveModel;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.BoneBehavior;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.GlobalBehaviorData;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.BehaviorManager;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountData;
|
||||
import com.ticxo.modelengine.api.model.bone.manager.MountManager;
|
||||
import com.ticxo.modelengine.api.model.bone.type.Mount;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController.MountInput;
|
||||
import com.ticxo.modelengine.api.nms.entity.wrapper.MoveController;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_20_R2.NMSFields;
|
||||
import java.util.Queue;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.MoverType;
|
||||
import net.minecraft.world.entity.ai.attributes.Attributes;
|
||||
import net.minecraft.world.entity.ai.control.MoveControl;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.apache.logging.log4j.util.TriConsumer;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.util.CraftVector;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
public class MoveControlWrapper extends MoveControl implements MoveController {
|
||||
protected final MoveControl original;
|
||||
protected final Queue<Runnable> runnables = new ConcurrentLinkedQueue<>();
|
||||
protected boolean isOnGround;
|
||||
|
||||
public MoveControlWrapper(Mob mob, MoveControl control) {
|
||||
super(mob);
|
||||
this.original = control;
|
||||
}
|
||||
|
||||
public boolean hasWanted() {
|
||||
return this.original.hasWanted();
|
||||
}
|
||||
|
||||
public double getSpeedModifier() {
|
||||
return this.original.getSpeedModifier();
|
||||
}
|
||||
|
||||
public void setWantedPosition(double x, double y, double z, double speed) {
|
||||
this.original.setWantedPosition(x, y, z, speed);
|
||||
}
|
||||
|
||||
public void strafe(float forward, float sideways) {
|
||||
this.original.strafe(forward, sideways);
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
this.isOnGround = this.mob.onGround();
|
||||
ModeledEntity modeledEntity = ModelEngineAPI.getModeledEntity(this.mob.getUUID());
|
||||
if (modeledEntity == null) {
|
||||
this.defaultTick();
|
||||
} else {
|
||||
GlobalBehaviorData mountData = modeledEntity.getMountData();
|
||||
BehaviorManager mainMountManager = mountData == null ? null : ((MountData)mountData).getMainMountManager();
|
||||
if (mainMountManager != null && ((MountManager)mainMountManager).isControlled()) {
|
||||
this.mob.setOnGround(true);
|
||||
this.disableWaterJumping();
|
||||
this.driverTick(asMountManager(mainMountManager));
|
||||
} else {
|
||||
this.defaultTick();
|
||||
}
|
||||
|
||||
this.passengerTick(modeledEntity, asMountManager(mainMountManager));
|
||||
|
||||
while (!this.runnables.isEmpty()) {
|
||||
this.runnables.poll().run();
|
||||
}
|
||||
|
||||
Vector mobMotion = this.toVector();
|
||||
RootMotionDelta rootMotion = modeledEntity.getRootMotionHandler().calculateRootMotion(mobMotion);
|
||||
this.fromVector(mobMotion);
|
||||
if (rootMotion != null) {
|
||||
Vector velocity = rootMotion.delta();
|
||||
if (!velocity.isZero() || !rootMotion.onGround()) {
|
||||
this.nullifyFallDistance();
|
||||
if (!TMath.isSimilar((float)velocity.getY(), 0.0F) || !rootMotion.onGround()) {
|
||||
this.mob.setDeltaMovement(this.mob.getDeltaMovement().multiply(1.0, 0.0, 1.0));
|
||||
}
|
||||
|
||||
this.mob.move(MoverType.SELF, new Vec3(velocity.getX(), velocity.getY(), velocity.getZ()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public double getWantedX() {
|
||||
return this.original.getWantedX();
|
||||
}
|
||||
|
||||
public double getWantedY() {
|
||||
return this.original.getWantedY();
|
||||
}
|
||||
|
||||
public double getWantedZ() {
|
||||
return this.original.getWantedZ();
|
||||
}
|
||||
|
||||
protected <T extends BehaviorManager<? extends Mount> & MountManager> void driverTick(T manager) {
|
||||
this.mob.setZza(0.0F);
|
||||
this.mob.setXxa(0.0F);
|
||||
this.updateRider(manager.getDriver(), manager.getActiveModel(), manager.getDriverBone(), MountController::updateDriverMovement);
|
||||
}
|
||||
|
||||
protected <T extends BehaviorManager<? extends Mount> & MountManager> void passengerTick(ModeledEntity modeledEntity, T manager) {
|
||||
for (ActiveModel activeModel : modeledEntity.getModels().values()) {
|
||||
activeModel.getMountManager()
|
||||
.ifPresent(
|
||||
mountManager -> {
|
||||
for (BoneBehavior seat : ((MountManager)mountManager).getSeats().values()) {
|
||||
for (Entity passenger : ((Mount)seat).getPassengers()) {
|
||||
this.updateRider(passenger, activeModel, (Mount)seat, MountController::updatePassengerMovement);
|
||||
}
|
||||
}
|
||||
|
||||
if (mountManager != manager && ((MountManager)mountManager).isControlled()) {
|
||||
this.updateRider(
|
||||
((MountManager)mountManager).getDriver(),
|
||||
activeModel,
|
||||
((MountManager)mountManager).getDriverBone(),
|
||||
MountController::updatePassengerMovement
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateRider(Entity entity, ActiveModel activeModel, Mount mountBone, TriConsumer<MountController, MoveController, ActiveModel> updateMethod) {
|
||||
MountController controller = this.getController(entity.getUniqueId());
|
||||
if (controller != null) {
|
||||
if (controller.getInput() == null) {
|
||||
controller.setInput(new MountInput());
|
||||
}
|
||||
|
||||
updateMethod.accept(controller, this, activeModel);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defaultTick() {
|
||||
this.original.tick();
|
||||
}
|
||||
|
||||
private void disableWaterJumping() {
|
||||
if (this.mob.isInWater()) {
|
||||
ReflectionUtils.set(this.mob, NMSFields.LIVING_ENTITY_noJumpDelay, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(float side, float up, float front, float speedModifier) {
|
||||
float speed = this.getSpeed();
|
||||
this.mob.setSpeed(speed * speedModifier);
|
||||
this.mob.setZza(front);
|
||||
this.mob.setYya(up);
|
||||
this.mob.setXxa(side);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void globalMove(float x, float y, float z, float speedModifier) {
|
||||
float speed = this.getSpeed();
|
||||
this.mob.setSpeed(speed * speedModifier);
|
||||
Vec3 vec = new Vec3(x, y, z).yRot(-this.mob.getYRot() * (float) (Math.PI / 180.0));
|
||||
this.mob.setXxa((float)vec.x);
|
||||
this.mob.setYya((float)vec.y);
|
||||
this.mob.setZza((float)vec.z);
|
||||
}
|
||||
|
||||
private void fromVector(Vector vector) {
|
||||
Vec3 vec = new Vec3(vector.getX(), vector.getY(), vector.getZ()).yRot(-this.mob.getYRot() * (float) (Math.PI / 180.0));
|
||||
this.mob.setXxa((float)vec.x);
|
||||
this.mob.setYya((float)vec.y);
|
||||
this.mob.setZza((float)vec.z);
|
||||
}
|
||||
|
||||
private Vector toVector() {
|
||||
return new Vector(
|
||||
Float.isNaN(this.mob.xxa) ? 0.0F : this.mob.xxa, Float.isNaN(this.mob.yya) ? 0.0F : this.mob.yya, Float.isNaN(this.mob.zza) ? 0.0F : this.mob.zza
|
||||
)
|
||||
.rotateAroundY(this.mob.getYRot() * (float) (Math.PI / 180.0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jump() {
|
||||
this.mob.getJumpControl().jump();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVelocity(double x, double y, double z) {
|
||||
this.mob.setDeltaMovement(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addVelocity(double x, double y, double z) {
|
||||
this.mob.setDeltaMovement(this.mob.getDeltaMovement().add(x, y, z));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullifyFallDistance() {
|
||||
this.mob.resetFallDistance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnGround() {
|
||||
return this.isOnGround;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInWater() {
|
||||
return this.mob.isInWater();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getSpeed() {
|
||||
return (float)this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector getVelocity() {
|
||||
return CraftVector.toBukkit(this.mob.getDeltaMovement());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queuePostTick(Runnable runnable) {
|
||||
this.runnables.add(runnable);
|
||||
}
|
||||
|
||||
private MountController getController(UUID uuid) {
|
||||
return ModelEngineAPI.getMountPairManager().getController(uuid);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <M extends BehaviorManager<? extends Mount> & MountManager> M asMountManager(Object manager) {
|
||||
return (M)manager;
|
||||
}
|
||||
}
|
||||
+269
@@ -0,0 +1,269 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity.hitbox;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.model.bone.ModelBone;
|
||||
import com.ticxo.modelengine.api.model.bone.type.SubHitbox;
|
||||
import com.ticxo.modelengine.api.nms.entity.HitboxEntity;
|
||||
import com.ticxo.modelengine.api.utils.math.OrientedBoundingBox;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.EntityUtils;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.core.NonNullList;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.damagesource.DamageSource;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.EquipmentSlot;
|
||||
import net.minecraft.world.entity.HumanoidArm;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Entity.RemovalReason;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftHumanEntity;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.util.CraftLocation;
|
||||
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class HitboxEntityImpl extends LivingEntity implements HitboxEntity {
|
||||
private final NonNullList<ItemStack> handItems = NonNullList.withSize(2, ItemStack.EMPTY);
|
||||
private final NonNullList<ItemStack> armorItems = NonNullList.withSize(4, ItemStack.EMPTY);
|
||||
private final ModelBone bone;
|
||||
private final SubHitbox subHitbox;
|
||||
private OBB obb;
|
||||
private volatile Vector3f location;
|
||||
private boolean markRemoved;
|
||||
|
||||
public HitboxEntityImpl(Level world, @NotNull ModelBone bone, @NotNull SubHitbox subHitbox) {
|
||||
super(EntityType.SILVERFISH, world);
|
||||
this.bone = bone;
|
||||
this.subHitbox = subHitbox;
|
||||
this.setInvulnerable(true);
|
||||
this.setNoGravity(true);
|
||||
this.noPhysics = true;
|
||||
}
|
||||
|
||||
public HumanoidArm getMainArm() {
|
||||
return HumanoidArm.RIGHT;
|
||||
}
|
||||
|
||||
public Iterable<ItemStack> getArmorSlots() {
|
||||
return this.armorItems;
|
||||
}
|
||||
|
||||
public ItemStack getItemBySlot(EquipmentSlot slot) {
|
||||
return switch (slot.getType()) {
|
||||
case HAND -> (ItemStack)this.handItems.get(slot.getIndex());
|
||||
case ARMOR -> (ItemStack)this.armorItems.get(slot.getIndex());
|
||||
default -> throw new IncompatibleClassChangeError();
|
||||
};
|
||||
}
|
||||
|
||||
public void setItemSlot(EquipmentSlot slot, ItemStack stack) {
|
||||
this.verifyEquippedItem(stack);
|
||||
switch (slot.getType()) {
|
||||
case HAND:
|
||||
this.onEquipItem(slot, this.handItems.set(slot.getIndex(), stack), stack);
|
||||
break;
|
||||
case ARMOR:
|
||||
this.onEquipItem(slot, this.armorItems.set(slot.getIndex(), stack), stack);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInvisible() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void push(Entity entity) {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected AABB makeBoundingBox() {
|
||||
if (this.subHitbox == null) {
|
||||
return super.makeBoundingBox();
|
||||
} else {
|
||||
Vector3f pos = this.subHitbox.getLocation();
|
||||
Vector3f dim = this.subHitbox.getDimension();
|
||||
if (this.subHitbox.isOBB()) {
|
||||
float halfX = dim.x * 0.5F;
|
||||
float halfY = dim.y * 0.5F;
|
||||
float halfZ = dim.z * 0.5F;
|
||||
Quaternionf rot = this.subHitbox.getRotation();
|
||||
float yaw = this.subHitbox.getYaw();
|
||||
this.obb = new OBB(pos.x - halfX, pos.y - halfY, pos.z - halfZ, pos.x + halfX, pos.y + halfY, pos.z + halfZ, rot, yaw);
|
||||
return this.obb;
|
||||
} else {
|
||||
float halfX = dim.x * 0.5F;
|
||||
float halfZ = dim.z * 0.5F;
|
||||
return new AABB(pos.x - halfX, pos.y, pos.z - halfZ, pos.x + halfX, pos.y + dim.y, pos.z + halfZ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
if (this.markRemoved) {
|
||||
this.discard();
|
||||
} else {
|
||||
super.tick();
|
||||
if (this.bone == null || this.subHitbox == null) {
|
||||
this.discard();
|
||||
} else if (!this.bone.getActiveModel().getModeledEntity().getBase().isAlive()) {
|
||||
this.discard();
|
||||
} else {
|
||||
Vector3f loc = this.location;
|
||||
if (loc != null && loc.isFinite()) {
|
||||
Vec3 vec = new Vec3(loc);
|
||||
this.setPos(vec);
|
||||
|
||||
for (org.bukkit.entity.Entity entity : this.subHitbox.getBoundEntities().values()) {
|
||||
EntityUtils.nms(entity).setPos(vec);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean fireImmune() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void pushEntities() {
|
||||
}
|
||||
|
||||
protected void doPush(Entity entity) {
|
||||
}
|
||||
|
||||
public void aiStep() {
|
||||
}
|
||||
|
||||
public boolean isPushable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hurt(DamageSource source, float amount) {
|
||||
for (org.bukkit.entity.Entity entity : this.subHitbox.getBoundEntities().values()) {
|
||||
EntityUtils.nms(entity).hurt(source, amount);
|
||||
}
|
||||
|
||||
if (this.subHitbox.getDamageMultiplier() <= 1.0E-5F) {
|
||||
return false;
|
||||
} else {
|
||||
CraftHumanEntity cause = source.getEntity() instanceof Player player ? player.getBukkitEntity() : null;
|
||||
return this.bone.getActiveModel().getModeledEntity().getBase().hurt(this, cause, source, amount * this.subHitbox.getDamageMultiplier());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public InteractionResult interact(Player player, InteractionHand hand) {
|
||||
if (player.getBukkitEntity() instanceof org.bukkit.entity.Player craftPlayer) {
|
||||
for (org.bukkit.entity.Entity entity : this.subHitbox.getBoundEntities().values()) {
|
||||
PlayerInteractAtEntityEvent event = new PlayerInteractAtEntityEvent(
|
||||
craftPlayer,
|
||||
entity,
|
||||
new Vector(0, 0, 0),
|
||||
hand == InteractionHand.OFF_HAND ? org.bukkit.inventory.EquipmentSlot.OFF_HAND : org.bukkit.inventory.EquipmentSlot.HAND
|
||||
);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if (!event.isCancelled()) {
|
||||
EntityUtils.nms(entity).interact(player, hand);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.subHitbox.getDamageMultiplier() <= 1.0E-5F) {
|
||||
return InteractionResult.PASS;
|
||||
} else {
|
||||
com.ticxo.modelengine.api.nms.entity.EntityHandler.InteractionResult result = this.bone
|
||||
.getActiveModel()
|
||||
.getModeledEntity()
|
||||
.getBase()
|
||||
.interact(
|
||||
this,
|
||||
player.getBukkitEntity(),
|
||||
hand == InteractionHand.MAIN_HAND ? org.bukkit.inventory.EquipmentSlot.HAND : org.bukkit.inventory.EquipmentSlot.OFF_HAND
|
||||
);
|
||||
|
||||
return switch (result) {
|
||||
case SUCCESS, SUCCESS_NO_ITEM_USED -> InteractionResult.SUCCESS;
|
||||
case CONSUME -> InteractionResult.CONSUME;
|
||||
case CONSUME_PARTIAL -> InteractionResult.CONSUME_PARTIAL;
|
||||
case PASS -> InteractionResult.PASS;
|
||||
case FAIL -> InteractionResult.FAIL;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public boolean shouldBeSaved() {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected int decreaseAirSupply(int air) {
|
||||
return air;
|
||||
}
|
||||
|
||||
protected int increaseAirSupply(int air) {
|
||||
return air;
|
||||
}
|
||||
|
||||
public void remove(RemovalReason reason) {
|
||||
super.remove(reason);
|
||||
ModelEngineAPI.setRenderCanceled(this.getId(), false);
|
||||
ModelEngineAPI.getInteractionTracker().removeHitbox(this.getEntityId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEntityId() {
|
||||
return this.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return this.getUUID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queueLocation(Vector3f location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getLocation() {
|
||||
return CraftLocation.toBukkit(this.position(), this.level().getWorld(), this.getBukkitYaw(), this.getXRot());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public OrientedBoundingBox getOrientedBoundingBox() {
|
||||
return this.obb == null ? null : this.obb.getBukkitOBB();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return !this.isRemoved() && !this.markRemoved;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markRemoved() {
|
||||
this.markRemoved = true;
|
||||
ModelEngineAPI.setRenderCanceled(this.getId(), false);
|
||||
ModelEngineAPI.getInteractionTracker().removeHitbox(this.getEntityId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModelBone getBone() {
|
||||
return this.bone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SubHitbox getSubHitbox() {
|
||||
return this.subHitbox;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity.hitbox;
|
||||
|
||||
import com.ticxo.modelengine.api.utils.math.OrientedBoundingBox;
|
||||
import java.util.Optional;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class OBB extends AABB {
|
||||
private final Quaternionf rotation;
|
||||
private final float yaw;
|
||||
private final OrientedBoundingBox bukkitOBB;
|
||||
|
||||
public OBB(Vec3 cornerA, Vec3 cornerB, Quaternionf rotation, float yaw) {
|
||||
this(cornerA.x, cornerA.y, cornerA.z, cornerB.x, cornerB.y, cornerB.z, rotation, yaw);
|
||||
}
|
||||
|
||||
public OBB(double minX, double minY, double minZ, double maxX, double maxY, double maxZ, Quaternionf rotation, float yaw) {
|
||||
super(minX, minY, minZ, maxX, maxY, maxZ);
|
||||
this.rotation = rotation;
|
||||
this.yaw = yaw;
|
||||
this.bukkitOBB = new OrientedBoundingBox(
|
||||
this.getCenter().toVector3f(), new Vector3f((float)this.getXsize(), (float)this.getYsize(), (float)this.getZsize()), rotation, yaw
|
||||
);
|
||||
}
|
||||
|
||||
public OBB makeOBBInstance(Vec3 position, Quaternionf rotation, float yaw) {
|
||||
return new OBB(position.add(this.minX, this.minY, this.minZ), position.add(this.maxX, this.maxY, this.maxZ), rotation, yaw);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public AABB inflate(double xInflate, double yInflate, double zInflate) {
|
||||
double minX = this.minX - xInflate;
|
||||
double minY = this.minY - yInflate;
|
||||
double minZ = this.minZ - zInflate;
|
||||
double maxX = this.maxX + xInflate;
|
||||
double maxY = this.maxY + yInflate;
|
||||
double maxZ = this.maxZ + zInflate;
|
||||
return new OBB(minX, minY, minZ, maxX, maxY, maxZ, this.rotation, this.yaw);
|
||||
}
|
||||
|
||||
public boolean intersects(@NotNull AABB aabb) {
|
||||
return aabb instanceof OBB obb ? this.intersects(obb) : super.intersects(aabb);
|
||||
}
|
||||
|
||||
public boolean intersects(OBB obb) {
|
||||
return this.bukkitOBB.intersects(obb.bukkitOBB);
|
||||
}
|
||||
|
||||
public boolean intersects(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
|
||||
OrientedBoundingBox obbB = new OrientedBoundingBox((float)minX, (float)minY, (float)minZ, (float)maxX, (float)maxY, (float)maxZ);
|
||||
return this.bukkitOBB.intersects(obbB);
|
||||
}
|
||||
|
||||
public Optional<Vec3> clip(Vec3 from, Vec3 to) {
|
||||
Quaternionf inverse = this.rotation.conjugate(new Quaternionf());
|
||||
Vec3 center = this.getCenter();
|
||||
float yaw = this.yaw * (float) (Math.PI / 180.0);
|
||||
Vec3 rFrom = new Vec3(from.subtract(center).toVector3f().rotateY(-yaw).rotate(inverse)).add(center);
|
||||
Vec3 rTo = new Vec3(to.subtract(center).toVector3f().rotateY(-yaw).rotate(inverse)).add(center);
|
||||
Optional<Vec3> result = super.clip(rFrom, rTo);
|
||||
if (result.isEmpty()) {
|
||||
return result;
|
||||
} else {
|
||||
Vec3 clip = new Vec3(result.get().subtract(center).toVector3f().rotate(this.rotation).rotateY(yaw)).add(center);
|
||||
return Optional.of(clip);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "OBB(rotation=" + this.getRotation() + ", yaw=" + this.getYaw() + ", bukkitOBB=" + this.getBukkitOBB() + ")";
|
||||
}
|
||||
|
||||
public Quaternionf getRotation() {
|
||||
return this.rotation;
|
||||
}
|
||||
|
||||
public float getYaw() {
|
||||
return this.yaw;
|
||||
}
|
||||
|
||||
public OrientedBoundingBox getBukkitOBB() {
|
||||
return this.bukkitOBB;
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.AmphibiousPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class AmphibiousNavigationWrapper extends AmphibiousPathNavigation {
|
||||
private final AmphibiousPathNavigation oldNav;
|
||||
|
||||
public AmphibiousNavigationWrapper(Mob mob, AmphibiousPathNavigation oldNav) {
|
||||
super(mob, mob.level());
|
||||
this.oldNav = oldNav;
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isStableDestination(BlockPos var0) {
|
||||
return this.oldNav.isStableDestination(var0);
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class FlyingNavigationWrapper extends FlyingPathNavigation {
|
||||
public FlyingNavigationWrapper(Mob mob, FlyingPathNavigation oldNav) {
|
||||
super(mob, mob.level());
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
this.setCanOpenDoors(oldNav.canOpenDoors());
|
||||
this.setCanPassDoors(oldNav.canPassDoors());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class GroundNavigationWrapper extends GroundPathNavigation {
|
||||
public GroundNavigationWrapper(Mob mob, GroundPathNavigation oldNav) {
|
||||
super(mob, mob.level());
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
this.setCanOpenDoors(oldNav.canOpenDoors());
|
||||
this.setCanPassDoors(oldNav.canPassDoors());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.WallClimberNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class WallClimberNavigationWrapper extends WallClimberNavigation {
|
||||
public WallClimberNavigationWrapper(Mob mob, WallClimberNavigation oldNav) {
|
||||
super(mob, mob.level());
|
||||
this.setCanFloat(oldNav.canFloat());
|
||||
this.setCanOpenDoors(oldNav.canOpenDoors());
|
||||
this.setCanPassDoors(oldNav.canPassDoors());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.entity.navigation;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.ai.navigation.WaterBoundPathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class WaterBoundNavigationWrapper extends WaterBoundPathNavigation {
|
||||
public WaterBoundNavigationWrapper(Mob mob) {
|
||||
super(mob, mob.level());
|
||||
}
|
||||
|
||||
public boolean moveTo(@Nullable Path newPath, double speed) {
|
||||
if (!ModelEngineAPI.getConfigCache().isOverrideMoveTo()) {
|
||||
return super.moveTo(newPath, speed);
|
||||
} else {
|
||||
if (newPath != null && !newPath.sameAs(this.path)) {
|
||||
int closestNode = newPath.getNextNodeIndex();
|
||||
double distanceSqr = this.getNodeDistanceSquared(newPath.getNextEntityPos(this.mob));
|
||||
|
||||
for (int i = closestNode + 1; i < newPath.getNodeCount(); i++) {
|
||||
double temp = this.getNodeDistanceSquared(newPath.getEntityPosAtNode(this.mob, i));
|
||||
if (temp < distanceSqr) {
|
||||
distanceSqr = temp;
|
||||
closestNode = i;
|
||||
}
|
||||
}
|
||||
|
||||
newPath.setNextNodeIndex(closestNode);
|
||||
}
|
||||
|
||||
return super.moveTo(newPath, speed);
|
||||
}
|
||||
}
|
||||
|
||||
private double getNodeDistanceSquared(Vec3 pos) {
|
||||
double x = pos.x - this.mob.getX();
|
||||
double y = pos.y - this.mob.getY();
|
||||
double z = pos.z - this.mob.getZ();
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
protected void followThePath() {
|
||||
Vec3 mobPosition = this.getTempMobPos();
|
||||
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
double distX = Math.abs(this.mob.getX() - targetNodePosition.x);
|
||||
double distY = Math.abs(this.mob.getY() - targetNodePosition.y);
|
||||
double distZ = Math.abs(this.mob.getZ() - targetNodePosition.z);
|
||||
boolean hasArrivedNode = distX < this.maxDistanceToWaypoint && distZ < this.maxDistanceToWaypoint && distY < 1.0;
|
||||
boolean canAdvance = hasArrivedNode || this.canCutCorner(this.path.getNextNode().type) && this.shouldTargetNextNodeInDirection(mobPosition);
|
||||
if (canAdvance) {
|
||||
this.path.advance();
|
||||
}
|
||||
|
||||
this.doStuckDetection(mobPosition);
|
||||
}
|
||||
|
||||
private boolean shouldTargetNextNodeInDirection(Vec3 mobPosition) {
|
||||
if (this.path.getNextNodeIndex() + 1 >= this.path.getNodeCount()) {
|
||||
return false;
|
||||
} else {
|
||||
Vec3 targetNodePosition = this.path.getNextEntityPos(this.mob);
|
||||
if (!mobPosition.closerThan(targetNodePosition, 2.0)) {
|
||||
return false;
|
||||
} else if (this.canMoveDirectly(mobPosition, this.path.getNextEntityPos(this.mob))) {
|
||||
return true;
|
||||
} else {
|
||||
Vec3 nextNodePosition = this.path.getEntityPosAtNode(this.mob, this.path.getNextNodeIndex() + 1);
|
||||
Vec3 nextNodeDir = nextNodePosition.subtract(targetNodePosition);
|
||||
Vec3 mobDir = mobPosition.subtract(targetNodePosition);
|
||||
return nextNodeDir.dot(mobDir) > 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+439
@@ -0,0 +1,439 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.network;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.interaction.DynamicHitbox;
|
||||
import com.ticxo.modelengine.api.model.ActiveModel;
|
||||
import com.ticxo.modelengine.api.model.ModelUpdaters;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.model.PivotOverride;
|
||||
import com.ticxo.modelengine.api.model.PivotOverrideRegistry;
|
||||
import com.ticxo.modelengine.api.model.bone.BoneBehaviorTypes;
|
||||
import com.ticxo.modelengine.api.model.bone.behavior.BoneBehavior;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer.Mount;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController;
|
||||
import com.ticxo.modelengine.api.mount.controller.MountController.MountInput;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.nms.network.CancelType;
|
||||
import com.ticxo.modelengine.api.nms.network.ClientDesyncMonitor;
|
||||
import com.ticxo.modelengine.api.nms.network.PipelineWrapper;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.api.utils.data.tracker.CollectionDataTracker;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_20_R2.NMSFields;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.patch.ServerboundInteractPacketWrapper;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.PacketInterceptor;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
import it.unimi.dsi.fastutil.Pair;
|
||||
import it.unimi.dsi.fastutil.ints.IntLinkedOpenHashSet;
|
||||
import it.unimi.dsi.fastutil.ints.IntListIterator;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.PacketListener;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.common.ClientboundKeepAlivePacket;
|
||||
import net.minecraft.network.protocol.common.ServerboundPongPacket;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundAnimatePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundEntityEventPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveMobEffectPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundPlayerInputPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Pos;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.PosRot;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Rot;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.network.syncher.SynchedEntityData.DataValue;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ModelEngineChannelHandler extends ChannelDuplexHandler {
|
||||
private static final MethodHandle MOVE_ENTITY_ID = entityIdGetter(NMSFields.CLIENTBOUND_MOVE_ENTITY_PACKET_entityId);
|
||||
private static final MethodHandle ROTATE_HEAD_ID = entityIdGetter(NMSFields.CLIENTBOUND_ROTATE_HEAD_PACKET_entityId);
|
||||
private final Player player;
|
||||
private final ServerPlayer serverPlayer;
|
||||
private final ModelUpdaters updaters;
|
||||
private final EntityHandler entityHandler;
|
||||
private final ClientDesyncMonitor desyncMonitor;
|
||||
private final PacketInterceptor writeInterceptors;
|
||||
private final PacketInterceptor readInterceptors;
|
||||
|
||||
private static MethodHandle entityIdGetter(NMSFields field) {
|
||||
try {
|
||||
return MethodHandles.lookup().unreflectGetter(ReflectionUtils.getField(field));
|
||||
} catch (IllegalAccessException var2) {
|
||||
throw new ExceptionInInitializerError(var2);
|
||||
}
|
||||
}
|
||||
|
||||
public ModelEngineChannelHandler(Player player, PipelineWrapper pipeline) {
|
||||
this.player = player;
|
||||
this.serverPlayer = ((CraftPlayer)player).getHandle();
|
||||
this.updaters = ModelEngineAPI.getAPI().getModelUpdaters();
|
||||
this.entityHandler = ModelEngineAPI.getEntityHandler();
|
||||
this.desyncMonitor = pipeline.getDesyncMonitor();
|
||||
this.writeInterceptors = new PacketInterceptor();
|
||||
this.writeInterceptors
|
||||
.register(ClientboundAddEntityPacket.class, this::handleAddEntity)
|
||||
.register(ClientboundRemoveEntitiesPacket.class, this::handleRemoveEntities)
|
||||
.register(Pos.class, packet -> this.handleEntityId(packet, MOVE_ENTITY_ID))
|
||||
.register(Rot.class, packet -> this.handleEntityId(packet, MOVE_ENTITY_ID))
|
||||
.register(PosRot.class, packet -> this.handleEntityId(packet, MOVE_ENTITY_ID))
|
||||
.register(ClientboundRotateHeadPacket.class, packet -> this.handleEntityId(packet, ROTATE_HEAD_ID))
|
||||
.register(ClientboundEntityEventPacket.class, this::handleEntityId)
|
||||
.register(ClientboundSetEntityMotionPacket.class, this::handleEntityMotion)
|
||||
.register(ClientboundTeleportEntityPacket.class, this::handleTeleportEntity)
|
||||
.register(ClientboundAnimatePacket.class, this::handleAnimate)
|
||||
.register(ClientboundSetEntityDataPacket.class, this::handleEntityData)
|
||||
.register(ClientboundSetEquipmentPacket.class, this::handleSetEquipment)
|
||||
.register(ClientboundRemoveMobEffectPacket.class, this::handleRemoveMobEffect)
|
||||
.register(ClientboundUpdateMobEffectPacket.class, this::handleUpdateMobEffect)
|
||||
.register(ClientboundKeepAlivePacket.class, this::handleKeepAlive)
|
||||
.register(ClientboundSetPassengersPacket.class, this::handlePassengers)
|
||||
.registerPost(ClientboundAddEntityPacket.class, this::handleAddEntityPost);
|
||||
this.readInterceptors = new PacketInterceptor();
|
||||
this.readInterceptors
|
||||
.register(ServerboundInteractPacket.class, this::handleInteract)
|
||||
.register(ServerboundPlayerInputPacket.class, this::handlePlayerInput)
|
||||
.register(ServerboundPongPacket.class, this::handlePong)
|
||||
.register(net.minecraft.network.protocol.game.ServerboundMovePlayerPacket.Pos.class, this::handlePlayerMove)
|
||||
.register(net.minecraft.network.protocol.game.ServerboundMovePlayerPacket.PosRot.class, this::handlePlayerMove);
|
||||
}
|
||||
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
if (!(msg instanceof Packet packet)) {
|
||||
super.write(ctx, msg, promise);
|
||||
} else {
|
||||
try {
|
||||
if (packet instanceof ClientboundBundlePacket bundle) {
|
||||
ArrayList<Packet<ClientGamePacketListener>> list = new ArrayList<>();
|
||||
|
||||
for (Packet<ClientGamePacketListener> subPacket : bundle.subPackets()) {
|
||||
Packet result = this.writeInterceptors.accept(subPacket);
|
||||
if (result != null) {
|
||||
list.add(result);
|
||||
|
||||
for (Packet p : this.writeInterceptors.acceptPost(result)) {
|
||||
list.add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!list.isEmpty()) {
|
||||
Packet var13 = new ClientboundBundlePacket(list);
|
||||
super.write(ctx, var13, promise);
|
||||
}
|
||||
} else {
|
||||
Packet var14 = this.writeInterceptors.accept(packet);
|
||||
if (var14 == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Collection<Packet> post = this.writeInterceptors.acceptPost(var14);
|
||||
if (post.isEmpty()) {
|
||||
super.write(ctx, var14, promise);
|
||||
} else {
|
||||
ArrayList<Packet<ClientGamePacketListener>> list = new ArrayList<>(post.size() + 1);
|
||||
list.add(var14);
|
||||
|
||||
for (Packet p : post) {
|
||||
list.add(p);
|
||||
}
|
||||
|
||||
Packet var15 = new ClientboundBundlePacket(list);
|
||||
super.write(ctx, var15, promise);
|
||||
}
|
||||
}
|
||||
} catch (Throwable var12) {
|
||||
var12.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void channelRead(@NotNull ChannelHandlerContext ctx, @NotNull Object msg) throws Exception {
|
||||
if (!(msg instanceof Packet)) {
|
||||
super.channelRead(ctx, msg);
|
||||
} else {
|
||||
Packet packet = this.readInterceptors.accept((Packet)msg);
|
||||
if (packet != null) {
|
||||
super.channelRead(ctx, packet);
|
||||
this.readInterceptors.acceptPost(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket handleAddEntity(ClientboundAddEntityPacket packet) {
|
||||
int id = packet.getId();
|
||||
this.markViewersDirty(id);
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private void markViewersDirty(int id) {
|
||||
ModeledEntity model = this.updaters.getModeledEntity(id);
|
||||
if (model != null) {
|
||||
model.getBase().getData().markViewersDirty();
|
||||
}
|
||||
}
|
||||
|
||||
private List<Packet> handleAddEntityPost(ClientboundAddEntityPacket packet) {
|
||||
return this.handleMount(packet.getUUID());
|
||||
}
|
||||
|
||||
private List<Packet> handleMount(UUID uuid) {
|
||||
Pair<ActiveModel, MountController> pair = ModelEngineAPI.getMountPairManager().get(uuid);
|
||||
if (pair == null) {
|
||||
return null;
|
||||
} else {
|
||||
ActiveModel model = (ActiveModel)pair.left();
|
||||
ArrayList<Packet> list = new ArrayList<>();
|
||||
model.getBehaviorRenderer(BoneBehaviorTypes.MOUNT).ifPresent(behaviorRenderer -> {
|
||||
if (behaviorRenderer instanceof MountRenderer renderer) {
|
||||
MountController controller = (MountController)pair.right();
|
||||
if (controller.getMount() instanceof BoneBehavior behavior) {
|
||||
Mount mount = renderer.getRendered().get(behavior.getBone().getBoneId());
|
||||
CollectionDataTracker<Integer> ids = mount.getPassengers();
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(mount.getMountId());
|
||||
buf.writeVarInt(ids.size());
|
||||
ids.forEach(buf::writeVarInt);
|
||||
list.add(new ClientboundSetPassengersPacket(buf));
|
||||
}
|
||||
}
|
||||
});
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundRemoveEntitiesPacket handleRemoveEntities(ClientboundRemoveEntitiesPacket packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int size = buf.readVarInt();
|
||||
HashSet<Integer> set = new HashSet<>(size);
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
int id = buf.readVarInt();
|
||||
this.markViewersDirty(id);
|
||||
if (ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE) {
|
||||
set.add(id);
|
||||
}
|
||||
}
|
||||
|
||||
if (set.size() == size) {
|
||||
return packet;
|
||||
} else {
|
||||
buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(set.size());
|
||||
|
||||
for (int id : set) {
|
||||
buf.writeVarInt(id);
|
||||
}
|
||||
|
||||
return new ClientboundRemoveEntitiesPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private <U extends PacketListener, T extends Packet<U>> Packet<U> handleEntityId(T packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int id = buf.readVarInt();
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private <U extends PacketListener, T extends Packet<U>> Packet<U> handleEntityId(T packet, MethodHandle idGetter) {
|
||||
int id;
|
||||
try {
|
||||
id = (int)idGetter.invoke((Packet)packet);
|
||||
} catch (Throwable var5) {
|
||||
throw new IllegalStateException(var5);
|
||||
}
|
||||
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundSetEntityMotionPacket handleEntityMotion(ClientboundSetEntityMotionPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundTeleportEntityPacket handleTeleportEntity(ClientboundTeleportEntityPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundAnimatePacket handleAnimate(ClientboundAnimatePacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket handleEntityData(ClientboundSetEntityDataPacket packet) {
|
||||
CancelType type = ModelEngineAPI.shouldShow(this.player, packet.id());
|
||||
if (type == CancelType.HIDE) {
|
||||
return null;
|
||||
} else if (packet.id() != this.player.getEntityId() && type == CancelType.SHOW) {
|
||||
return packet;
|
||||
} else {
|
||||
ModeledEntity modeledEntity = this.updaters.getModeledEntity(packet.id());
|
||||
if (modeledEntity == null) {
|
||||
return packet;
|
||||
} else {
|
||||
if (this.entityHandler.isForcedInvisible(modeledEntity.getBase().getUUID())) {
|
||||
ArrayList<DataValue<?>> entityData = new ArrayList<>();
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(packet.id());
|
||||
|
||||
for (DataValue<?> item : packet.packedItems()) {
|
||||
if (item.id() == 0) {
|
||||
byte data = (Byte)item.value();
|
||||
data = TMath.setBit(data, 5, true);
|
||||
entityData.add(new DataValue(0, EntityDataSerializers.BYTE, data));
|
||||
} else {
|
||||
entityData.add(item);
|
||||
}
|
||||
}
|
||||
|
||||
buf.writeByte(255);
|
||||
packet = new ClientboundSetEntityDataPacket(packet.id(), entityData);
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEquipmentPacket handleSetEquipment(ClientboundSetEquipmentPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getEntity()) == CancelType.SHOW ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundRemoveMobEffectPacket handleRemoveMobEffect(ClientboundRemoveMobEffectPacket packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int id = buf.readVarInt();
|
||||
return ModelEngineAPI.shouldShow(this.player, id) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundUpdateMobEffectPacket handleUpdateMobEffect(ClientboundUpdateMobEffectPacket packet) {
|
||||
return ModelEngineAPI.shouldShow(this.player, packet.getEntityId()) != CancelType.HIDE ? packet : null;
|
||||
}
|
||||
|
||||
private ClientboundKeepAlivePacket handleKeepAlive(ClientboundKeepAlivePacket packet) {
|
||||
if (this.desyncMonitor.clientTickShifted() || this.desyncMonitor.shouldRetest()) {
|
||||
this.desyncMonitor.startTest();
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket handlePassengers(ClientboundSetPassengersPacket packet) {
|
||||
PivotOverrideRegistry registry = ModelEngineAPI.getPivotOverrideRegistry();
|
||||
Optional<PivotOverride> maybeOverride = registry.get(packet.getVehicle());
|
||||
if (maybeOverride.isEmpty()) {
|
||||
return packet;
|
||||
} else {
|
||||
PivotOverride override = maybeOverride.get();
|
||||
Collection<Integer> extra = override.getPassengers().get();
|
||||
if (extra.isEmpty()) {
|
||||
return packet;
|
||||
} else {
|
||||
int[] existing = packet.getPassengers();
|
||||
IntLinkedOpenHashSet merged = new IntLinkedOpenHashSet(existing.length + extra.size());
|
||||
|
||||
for (int id : existing) {
|
||||
merged.add(id);
|
||||
}
|
||||
|
||||
boolean changed = false;
|
||||
|
||||
for (int id : extra) {
|
||||
if (merged.add(id)) {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!changed) {
|
||||
return packet;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(packet.getVehicle());
|
||||
buf.writeVarInt(merged.size());
|
||||
IntListIterator var16 = merged.iterator();
|
||||
|
||||
while (var16.hasNext()) {
|
||||
int idx = (Integer)var16.next();
|
||||
buf.writeVarInt(idx);
|
||||
}
|
||||
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ServerboundInteractPacket handleInteract(ServerboundInteractPacket packet) {
|
||||
FriendlyByteBuf buf = NetworkUtils.readData(packet);
|
||||
int entityId = buf.readVarInt();
|
||||
if (entityId == DynamicHitbox.getHitboxId()) {
|
||||
DynamicHitbox hitbox = ModelEngineAPI.getInteractionTracker().getDynamicHitbox(this.player.getUniqueId());
|
||||
if (hitbox != null) {
|
||||
return new ServerboundInteractPacketWrapper(entityId, hitbox.getTarget(), packet);
|
||||
}
|
||||
}
|
||||
|
||||
ActiveModel activeModel = ModelEngineAPI.getInteractionTracker().getModelRelay(entityId);
|
||||
if (activeModel != null) {
|
||||
ModeledEntity modeledEntity = activeModel.getModeledEntity();
|
||||
if (modeledEntity == null) {
|
||||
return packet;
|
||||
} else {
|
||||
int id = modeledEntity.getBase().getEntityId();
|
||||
return new ServerboundInteractPacketWrapper(entityId, id, packet);
|
||||
}
|
||||
} else {
|
||||
Integer relayed = ModelEngineAPI.getInteractionTracker().getEntityRelay(entityId);
|
||||
return relayed != null ? new ServerboundInteractPacketWrapper(entityId, relayed, packet) : packet;
|
||||
}
|
||||
}
|
||||
|
||||
private ServerboundPlayerInputPacket handlePlayerInput(ServerboundPlayerInputPacket inputPacket) {
|
||||
MountController controller = ModelEngineAPI.getMountPairManager().getController(this.player.getUniqueId());
|
||||
if (controller != null) {
|
||||
MountInput input = controller.getInput();
|
||||
if (input == null) {
|
||||
controller.setInput(new MountInput(inputPacket.getXxa(), inputPacket.getZza(), inputPacket.isJumping(), inputPacket.isShiftKeyDown()));
|
||||
} else {
|
||||
input.setSide(inputPacket.getXxa());
|
||||
input.setFront(inputPacket.getZza());
|
||||
input.setJump(inputPacket.isJumping());
|
||||
input.setSneak(inputPacket.isShiftKeyDown());
|
||||
}
|
||||
}
|
||||
|
||||
return inputPacket;
|
||||
}
|
||||
|
||||
private ServerboundPongPacket handlePong(ServerboundPongPacket packet) {
|
||||
this.desyncMonitor.recordPongTime(System.currentTimeMillis());
|
||||
return null;
|
||||
}
|
||||
|
||||
private ServerboundMovePlayerPacket handlePlayerMove(ServerboundMovePlayerPacket packet) {
|
||||
this.desyncMonitor.recordClientSyncTime(System.currentTimeMillis());
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.network;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ticxo.modelengine.api.nms.network.ClientDesyncMonitor;
|
||||
import com.ticxo.modelengine.api.nms.network.NetworkHandler;
|
||||
import com.ticxo.modelengine.api.nms.network.PipelineWrapper;
|
||||
import com.ticxo.modelengine.api.nms.network.ProtectedPacketUnpacker;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.NMSFields;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.Bundler;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.NetworkUtils;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.Map.Entry;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.common.ClientboundPingPacket;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class NetworkHandlerImpl implements NetworkHandler {
|
||||
public static NetworkHandlerImpl instance;
|
||||
private final Map<UUID, PipelineWrapper> pipelines = Maps.newConcurrentMap();
|
||||
private final Map<UUID, Bundler> bundles = Maps.newConcurrentMap();
|
||||
private volatile boolean isBatching;
|
||||
|
||||
public NetworkHandlerImpl() {
|
||||
if (instance != null) {
|
||||
throw new IllegalStateException("Network handler already initialized");
|
||||
} else {
|
||||
instance = this;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getProtocolVersion() {
|
||||
return SharedConstants.getProtocolVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<PipelineWrapper> getPipeline(UUID uuid) {
|
||||
return Optional.ofNullable(this.pipelines.get(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePipeline(UUID uuid) {
|
||||
this.pipelines.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectChannel(Player player) {
|
||||
ServerGamePacketListenerImpl listener = ((CraftPlayer)player).getHandle().connection;
|
||||
Connection connection = ReflectionUtils.get(listener, NMSFields.SERVER_COMMON_PACKET_LISTENER_IMPL_connection);
|
||||
ChannelPipeline pipeline = connection.channel.pipeline();
|
||||
PipelineWrapper wrapper = new PipelineWrapper(player, connection.channel);
|
||||
ModelEngineChannelHandler handler = new ModelEngineChannelHandler(player, wrapper);
|
||||
this.pipelines.put(player.getUniqueId(), wrapper);
|
||||
this.bundles.put(player.getUniqueId(), new Bundler());
|
||||
|
||||
for (String name : pipeline.toMap().keySet()) {
|
||||
if (pipeline.get(name) instanceof Connection) {
|
||||
pipeline.addBefore(name, "protected_packet_unpacker", new ProtectedPacketUnpacker());
|
||||
pipeline.addBefore(name, "model_engine_packet_handler", handler);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ejectChannel(Player player) {
|
||||
ServerGamePacketListenerImpl listener = ((CraftPlayer)player).getHandle().connection;
|
||||
Connection connection = ReflectionUtils.get(listener, NMSFields.SERVER_COMMON_PACKET_LISTENER_IMPL_connection);
|
||||
Channel channel = connection.channel;
|
||||
channel.eventLoop().submit(() -> {
|
||||
channel.pipeline().remove("protected_packet_unpacker");
|
||||
channel.pipeline().remove("model_engine_packet_handler");
|
||||
return null;
|
||||
});
|
||||
this.removePipeline(player.getUniqueId());
|
||||
this.bundles.remove(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ping(UUID uuid) {
|
||||
this.getPipeline(uuid).ifPresent(wrapper -> {
|
||||
ClientDesyncMonitor monitor = wrapper.getDesyncMonitor();
|
||||
if (monitor.shouldPing()) {
|
||||
monitor.markPingSent();
|
||||
int id = monitor.getPingCounter().getAndIncrement();
|
||||
NetworkUtils.sendNow(uuid, new ClientboundPingPacket(id));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startBatch() {
|
||||
this.isBatching = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endBatch() {
|
||||
this.isBatching = false;
|
||||
|
||||
for (Entry<UUID, Bundler> entry : this.bundles.entrySet()) {
|
||||
PipelineWrapper pipeline = this.pipelines.get(entry.getKey());
|
||||
if (pipeline != null) {
|
||||
Bundler bundler = entry.getValue();
|
||||
synchronized (bundler) {
|
||||
bundler.bundle(pipeline::writeAndFlush);
|
||||
bundler.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void appendPacket(UUID uuid, Packet packet) {
|
||||
Bundler bundler = this.bundles.get(uuid);
|
||||
if (bundler != null) {
|
||||
bundler.appendPacket(packet);
|
||||
}
|
||||
}
|
||||
|
||||
public void appendPackets(UUID uuid, Collection<? extends Packet> collection) {
|
||||
Bundler bundler = this.bundles.get(uuid);
|
||||
if (bundler != null) {
|
||||
bundler.appendPacket(collection);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBatching() {
|
||||
return this.isBatching;
|
||||
}
|
||||
}
|
||||
+408
@@ -0,0 +1,408 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.network.patch;
|
||||
|
||||
import com.destroystokyo.paper.event.player.PlayerUseUnknownEntityEvent;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import com.mojang.logging.LogUtils;
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.ServerInfo;
|
||||
import com.ticxo.modelengine.api.entity.BaseEntity;
|
||||
import com.ticxo.modelengine.api.events.BaseEntityInteractEvent;
|
||||
import com.ticxo.modelengine.api.events.BaseEntityInteractEvent.Action;
|
||||
import com.ticxo.modelengine.api.model.ActiveModel;
|
||||
import com.ticxo.modelengine.api.nms.entity.EntityHandler;
|
||||
import com.ticxo.modelengine.api.utils.ReflectionUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.NMSMethods;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Nullable;
|
||||
import net.minecraft.advancements.CriteriaTriggers;
|
||||
import net.minecraft.network.protocol.PacketUtils;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
|
||||
import net.minecraft.network.protocol.game.ServerGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundUseItemPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket.Handler;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.ExperienceOrb;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.animal.Bucketable;
|
||||
import net.minecraft.world.entity.animal.allay.Allay;
|
||||
import net.minecraft.world.entity.item.ItemEntity;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.entity.projectile.AbstractArrow;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.level.ClipContext;
|
||||
import net.minecraft.world.level.GameType;
|
||||
import net.minecraft.world.level.ClipContext.Block;
|
||||
import net.minecraft.world.level.ClipContext.Fluid;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.HitResult;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraft.world.phys.HitResult.Type;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.CraftServer;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.event.CraftEventFactory;
|
||||
import org.bukkit.event.Event.Result;
|
||||
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.util.Consumer;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class PatchedServerGamePacketListener {
|
||||
private static final Logger LOGGER = LogUtils.getLogger();
|
||||
|
||||
public static void handleInteract(ServerboundInteractPacketWrapper interactPacket, ServerGamePacketListener listener) {
|
||||
if (listener instanceof ServerGamePacketListenerImpl connection) {
|
||||
PacketUtils.ensureRunningOnSameThread(interactPacket, connection, connection.getPlayer().serverLevel());
|
||||
final ServerPlayer player = connection.getPlayer();
|
||||
if (!player.isImmobile()) {
|
||||
final CraftPlayer craftPlayer = player.getBukkitEntity();
|
||||
final ServerLevel serverLevel = player.serverLevel();
|
||||
final CraftServer craftServer = serverLevel.getServer().server;
|
||||
final Entity entity = interactPacket.getTarget(serverLevel);
|
||||
interactPacket.dispatch(new Handler() {
|
||||
public void onInteraction(InteractionHand hand) {
|
||||
if (interactPacket.isFakeInteraction()) {
|
||||
EntityHandler entityHandler = ModelEngineAPI.getNMSHandler().getEntityHandler();
|
||||
entityHandler.forceUseItem(craftPlayer, EquipmentSlot.HAND);
|
||||
entityHandler.forceUseItem(craftPlayer, EquipmentSlot.OFF_HAND);
|
||||
}
|
||||
}
|
||||
|
||||
public void onInteraction(InteractionHand hand, Vec3 pos) {
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
}
|
||||
});
|
||||
if (entity != player || player.isSpectator()) {
|
||||
player.resetLastActionTime();
|
||||
player.setShiftKeyDown(interactPacket.isUsingSecondaryAction());
|
||||
final ActiveModel activeModel = ModelEngineAPI.getInteractionTracker().getModelRelay(interactPacket.getOriginalId());
|
||||
if (activeModel != null) {
|
||||
final BaseEntity<?> base = activeModel.getModeledEntity().getBase();
|
||||
interactPacket.dispatch(
|
||||
new Handler() {
|
||||
public void onInteraction(InteractionHand hand) {
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT,
|
||||
EquipmentSlot.HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.HAND),
|
||||
null
|
||||
)
|
||||
);
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT,
|
||||
EquipmentSlot.OFF_HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.OFF_HAND),
|
||||
null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onInteraction(InteractionHand hand, Vec3 pos) {
|
||||
Vector position = new Vector(pos.x, pos.y, pos.z);
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT_ON,
|
||||
EquipmentSlot.HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.HAND),
|
||||
position
|
||||
)
|
||||
);
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.INTERACT_ON,
|
||||
EquipmentSlot.OFF_HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.OFF_HAND),
|
||||
position
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
craftServer.getPluginManager()
|
||||
.callEvent(
|
||||
new BaseEntityInteractEvent(
|
||||
craftPlayer,
|
||||
base,
|
||||
activeModel,
|
||||
Action.ATTACK,
|
||||
EquipmentSlot.HAND,
|
||||
interactPacket.isUsingSecondaryAction(),
|
||||
craftPlayer.getInventory().getItem(EquipmentSlot.HAND),
|
||||
null
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (entity == null) {
|
||||
if (ServerInfo.IS_PAPER) {
|
||||
interactPacket.dispatch(
|
||||
new Handler() {
|
||||
public void onInteraction(InteractionHand hand) {
|
||||
PatchedServerGamePacketListener.callPlayerUseUnknownEntityEvent(craftServer, craftPlayer, interactPacket, hand, null);
|
||||
}
|
||||
|
||||
public void onInteraction(InteractionHand hand, Vec3 pos) {
|
||||
PatchedServerGamePacketListener.callPlayerUseUnknownEntityEvent(craftServer, craftPlayer, interactPacket, hand, pos);
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
PatchedServerGamePacketListener.callPlayerUseUnknownEntityEvent(
|
||||
craftServer, craftPlayer, interactPacket, InteractionHand.MAIN_HAND, null
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
} else if (serverLevel.getWorldBorder().isWithinBounds(entity.blockPosition())) {
|
||||
AABB boundingBox = entity.getBoundingBox();
|
||||
if (!(boundingBox.distanceToSqr(player.getEyePosition()) >= ServerGamePacketListenerImpl.MAX_INTERACTION_DISTANCE)) {
|
||||
interactPacket.dispatch(
|
||||
new Handler() {
|
||||
private void performInteraction(
|
||||
InteractionHand enumhand, PatchedServerGamePacketListener.EntityInteraction entityInteraction, PlayerInteractEntityEvent event
|
||||
) {
|
||||
ItemStack itemstack = player.getItemInHand(enumhand);
|
||||
if (itemstack.isItemEnabled(serverLevel.enabledFeatures())) {
|
||||
ItemStack itemstack1 = itemstack.copy();
|
||||
ItemStack itemInHand = player.getItemInHand(enumhand);
|
||||
boolean triggerLeashUpdate = itemInHand.getItem() == Items.LEAD && entity instanceof Mob;
|
||||
Item origItem = player.getInventory().getSelected().getItem();
|
||||
craftServer.getPluginManager().callEvent(event);
|
||||
if (entity instanceof Bucketable
|
||||
&& entity instanceof LivingEntity
|
||||
&& origItem.asItem() == Items.WATER_BUCKET
|
||||
&& (event.isCancelled() || player.getInventory().getSelected().getItem() != origItem)) {
|
||||
entity.getEntityData().resendPossiblyDesyncedEntity(player);
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
|
||||
if (triggerLeashUpdate && (event.isCancelled() || player.getInventory().getSelected().getItem() != origItem)) {
|
||||
connection.send(new ClientboundSetEntityLinkPacket(entity, ((Mob)entity).getLeashHolder()));
|
||||
}
|
||||
|
||||
if (event.isCancelled() || player.getInventory().getSelected().getItem() != origItem) {
|
||||
entity.getEntityData().refresh(player);
|
||||
if (entity instanceof Allay allay) {
|
||||
connection.send(
|
||||
new ClientboundSetEquipmentPacket(
|
||||
entity.getId(),
|
||||
Arrays.stream(net.minecraft.world.entity.EquipmentSlot.values())
|
||||
.map(slot -> Pair.of(slot, allay.stripMeta(allay.getItemBySlot(slot), true)))
|
||||
.collect(Collectors.toList())
|
||||
)
|
||||
);
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
}
|
||||
|
||||
if (event.isCancelled()) {
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
return;
|
||||
}
|
||||
|
||||
InteractionResult enuminteractionresult = entityInteraction.run(player, entity, enumhand);
|
||||
if (!itemInHand.isEmpty() && itemInHand.getCount() <= -1) {
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
|
||||
if (enuminteractionresult.consumesAction()) {
|
||||
CriteriaTriggers.PLAYER_INTERACTED_WITH_ENTITY.trigger(player, itemstack1, entity);
|
||||
if (enuminteractionresult.shouldSwing()) {
|
||||
player.swing(enumhand, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onInteraction(@NotNull InteractionHand hand) {
|
||||
this.performInteraction(
|
||||
hand,
|
||||
Player::interactOn,
|
||||
new PlayerInteractEntityEvent(
|
||||
connection.getCraftPlayer(),
|
||||
entity.getBukkitEntity(),
|
||||
hand == InteractionHand.OFF_HAND ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onInteraction(@NotNull InteractionHand hand, @NotNull Vec3 pos) {
|
||||
this.performInteraction(
|
||||
hand,
|
||||
(entityplayer, entity1, enumhand1) -> entity1.interactAt(entityplayer, pos, enumhand1),
|
||||
new PlayerInteractAtEntityEvent(
|
||||
connection.getCraftPlayer(),
|
||||
entity.getBukkitEntity(),
|
||||
new Vector(pos.x, pos.y, pos.z),
|
||||
hand == InteractionHand.OFF_HAND ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void onAttack() {
|
||||
if (!(entity instanceof ItemEntity)
|
||||
&& !(entity instanceof ExperienceOrb)
|
||||
&& !(entity instanceof AbstractArrow)
|
||||
&& (entity != player || player.isSpectator())) {
|
||||
ItemStack itemstack = player.getItemInHand(InteractionHand.MAIN_HAND);
|
||||
if (itemstack.isItemEnabled(serverLevel.enabledFeatures())) {
|
||||
player.attack(entity);
|
||||
if (!itemstack.isEmpty() && itemstack.getCount() <= -1) {
|
||||
player.containerMenu.sendAllDataToRemote();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void handleUseItem(ServerboundUseItemPacket packet, ServerGamePacketListener listener, Consumer<InteractionResult> afterUse) {
|
||||
if (listener instanceof ServerGamePacketListenerImpl connection) {
|
||||
ServerPlayer player = connection.getPlayer();
|
||||
PacketUtils.ensureRunningOnSameThread(packet, connection, player.serverLevel());
|
||||
if (!player.isImmobile()) {
|
||||
boolean shouldLimit = Boolean.TRUE
|
||||
.equals(ReflectionUtils.call(connection, NMSMethods.SERVER_GAME_PACKET_LISTENER_IMPL_checkLimit, packet.timestamp));
|
||||
if (shouldLimit) {
|
||||
connection.ackBlockChangesUpTo(packet.getSequence());
|
||||
ServerLevel worldserver = player.serverLevel();
|
||||
InteractionHand enumhand = packet.getHand();
|
||||
ItemStack itemstack = player.getItemInHand(enumhand);
|
||||
player.resetLastActionTime();
|
||||
if (!itemstack.isEmpty() && itemstack.isItemEnabled(worldserver.enabledFeatures())) {
|
||||
float f1 = player.getXRot();
|
||||
float f2 = player.getYRot();
|
||||
double d0 = player.getX();
|
||||
double d1 = player.getY() + player.getEyeHeight();
|
||||
double d2 = player.getZ();
|
||||
Vec3 vec3d = new Vec3(d0, d1, d2);
|
||||
float f3 = Mth.cos(-f2 * (float) (Math.PI / 180.0) - (float) Math.PI);
|
||||
float f4 = Mth.sin(-f2 * (float) (Math.PI / 180.0) - (float) Math.PI);
|
||||
float f5 = -Mth.cos(-f1 * (float) (Math.PI / 180.0));
|
||||
float f6 = Mth.sin(-f1 * (float) (Math.PI / 180.0));
|
||||
float f7 = f4 * f5;
|
||||
float f8 = f3 * f5;
|
||||
double d3 = player.gameMode.getGameModeForPlayer() == GameType.CREATIVE ? 5.0 : 4.5;
|
||||
Vec3 vec3d1 = vec3d.add(f7 * d3, f6 * d3, f8 * d3);
|
||||
HitResult movingobjectposition = player.level().clip(new ClipContext(vec3d, vec3d1, Block.OUTLINE, Fluid.NONE, player));
|
||||
boolean cancelled;
|
||||
if (movingobjectposition != null && movingobjectposition.getType() == Type.BLOCK) {
|
||||
BlockHitResult movingobjectpositionblock = (BlockHitResult)movingobjectposition;
|
||||
if (player.gameMode.firedInteract
|
||||
&& player.gameMode.interactPosition.equals(movingobjectpositionblock.getBlockPos())
|
||||
&& player.gameMode.interactHand == enumhand
|
||||
&& ItemStack.isSameItemSameTags(player.gameMode.interactItemStack, itemstack)) {
|
||||
cancelled = player.gameMode.interactResult;
|
||||
} else {
|
||||
PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(
|
||||
player,
|
||||
org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK,
|
||||
movingobjectpositionblock.getBlockPos(),
|
||||
movingobjectpositionblock.getDirection(),
|
||||
itemstack,
|
||||
true,
|
||||
enumhand,
|
||||
movingobjectpositionblock.getLocation()
|
||||
);
|
||||
cancelled = event.useItemInHand() == Result.DENY;
|
||||
}
|
||||
|
||||
player.gameMode.firedInteract = false;
|
||||
} else {
|
||||
PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(
|
||||
player, org.bukkit.event.block.Action.RIGHT_CLICK_AIR, itemstack, enumhand
|
||||
);
|
||||
cancelled = event.useItemInHand() == Result.DENY;
|
||||
}
|
||||
|
||||
if (cancelled) {
|
||||
player.resyncUsingItem(player);
|
||||
player.getBukkitEntity().updateInventory();
|
||||
return;
|
||||
}
|
||||
|
||||
itemstack = player.getItemInHand(enumhand);
|
||||
if (itemstack.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
InteractionResult enuminteractionresult = player.gameMode.useItem(player, worldserver, itemstack, enumhand);
|
||||
afterUse.accept(enuminteractionresult);
|
||||
if (enuminteractionresult.shouldSwing()) {
|
||||
player.swing(enumhand, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void callPlayerUseUnknownEntityEvent(
|
||||
CraftServer cserver, CraftPlayer craftPlayer, ServerboundInteractPacket packet, InteractionHand hand, @Nullable Vec3 vector
|
||||
) {
|
||||
cserver.getPluginManager()
|
||||
.callEvent(
|
||||
new PlayerUseUnknownEntityEvent(
|
||||
craftPlayer,
|
||||
packet.getEntityId(),
|
||||
packet.isAttack(),
|
||||
hand == InteractionHand.MAIN_HAND ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND,
|
||||
vector != null ? new Vector(vector.x, vector.y, vector.z) : null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface EntityInteraction {
|
||||
InteractionResult run(ServerPlayer var1, Entity var2, InteractionHand var3);
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.network.patch;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.interaction.InteractionTracker;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.NetworkUtils;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.game.ServerGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundInteractPacket.Handler;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ServerboundInteractPacketWrapper extends ServerboundInteractPacket {
|
||||
private final int originalId;
|
||||
private final int relayedId;
|
||||
private final ServerboundInteractPacket original;
|
||||
|
||||
public ServerboundInteractPacketWrapper(int originalId, int relayedId, ServerboundInteractPacket original) {
|
||||
super(NetworkUtils.readData(original));
|
||||
this.originalId = originalId;
|
||||
this.relayedId = relayedId;
|
||||
this.original = original;
|
||||
}
|
||||
|
||||
public void write(FriendlyByteBuf var0) {
|
||||
this.original.write(var0);
|
||||
}
|
||||
|
||||
public void handle(ServerGamePacketListener var0) {
|
||||
PatchedServerGamePacketListener.handleInteract(this, var0);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Entity getTarget(ServerLevel var0) {
|
||||
return var0.getEntityOrPart(this.relayedId);
|
||||
}
|
||||
|
||||
public boolean isFakeInteraction() {
|
||||
InteractionTracker tracker = ModelEngineAPI.getInteractionTracker();
|
||||
return tracker.getModelRelay(this.originalId) != null || tracker.getEntityRelay(this.originalId) != null;
|
||||
}
|
||||
|
||||
public boolean isUsingSecondaryAction() {
|
||||
return this.original.isUsingSecondaryAction();
|
||||
}
|
||||
|
||||
public void dispatch(Handler var0) {
|
||||
this.original.dispatch(var0);
|
||||
}
|
||||
|
||||
public int getOriginalId() {
|
||||
return this.originalId;
|
||||
}
|
||||
|
||||
public int getRelayedId() {
|
||||
return this.relayedId;
|
||||
}
|
||||
|
||||
public ServerboundInteractPacket getOriginal() {
|
||||
return this.original;
|
||||
}
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.network.utils;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class BulkEntityDataEncoder {
|
||||
private static final byte PACKET_TYPE_BULK_DATA = 0;
|
||||
public static final int FIELD_TRANSLATION = 0;
|
||||
public static final int FIELD_LEFT_ROTATION = 1;
|
||||
public static final int FIELD_SCALE = 2;
|
||||
public static final int FIELD_RIGHT_ROTATION = 3;
|
||||
public static final int FIELD_TRANSFORM_DURATION = 4;
|
||||
public static final int FIELD_GLOW_DATA = 5;
|
||||
public static final int FIELD_BRIGHTNESS = 6;
|
||||
public static final int FIELD_RENDER_DATA = 7;
|
||||
private final FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer(256));
|
||||
private int entryCount;
|
||||
private final int entryCountIndex;
|
||||
|
||||
public BulkEntityDataEncoder() {
|
||||
this.buf.writeByte(0);
|
||||
this.entryCountIndex = this.buf.writerIndex();
|
||||
this.buf.writeVarInt(0);
|
||||
this.entryCount = 0;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter startEntry(int entityId) {
|
||||
this.entryCount++;
|
||||
this.buf.writeVarInt(entityId);
|
||||
return new BulkEntityDataEncoder.EntryWriter(this.buf);
|
||||
}
|
||||
|
||||
public byte[] build() {
|
||||
int endIndex = this.buf.writerIndex();
|
||||
this.buf.writerIndex(this.entryCountIndex);
|
||||
this.buf.writeVarInt(this.entryCount);
|
||||
this.buf.writerIndex(endIndex);
|
||||
byte[] data = new byte[this.buf.readableBytes()];
|
||||
this.buf.getBytes(this.buf.readerIndex(), data);
|
||||
this.buf.release();
|
||||
return data;
|
||||
}
|
||||
|
||||
public int getEntryCount() {
|
||||
return this.entryCount;
|
||||
}
|
||||
|
||||
public static short floatToHalf(float f) {
|
||||
int bits = Float.floatToRawIntBits(f);
|
||||
int sign = bits >>> 16 & 32768;
|
||||
int exp = (bits >>> 23 & 0xFF) - 127;
|
||||
int mantissa = bits & 8388607;
|
||||
if (exp >= 16) {
|
||||
return (short)(sign | 31744);
|
||||
} else if (exp >= -14) {
|
||||
return (short)(sign | exp + 15 << 10 | mantissa >>> 13);
|
||||
} else if (exp >= -24) {
|
||||
int shift = -14 - exp;
|
||||
return (short)(sign | (8388608 | mantissa) >>> 13 + shift);
|
||||
} else {
|
||||
return (short)sign;
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeHalf(FriendlyByteBuf buf, float f) {
|
||||
buf.writeShort(floatToHalf(f));
|
||||
}
|
||||
|
||||
public static class EntryWriter {
|
||||
private final FriendlyByteBuf buf;
|
||||
private final int bitmaskIndex;
|
||||
private byte bitmask;
|
||||
|
||||
EntryWriter(FriendlyByteBuf buf) {
|
||||
this.buf = buf;
|
||||
this.bitmaskIndex = buf.writerIndex();
|
||||
buf.writeByte(0);
|
||||
this.bitmask = 0;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter translation(Vector3f pos) {
|
||||
this.bitmask = (byte)(this.bitmask | 1);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, pos.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, pos.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, pos.z);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter leftRotation(Quaternionf rot) {
|
||||
this.bitmask = (byte)(this.bitmask | 2);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.z);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.w);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter scale(Vector3f scale) {
|
||||
this.bitmask = (byte)(this.bitmask | 4);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, scale.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, scale.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, scale.z);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter rightRotation(Quaternionf rot) {
|
||||
this.bitmask = (byte)(this.bitmask | 8);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.x);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.y);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.z);
|
||||
BulkEntityDataEncoder.writeHalf(this.buf, rot.w);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter transformDuration(int duration) {
|
||||
this.bitmask = (byte)(this.bitmask | 16);
|
||||
this.buf.writeVarInt(duration);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter glowData(byte sharedData, int glowColor) {
|
||||
this.bitmask = (byte)(this.bitmask | 32);
|
||||
this.buf.writeByte(sharedData);
|
||||
this.buf.writeInt(glowColor);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter brightness(int brightness) {
|
||||
this.bitmask = (byte)(this.bitmask | 64);
|
||||
this.buf.writeInt(brightness);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BulkEntityDataEncoder.EntryWriter renderData(byte billboard, float viewRange, byte displayType) {
|
||||
this.bitmask = (byte)(this.bitmask | 128);
|
||||
this.buf.writeByte(billboard);
|
||||
this.buf.writeFloat(viewRange);
|
||||
this.buf.writeByte(displayType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void finish() {
|
||||
this.buf.setByte(this.bitmaskIndex, this.bitmask);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.network.utils;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.nms.network.ProtectedPacket;
|
||||
import com.ticxo.modelengine.api.utils.config.ConfigProperty;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
|
||||
|
||||
public class Bundler {
|
||||
private static int BUNDLE_SIZE;
|
||||
private final List<List<Packet<ClientGamePacketListener>>> bundles = new ArrayList<>();
|
||||
|
||||
public synchronized void appendPacket(Packet<ClientGamePacketListener> packet) {
|
||||
if (this.bundles.isEmpty()) {
|
||||
this.bundles.add(new ArrayList<>());
|
||||
}
|
||||
|
||||
List<Packet<ClientGamePacketListener>> bundleList = this.bundles.get(this.bundles.size() - 1);
|
||||
if (bundleList.size() == BUNDLE_SIZE) {
|
||||
bundleList = new ArrayList<>();
|
||||
this.bundles.add(bundleList);
|
||||
}
|
||||
|
||||
bundleList.add(packet);
|
||||
}
|
||||
|
||||
public synchronized void appendPacket(Collection<? extends Packet> packets) {
|
||||
if (!packets.isEmpty()) {
|
||||
List<Packet<ClientGamePacketListener>> bundleList = this.bundles.isEmpty() ? null : this.bundles.get(this.bundles.size() - 1);
|
||||
if (bundleList == null || bundleList.size() + packets.size() > BUNDLE_SIZE) {
|
||||
bundleList = new ArrayList<>();
|
||||
this.bundles.add(bundleList);
|
||||
}
|
||||
|
||||
for (Packet p : packets) {
|
||||
if (bundleList.size() == BUNDLE_SIZE) {
|
||||
bundleList = new ArrayList<>();
|
||||
this.bundles.add(bundleList);
|
||||
}
|
||||
|
||||
bundleList.add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.bundles.clear();
|
||||
}
|
||||
|
||||
public void bundle(Consumer<Object> send) {
|
||||
for (List<Packet<ClientGamePacketListener>> list : this.bundles) {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(new ClientboundBundlePacket(list));
|
||||
send.accept(wrapped);
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
ModelEngineAPI.getAPI().getConfigManager().registerReferenceUpdate(() -> BUNDLE_SIZE = Math.min(ConfigProperty.BUNDLE_SIZE.getInt(), 4096));
|
||||
}
|
||||
}
|
||||
+271
@@ -0,0 +1,271 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.network.utils;
|
||||
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.lod.AnimationLODHandler.LODTracker;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
import com.ticxo.modelengine.api.nms.network.ProtectedPacket;
|
||||
import com.ticxo.modelengine.api.utils.data.PositionCodec;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.NetworkHandlerImpl;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Pos;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class NetworkUtils {
|
||||
public static FriendlyByteBuf createByteBuf() {
|
||||
return new FriendlyByteBuf(Unpooled.buffer());
|
||||
}
|
||||
|
||||
public static FriendlyByteBuf createByteBuf(Consumer<FriendlyByteBuf> consumer) {
|
||||
FriendlyByteBuf buf = createByteBuf();
|
||||
consumer.accept(buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
public static FriendlyByteBuf readData(Packet<?> packet) {
|
||||
FriendlyByteBuf buf = createByteBuf();
|
||||
packet.write(buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
public static Packets.PacketSupplier createPivotSpawn(int id, UUID uuid, Vector3f pos) {
|
||||
return playerUUID -> ModelEngineAPI.getPlayerProtocolVersion(playerUUID) >= 764
|
||||
? new ClientboundAddEntityPacket(id, uuid, pos.x, pos.y - 0.5, pos.z, 0.0F, 0.0F, EntityType.AREA_EFFECT_CLOUD, 0, Vec3.ZERO, 0.0)
|
||||
: new ClientboundAddEntityPacket(id, uuid, pos.x, pos.y - 0.375, pos.z, 0.0F, 0.0F, EntityType.AREA_EFFECT_CLOUD, 0, Vec3.ZERO, 0.0);
|
||||
}
|
||||
|
||||
public static Packets.PacketSupplier createPivotTeleport(int id, Vector3f pos) {
|
||||
FriendlyByteBuf buf = createByteBuf();
|
||||
buf.writeVarInt(id);
|
||||
buf.writeDouble(pos.x);
|
||||
buf.writeDouble(pos.y - 0.375);
|
||||
buf.writeDouble(pos.z);
|
||||
buf.writeByte(0);
|
||||
buf.writeByte(0);
|
||||
buf.writeBoolean(false);
|
||||
ClientboundTeleportEntityPacket tpHigher = new ClientboundTeleportEntityPacket(buf);
|
||||
buf = createByteBuf();
|
||||
buf.writeVarInt(id);
|
||||
buf.writeDouble(pos.x);
|
||||
buf.writeDouble(pos.y - 0.5);
|
||||
buf.writeDouble(pos.z);
|
||||
buf.writeByte(0);
|
||||
buf.writeByte(0);
|
||||
buf.writeBoolean(false);
|
||||
ClientboundTeleportEntityPacket tpLower = new ClientboundTeleportEntityPacket(buf);
|
||||
return playerUUID -> ModelEngineAPI.getPlayerProtocolVersion(playerUUID) >= 764 ? tpLower : tpHigher;
|
||||
}
|
||||
|
||||
public static Object createPivotMove(int id, @Nullable PositionCodec codec, Vector3f pos) {
|
||||
if (codec == null) {
|
||||
return createPivotTeleport(id, pos);
|
||||
} else {
|
||||
return switch (codec.update(pos.x, pos.y, pos.z)) {
|
||||
case UNCHANGED -> null;
|
||||
case MOVE -> new Pos(id, codec.getDeltaX(), codec.getDeltaY(), codec.getDeltaZ(), false);
|
||||
case ABSOLUTE -> createPivotTeleport(id, pos);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static Packets.PacketSupplier lodWrapper(ModeledEntity modeledEntity, BiFunction<UUID, LODTracker, Packet<ClientGamePacketListener>> packetFunction) {
|
||||
return uuid -> {
|
||||
LODTracker tracker = modeledEntity.getAnimationLodHandler().tick(uuid);
|
||||
return tracker.isCanSkip() ? null : packetFunction.apply(uuid, tracker);
|
||||
};
|
||||
}
|
||||
|
||||
public static void send(UUID target, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
handler.appendPacket(target, packet);
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void send(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
handler.appendPacket(player, packet);
|
||||
}
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void send(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet, Predicate<Player> predicate) {
|
||||
if (packet != null) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
handler.appendPacket(player, packet);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
|
||||
for (UUID playerx : targets) {
|
||||
Player p = Bukkit.getPlayer(playerx);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(playerx).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRaw(UUID target, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRaw(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRaw(Set<UUID> targets, @Nullable Packet<? super ClientGamePacketListener> packet, Predicate<Player> predicate) {
|
||||
if (packet != null) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendNow(UUID target, @Nullable Packet<? super ClientGamePacketListener> packet) {
|
||||
if (packet != null) {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(packet);
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlushNow(wrapped));
|
||||
}
|
||||
}
|
||||
|
||||
private static Packet<ClientGamePacketListener> bundle(Collection<Packet<ClientGamePacketListener>> packets) {
|
||||
return (Packet<ClientGamePacketListener>)(packets.size() == 1 ? packets.iterator().next() : new ClientboundBundlePacket(packets));
|
||||
}
|
||||
|
||||
public static void sendBundled(UUID target, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
collection.compile(target, packets -> {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
handler.appendPackets(target, packets);
|
||||
} else {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(bundle(packets));
|
||||
handler.getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(wrapped));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundled(Set<UUID> targets, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
collection.compile(player, packets -> handler.appendPackets(player, packets));
|
||||
}
|
||||
} else {
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> collection.compile(player, packets -> {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(bundle(packets));
|
||||
pipeline.writeAndFlush(wrapped);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundled(Set<UUID> targets, Packets collection, Predicate<Player> predicate) {
|
||||
if (!collection.isEmpty()) {
|
||||
NetworkHandlerImpl handler = NetworkHandlerImpl.instance;
|
||||
if (handler.isBatching()) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
collection.compile(player, packets -> handler.appendPackets(player, packets));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (UUID playerx : targets) {
|
||||
Player p = Bukkit.getPlayer(playerx);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(playerx).ifPresent(pipeline -> collection.compile(playerx, packets -> {
|
||||
ProtectedPacket wrapped = new ProtectedPacket(bundle(packets));
|
||||
pipeline.writeAndFlush(wrapped);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundledRaw(UUID target, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
collection.compile(target, packets -> {
|
||||
Packet<ClientGamePacketListener> packet = bundle(packets);
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(target).ifPresent(pipeline -> pipeline.writeAndFlush(packet));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundledRaw(Set<UUID> targets, Packets collection) {
|
||||
if (!collection.isEmpty()) {
|
||||
for (UUID player : targets) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> collection.compile(player, packets -> {
|
||||
Packet<ClientGamePacketListener> packet = bundle(packets);
|
||||
pipeline.writeAndFlush(packet);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBundledRaw(Set<UUID> targets, Packets collection, Predicate<Player> predicate) {
|
||||
if (!collection.isEmpty()) {
|
||||
for (UUID player : targets) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if (p != null && predicate.test(p)) {
|
||||
ModelEngineAPI.getNetworkHandler().getPipeline(player).ifPresent(pipeline -> collection.compile(player, packets -> {
|
||||
Packet<ClientGamePacketListener> packet = bundle(packets);
|
||||
pipeline.writeAndFlush(packet);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.network.utils;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ticxo.modelengine.api.utils.logger.TLogger;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
|
||||
public class PacketInterceptor {
|
||||
private final Map<Class<? extends Packet>, PacketInterceptor.Modifier<? extends Packet>> registry = Maps.newConcurrentMap();
|
||||
private final Map<Class<? extends Packet>, PacketInterceptor.Listener<? extends Packet>> postRegistry = Maps.newConcurrentMap();
|
||||
|
||||
public <T extends Packet> PacketInterceptor register(Class<T> clazz, Function<T, Packet> function) {
|
||||
this.registry.put(clazz, new Modifier<>(clazz, function));
|
||||
return this;
|
||||
}
|
||||
|
||||
public <T extends Packet> PacketInterceptor registerPost(Class<T> clazz, Function<T, Collection<Packet>> consumer) {
|
||||
this.postRegistry.put(clazz, new Listener<>(clazz, consumer));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packet accept(Packet original) {
|
||||
if (original == null) {
|
||||
return null;
|
||||
} else {
|
||||
PacketInterceptor.Modifier<?> modifier = this.registry.get(original.getClass());
|
||||
return modifier == null ? original : modifier.modify(original);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Packet> acceptPost(Packet original) {
|
||||
if (original == null) {
|
||||
return List.of();
|
||||
} else {
|
||||
PacketInterceptor.Listener<?> listener = this.postRegistry.get(original.getClass());
|
||||
return (Collection<Packet>)(listener != null ? listener.listen(original) : List.of());
|
||||
}
|
||||
}
|
||||
|
||||
static class Listener<T extends Packet> {
|
||||
private final Class<T> clazz;
|
||||
private final Function<T, Collection<Packet>> function;
|
||||
|
||||
public Collection<Packet> listen(Packet original) {
|
||||
try {
|
||||
Collection<Packet> collection = this.function.apply(this.clazz.cast(original));
|
||||
return (Collection<Packet>)(collection == null ? List.of() : collection);
|
||||
} catch (Throwable var3) {
|
||||
TLogger.error("An error had occurred while intercepting the packet " + this.clazz.getSimpleName());
|
||||
var3.printStackTrace();
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
public Listener(Class<T> clazz, Function<T, Collection<Packet>> function) {
|
||||
this.clazz = clazz;
|
||||
this.function = function;
|
||||
}
|
||||
}
|
||||
|
||||
static class Modifier<T extends Packet> {
|
||||
private final Class<T> clazz;
|
||||
private final Function<T, Packet> function;
|
||||
|
||||
public Packet modify(Packet original) {
|
||||
try {
|
||||
return this.function.apply(this.clazz.cast(original));
|
||||
} catch (Throwable var3) {
|
||||
TLogger.error("An error had occurred while modifying the packet " + this.clazz.getSimpleName());
|
||||
var3.printStackTrace();
|
||||
return original;
|
||||
}
|
||||
}
|
||||
|
||||
public Modifier(Class<T> clazz, Function<T, Packet> function) {
|
||||
this.clazz = clazz;
|
||||
this.function = function;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.network.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class Packets extends ArrayList<Object> {
|
||||
public Packets(int initialCapacity, float loadFactor) {
|
||||
super(initialCapacity);
|
||||
}
|
||||
|
||||
public Packets(int initialCapacity) {
|
||||
super(initialCapacity);
|
||||
}
|
||||
|
||||
public Packets() {
|
||||
}
|
||||
|
||||
public Packets(@NotNull Collection<?> c) {
|
||||
super((Collection<? extends Object>)c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Object obj) {
|
||||
return obj == null ? false : super.add(obj);
|
||||
}
|
||||
|
||||
public boolean add(Packet<ClientGamePacketListener> packet) {
|
||||
return packet == null ? false : super.add(packet);
|
||||
}
|
||||
|
||||
private Collection<Packet<ClientGamePacketListener>> compile(UUID player) {
|
||||
Collection<Packet<ClientGamePacketListener>> list = new ArrayList<>();
|
||||
|
||||
for (Object obj : this) {
|
||||
if (obj instanceof Packet) {
|
||||
list.add((Packet<ClientGamePacketListener>)obj);
|
||||
} else if (obj instanceof Packets.PacketSupplier supplier) {
|
||||
Packet<ClientGamePacketListener> packet = supplier.supply(player);
|
||||
if (packet != null) {
|
||||
list.add(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public void compile(UUID player, Consumer<Collection<Packet<ClientGamePacketListener>>> ifNotEmpty) {
|
||||
Collection<Packet<ClientGamePacketListener>> set = this.compile(player);
|
||||
if (!set.isEmpty()) {
|
||||
ifNotEmpty.accept(set);
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PacketSupplier {
|
||||
Packet<ClientGamePacketListener> supply(UUID var1);
|
||||
}
|
||||
}
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.HeldItemRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.HeldItemRenderer.Item;
|
||||
import com.ticxo.modelengine.api.utils.data.PositionCodec;
|
||||
import com.ticxo.modelengine.api.utils.data.tracker.CollectionDataTracker;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.Packets;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Pos;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack;
|
||||
|
||||
public class HeldItemParser implements BehaviorRendererParser<HeldItemRenderer> {
|
||||
private float viewRange = 4096.0F;
|
||||
|
||||
public void sendToClients(HeldItemRenderer renderer) {
|
||||
this.viewRange = renderer.getActiveModel().getViewRange();
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
public void destroy(HeldItemRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, HeldItemRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
Location location = renderer.getActiveModel().getModeledEntity().getBase().getLocation();
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotSpawn(location, renderer));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(renderer));
|
||||
|
||||
for (Item item : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemSpawn(location, item));
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, true));
|
||||
}
|
||||
|
||||
for (Item item : renderer.getSpawnQueue().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemSpawn(location, item));
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, true));
|
||||
}
|
||||
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(renderer));
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, HeldItemRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
set.add(this.teleport(renderer));
|
||||
|
||||
for (Item item : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, false));
|
||||
}
|
||||
|
||||
Location location = renderer.getActiveModel().getModeledEntity().getBase().getLocation();
|
||||
|
||||
for (Item item : renderer.getSpawnQueue().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemSpawn(location, item));
|
||||
set.add((Packet<ClientGamePacketListener>)this.itemData(item, true));
|
||||
}
|
||||
|
||||
Map<String, Item> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size());
|
||||
destroy.forEach((s, item) -> buf.writeVarInt(item.getId()));
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
if (renderer.getPassengers().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(renderer));
|
||||
renderer.getPassengers().clearDirty();
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, HeldItemRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Map<String, Item> items = renderer.getRendered();
|
||||
buf.writeVarInt(1 + items.size());
|
||||
buf.writeVarInt(renderer.getId());
|
||||
items.forEach((s, item) -> buf.writeVarInt(item.getId()));
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket pivotSpawn(Location location, HeldItemRenderer renderer) {
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getId(), renderer.getUuid(), location.getX(), location.getY(), location.getZ(), 0.0F, 0.0F, EntityType.ARMOR_STAND, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(HeldItemRenderer renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 15, EntityDataSerializers.BYTE, (byte)16);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket itemSpawn(Location location, Item renderer) {
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getId(), renderer.getUuid(), location.getX(), location.getY(), location.getZ(), 0.0F, 0.0F, EntityType.ITEM_DISPLAY, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket itemData(Item renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
if (spawn) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
EntityUtils.writeData(buf, 9, EntityDataSerializers.INT, 1);
|
||||
EntityUtils.writeData(buf, 17, EntityDataSerializers.FLOAT, this.viewRange);
|
||||
} else if (renderer.isTransformDirty()) {
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.INT, 0);
|
||||
}
|
||||
|
||||
renderer.getGlowing().ifDirty(flag -> EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)(flag ? 96 : 32)), spawn);
|
||||
renderer.getGlowColor().ifDirty(color -> EntityUtils.writeData(buf, 22, EntityDataSerializers.INT, color), spawn);
|
||||
renderer.getPosition().ifDirty(vector3f -> EntityUtils.writeData(buf, 11, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
renderer.getScale().ifDirty(vector3f -> EntityUtils.writeData(buf, 12, EntityDataSerializers.VECTOR3, vector3f), spawn);
|
||||
renderer.getRotation().ifDirty(quaternionf -> EntityUtils.writeData(buf, 13, EntityDataSerializers.QUATERNION, quaternionf), spawn);
|
||||
renderer.getModel().ifDirty(itemStack -> EntityUtils.writeData(buf, 23, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(itemStack)), spawn);
|
||||
renderer.getDisplay()
|
||||
.ifDirty(display -> EntityUtils.writeData(buf, 24, EntityDataSerializers.BYTE, display == null ? 0 : (byte)display.ordinal()), spawn);
|
||||
buf.writeByte(255);
|
||||
renderer.clearDirty();
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
}
|
||||
|
||||
private Object teleport(HeldItemRenderer renderer) {
|
||||
Location location = renderer.getActiveModel().getModeledEntity().getBase().getLocation();
|
||||
PositionCodec codec = renderer.getPositionCodec();
|
||||
if (codec != null) {
|
||||
switch (codec.update(location.getX(), location.getY(), location.getZ())) {
|
||||
case UNCHANGED:
|
||||
return null;
|
||||
case MOVE:
|
||||
return new Pos(renderer.getId(), codec.getDeltaX(), codec.getDeltaY(), codec.getDeltaZ(), false);
|
||||
case ABSOLUTE:
|
||||
}
|
||||
}
|
||||
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
buf.writeDouble(location.getX());
|
||||
buf.writeDouble(location.getY());
|
||||
buf.writeDouble(location.getZ());
|
||||
buf.writeByte(0);
|
||||
buf.writeByte(0);
|
||||
buf.writeBoolean(false);
|
||||
return new ClientboundTeleportEntityPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket mount(HeldItemRenderer renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getId());
|
||||
CollectionDataTracker<Integer> set = renderer.getPassengers();
|
||||
buf.writeVarInt(set.size());
|
||||
set.forEach(buf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
}
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.parser.behavior;
|
||||
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.LeashRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.LeashRenderer.Leash;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.Packets;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class LeashParser implements BehaviorRendererParser<LeashRenderer> {
|
||||
public void sendToClients(LeashRenderer renderer) {
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
HashSet<UUID> stop = new HashSet<>(data.getStopTracking());
|
||||
stop.removeAll(data.getTracking().keySet());
|
||||
this.remove(stop, renderer);
|
||||
}
|
||||
|
||||
public void destroy(LeashRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, LeashRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Leash leash : renderer.getRendered().values()) {
|
||||
set.add(this.pivotSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, true));
|
||||
}
|
||||
|
||||
for (Leash leash : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, true));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, LeashRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Leash leash : renderer.getRendered().values()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, false));
|
||||
set.add(this.move(leash));
|
||||
}
|
||||
|
||||
for (Leash leash : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashSpawn(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.leashData(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(leash));
|
||||
set.add((Packet<ClientGamePacketListener>)this.link(leash, true));
|
||||
}
|
||||
|
||||
Map<String, Leash> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
set.add(
|
||||
(Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(
|
||||
IntArrayList.toList(destroy.values().stream().mapMultiToInt((leashx, intConsumer) -> {
|
||||
intConsumer.accept(leashx.getLeashId());
|
||||
intConsumer.accept(leashx.getPivotId());
|
||||
}))
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, LeashRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Map<String, Leash> leashes = renderer.getRendered();
|
||||
buf.writeVarInt(leashes.size());
|
||||
leashes.forEach((s, leash) -> buf.writeVarInt(leash.getLeashId()));
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(Leash renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return NetworkUtils.createPivotSpawn(renderer.getPivotId(), renderer.getPivotUUID(), pos);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket leashSpawn(Leash renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getLeashId(), renderer.getLeastUUID(), pos.x, pos.y, pos.z, 0.0F, 0.0F, EntityType.SLIME, 0, Vec3.ZERO, 0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(Leash renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getLeashId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket leashData(Leash renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getLeashId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 16, EntityDataSerializers.INT, 0);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket mount(Leash renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(renderer.getLeashId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private Object move(Leash renderer) {
|
||||
if (!renderer.getPosition().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return NetworkUtils.createPivotMove(renderer.getPivotId(), renderer.getPositionCodec(), pos);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientboundSetEntityLinkPacket link(Leash renderer, boolean spawn) {
|
||||
if (!spawn && !renderer.getConnected().isDirty()) {
|
||||
return null;
|
||||
} else {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeInt(renderer.getLeashId());
|
||||
buf.writeInt(renderer.getConnected().get());
|
||||
renderer.getConnected().clearDirty();
|
||||
return new ClientboundSetEntityLinkPacket(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
package com.ticxo.modelengine.v1_20_R2.parser.behavior;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.ticxo.modelengine.api.entity.data.IEntityData;
|
||||
import com.ticxo.modelengine.api.model.bone.render.BehaviorRendererParser;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer;
|
||||
import com.ticxo.modelengine.api.model.bone.render.renderer.MountRenderer.Mount;
|
||||
import com.ticxo.modelengine.api.utils.data.tracker.CollectionDataTracker;
|
||||
import com.ticxo.modelengine.api.utils.math.TMath;
|
||||
import com.ticxo.modelengine.v1_20_R2.entity.EntityUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.NetworkUtils;
|
||||
import com.ticxo.modelengine.v1_20_R2.network.utils.Packets;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.Rot;
|
||||
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket.AttributeSnapshot;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.ai.attributes.Attributes;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class MountParser implements BehaviorRendererParser<MountRenderer> {
|
||||
public void sendToClients(MountRenderer renderer) {
|
||||
IEntityData data = renderer.getModelRenderer().getActiveModel().getModeledEntity().getBase().getData();
|
||||
this.update(data.getTracking().keySet(), renderer);
|
||||
this.spawn(data.getStartTracking(), renderer);
|
||||
this.remove(data.getStopTracking(), renderer);
|
||||
}
|
||||
|
||||
public void destroy(MountRenderer renderer) {
|
||||
IEntityData data = renderer.getActiveModel().getModeledEntity().getBase().getData();
|
||||
HashSet<UUID> inRange = new HashSet<>(data.getStartTracking());
|
||||
inRange.addAll(data.getTracking().keySet());
|
||||
inRange.addAll(data.getStopTracking());
|
||||
this.remove(inRange, renderer);
|
||||
}
|
||||
|
||||
private void spawn(Set<UUID> targets, MountRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Mount mount : renderer.getRendered().values()) {
|
||||
set.add(this.pivotSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountHealth(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountMaxHealth(mount));
|
||||
}
|
||||
|
||||
for (Mount mount : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountHealth(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountMaxHealth(mount));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void update(Set<UUID> targets, MountRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
Packets set = new Packets();
|
||||
|
||||
for (Mount mount : renderer.getRendered().values()) {
|
||||
if (mount.getPosition().isDirty()) {
|
||||
set.add(this.pivotMove(mount));
|
||||
mount.getPosition().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getYaw().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountRotate(mount));
|
||||
mount.getYaw().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getHealth().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountHealth(mount));
|
||||
mount.getHealth().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getMaxHealth().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountMaxHealth(mount));
|
||||
mount.getMaxHealth().clearDirty();
|
||||
}
|
||||
|
||||
if (mount.getPassengers().isDirty()) {
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
mount.getPassengers().clearDirty();
|
||||
}
|
||||
}
|
||||
|
||||
for (Mount mount : renderer.getSpawnQueue().values()) {
|
||||
set.add(this.pivotSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountSpawn(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mountData(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.pivotMount(mount));
|
||||
set.add((Packet<ClientGamePacketListener>)this.mount(mount));
|
||||
}
|
||||
|
||||
Map<String, Mount> destroy = renderer.getDestroyQueue();
|
||||
if (!destroy.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(destroy.size() * 2);
|
||||
destroy.forEach((s, mount) -> {
|
||||
buf.writeVarInt(mount.getPivotId());
|
||||
buf.writeVarInt(mount.getMountId());
|
||||
});
|
||||
set.add((Packet<ClientGamePacketListener>)(new ClientboundRemoveEntitiesPacket(buf)));
|
||||
}
|
||||
|
||||
NetworkUtils.sendBundled(targets, set);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(Set<UUID> targets, MountRenderer renderer) {
|
||||
if (!targets.isEmpty()) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Collection<Mount> mounts = renderer.getRendered().values();
|
||||
buf.writeVarInt(mounts.size() * 2);
|
||||
mounts.forEach(mount -> {
|
||||
buf.writeVarInt(mount.getPivotId());
|
||||
buf.writeVarInt(mount.getMountId());
|
||||
});
|
||||
NetworkUtils.send(targets, new ClientboundRemoveEntitiesPacket(buf));
|
||||
}
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotSpawn(Mount renderer) {
|
||||
return NetworkUtils.createPivotSpawn(renderer.getPivotId(), renderer.getPivotUuid(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket pivotData(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 8, EntityDataSerializers.FLOAT, 0.0F);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundAddEntityPacket mountSpawn(Mount renderer) {
|
||||
Vector3f pos = renderer.getPosition().get();
|
||||
return new ClientboundAddEntityPacket(
|
||||
renderer.getMountId(),
|
||||
renderer.getMountUuid(),
|
||||
pos.x,
|
||||
pos.y,
|
||||
pos.z,
|
||||
0.0F,
|
||||
TMath.byteToRot(renderer.getYaw().get()),
|
||||
EntityType.ARMOR_STAND,
|
||||
0,
|
||||
Vec3.ZERO,
|
||||
0.0
|
||||
);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket mountData(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
EntityUtils.writeData(buf, 0, EntityDataSerializers.BYTE, (byte)32);
|
||||
EntityUtils.writeData(buf, 1, EntityDataSerializers.INT, Integer.MAX_VALUE);
|
||||
EntityUtils.writeData(buf, 15, EntityDataSerializers.BYTE, (byte)16);
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket pivotMount(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getPivotId());
|
||||
buf.writeVarInt(1);
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundSetPassengersPacket mount(Mount renderer) {
|
||||
CollectionDataTracker<Integer> ids = renderer.getPassengers();
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
buf.writeVarInt(ids.size());
|
||||
ids.forEach(buf::writeVarInt);
|
||||
return new ClientboundSetPassengersPacket(buf);
|
||||
}
|
||||
|
||||
private Packets.PacketSupplier pivotMove(Mount renderer) {
|
||||
return NetworkUtils.createPivotTeleport(renderer.getPivotId(), renderer.getPosition().get());
|
||||
}
|
||||
|
||||
private Rot mountRotate(Mount renderer) {
|
||||
FriendlyByteBuf rot = NetworkUtils.createByteBuf();
|
||||
rot.writeVarInt(renderer.getMountId());
|
||||
rot.writeByte(renderer.getYaw().get());
|
||||
rot.writeByte(0);
|
||||
rot.writeBoolean(false);
|
||||
return Rot.read(rot);
|
||||
}
|
||||
|
||||
private ClientboundSetEntityDataPacket mountHealth(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
EntityUtils.writeData(buf, 9, EntityDataSerializers.FLOAT, renderer.getHealth().get());
|
||||
buf.writeByte(255);
|
||||
return new ClientboundSetEntityDataPacket(buf);
|
||||
}
|
||||
|
||||
private ClientboundUpdateAttributesPacket mountMaxHealth(Mount renderer) {
|
||||
FriendlyByteBuf buf = NetworkUtils.createByteBuf();
|
||||
Collection<AttributeSnapshot> attributes = Lists.newArrayList();
|
||||
attributes.add(new AttributeSnapshot(Attributes.MAX_HEALTH, renderer.getMaxHealth().get().floatValue(), Lists.newArrayList()));
|
||||
buf.writeVarInt(renderer.getMountId());
|
||||
buf.writeCollection(attributes, (buf2, attribute) -> {
|
||||
buf2.writeResourceLocation(BuiltInRegistries.ATTRIBUTE.getKey(attribute.getAttribute()));
|
||||
buf2.writeDouble(attribute.getBase());
|
||||
buf2.writeCollection(attribute.getModifiers(), (buf3, modifier) -> {
|
||||
buf3.writeUUID(modifier.getId());
|
||||
buf3.writeDouble(modifier.getAmount());
|
||||
buf3.writeByte(modifier.getOperation().toValue());
|
||||
});
|
||||
});
|
||||
return new ClientboundUpdateAttributesPacket(buf);
|
||||
}
|
||||
}
|
||||
Loaded 100 of 1072 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user