Skip to content

Commit

Permalink
better hitboxes that don't tp/lag
Browse files Browse the repository at this point in the history
  • Loading branch information
DoggySazHi committed Jun 21, 2024
1 parent 80b7a86 commit 8a7cf08
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void onEnable(){
instance = this;

config.addDefault("hitbox-y-offset", 1.2);
// config.addDefault("debug-mode", false);

config.options().copyDefaults(true);
saveConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,43 +81,35 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
query.hitbox = hitbox;

hitbox.setVisibleByDefault(false);
hitbox.setTeleportDuration(1);
// hitbox.setTeleportDuration(1);
player.showEntity(GensouJank.getInstance(), hitbox);

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

// I do not particularly like Bukkit's API, soooo
hitbox.getHandle().setTransformation(new Transformation(
null,
new Vector3f((float) -player.getWidth() / 2.0f, (float) -box.getHeight(), (float) -player.getWidth() / 2.0f),
null,
new Vector3f((float) box.getWidthX(), (float) box.getHeight(), (float) box.getWidthZ()),
null
));

// 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(
null,
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
));

var onGround = player.isOnGround();
// compensate for player movement - step one tick ahead
var velocityX = query.lastBoundingBox == null ? 0 : timerBox.getMinX() - query.lastBoundingBox.getMinX();
var velocityY = query.lastBoundingBox == null ? 0 : timerBox.getMinY() - query.lastBoundingBox.getMinY();
var velocityZ = query.lastBoundingBox == null ? 0 : timerBox.getMinZ() - query.lastBoundingBox.getMinZ();

query.hitbox.teleport(new Location(
player.getWorld(), timerBox.getMinX() + 1 * velocityX, timerBox.getMinY() + (onGround ? 0 : 1 * velocityY), timerBox.getMinZ() + 1 * velocityZ
));

query.lastBoundingBox = timerBox;

// Bukkit.getLogger().info("[GensouJank] " + player.getName() + " position: " + player.getLocation());
}, 0L, 1L);
} else if (args[2].equalsIgnoreCase("false") && query.debugTask != null) {
query.debugTask.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.phys.AABB;
import org.bukkit.Bukkit;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -26,11 +25,10 @@ private void makeBoundingBox(CallbackInfoReturnable<AABB> cir) {
var uuid = player.getUUID();

if (gensouJank$instance == null) {
Bukkit.getLogger().info("[GensouJank] Creating new TouhouPlayer instance for " + uuid + "...");
gensouJank$instance = TouhouPlayers.players.computeIfAbsent(uuid, o -> new TouhouPlayer(uuid));
TouhouPlayers.players.put(uuid, gensouJank$instance);
} else if (!gensouJank$instance.uuid.equals(uuid)) {
Bukkit.getLogger().info("[GensouJank] Updating TouhouPlayer instance " + gensouJank$instance.uuid + " -> " + uuid + "...");
// UUID gets changed when the player fully logs in (changes to Minecraft UUID rather than entity UUID)
TouhouPlayers.players.remove(gensouJank$instance.uuid);
TouhouPlayers.players.put(uuid, gensouJank$instance);
gensouJank$instance.uuid = uuid;
Expand Down

0 comments on commit 8a7cf08

Please sign in to comment.