Add compass command

This commit is contained in:
William Blake Galbreath
2025-01-12 11:50:31 -08:00
committed by granny
parent a771a5beb7
commit a5c728eafe
8 changed files with 134 additions and 253 deletions

View File

@@ -8,7 +8,7 @@
import org.slf4j.Logger;
public class Commands {
@@ -245,6 +_,11 @@
@@ -245,6 +_,12 @@
StopCommand.register(this.dispatcher);
TransferCommand.register(this.dispatcher);
WhitelistCommand.register(this.dispatcher);
@@ -17,6 +17,7 @@
+ org.purpurmc.purpur.command.PingCommand.register(this.dispatcher); // Purpur - Add ping command
+ org.purpurmc.purpur.command.UptimeCommand.register(this.dispatcher); // Purpur - Add uptime command
+ org.purpurmc.purpur.command.TPSBarCommand.register(this.dispatcher); // Purpur - Implement TPSBar
+ org.purpurmc.purpur.command.CompassCommand.register(this.dispatcher); // Purpur - Add compass command
}
if (selection.includeIntegrated) {

View File

@@ -1,28 +1,31 @@
--- a/net/minecraft/server/level/ServerPlayer.java
+++ b/net/minecraft/server/level/ServerPlayer.java
@@ -393,6 +_,8 @@
@@ -393,6 +_,9 @@
public com.destroystokyo.paper.event.entity.PlayerNaturallySpawnCreaturesEvent playerNaturallySpawnedEvent; // Paper - PlayerNaturallySpawnCreaturesEvent
public @Nullable String clientBrandName = null; // Paper - Brand support
public org.bukkit.event.player.PlayerQuitEvent.QuitReason quitReason = null; // Paper - Add API for quit reason; there are a lot of changes to do if we change all methods leading to the event
+ public boolean purpurClient = false; // Purpur - Purpur client support
+ private boolean tpsBar = false; // Purpur - Implement TPSBar
+ private boolean compassBar = false; // Purpur - Add compass command
// Paper start - rewrite chunk system
private ca.spottedleaf.moonrise.patches.chunk_system.player.RegionizedPlayerChunkLoader.PlayerChunkLoaderData chunkLoader;
@@ -561,6 +_,8 @@
@@ -561,6 +_,9 @@
if (tag != null) {
BlockPos.CODEC.parse(NbtOps.INSTANCE, tag).resultOrPartial(LOGGER::error).ifPresent(pos -> this.raidOmenPosition = pos);
}
+
+ if (compound.contains("Purpur.TPSBar")) { this.tpsBar = compound.getBoolean("Purpur.TPSBar"); } // Purpur - Implement TPSBar
+ if (compound.contains("Purpur.CompassBar")) { this.compassBar = compound.getBoolean("Purpur.CompassBar"); } // Purpur - Add compass command
}
@Override
@@ -605,6 +_,7 @@
@@ -605,6 +_,8 @@
}
this.saveEnderPearls(compound);
+ compound.putBoolean("Purpur.TPSBar", this.tpsBar); // Purpur - Implement TPSBar
+ compound.putBoolean("Purpur.CompassBar", this.compassBar); // Purpur - Add compass command
}
private void saveParentVehicle(CompoundTag tag) {
@@ -203,7 +206,7 @@
public ServerStatsCounter getStats() {
return this.stats;
@@ -3077,4 +_,36 @@
@@ -3077,4 +_,46 @@
return (org.bukkit.craftbukkit.entity.CraftPlayer) super.getBukkitEntity();
}
// CraftBukkit end
@@ -239,4 +242,14 @@
+ this.tpsBar = tpsBar;
+ }
+ // Purpur end - Implement TPSBar
+
+ // Purpur start - Add compass command
+ public boolean compassBar() {
+ return this.compassBar;
+ }
+
+ public void compassBar(boolean compassBar) {
+ this.compassBar = compassBar;
+ }
+ // Purpur end - Add compass command
}

View File

@@ -243,6 +243,11 @@ public class PurpurConfig {
public static String commandTPSBarTextColorMedium = "<gradient:#ffff55:#ffaa00><text></gradient>";
public static String commandTPSBarTextColorLow = "<gradient:#ff5555:#aa0000><text></gradient>";
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;
public static boolean commandGamemodeRequiresPermission = false;
public static boolean hideHiddenPlayersFromEntitySelector = false;
public static String uptimeFormat = "<days><hours><minutes><seconds>";
@@ -265,6 +270,13 @@ public class PurpurConfig {
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);
commandGamemodeRequiresPermission = getBoolean("settings.command.gamemode.requires-specific-permission", commandGamemodeRequiresPermission);
hideHiddenPlayersFromEntitySelector = getBoolean("settings.command.hide-hidden-players-from-entity-selector", hideHiddenPlayersFromEntitySelector);
uptimeFormat = getString("settings.command.uptime.format", uptimeFormat);

View File

@@ -215,6 +215,7 @@ public class PurpurWorldConfig {
public float enderPearlEndermiteChance = 0.05F;
public int glowBerriesEatGlowDuration = 0;
public boolean shulkerBoxItemDropContentsWhenDestroyed = true;
public boolean compassItemShowsBossBar = false;
private void itemSettings() {
itemImmuneToCactus.clear();
getList("gameplay-mechanics.item.immune.cactus", new ArrayList<>()).forEach(key -> {
@@ -263,6 +264,7 @@ public class PurpurWorldConfig {
enderPearlEndermiteChance = (float) getDouble("gameplay-mechanics.item.ender-pearl.endermite-spawn-chance", enderPearlEndermiteChance);
glowBerriesEatGlowDuration = getInt("gameplay-mechanics.item.glow_berries.eat-glow-duration", glowBerriesEatGlowDuration);
shulkerBoxItemDropContentsWhenDestroyed = getBoolean("gameplay-mechanics.item.shulker_box.drop-contents-when-destroyed", shulkerBoxItemDropContentsWhenDestroyed);
compassItemShowsBossBar = getBoolean("gameplay-mechanics.item.compass.holding-shows-bossbar", compassItemShowsBossBar);
}
public double minecartMaxSpeed = 0.4D;

View File

@@ -0,0 +1,27 @@
package org.purpurmc.purpur.command;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer;
import org.purpurmc.purpur.task.CompassTask;
public class CompassCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("compass")
.requires(listener -> listener.hasPermission(2, "bukkit.command.compass"))
.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;
})
);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,68 @@
package org.purpurmc.purpur.task;
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.purpurmc.purpur.PurpurConfig;
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 < PurpurConfig.commandCompassBarTickInterval) {
return;
}
tick = 0;
MinecraftServer.getServer().getAllLevels().forEach((level) -> {
if (level.purpurConfig.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(""), PurpurConfig.commandCompassBarProgressPercent, PurpurConfig.commandCompassBarProgressColor, PurpurConfig.commandCompassBarProgressOverlay);
}
@Override
void updateBossBar(BossBar bossbar, Player player) {
float yaw = player.getLocation().getYaw();
int length = PurpurConfig.commandCompassBarTitle.length();
int pos = (int) ((normalize(yaw) * (length / 720F)) + (length / 2F));
bossbar.name(Component.text(PurpurConfig.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;
}
}