Skip to content

Commit

Permalink
optimize by avoiding reoccurring bukkit tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
DoggySazHi committed Jun 21, 2024
1 parent 8a7cf08 commit 8b011b8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public void onPlayerQuit(PlayerQuitEvent event) {
var query = TouhouPlayers.players.get(player.getUniqueId());

if (query != null) {
if (query.debugTask != null) {
query.debugTask.cancel();
}

if (query.hitbox != null) {
query.hitbox.remove();
}
Expand Down Expand Up @@ -71,7 +67,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
query.bossMode = (args[1].equalsIgnoreCase("true"));

if (args.length > 2) {
if (args[2].equalsIgnoreCase("true") && query.debugTask == null) {
if (args[2].equalsIgnoreCase("true") && query.hitbox == null) {
// Debug mode: show hitbox using a block display entity (glass block)

var world = player.getWorld();
Expand All @@ -80,11 +76,11 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
var hitbox = (CraftBlockDisplay) world.spawnEntity(hitboxLocation, EntityType.BLOCK_DISPLAY);
query.hitbox = hitbox;

// Only show hitbox to the player
hitbox.setVisibleByDefault(false);
// hitbox.setTeleportDuration(1);
player.showEntity(GensouJank.getInstance(), hitbox);

// Set hitbox to glass block
// Set hitbox to spawner block
hitbox.setBlock(Material.SPAWNER.createBlockData());

// I do not particularly like Bukkit's API, soooo
Expand All @@ -97,23 +93,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

// Make the hitbox follow the player
player.addPassenger(hitbox);

query.debugTask = Bukkit.getScheduler().runTaskTimer(GensouJank.getInstance(), () -> {
var timerBox = player.getBoundingBox();
var tHitBox = query.hitbox;

tHitBox.getHandle().setTransformation(new Transformation(
new Vector3f((float) -player.getWidth() / 2.0f, (float) -timerBox.getHeight(), (float) -player.getWidth() / 2.0f),
null,
new Vector3f((float) timerBox.getWidthX(), (float) timerBox.getHeight(), (float) timerBox.getWidthZ()),
null
));

query.lastBoundingBox = timerBox;
}, 0L, 1L);
} else if (args[2].equalsIgnoreCase("false") && query.debugTask != null) {
query.debugTask.cancel();
query.debugTask = null;
} else if (args[2].equalsIgnoreCase("false") && query.hitbox != null) {
query.hitbox.remove();
query.hitbox = null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package net.gensokyoreimagined.gensoujankmod;

import org.bukkit.craftbukkit.entity.CraftBlockDisplay;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.BoundingBox;

import java.util.UUID;

Expand All @@ -12,8 +10,6 @@ public class TouhouPlayer {

public double adjustY = TouhouPlayers.defaultAdjustY;
public boolean bossMode = false;
public BukkitTask debugTask;
public BoundingBox lastBoundingBox;
public CraftBlockDisplay hitbox;

public TouhouPlayer(UUID uuid) {
Expand Down

0 comments on commit 8b011b8

Please sign in to comment.