Skip to content

Commit

Permalink
Refactor ticking logic into a dedicated method.
Browse files Browse the repository at this point in the history
Extracted the ticking logic into a `startTicking` method for better readability and reusability. Adjusted the implementation to ensure consistency when setting the ticking state for player characters.
  • Loading branch information
NonSwag committed Jan 19, 2025
1 parent 9de78e9 commit c133fbb
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,9 @@ public boolean spawn(Location location) {
entity.setSleepingIgnored(true);
}

if (ticking) plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, scheduledTask -> {
if (!ticking || entity == null) scheduledTask.cancel();
else if (entity.isValid()) serverPlayer.doTick();
}, 1, 1);

preSpawn(this.entity);
applySkinPartConfig(serverPlayer);
startTicking(serverPlayer);
return true;
}

Expand Down Expand Up @@ -331,6 +327,21 @@ private void spawnDisplayNameHologram(Player player) {
displayNameHologram = player.getWorld().spawn(location, TextDisplay.class, this::updateDisplayNameHologram);
}

@Override
public boolean setTicking(boolean ticking) {
if (!super.setTicking(ticking)) return false;
getEntity(CraftPlayer.class).map(CraftPlayer::getHandle)
.ifPresent(this::startTicking);
return true;
}

private void startTicking(ServerPlayer serverPlayer) {
if (ticking) plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, scheduledTask -> {
if (!ticking || entity == null) scheduledTask.cancel();
else if (serverPlayer.valid) serverPlayer.doTick();
}, 1, 1);
}

private boolean update() {
if (entity == null) return false;
var handle = ((CraftPlayer) entity).getHandle();
Expand Down

0 comments on commit c133fbb

Please sign in to comment.