From 57c0185b452b213aa8cc49c35f75bbc2adaa2f3e Mon Sep 17 00:00:00 2001 From: David Choo Date: Tue, 17 Aug 2021 22:44:33 -0400 Subject: [PATCH] Prevent projectiles from blocking the player's vision (#2472) Prevent Snowballs, Eggs, and other throwable projectiles from blocking the player's screen --- bootstrap/standalone/pom.xml | 6 +- .../org/geysermc/connector/entity/Entity.java | 15 +++- .../connector/entity/ThrowableItemEntity.java | 76 +++++++++++++++++++ .../connector/entity/ThrownPotionEntity.java | 2 +- .../entity/living/ArmorStandEntity.java | 19 ++--- .../connector/entity/type/EntityType.java | 8 +- .../network/session/GeyserSession.java | 31 ++++---- 7 files changed, 123 insertions(+), 34 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/entity/ThrowableItemEntity.java diff --git a/bootstrap/standalone/pom.xml b/bootstrap/standalone/pom.xml index 64aa54745..e338f9533 100644 --- a/bootstrap/standalone/pom.xml +++ b/bootstrap/standalone/pom.xml @@ -43,17 +43,17 @@ org.jline jline-terminal - 3.9.0 + 3.20.0 org.jline jline-terminal-jna - 3.9.0 + 3.20.0 org.jline jline-reader - 3.9.0 + 3.20.0 org.apache.logging.log4j diff --git a/connector/src/main/java/org/geysermc/connector/entity/Entity.java b/connector/src/main/java/org/geysermc/connector/entity/Entity.java index 04a876f8f..eb9b8ec1c 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/Entity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/Entity.java @@ -249,10 +249,7 @@ public class Entity { // Swimming is ignored here and instead we rely on the pose metadata.getFlags().setFlag(EntityFlag.GLIDING, (xd & 0x80) == 0x80); - // Armour stands are handled in their own class - if (!this.is(ArmorStandEntity.class)) { - metadata.getFlags().setFlag(EntityFlag.INVISIBLE, (xd & 0x20) == 0x20); - } + setInvisible(session, (xd & 0x20) == 0x20); } break; case 1: // Air/bubbles @@ -343,6 +340,16 @@ public class Entity { return 300; } + /** + * Set a boolean - whether the entity is invisible or visible + * + * @param session the Geyser session + * @param value true if the entity is invisible + */ + protected void setInvisible(GeyserSession session, boolean value) { + metadata.getFlags().setFlag(EntityFlag.INVISIBLE, value); + } + /** * x = Pitch, y = HeadYaw, z = Yaw * diff --git a/connector/src/main/java/org/geysermc/connector/entity/ThrowableItemEntity.java b/connector/src/main/java/org/geysermc/connector/entity/ThrowableItemEntity.java new file mode 100644 index 000000000..38a6204d4 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/entity/ThrowableItemEntity.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2019-2021 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.connector.entity; + +import com.nukkitx.math.vector.Vector3f; +import com.nukkitx.protocol.bedrock.data.entity.EntityFlag; +import org.geysermc.connector.entity.type.EntityType; +import org.geysermc.connector.network.session.GeyserSession; + +/** + * Used as a class for any projectile entity that looks like an item + */ +public class ThrowableItemEntity extends ThrowableEntity { + /** + * Number of ticks since the entity was spawned by the Java server + */ + private int age; + private boolean invisible; + + public ThrowableItemEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) { + super(entityId, geyserId, entityType, position, motion, rotation); + metadata.getFlags().setFlag(EntityFlag.INVISIBLE, true); + invisible = false; + } + + private void checkVisibility(GeyserSession session) { + if (invisible != metadata.getFlags().getFlag(EntityFlag.INVISIBLE)) { + if (!invisible) { + Vector3f playerPos = session.getPlayerEntity().getPosition(); + // Prevent projectiles from blocking the player's screen + if (age >= 4 || position.distanceSquared(playerPos) > 16) { + metadata.getFlags().setFlag(EntityFlag.INVISIBLE, false); + updateBedrockMetadata(session); + } + } else { + metadata.getFlags().setFlag(EntityFlag.INVISIBLE, true); + updateBedrockMetadata(session); + } + } + age++; + } + + @Override + public void tick(GeyserSession session) { + checkVisibility(session); + super.tick(session); + } + + @Override + protected void setInvisible(GeyserSession session, boolean value) { + invisible = value; + } +} diff --git a/connector/src/main/java/org/geysermc/connector/entity/ThrownPotionEntity.java b/connector/src/main/java/org/geysermc/connector/entity/ThrownPotionEntity.java index 735bf274c..578a460b6 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/ThrownPotionEntity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/ThrownPotionEntity.java @@ -41,7 +41,7 @@ import org.geysermc.connector.registry.type.ItemMapping; import java.util.EnumSet; -public class ThrownPotionEntity extends ThrowableEntity { +public class ThrownPotionEntity extends ThrowableItemEntity { private static final EnumSet NON_ENCHANTED_POTIONS = EnumSet.of(Potion.WATER, Potion.MUNDANE, Potion.THICK, Potion.AWKWARD); public ThrownPotionEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) { diff --git a/connector/src/main/java/org/geysermc/connector/entity/living/ArmorStandEntity.java b/connector/src/main/java/org/geysermc/connector/entity/living/ArmorStandEntity.java index a616f9269..d447f6379 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/living/ArmorStandEntity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/living/ArmorStandEntity.java @@ -116,15 +116,7 @@ public class ArmorStandEntity extends LivingEntity { @Override public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) { super.updateBedrockMetadata(entityMetadata, session); - if (entityMetadata.getId() == 0 && entityMetadata.getType() == MetadataType.BYTE) { - byte xd = (byte) entityMetadata.getValue(); - - // Check if the armour stand is invisible and store accordingly - if (primaryEntity) { - isInvisible = (xd & 0x20) == 0x20; - updateSecondEntityStatus(false); - } - } else if (entityMetadata.getId() == 2) { + if (entityMetadata.getId() == 2) { updateSecondEntityStatus(false); } else if (entityMetadata.getId() == 15 && entityMetadata.getType() == MetadataType.BYTE) { byte xd = (byte) entityMetadata.getValue(); @@ -242,6 +234,15 @@ public class ArmorStandEntity extends LivingEntity { } } + @Override + protected void setInvisible(GeyserSession session, boolean value) { + // Check if the armour stand is invisible and store accordingly + if (primaryEntity) { + isInvisible = value; + updateSecondEntityStatus(false); + } + } + @Override public void setHelmet(ItemData helmet) { super.setHelmet(helmet); diff --git a/connector/src/main/java/org/geysermc/connector/entity/type/EntityType.java b/connector/src/main/java/org/geysermc/connector/entity/type/EntityType.java index 582e2cbfa..094cb4d61 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/type/EntityType.java +++ b/connector/src/main/java/org/geysermc/connector/entity/type/EntityType.java @@ -107,7 +107,7 @@ public enum EntityType { PRIMED_TNT(TNTEntity.class, 65, 0.98f, 0.98f, 0.98f, 0f, "minecraft:tnt"), FALLING_BLOCK(FallingBlockEntity.class, 66, 0.98f, 0.98f), MOVING_BLOCK(Entity.class, 67, 0f), - THROWN_EXP_BOTTLE(ThrowableEntity.class, 68, 0.25f, 0.25f, 0f, 0f, "minecraft:xp_bottle"), + THROWN_EXP_BOTTLE(ThrowableItemEntity.class, 68, 0.25f, 0.25f, 0f, 0f, "minecraft:xp_bottle"), EXPERIENCE_ORB(ExpOrbEntity.class, 69, 0f, 0f, 0f, 0f, "minecraft:xp_orb"), EYE_OF_ENDER(Entity.class, 70, 0.25f, 0.25f, 0f, 0f, "minecraft:eye_of_ender_signal"), END_CRYSTAL(EnderCrystalEntity.class, 71, 2.0f, 2.0f, 2.0f, 0f, "minecraft:ender_crystal"), @@ -121,13 +121,13 @@ public enum EntityType { DRAGON_FIREBALL(ItemedFireballEntity.class, 79, 1.0f), ARROW(TippedArrowEntity.class, 80, 0.25f, 0.25f), SPECTRAL_ARROW(AbstractArrowEntity.class, 80, 0.25f, 0.25f, 0.25f, 0f, "minecraft:arrow"), - SNOWBALL(ThrowableEntity.class, 81, 0.25f), - THROWN_EGG(ThrowableEntity.class, 82, 0.25f, 0.25f, 0.25f, 0f, "minecraft:egg"), + SNOWBALL(ThrowableItemEntity.class, 81, 0.25f), + THROWN_EGG(ThrowableItemEntity.class, 82, 0.25f, 0.25f, 0.25f, 0f, "minecraft:egg"), PAINTING(PaintingEntity.class, 83, 0f), MINECART(MinecartEntity.class, 84, 0.7f, 0.98f, 0.98f, 0.35f), FIREBALL(ItemedFireballEntity.class, 85, 1.0f), THROWN_POTION(ThrownPotionEntity.class, 86, 0.25f, 0.25f, 0.25f, 0f, "minecraft:splash_potion"), - THROWN_ENDERPEARL(ThrowableEntity.class, 87, 0.25f, 0.25f, 0.25f, 0f, "minecraft:ender_pearl"), + THROWN_ENDERPEARL(ThrowableItemEntity.class, 87, 0.25f, 0.25f, 0.25f, 0f, "minecraft:ender_pearl"), LEASH_KNOT(LeashKnotEntity.class, 88, 0.5f, 0.375f), WITHER_SKULL(WitherSkullEntity.class, 89, 0.3125f), BOAT(BoatEntity.class, 90, 0.6f, 1.6f, 1.6f, 0.35f), diff --git a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java index a77d370c5..5d7330794 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java @@ -75,6 +75,7 @@ import org.geysermc.connector.common.AuthType; import org.geysermc.connector.configuration.EmoteOffhandWorkaroundOption; import org.geysermc.connector.entity.Entity; import org.geysermc.connector.entity.ItemFrameEntity; +import org.geysermc.connector.entity.ThrowableEntity; import org.geysermc.connector.entity.Tickable; import org.geysermc.connector.entity.attribute.GeyserAttributeType; import org.geysermc.connector.entity.player.SessionPlayerEntity; @@ -904,21 +905,25 @@ public class GeyserSession implements CommandSender { * Called every 50 milliseconds - one Minecraft tick. */ protected void tick() { - // Check to see if the player's position needs updating - a position update should be sent once every 3 seconds - if (spawned && (System.currentTimeMillis() - lastMovementTimestamp) > 3000) { - // Recalculate in case something else changed position - Vector3d position = collisionManager.adjustBedrockPosition(playerEntity.getPosition(), playerEntity.isOnGround()); - // A null return value cancels the packet - if (position != null) { - ClientPlayerPositionPacket packet = new ClientPlayerPositionPacket(playerEntity.isOnGround(), - position.getX(), position.getY(), position.getZ()); - sendDownstreamPacket(packet); + try { + // Check to see if the player's position needs updating - a position update should be sent once every 3 seconds + if (spawned && (System.currentTimeMillis() - lastMovementTimestamp) > 3000) { + // Recalculate in case something else changed position + Vector3d position = collisionManager.adjustBedrockPosition(playerEntity.getPosition(), playerEntity.isOnGround()); + // A null return value cancels the packet + if (position != null) { + ClientPlayerPositionPacket packet = new ClientPlayerPositionPacket(playerEntity.isOnGround(), + position.getX(), position.getY(), position.getZ()); + sendDownstreamPacket(packet); + } + lastMovementTimestamp = System.currentTimeMillis(); } - lastMovementTimestamp = System.currentTimeMillis(); - } - for (Tickable entity : entityCache.getTickableEntities()) { - entity.tick(this); + for (Tickable entity : entityCache.getTickableEntities()) { + entity.tick(this); + } + } catch (Throwable throwable) { + throwable.printStackTrace(); } }