feat: enhance EntityLib with packet dispatcher and server version check
This commit is contained in:
parent
35111c0149
commit
d7eda34611
5 changed files with 63 additions and 37 deletions
|
@ -13,6 +13,7 @@ public final class APIConfig {
|
||||||
private boolean platformLogger = false;
|
private boolean platformLogger = false;
|
||||||
private boolean bstats = true;
|
private boolean bstats = true;
|
||||||
private boolean forceBundle = false;
|
private boolean forceBundle = false;
|
||||||
|
private boolean ignoreServerVersionVerify = false;
|
||||||
|
|
||||||
public APIConfig(PacketEventsAPI<?> packetEvents) {
|
public APIConfig(PacketEventsAPI<?> packetEvents) {
|
||||||
this.packetEvents = packetEvents;
|
this.packetEvents = packetEvents;
|
||||||
|
@ -53,6 +54,11 @@ public final class APIConfig {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public @NotNull APIConfig ignoreServerVersionVerify() {
|
||||||
|
this.ignoreServerVersionVerify = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isDebugMode() {
|
public boolean isDebugMode() {
|
||||||
return debugMode;
|
return debugMode;
|
||||||
}
|
}
|
||||||
|
@ -83,4 +89,7 @@ public final class APIConfig {
|
||||||
&& EntityLib.getOptionalApi().get().getPacketEvents().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_19_4);
|
&& EntityLib.getOptionalApi().get().getPacketEvents().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_19_4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean shouldIgnoreServerVersionVerify() {
|
||||||
|
return ignoreServerVersionVerify;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.github.retrooper.packetevents.PacketEventsAPI;
|
||||||
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
|
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
|
||||||
import com.github.retrooper.packetevents.protocol.player.UserProfile;
|
import com.github.retrooper.packetevents.protocol.player.UserProfile;
|
||||||
import com.github.retrooper.packetevents.protocol.world.Location;
|
import com.github.retrooper.packetevents.protocol.world.Location;
|
||||||
|
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
|
||||||
import me.tofaa.entitylib.container.EntityContainer;
|
import me.tofaa.entitylib.container.EntityContainer;
|
||||||
import me.tofaa.entitylib.tick.TickContainer;
|
import me.tofaa.entitylib.tick.TickContainer;
|
||||||
import me.tofaa.entitylib.wrapper.WrapperEntity;
|
import me.tofaa.entitylib.wrapper.WrapperEntity;
|
||||||
|
@ -14,6 +15,8 @@ import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the API for EntityLib.
|
* Represents the API for EntityLib.
|
||||||
|
@ -55,7 +58,8 @@ public interface EntityLibAPI<T> {
|
||||||
*/
|
*/
|
||||||
void addTickContainer(@NotNull TickContainer<?, T> tickContainer);
|
void addTickContainer(@NotNull TickContainer<?, T> tickContainer);
|
||||||
|
|
||||||
@NotNull
|
@NotNull BiConsumer<UUID, PacketWrapper<?>> getPacketDispatcher();
|
||||||
EntityContainer getDefaultContainer();
|
|
||||||
|
@NotNull EntityContainer getDefaultContainer();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,60 +140,60 @@ public class EntityMeta implements EntityMetadataProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
public short getAirTicks() {
|
public short getAirTicks() {
|
||||||
return this.metadata.getIndex((byte)1, (short) 300);
|
return this.metadata.getIndex((byte) 1, (short) 300);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAirTicks(short value) {
|
public void setAirTicks(short value) {
|
||||||
this.metadata.setIndex((byte)1, EntityDataTypes.SHORT, value);
|
this.metadata.setIndex((byte) 1, EntityDataTypes.SHORT, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Component getCustomName() {
|
public Component getCustomName() {
|
||||||
Optional<Component> component = this.metadata.getIndex((byte)2, Optional.empty());
|
Optional<Component> component = this.metadata.getIndex((byte) 2, Optional.empty());
|
||||||
return component.orElse(null);
|
return component.orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCustomName(Component value) {
|
public void setCustomName(Component value) {
|
||||||
this.metadata.setIndex((byte)2, EntityDataTypes.OPTIONAL_ADV_COMPONENT, Optional.ofNullable(value));
|
this.metadata.setIndex((byte) 2, EntityDataTypes.OPTIONAL_ADV_COMPONENT, Optional.ofNullable(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isCustomNameVisible() {
|
public boolean isCustomNameVisible() {
|
||||||
return this.metadata.getIndex((byte)3, false);
|
return this.metadata.getIndex((byte) 3, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCustomNameVisible(boolean value) {
|
public void setCustomNameVisible(boolean value) {
|
||||||
this.metadata.setIndex((byte)3, EntityDataTypes.BOOLEAN, value);
|
this.metadata.setIndex((byte) 3, EntityDataTypes.BOOLEAN, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSilent() {
|
public boolean isSilent() {
|
||||||
return this.metadata.getIndex((byte)4, false);
|
return this.metadata.getIndex((byte) 4, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSilent(boolean value) {
|
public void setSilent(boolean value) {
|
||||||
this.metadata.setIndex((byte)4, EntityDataTypes.BOOLEAN, value);
|
this.metadata.setIndex((byte) 4, EntityDataTypes.BOOLEAN, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasNoGravity() {
|
public boolean hasNoGravity() {
|
||||||
return this.metadata.getIndex((byte)5, true);
|
return this.metadata.getIndex((byte) 5, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHasNoGravity(boolean value) {
|
public void setHasNoGravity(boolean value) {
|
||||||
this.metadata.setIndex((byte)5, EntityDataTypes.BOOLEAN, value);
|
this.metadata.setIndex((byte) 5, EntityDataTypes.BOOLEAN, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityPose getPose() {
|
public EntityPose getPose() {
|
||||||
return this.metadata.getIndex((byte)6, EntityPose.STANDING);
|
return this.metadata.getIndex((byte) 6, EntityPose.STANDING);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPose(EntityPose value) {
|
public void setPose(EntityPose value) {
|
||||||
this.metadata.setIndex((byte)6, EntityDataTypes.ENTITY_POSE, value);
|
this.metadata.setIndex((byte) 6, EntityDataTypes.ENTITY_POSE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTicksFrozenInPowderedSnow() {
|
public int getTicksFrozenInPowderedSnow() {
|
||||||
return this.metadata.getIndex((byte)7, 0);
|
return this.metadata.getIndex((byte) 7, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTicksFrozenInPowderedSnow(int value) {
|
public void setTicksFrozenInPowderedSnow(int value) {
|
||||||
this.metadata.setIndex((byte)7, EntityDataTypes.INT, value);
|
this.metadata.setIndex((byte) 7, EntityDataTypes.INT, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public WrapperPlayServerEntityMetadata createPacket() {
|
public WrapperPlayServerEntityMetadata createPacket() {
|
||||||
|
@ -201,6 +201,7 @@ public class EntityMeta implements EntityMetadataProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void isVersionNewer(ServerVersion version) {
|
protected static void isVersionNewer(ServerVersion version) {
|
||||||
|
if (EntityLib.getApi().getSettings().shouldIgnoreServerVersionVerify()) return;
|
||||||
if (EntityLib.getOptionalApi().isPresent()) {
|
if (EntityLib.getOptionalApi().isPresent()) {
|
||||||
if (!EntityLib.getApi().getPacketEvents().getServerManager().getVersion().is(VersionComparison.NEWER_THAN, version)) {
|
if (!EntityLib.getApi().getPacketEvents().getServerManager().getVersion().is(VersionComparison.NEWER_THAN, version)) {
|
||||||
throw new InvalidVersionException("This method is only available for versions newer than " + version.name() + ".");
|
throw new InvalidVersionException("This method is only available for versions newer than " + version.name() + ".");
|
||||||
|
@ -228,7 +229,8 @@ public class EntityMeta implements EntityMetadataProvider {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Annoying java 8 not letting me do OFFSET + amount in the method call so this is a workaround
|
* Annoying java 8 not letting me do OFFSET + amount in the method call so this is a workaround
|
||||||
* @param value the value to offset
|
*
|
||||||
|
* @param value the value to offset
|
||||||
* @param amount the amount to offset by
|
* @param amount the amount to offset by
|
||||||
* @return the offset value
|
* @return the offset value
|
||||||
*/
|
*/
|
||||||
|
@ -257,7 +259,7 @@ public class EntityMeta implements EntityMetadataProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMaskBit(int index, byte bit, boolean value) {
|
public void setMaskBit(int index, byte bit, boolean value) {
|
||||||
byte mask = getMask((byte)index);
|
byte mask = getMask((byte) index);
|
||||||
boolean currentValue = (mask & bit) == bit;
|
boolean currentValue = (mask & bit) == bit;
|
||||||
if (currentValue == value) {
|
if (currentValue == value) {
|
||||||
return;
|
return;
|
||||||
|
@ -267,7 +269,7 @@ public class EntityMeta implements EntityMetadataProvider {
|
||||||
} else {
|
} else {
|
||||||
mask &= (byte) ~bit;
|
mask &= (byte) ~bit;
|
||||||
}
|
}
|
||||||
setMask((byte)index, mask);
|
setMask((byte) index, mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -509,18 +509,9 @@ public class WrapperEntity implements Tickable {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void sendPacket(UUID user, PacketWrapper<?> wrapper) {
|
private static void sendPacket(UUID user, PacketWrapper<?> wrapper) {
|
||||||
if (wrapper == null) return;
|
if (wrapper != null) {
|
||||||
|
EntityLib.getApi().getPacketDispatcher().accept(user, wrapper);
|
||||||
Object channel = EntityLib.getApi().getPacketEvents().getProtocolManager().getChannel(user);
|
|
||||||
if (channel == null) {
|
|
||||||
if (EntityLib.getApi().getSettings().isDebugMode()) {
|
|
||||||
EntityLib.getPlatform().getLogger().warning("Failed to send packet to " + user + " because the channel was null. They may be disconnected/not online.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityLib.getApi().getPacketEvents().getProtocolManager().sendPacket(channel, wrapper);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasNoGravity() {
|
public boolean hasNoGravity() {
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package me.tofaa.entitylib.common;
|
package me.tofaa.entitylib.common;
|
||||||
|
|
||||||
import com.github.retrooper.packetevents.PacketEventsAPI;
|
import com.github.retrooper.packetevents.PacketEventsAPI;
|
||||||
|
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
|
||||||
import me.tofaa.entitylib.APIConfig;
|
import me.tofaa.entitylib.APIConfig;
|
||||||
|
import me.tofaa.entitylib.EntityLib;
|
||||||
import me.tofaa.entitylib.EntityLibAPI;
|
import me.tofaa.entitylib.EntityLibAPI;
|
||||||
import me.tofaa.entitylib.Platform;
|
import me.tofaa.entitylib.Platform;
|
||||||
import me.tofaa.entitylib.container.EntityContainer;
|
import me.tofaa.entitylib.container.EntityContainer;
|
||||||
|
@ -10,22 +12,38 @@ import me.tofaa.entitylib.wrapper.WrapperEntity;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
|
||||||
public abstract class AbstractEntityLibAPI<P, T> implements EntityLibAPI<T> {
|
public abstract class AbstractEntityLibAPI<P, T> implements EntityLibAPI<T> {
|
||||||
|
|
||||||
protected final Platform<P> platform;
|
protected final Platform<P> platform;
|
||||||
protected final PacketEventsAPI<?> packetEvents;
|
protected final PacketEventsAPI<?> packetEvents;
|
||||||
protected final APIConfig settings;
|
protected final APIConfig settings;
|
||||||
protected final Collection<TickContainer<?, T>> tickContainers;
|
protected final Collection<TickContainer<?, T>> tickContainers;
|
||||||
protected final EntityContainer defaultEntityContainer = EntityContainer.basic();
|
protected final EntityContainer defaultEntityContainer = EntityContainer.basic();
|
||||||
|
protected final BiConsumer<UUID, PacketWrapper<?>> packetDispatcher;
|
||||||
|
|
||||||
protected AbstractEntityLibAPI(Platform<P> platform, APIConfig settings) {
|
protected AbstractEntityLibAPI(Platform<P> platform, APIConfig settings) {
|
||||||
this.platform = platform;
|
this.platform = platform;
|
||||||
this.packetEvents = settings.getPacketEvents();
|
this.packetEvents = settings.getPacketEvents();
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
this.tickContainers = settings.shouldTickTickables() ? new HashSet<>() : Collections.emptyList();
|
this.tickContainers = settings.shouldTickTickables() ? new HashSet<>() : Collections.emptyList();
|
||||||
|
this.packetDispatcher = (user, wrapper) -> {
|
||||||
|
Object channel = EntityLib.getApi().getPacketEvents().getProtocolManager().getChannel(user);
|
||||||
|
if (channel == null) {
|
||||||
|
if (EntityLib.getApi().getSettings().isDebugMode()) {
|
||||||
|
EntityLib.getPlatform().getLogger().warning("Failed to send packet to " + user + " because the channel was null. They may be disconnected/not online.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
EntityLib.getApi().getPacketEvents().getProtocolManager().sendPacket(channel, wrapper);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -36,7 +54,6 @@ public abstract class AbstractEntityLibAPI<P, T> implements EntityLibAPI<T> {
|
||||||
@Override
|
@Override
|
||||||
public @Nullable WrapperEntity getEntity(@NotNull UUID uuid) {
|
public @Nullable WrapperEntity getEntity(@NotNull UUID uuid) {
|
||||||
return defaultEntityContainer.getEntity(uuid);
|
return defaultEntityContainer.getEntity(uuid);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -49,9 +66,8 @@ public abstract class AbstractEntityLibAPI<P, T> implements EntityLibAPI<T> {
|
||||||
return defaultEntityContainer;
|
return defaultEntityContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
@Override
|
||||||
public APIConfig getSettings() {
|
public @NotNull APIConfig getSettings() {
|
||||||
return settings;
|
return settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,9 +76,13 @@ public abstract class AbstractEntityLibAPI<P, T> implements EntityLibAPI<T> {
|
||||||
return packetEvents;
|
return packetEvents;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<TickContainer<?, T>> getTickContainers() {
|
public @NotNull BiConsumer<UUID, PacketWrapper<?>> getPacketDispatcher() {
|
||||||
|
return packetDispatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Collection<TickContainer<?, T>> getTickContainers() {
|
||||||
return tickContainers;
|
return tickContainers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue