Skip to content

Commit

Permalink
fix invisible command with disguised/faked entities
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 19, 2021
1 parent 6f00c60 commit 0025442
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
Expand Up @@ -23,6 +23,10 @@

public abstract class EntityHelper {

public void setInvisible(Entity entity, boolean invisible) {
// Do nothing on older versions
}

public abstract double getAbsorption(LivingEntity entity);

public abstract void setAbsorption(LivingEntity entity, double value);
Expand Down
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.scripts.commands.entity;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.npc.traits.InvisibleTrait;
Expand Down Expand Up @@ -87,6 +88,23 @@ else if (!scriptEntry.hasObject("target")
}
}

public void setInvisible(EntityTag entity, boolean visible) {
if (entity.getBukkitEntity() instanceof ArmorStand) {
((ArmorStand) entity.getBukkitEntity()).setVisible(visible);
}
else if (entity.isLivingEntity() && !entity.isFake) {
if (visible) {
entity.getLivingEntity().removePotionEffect(PotionEffectType.INVISIBILITY);
}
else {
new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1).apply(entity.getLivingEntity());
}
}
else {
NMSHandler.getEntityHelper().setInvisible(entity.getBukkitEntity(), !visible);
}
}

@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag state = scriptEntry.getElement("state");
Expand Down Expand Up @@ -115,37 +133,17 @@ public void execute(ScriptEntry scriptEntry) {
else {
switch (Action.valueOf(state.asString().toUpperCase())) {
case FALSE:
if (target.getBukkitEntity() instanceof ArmorStand) {
((ArmorStand) target.getBukkitEntity()).setVisible(true);
}
else {
target.getLivingEntity().removePotionEffect(PotionEffectType.INVISIBILITY);
}
setInvisible(target, true);
break;
case TRUE:
if (target.getBukkitEntity() instanceof ArmorStand) {
((ArmorStand) target.getBukkitEntity()).setVisible(false);
}
else {
new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1).apply(target.getLivingEntity());
}
setInvisible(target, false);
break;
case TOGGLE:
if (target.getBukkitEntity() instanceof ArmorStand) {
if (((ArmorStand) target.getBukkitEntity()).isVisible()) {
((ArmorStand) target.getBukkitEntity()).setVisible(true);
}
else {
((ArmorStand) target.getBukkitEntity()).setVisible(false);
}
setInvisible(target, !((ArmorStand) target.getBukkitEntity()).isVisible());
}
else {
if (target.getLivingEntity().hasPotionEffect(PotionEffectType.INVISIBILITY)) {
target.getLivingEntity().removePotionEffect(PotionEffectType.INVISIBILITY);
}
else {
new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1).apply(target.getLivingEntity());
}
setInvisible(target, target.getLivingEntity().hasPotionEffect(PotionEffectType.INVISIBILITY));
}
break;
}
Expand Down
Expand Up @@ -41,6 +41,11 @@ public class EntityHelperImpl extends EntityHelper {

public static final MethodHandle ENTITY_ONGROUND_SETTER = ReflectionHelper.getFinalSetter(net.minecraft.server.v1_16_R3.Entity.class, "onGround");

@Override
public void setInvisible(Entity entity, boolean invisible) {
((CraftEntity) entity).getHandle().setInvisible(invisible);
}

@Override
public double getAbsorption(LivingEntity entity) {
return entity.getAbsorptionAmount();
Expand Down

0 comments on commit 0025442

Please sign in to comment.