Don't show self disguise of a player, change how scaling is stored, some code cleanup

This commit is contained in:
libraryaddict 2024-10-03 15:45:13 +13:00
parent 23dfa93ece
commit 1d6c59b5d5
8 changed files with 136 additions and 105 deletions

View File

@ -44,7 +44,7 @@ public class DisguiseViewSelfCommand implements CommandExecutor {
// If they're disguised, tall disguises are hidden, it's a tall disguise
// Then tell the player, it's not a bug! The disguise is too tall
if (disguise != null && !disguise.isTallDisguisesVisible() &&
(!NmsVersion.v1_21_R1.isSupported() || !DisguiseConfig.isTallSelfDisguisesScaling() ||
(!NmsVersion.v1_20_R4.isSupported() || !DisguiseConfig.isTallSelfDisguisesScaling() ||
(disguise.isMiscDisguise() || disguise.getType() == DisguiseType.ENDER_DRAGON)) && disguise.canSee(player) &&
DisguiseUtilities.isTallDisguise(disguise)) {
LibsMsg.VIEW_SELF_TALL_NOTE.send(player);

View File

@ -5,7 +5,6 @@ import com.github.retrooper.packetevents.protocol.attribute.Attributes;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfo;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerUpdateAttributes;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import me.libraryaddict.disguise.DisguiseAPI;
@ -29,7 +28,6 @@ import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
@ -60,25 +58,38 @@ import java.util.UUID;
import java.util.concurrent.TimeUnit;
public abstract class Disguise {
/**
* In use doesn't mean that this disguise is active. It means that Lib's Disguises still stores a reference to
* the disguise. Though it also means the disguise is active.
* getEntity() can still return null if this disguise is active after despawn, logout, etc.
*
* @return isDisguiseInUse
*/
@Getter
private transient boolean disguiseInUse;
private final DisguiseType disguiseType;
private transient BukkitRunnable runnable;
/**
* -- GETTER --
* Get the disguised entity
*
* @return entity
* The entity that is disguised
*/
@Getter
private transient Entity entity;
@Getter
private boolean hearSelfDisguise = DisguiseConfig.isSelfDisguisesSoundsReplaced();
@Getter
private boolean hideArmorFromSelf = DisguiseConfig.isHidingArmorFromSelf();
@Getter
private boolean hideHeldItemFromSelf = DisguiseConfig.isHidingHeldItemFromSelf();
private boolean keepDisguisePlayerDeath = DisguiseConfig.isKeepDisguiseOnPlayerDeath();
@Getter
private boolean keepDisguiseOnPlayerDeath = DisguiseConfig.isKeepDisguiseOnPlayerDeath();
@Getter
private boolean modifyBoundingBox = DisguiseConfig.isModifyBoundingBox();
private boolean playerHiddenFromTab = DisguiseConfig.isHideDisguisedPlayers();
private boolean replaceSounds = DisguiseConfig.isSoundEnabled();
@Setter
@Getter
private boolean mobsIgnoreDisguise;
@Getter
private boolean velocitySent = DisguiseConfig.isVelocitySent();
private boolean viewSelfDisguise = DisguiseConfig.isViewDisguises() && DisguiseConfig.isViewSelfDisguisesDefault();
@Getter
@ -87,13 +98,8 @@ public abstract class Disguise {
private BarColor bossBarColor = DisguiseConfig.getBossBarColor();
@Getter
private BarStyle bossBarStyle = DisguiseConfig.getBossBarStyle();
@Getter(value = AccessLevel.PRIVATE)
private final NamespacedKey bossBar = new NamespacedKey("libsdisguises", UUID.randomUUID().toString());
/**
* -- GETTER --
* Get the flag watcher
*
* @return flagWatcher
* The unique FlagWatcher of this disguise
*/
@Getter
private FlagWatcher watcher;
@ -125,17 +131,15 @@ public abstract class Disguise {
@Setter
private String soundGroup;
private UUID uuid = ReflectionManager.getRandomUUID();
/**
* The biggest we'll allow the self disguise to be scaled up to, including disguise applied scale
*/
@Getter
private double selfDisguiseTallScaleMax = 1;
@Getter
private boolean scalePlayerToDisguise = DisguiseConfig.isScaleSelfDisguises();
@Getter
private final DisguiseInternals internals;
public Disguise(DisguiseType disguiseType) {
this.disguiseType = disguiseType;
this.disguiseName = disguiseType.toReadable();
this.internals = new DisguiseInternals(this);
}
public HashMap<String, Object> getCustomData() {
@ -210,7 +214,7 @@ public abstract class Disguise {
public abstract double getHeight();
public double getNameHeightScale() {
if (!NmsVersion.v1_21_R1.isSupported() || isMiscDisguise()) {
if (!NmsVersion.v1_20_R4.isSupported() || isMiscDisguise()) {
return 1;
}
@ -220,7 +224,7 @@ public abstract class Disguise {
if (watcherScale != null) {
finalScale = watcherScale;
} else {
finalScale = DisguiseUtilities.getActualEntityScale(getEntity());
finalScale = getInternals().getEntityScaleWithoutLibsDisguises();
}
// Clamp
@ -366,14 +370,14 @@ public abstract class Disguise {
}
private void removeBossBar() {
BossBar bossBar = Bukkit.getBossBar(getBossBar());
BossBar bossBar = Bukkit.getBossBar(getInternals().getBossBar());
if (bossBar == null) {
return;
}
bossBar.removeAll();
Bukkit.removeBossBar(getBossBar());
Bukkit.removeBossBar(getInternals().getBossBar());
}
public void setNotifyBar(DisguiseConfig.NotifyBar bar) {
@ -432,8 +436,8 @@ public abstract class Disguise {
removeBossBar();
BossBar bar = Bukkit.createBossBar(getBossBar(), BaseComponent.toLegacyText(LibsMsg.ACTION_BAR_MESSAGE.getBase(getDisguiseName())),
getBossBarColor(), getBossBarStyle());
BossBar bar = Bukkit.createBossBar(getInternals().getBossBar(),
BaseComponent.toLegacyText(LibsMsg.ACTION_BAR_MESSAGE.getBase(getDisguiseName())), getBossBarColor(), getBossBarStyle());
bar.setProgress(1);
bar.addPlayer((Player) getEntity());
}
@ -518,7 +522,7 @@ public abstract class Disguise {
}
if (getEntity() instanceof Player && isSelfDisguiseVisible() && !isTallDisguisesVisible() && isTallDisguise()) {
if (DisguiseConfig.isTallSelfDisguisesScaling() && NmsVersion.v1_21_R1.isSupported() && canScaleDisguise()) {
if (DisguiseConfig.isTallSelfDisguisesScaling() && NmsVersion.v1_20_R4.isSupported() && canScaleDisguise()) {
adjustTallSelfDisguiseScale();
} else {
setSelfDisguiseVisible(false);
@ -533,7 +537,7 @@ public abstract class Disguise {
}
protected boolean canScaleDisguise() {
return !isMiscDisguise() && getType() != DisguiseType.ENDER_DRAGON;
return NmsVersion.v1_20_R4.isSupported() && !isMiscDisguise() && getType() != DisguiseType.ENDER_DRAGON;
}
public void setScalePlayerToDisguise(boolean scalePlayerToDisguise) {
@ -548,40 +552,41 @@ public abstract class Disguise {
}
protected void adjustTallSelfDisguiseScale() {
if (!NmsVersion.v1_21_R1.isSupported() || !canScaleDisguise()) {
if (!canScaleDisguise()) {
return;
}
// Get the scale, default to "not scaled" if not a player
double playerHeightScale = DisguiseUtilities.getActualEntityScale(getEntity());
double entityScaleWithoutLibsDisguises = getInternals().getEntityScaleWithoutLibsDisguises();
double disguiseHeight = getHeight() * getNameHeightScale();
// Here we have the scale of the player itself, where they'd be scaled up or down to match the disguise's scale
// So a disguise that's 0.5 blocks high, will have the player be given something like 0.33 scale
double playerScale = disguiseHeight / (1.8 * playerHeightScale);
double playerScale = disguiseHeight / (1.8 * entityScaleWithoutLibsDisguises);
playerScale = Math.min(playerScale, DisguiseConfig.getScaleSelfDisguisesMax());
// The max size the self disguise is allowed to be, as it'd hide the player's view
double prevScale = this.selfDisguiseTallScaleMax;
double prevScale = getInternals().getSelfDisguiseTallScaleMax();
double newScale = DisguiseUtilities.isTallDisguise(this) ? (1.4 * playerScale) / disguiseHeight : 1;
// Adjust so it's not blocking eyes. So smaller than normal
// And ofc, it's 1 if the disguise was not too tall to begin with
this.selfDisguiseTallScaleMax = DisguiseUtilities.isTallDisguise(this) ? (1.4 * playerScale) / disguiseHeight : 1;
getInternals().setSelfDisguiseTallScaleMax(newScale);
if (!isDisguiseInUse() || !(getEntity() instanceof Player) || !canScaleDisguise() ||
!((TargetedDisguise) this).canSee((Player) getEntity())) {
return;
}
if (prevScale != selfDisguiseTallScaleMax && isSelfDisguiseVisible()) {
if (prevScale != newScale && isSelfDisguiseVisible()) {
double scaleToSend;
if (((LivingWatcher) getWatcher()).getScale() != null) {
scaleToSend = ((LivingWatcher) getWatcher()).getScale();
} else {
scaleToSend = DisguiseUtilities.getActualEntityScale(getEntity());
scaleToSend = entityScaleWithoutLibsDisguises;
}
scaleToSend = Math.min(scaleToSend, getSelfDisguiseTallScaleMax());
scaleToSend = Math.min(scaleToSend, newScale);
// The scale of the self disguise, not the player
WrapperPlayServerUpdateAttributes.Property property =
@ -650,17 +655,6 @@ public abstract class Disguise {
return this;
}
/**
* In use doesn't mean that this disguise is active. It means that Lib's Disguises still stores a reference to
* the disguise.
* getEntity() can still return null if this disguise is active after despawn, logout, etc.
*
* @return isDisguiseInUse
*/
public boolean isDisguiseInUse() {
return disguiseInUse;
}
/**
* Will a disguised player appear in tab
*/
@ -686,10 +680,6 @@ public abstract class Disguise {
return hideHeldItemFromSelf;
}
public boolean isHideArmorFromSelf() {
return hideArmorFromSelf;
}
public Disguise setHideArmorFromSelf(boolean hideArmor) {
this.hideArmorFromSelf = hideArmor;
@ -700,10 +690,6 @@ public abstract class Disguise {
return this;
}
public boolean isHideHeldItemFromSelf() {
return hideHeldItemFromSelf;
}
public Disguise setHideHeldItemFromSelf(boolean hideHeldItem) {
this.hideHeldItemFromSelf = hideHeldItem;
@ -714,12 +700,8 @@ public abstract class Disguise {
return this;
}
public boolean isKeepDisguiseOnPlayerDeath() {
return this.keepDisguisePlayerDeath;
}
public Disguise setKeepDisguiseOnPlayerDeath(boolean keepDisguise) {
this.keepDisguisePlayerDeath = keepDisguise;
this.keepDisguiseOnPlayerDeath = keepDisguise;
return this;
}
@ -732,10 +714,6 @@ public abstract class Disguise {
return false;
}
public boolean isModifyBoundingBox() {
return modifyBoundingBox;
}
public Disguise setModifyBoundingBox(boolean modifyBox) {
if (isModifyBoundingBox() != modifyBox) {
this.modifyBoundingBox = modifyBox;
@ -786,10 +764,6 @@ public abstract class Disguise {
return replaceSounds;
}
public boolean isVelocitySent() {
return velocitySent;
}
public Disguise setVelocitySent(boolean sendVelocity) {
this.velocitySent = sendVelocity;
@ -851,7 +825,7 @@ public abstract class Disguise {
return true;
}
if (NmsVersion.v1_21_R1.isSupported()) {
if (NmsVersion.v1_20_R4.isSupported()) {
DisguiseUtilities.removeSelfDisguiseScale(getEntity());
}
@ -906,7 +880,6 @@ public abstract class Disguise {
}
if (isHidePlayer() && getEntity() instanceof Player && ((Player) getEntity()).isOnline()) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (!((TargetedDisguise) this).canSee(player)) {
continue;
@ -935,10 +908,6 @@ public abstract class Disguise {
return true;
}
public boolean isHearSelfDisguise() {
return hearSelfDisguise;
}
public Disguise setHearSelfDisguise(boolean hearSelfDisguise) {
this.hearSelfDisguise = hearSelfDisguise;
@ -1007,7 +976,7 @@ public abstract class Disguise {
@Deprecated
public Disguise setViewSelfDisguise(boolean viewSelfDisguise) {
if (viewSelfDisguise && !isTallDisguisesVisible() && isTallDisguise()) {
if (DisguiseConfig.isTallSelfDisguisesScaling() && NmsVersion.v1_21_R1.isSupported() && canScaleDisguise()) {
if (DisguiseConfig.isTallSelfDisguisesScaling() && NmsVersion.v1_20_R4.isSupported() && canScaleDisguise() && !isPlayerDisguise()) {
adjustTallSelfDisguiseScale();
} else {
viewSelfDisguise = false;
@ -1020,13 +989,11 @@ public abstract class Disguise {
this.viewSelfDisguise = viewSelfDisguise;
if (getEntity() != null && getEntity() instanceof Player) {
if (DisguiseAPI.getDisguise((Player) getEntity(), getEntity()) == this) {
if (isSelfDisguiseVisible()) {
DisguiseUtilities.setupFakeDisguise(this);
} else {
DisguiseUtilities.removeSelfDisguise(this);
}
if (getEntity() instanceof Player && DisguiseAPI.getDisguise((Player) getEntity(), getEntity()) == this) {
if (isSelfDisguiseVisible()) {
DisguiseUtilities.setupFakeDisguise(this);
} else {
DisguiseUtilities.removeSelfDisguise(this);
}
}
@ -1177,12 +1144,4 @@ public abstract class Disguise {
public boolean stopDisguise() {
return removeDisguise();
}
public boolean isMobsIgnoreDisguise() {
return mobsIgnoreDisguise;
}
public void setMobsIgnoreDisguise(boolean mobsIgnoreDisguise) {
this.mobsIgnoreDisguise = mobsIgnoreDisguise;
}
}

View File

@ -0,0 +1,70 @@
package me.libraryaddict.disguise.disguisetypes;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.LivingEntity;
import java.util.UUID;
@RequiredArgsConstructor
/**
* Not intended to be used by external plugins
*/ public class DisguiseInternals<D extends Disguise> {
private final D disguise;
/**
* The entity scale when Libs Disguises is not excluded from attributes
*/
private double entityScaleWithLibsDisguises = Double.MIN_VALUE;
/**
* The entity scale when Libs Disguises is excluded from attributes
*/
private double entityScaleWithoutLibsDisguises = Double.MIN_VALUE;
/**
* The entity scale that was last sent in a packet, used to try avoid wasting our time
*/
private double entityScaleLastSentViaPackets = Double.MIN_VALUE;
/**
* The biggest we'll allow the self disguise to be scaled up to, including disguise applied scale
*/
@Setter
@Getter
private double selfDisguiseTallScaleMax = 1;
@Getter
private final NamespacedKey bossBar = new NamespacedKey("libsdisguises", UUID.randomUUID().toString());
public double getEntityScaleWithoutLibsDisguises() {
double actualScale = ((LivingEntity) disguise.getEntity()).getAttribute(Attribute.GENERIC_SCALE).getValue();
// If nothing has changed
if (entityScaleWithLibsDisguises != actualScale) {
// Update the packet field as well, as we expect it to change
entityScaleWithLibsDisguises = entityScaleLastSentViaPackets = actualScale;
entityScaleWithoutLibsDisguises = DisguiseUtilities.getEntityScaleWithoutLibsDisguises(disguise.getEntity());
}
return entityScaleWithoutLibsDisguises;
}
public double getPacketEntityScale(double scaleInPacket) {
if (entityScaleLastSentViaPackets == scaleInPacket) {
return entityScaleWithoutLibsDisguises;
}
entityScaleLastSentViaPackets = scaleInPacket;
// If disguise cant be scaled
// If a scale cannot be applied to the entity (or it is null)
if (disguise.isMiscDisguise() || !(disguise.getEntity() instanceof LivingEntity)) {
return scaleInPacket;
}
getEntityScaleWithoutLibsDisguises();
entityScaleLastSentViaPackets = scaleInPacket;
return entityScaleWithoutLibsDisguises;
}
}

View File

@ -16,6 +16,7 @@ import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.annotations.MethodGroupType;
import me.libraryaddict.disguise.utilities.reflection.annotations.MethodOnlyUsedBy;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsAddedIn;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsRemovedIn;
import org.bukkit.Color;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
@ -55,14 +56,14 @@ public class LivingWatcher extends FlagWatcher {
return clone;
}
@NmsAddedIn(NmsVersion.v1_21_R1)
@NmsAddedIn(NmsVersion.v1_20_R4)
public Double getScale() {
return viewScale;
}
@NmsAddedIn(NmsVersion.v1_21_R1)
@NmsAddedIn(NmsVersion.v1_20_R4)
public void setScale(Double viewScale) {
if (!NmsVersion.v1_21_R1.isSupported()) {
if (!NmsVersion.v1_20_R4.isSupported()) {
return;
}
@ -87,14 +88,14 @@ public class LivingWatcher extends FlagWatcher {
if (getScale() != null) {
scaleToSend = getScale();
} else {
scaleToSend = DisguiseUtilities.getActualEntityScale(getDisguise().getEntity());
scaleToSend = DisguiseUtilities.getEntityScaleWithoutLibsDisguises(getDisguise().getEntity());
}
Entity entity = getDisguise().getEntity();
for (Player player : DisguiseUtilities.getPerverts(getDisguise())) {
double toSend = player == entity && DisguiseConfig.isTallSelfDisguisesScaling() ?
Math.min(getDisguise().getSelfDisguiseTallScaleMax(), scaleToSend) : scaleToSend;
Math.min(getDisguise().getInternals().getSelfDisguiseTallScaleMax(), scaleToSend) : scaleToSend;
WrapperPlayServerUpdateAttributes.Property property =
new WrapperPlayServerUpdateAttributes.Property(Attributes.GENERIC_SCALE, toSend, new ArrayList<>());

View File

@ -405,7 +405,7 @@ public class DisguiseUtilities {
}
public static void removeSelfDisguiseScale(Entity entity) {
if (!NmsVersion.v1_21_R1.isSupported() || isInvalidFile() || !(entity instanceof LivingEntity)) {
if (!NmsVersion.v1_20_R4.isSupported() || isInvalidFile() || !(entity instanceof LivingEntity)) {
return;
}
@ -3464,7 +3464,7 @@ public class DisguiseUtilities {
else if (index == MetaIndex.TEXT_DISPLAY_TEXT) {
val = getAdventureChat(newNames[i]);
} else if (index == MetaIndex.DISPLAY_SCALE && !disguise.isMiscDisguise()) {
Double scale = viewer == disguise.getEntity() ? disguise.getSelfDisguiseTallScaleMax() :
Double scale = viewer == disguise.getEntity() ? disguise.getInternals().getSelfDisguiseTallScaleMax() :
((LivingWatcher) disguise.getWatcher()).getScale();
// TODO Expand this out
} else if (index == MetaIndex.DISPLAY_BILLBOARD_RENDER_CONSTRAINTS) {
@ -3513,7 +3513,7 @@ public class DisguiseUtilities {
/**
* Grabs the scale of the entity as if the LibsDisguises: attributes did not exist
*/
public static double getActualEntityScale(Entity entity) {
public static double getEntityScaleWithoutLibsDisguises(Entity entity) {
if (!(entity instanceof LivingEntity)) {
return 1;
}

View File

@ -56,13 +56,14 @@ public class PacketHandlerAttributes implements IPacketHandler<WrapperPlayServer
// If it's for the self disguise and the disguise had to be scaled down
if (entity == observer && DisguiseConfig.isTallSelfDisguisesScaling()) {
attributes.add(new WrapperPlayServerUpdateAttributes.Property(Attributes.GENERIC_SCALE,
Math.min(disguise.getSelfDisguiseTallScaleMax(), scale == null ? property.getValue() : scale), new ArrayList<>()));
Math.min(disguise.getInternals().getSelfDisguiseTallScaleMax(), scale == null ? property.getValue() : scale),
new ArrayList<>()));
} else {
if (scale == null) {
attributes.add(property);
} else {
attributes.add(new WrapperPlayServerUpdateAttributes.Property(Attributes.GENERIC_SCALE, scale, new ArrayList<>()));
scale = disguise.getInternals().getPacketEntityScale(property.getValue());
}
attributes.add(new WrapperPlayServerUpdateAttributes.Property(Attributes.GENERIC_SCALE, scale, new ArrayList<>()));
}
} else if (property.getAttribute() == Attributes.GENERIC_GRAVITY) {
attributes.add(property);

View File

@ -307,16 +307,16 @@ public class PacketHandlerSpawn implements IPacketHandler {
if (disguise.getWatcher() instanceof LivingWatcher) {
List<WrapperPlayServerUpdateAttributes.Property> attributes = new ArrayList<>();
if (NmsVersion.v1_21_R1.isSupported()) {
if (NmsVersion.v1_20_R4.isSupported()) {
Double scale = ((LivingWatcher) disguise.getWatcher()).getScale();
if (observer == disguisedEntity && DisguiseConfig.isTallSelfDisguisesScaling()) {
if (scale == null) {
scale = DisguiseUtilities.getActualEntityScale(observer);
scale = DisguiseUtilities.getEntityScaleWithoutLibsDisguises(observer);
}
attributes.add(new WrapperPlayServerUpdateAttributes.Property(Attributes.GENERIC_SCALE,
Math.min(disguise.getSelfDisguiseTallScaleMax(), scale), new ArrayList<>()));
Math.min(disguise.getInternals().getSelfDisguiseTallScaleMax(), scale), new ArrayList<>()));
} else if (scale != null) {
attributes.add(new WrapperPlayServerUpdateAttributes.Property(Attributes.GENERIC_SCALE, scale, new ArrayList<>()));
}

View File

@ -182,7 +182,7 @@ public class DisguiseMethods {
"setHidePlayer", "setExpires", "setNotifyBar", "setBossBarColor", "setBossBarStyle", "setTallDisguisesVisible",
"setDynamicName", "setSoundGroup", "setDisguiseName", "setDeadmau5Ears"));
if (NmsVersion.v1_21_R1.isSupported()) {
if (NmsVersion.v1_20_R4.isSupported()) {
extraMethods.add("setScalePlayerToDisguise");
}