From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: William Blake Galbreath Date: Sat, 24 Jul 2021 00:07:31 -0500 Subject: [PATCH] Purpur: Add compass command Original license: MIT Original project: https://github.com/PurpurMC/Purpur diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java index 564830a1d511716ab977923ba43fa38c475f484f..2c5afab3cc0c66a2e63c1f647a033ca1a58f9e39 100644 --- a/src/main/java/net/minecraft/commands/Commands.java +++ b/src/main/java/net/minecraft/commands/Commands.java @@ -216,6 +216,7 @@ public class Commands { StopCommand.register(this.dispatcher); WhitelistCommand.register(this.dispatcher); org.dreeam.leaf.commands.TPSBarCommand.register(this.dispatcher); // Purpur + org.dreeam.leaf.commands.CompassCommand.register(this.dispatcher); // Purpur } if (environment.includeIntegrated) { diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java index d4ae8fc06ac5931273e6ced45158d8a3144a0bdf..336691038e2b1a68e1cd40d7015d42acceb2b069 100644 --- a/src/main/java/net/minecraft/server/level/ServerPlayer.java +++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java @@ -269,6 +269,7 @@ public class ServerPlayer extends Player { public PlayerNaturallySpawnCreaturesEvent playerNaturallySpawnedEvent; // Paper public org.bukkit.event.player.PlayerQuitEvent.QuitReason quitReason = null; // Paper - there are a lot of changes to do if we change all methods leading to the event private boolean tpsBar = false; // Purpur + private boolean compassBar = false; // Purpur public ServerPlayer(MinecraftServer server, ServerLevel world, GameProfile profile) { super(world, world.getSharedSpawnPos(), world.getSharedSpawnAngle(), profile); @@ -508,6 +509,7 @@ public class ServerPlayer extends Player { } if (nbt.contains("Purpur.TPSBar")) { this.tpsBar = nbt.getBoolean("Purpur.TPSBar"); } // Purpur + if (nbt.contains("Purpur.CompassBar")) { this.compassBar = nbt.getBoolean("Purpur.CompassBar"); } // Purpur } @Override @@ -575,6 +577,7 @@ public class ServerPlayer extends Player { this.getBukkitEntity().setExtraData(nbt); // CraftBukkit nbt.putBoolean("Purpur.TPSBar", this.tpsBar); // Purpur + nbt.putBoolean("Purpur.CompassBar", this.compassBar); // Purpur } // CraftBukkit start - World fallback code, either respawn location or global spawn @@ -2560,5 +2563,13 @@ public class ServerPlayer extends Player { public void tpsBar(boolean tpsBar) { this.tpsBar = tpsBar; } + + public boolean compassBar() { + return this.compassBar; + } + + public void compassBar(boolean compassBar) { + this.compassBar = compassBar; + } // Purpur end } diff --git a/src/main/java/org/dreeam/leaf/LeafConfig.java b/src/main/java/org/dreeam/leaf/LeafConfig.java index a0bd1ec3f09959c7f72b484658c1c94a16818281..e56151fe0dbc0c867dff728a55daa35c7714d3a6 100644 --- a/src/main/java/org/dreeam/leaf/LeafConfig.java +++ b/src/main/java/org/dreeam/leaf/LeafConfig.java @@ -170,6 +170,11 @@ public class LeafConfig { public static String commandTPSBarTextColorMedium = ""; public static String commandTPSBarTextColorLow = ""; public static int commandTPSBarTickInterval = 20; + public static String commandCompassBarTitle = "S \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 SW \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 W \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 NW \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 N \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 NE \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 E \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 SE \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 S \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 SW \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 W \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 NW \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 N \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 NE \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 E \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 SE \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 \u25C8 \u00B7 "; + public static BossBar.Overlay commandCompassBarProgressOverlay = BossBar.Overlay.PROGRESS; + public static BossBar.Color commandCompassBarProgressColor = BossBar.Color.BLUE; + public static float commandCompassBarProgressPercent = 1.0F; + public static int commandCompassBarTickInterval = 5; private static void commandSettings() { commandTPSBarTitle = getString("settings.command.tpsbar.title", commandTPSBarTitle); @@ -182,5 +187,17 @@ public class LeafConfig { commandTPSBarTextColorMedium = getString("settings.command.tpsbar.text-color.medium", commandTPSBarTextColorMedium); commandTPSBarTextColorLow = getString("settings.command.tpsbar.text-color.low", commandTPSBarTextColorLow); commandTPSBarTickInterval = getInt("settings.command.tpsbar.tick-interval", commandTPSBarTickInterval); + + commandCompassBarTitle = getString("settings.command.compass.title", commandCompassBarTitle); + commandCompassBarProgressOverlay = BossBar.Overlay.valueOf(getString("settings.command.compass.overlay", commandCompassBarProgressOverlay.name())); + commandCompassBarProgressColor = BossBar.Color.valueOf(getString("settings.command.compass.progress-color", commandCompassBarProgressColor.name())); + commandCompassBarProgressPercent = (float) getDouble("settings.command.compass.percent", commandCompassBarProgressPercent); + commandCompassBarTickInterval = getInt("settings.command.compass.tick-interval", commandCompassBarTickInterval); + + } + + public static boolean compassItemShowsBossBar = false; + private static void itemSettings() { + compassItemShowsBossBar = getBoolean("gameplay-mechanics.item.compass.holding-shows-bossbar", compassItemShowsBossBar); } } diff --git a/src/main/java/org/dreeam/leaf/commands/CompassCommand.java b/src/main/java/org/dreeam/leaf/commands/CompassCommand.java new file mode 100644 index 0000000000000000000000000000000000000000..a3c85794307e906c4343d490e0e10daaab0efb38 --- /dev/null +++ b/src/main/java/org/dreeam/leaf/commands/CompassCommand.java @@ -0,0 +1,27 @@ +package org.dreeam.leaf.commands; + +import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.server.level.ServerPlayer; +import org.dreeam.leaf.tasks.CompassTask; + +public class CompassCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(Commands.literal("compass") + .requires(listener -> listener.hasPermission(2)) + .executes(context -> { + ServerPlayer player = context.getSource().getPlayerOrException(); + CompassTask task = CompassTask.instance(); + if (player.compassBar()) { + task.removePlayer(player.getBukkitEntity()); + player.compassBar(false); + } else { + task.addPlayer(player.getBukkitEntity()); + player.compassBar(true); + } + return 1; + }) + ).setPermission("bukkit.command.compass"); + } +} diff --git a/src/main/java/org/dreeam/leaf/tasks/BossBarTask.java b/src/main/java/org/dreeam/leaf/tasks/BossBarTask.java index ae629c689779339589de3ec28fb6a79bf54da583..7dacf4f21d082dd9a19db6e5122d06b5bda8fc95 100644 --- a/src/main/java/org/dreeam/leaf/tasks/BossBarTask.java +++ b/src/main/java/org/dreeam/leaf/tasks/BossBarTask.java @@ -90,10 +90,12 @@ public abstract class BossBarTask extends BukkitRunnable { public static void startAll() { TPSBarTask.instance().start(); + CompassTask.instance().start(); } public static void stopAll() { TPSBarTask.instance().stop(); + CompassTask.instance().stop(); } public static void addToAll(ServerPlayer player) { @@ -101,9 +103,13 @@ public abstract class BossBarTask extends BukkitRunnable { if (player.tpsBar()) { TPSBarTask.instance().addPlayer(bukkit); } + if (player.compassBar()) { + CompassTask.instance().addPlayer(bukkit); + } } public static void removeFromAll(Player player) { TPSBarTask.instance().removePlayer(player); + CompassTask.instance().removePlayer(player); } } diff --git a/src/main/java/org/dreeam/leaf/tasks/CompassTask.java b/src/main/java/org/dreeam/leaf/tasks/CompassTask.java new file mode 100644 index 0000000000000000000000000000000000000000..3ecd9bb435e5066176da88f7d50d4920cee017e5 --- /dev/null +++ b/src/main/java/org/dreeam/leaf/tasks/CompassTask.java @@ -0,0 +1,68 @@ +package org.dreeam.leaf.tasks; + +import net.kyori.adventure.bossbar.BossBar; +import net.kyori.adventure.text.Component; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.item.Items; +import org.bukkit.entity.Player; +import org.dreeam.leaf.LeafConfig; + +public class CompassTask extends BossBarTask { + private static CompassTask instance; + + private int tick = 0; + + public static CompassTask instance() { + if (instance == null) { + instance = new CompassTask(); + } + return instance; + } + + @Override + public void run() { + if (++tick < LeafConfig.commandCompassBarTickInterval) { + return; + } + tick = 0; + + MinecraftServer.getServer().getAllLevels().forEach((level) -> { + if (LeafConfig.compassItemShowsBossBar) { + level.players().forEach(player -> { + if (!player.compassBar()) { + if (player.getMainHandItem().getItem() != Items.COMPASS && player.getOffhandItem().getItem() != Items.COMPASS) { + removePlayer(player.getBukkitEntity()); + } else if (!hasPlayer(player.getUUID())) { + addPlayer(player.getBukkitEntity()); + } + } + }); + } + }); + + super.run(); + } + + @Override + BossBar createBossBar() { + return BossBar.bossBar(Component.text(""), LeafConfig.commandCompassBarProgressPercent, LeafConfig.commandCompassBarProgressColor, LeafConfig.commandCompassBarProgressOverlay); + } + + @Override + void updateBossBar(BossBar bossbar, Player player) { + float yaw = player.getLocation().getYaw(); + int length = LeafConfig.commandCompassBarTitle.length(); + int pos = (int) ((normalize(yaw) * (length / 720F)) + (length / 2F)); + bossbar.name(Component.text(LeafConfig.commandCompassBarTitle.substring(pos - 25, pos + 25))); + } + + private float normalize(float yaw) { + while (yaw < -180.0F) { + yaw += 360.0F; + } + while (yaw > 180.0F) { + yaw -= 360.0F; + } + return yaw; + } +}