fix some stuff

This commit is contained in:
Ethan 2024-08-10 13:18:30 +08:00
parent c5f1f8375c
commit 99e7385bdc
4 changed files with 28 additions and 16 deletions

View file

@ -39,14 +39,17 @@ import java.util.UUID;
* Used as a class for any projectile entity that looks like an item
*/
public class ThrowableItemEntity extends ThrowableEntity {
private boolean invisible;
/**
* Number of draw ticks since the entity was spawned by the Java server
*/
private int age;
private boolean invisible;
public ThrowableItemEntity(GeyserSession session, int entityId, long geyserId, UUID uuid, EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
super(session, entityId, geyserId, uuid, definition, position, motion, yaw, pitch, headYaw);
setFlag(EntityFlag.INVISIBLE, true);
invisible = false;
age = 0;
age = 1;
}
@Override
@ -58,10 +61,14 @@ public class ThrowableItemEntity extends ThrowableEntity {
private void checkVisibility() {
age++;
Vector3f playerPos = session.getPlayerEntity().getPosition().sub(0, session.getPlayerEntity().getDefinition().offset(),0);
// Prevent projectiles from blocking the player's screen
setInvisible((age <= 2 && !session.isTickingFrozen()) || (playerPos.distanceSquared(position.add(0, definition.offset(), 0)) < 12.25));
if (session.isTickingFrozen()) {
Vector3f playerPos = session.getPlayerEntity().getPosition().sub(0, session.getPlayerEntity().getDefinition().offset(), 0);
setInvisible(playerPos.distanceSquared(position.add(0, definition.offset(), 0)) < 12.25);
} else {
setInvisible(age <= 2);
}
if (invisible != getFlag(EntityFlag.INVISIBLE)) {
setFlag(EntityFlag.INVISIBLE, invisible);

View file

@ -587,7 +587,7 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
private boolean advancedTooltips = false;
/**
* The thread that will run every 50 milliseconds - one Minecraft tick.
* The thread that will run every game tick.
*/
private ScheduledFuture<?> tickThread = null;
@ -628,14 +628,15 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
private MinecraftProtocol protocol;
@Getter
private int millisecondsPerTick = 50;
private int nanosecondsPerTick = 50000000;
@Getter
private float millisecondsPerTick = 50.0f;
private boolean tickingFrozen = false;
/**
* The amount of ticks requested by the server that the game should proceed with, even if the game tick loop is frozen.
*/
@Setter
private int stepTicks = 0;
private boolean gameShouldUpdate = true;
public GeyserSession(GeyserImpl geyser, BedrockServerSession bedrockServerSession, EventLoop eventLoop) {
this.geyser = geyser;
@ -904,7 +905,7 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
boolean floodgate = this.remoteServer.authType() == AuthType.FLOODGATE;
// Start ticking
tickThread = eventLoop.scheduleAtFixedRate(this::tick, millisecondsPerTick, millisecondsPerTick, TimeUnit.MILLISECONDS);
tickThread = eventLoop.scheduleAtFixedRate(this::tick, nanosecondsPerTick, nanosecondsPerTick, TimeUnit.NANOSECONDS);
this.protocol.setUseDefaultListeners(false);
@ -1224,12 +1225,14 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
public void updateTickingState(float tickRate, boolean frozen) {
tickThread.cancel(false);
this.tickingFrozen = frozen;
millisecondsPerTick = Math.round(1000.0f / MathUtils.clamp(tickRate, 1.0f, 10000.0f));
tickThread = eventLoop.scheduleAtFixedRate(this::tick, millisecondsPerTick, millisecondsPerTick, TimeUnit.MILLISECONDS);
millisecondsPerTick = 1000.0f / tickRate;
nanosecondsPerTick = MathUtils.ceil(1000000000.0f / MathUtils.clamp(tickRate, 1.0f, 10000.0f));
tickThread = eventLoop.scheduleAtFixedRate(this::tick, nanosecondsPerTick, nanosecondsPerTick, TimeUnit.NANOSECONDS);
}
/**
* Called every Minecraft tick - 1000/tickRate milliseconds.
* Called every Minecraft tick.
*/
protected void tick() {
try {
@ -1267,7 +1270,7 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
isInWorldBorderWarningArea = false;
}
gameShouldUpdate = !tickingFrozen || stepTicks > 0;
boolean gameShouldUpdate = !tickingFrozen || stepTicks > 0;
if (stepTicks > 0) {
--stepTicks;
}

View file

@ -66,7 +66,7 @@ public class BedrockMobEquipmentTranslator extends PacketTranslator<MobEquipment
// (No need to send a release item packet - Java doesn't do this when swapping items)
// Required to do it a tick later or else it doesn't register
session.scheduleInEventLoop(() -> session.useItem(Hand.MAIN_HAND),
session.getMillisecondsPerTick(), TimeUnit.MILLISECONDS);
session.getNanosecondsPerTick(), TimeUnit.NANOSECONDS);
}
if (oldItem.getJavaId() != newItem.getJavaId()) {

View file

@ -58,7 +58,8 @@ public class CooldownUtils {
CooldownType sessionPreference = session.getPreferencesCache().getCooldownPreference();
if (sessionPreference == CooldownType.DISABLED) return;
if (session.getAttackSpeed() == 0.0 || session.getAttackSpeed() > 20) return; // 0.0 usually happens on login and causes issues with visuals; anything above 20 means a plugin like OldCombatMechanics is being used
if (session.getAttackSpeed() == 0.0 || session.getAttackSpeed() > 20)
return; // 0.0 usually happens on login and causes issues with visuals; anything above 20 means a plugin like OldCombatMechanics is being used
// Set the times to stay a bit with no fade in nor out
SetTitlePacket titlePacket = new SetTitlePacket();
titlePacket.setType(SetTitlePacket.Type.TIMES);
@ -91,7 +92,8 @@ public class CooldownUtils {
*/
private static void computeCooldown(GeyserSession session, CooldownType sessionPreference, long lastHitTime) {
if (session.isClosed()) return; // Don't run scheduled tasks if the client left
if (lastHitTime != session.getLastHitTime()) return; // Means another cooldown has started so there's no need to continue this one
if (lastHitTime != session.getLastHitTime())
return; // Means another cooldown has started so there's no need to continue this one
SetTitlePacket titlePacket = new SetTitlePacket();
if (sessionPreference == CooldownType.ACTIONBAR) {
titlePacket.setType(SetTitlePacket.Type.ACTIONBAR);
@ -104,7 +106,7 @@ public class CooldownUtils {
session.sendUpstreamPacket(titlePacket);
if (hasCooldown(session)) {
session.scheduleInEventLoop(() ->
computeCooldown(session, sessionPreference, lastHitTime), session.getMillisecondsPerTick(), TimeUnit.MILLISECONDS); // Updated per tick. 1000 divided by 20 ticks equals 50
computeCooldown(session, sessionPreference, lastHitTime), session.getNanosecondsPerTick(), TimeUnit.NANOSECONDS); // Updated per tick. 1000 divided by 20 ticks equals 50
} else {
SetTitlePacket removeTitlePacket = new SetTitlePacket();
removeTitlePacket.setType(SetTitlePacket.Type.CLEAR);