9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2025-12-19 23:09:32 +00:00
Files
SakuraMC/patches/api/0003-Visibility-API.patch

231 lines
7.3 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samsuik <kfian294ma4@gmail.com>
Date: Tue, 21 Sep 2021 23:55:45 +0100
Subject: [PATCH] Visibility API
diff --git a/src/main/java/me/samsuik/sakura/player/visibility/Visibility.java b/src/main/java/me/samsuik/sakura/player/visibility/Visibility.java
new file mode 100644
index 0000000000000000000000000000000000000000..3df11f07ce533b8b911ec423be850374fcc8a7c1
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/player/visibility/Visibility.java
@@ -0,0 +1,198 @@
+package me.samsuik.sakura.player.visibility;
+
+import net.kyori.adventure.text.format.NamedTextColor;
+import net.kyori.adventure.text.format.TextColor;
+import org.bukkit.Material;
+
+import java.util.EnumSet;
+import java.util.Locale;
+
+public class Visibility {
+
+ private final EnumSet<Setting> settings = EnumSet.noneOf(Setting.class);
+
+ /**
+ * Checks if the provided setting has been modified.
+ *
+ * @param setting provided
+ * @return the setting has been modified
+ */
+ public boolean isToggled(Setting setting) {
+ return settings.contains(setting);
+ }
+
+ /**
+ * Checks if the provided setting is enabled.
+ *
+ * @param setting provided
+ * @return the setting is enabled
+ */
+ public boolean isEnabled(Setting setting) {
+ return setting.defaultValue != isToggled(setting);
+ }
+
+ /**
+ * Checks if the provided setting is disabled.
+ *
+ * @param setting provided
+ * @return the setting is disabled
+ */
+ public boolean isDisabled(Setting setting) {
+ return !isEnabled(setting);
+ }
+
+ /**
+ * Toggles the provided settings state.
+ * [ true -> false, false -> true ]
+ * NOTE: the default value can vary.
+ *
+ * @param setting provided
+ */
+ public void toggle(Setting setting) {
+ if (!settings.remove(setting)) {
+ settings.add(setting);
+ }
+ }
+
+ /**
+ * Toggles the provided settings state if it matches the modified state.
+ *
+ * @param setting provided
+ * @param modified fun
+ */
+ public void toggle(Setting setting, boolean modified) {
+ if (isToggled(setting) == modified) {
+ toggle(setting);
+ }
+ }
+
+ /**
+ * Sets the provided settings state.
+ *
+ * @param setting provided
+ * @param enable whether to enable this setting
+ */
+ public void set(Setting setting, boolean enable) {
+ if (isEnabled(setting) && !enable || isDisabled(setting) && enable) {
+ toggle(setting);
+ }
+ }
+
+ /**
+ * Toggles all settings based on the logic below.
+ * IF: settings are untouched or have been modified
+ * THEN: disable all settings
+ * IF: all settings are disabled
+ * THEN: enable all settings
+ */
+ public void toggleAll() {
+ boolean toggled = true; // true == all toggled
+
+ // If something is enabled set state to true
+ for (Setting setting : Setting.values()) {
+ toggled &= isToggled(setting);
+ }
+
+ // apply the opposite of the state so that it toggles
+ for (Setting setting : Setting.values()) {
+ toggle(setting, toggled);
+ }
+ }
+
+ /**
+ * Whether the settings have been modified.
+ * This is exposed to allow for quick return optimisations.
+ *
+ * @return settings have been modified
+ */
+ public boolean isModified() {
+ return !settings.isEmpty();
+ }
+
+ /**
+ * Current visibility settings state.
+ *
+ * @return state
+ */
+ public State getState() {
+ if (this.settings.isEmpty()) {
+ return State.ENABLED;
+ } else if (this.settings.size() != Setting.values().length) {
+ return State.MODIFIED;
+ } else {
+ return State.DISABLED;
+ }
+ }
+
+ public enum Setting {
+ TNT_VISIBILITY("TNT Visibility", Material.TNT, NamedTextColor.RED, true),
+ SAND_VISIBILITY("Sand Visibility", Material.SAND, NamedTextColor.YELLOW, true),
+ MINIMAL("Minimal TNT/Sand", Material.NETHER_BRICK_SLAB, NamedTextColor.GOLD, false),
+ EXPLOSIONS("Explosion Particles", Material.COBWEB, NamedTextColor.WHITE, true),
+ ENCHANTMENT_GLINT("Enchantment Glint", Material.ENCHANTED_BOOK, NamedTextColor.DARK_PURPLE, true),
+ SPAWNERS("Spawner Visibility", Material.SPAWNER, NamedTextColor.DARK_GRAY, true),
+ REDSTONE("Redstone Animations", Material.REDSTONE, NamedTextColor.DARK_RED, true),
+ PISTONS("Piston Animations", Material.PISTON, NamedTextColor.GOLD, true);
+
+ private final String friendlyName;
+ private final Material material;
+ private final TextColor colour;
+ private final boolean defaultValue;
+
+ Setting(String friendlyName, Material material, TextColor color, boolean defaultValue) {
+ this.friendlyName = friendlyName;
+ this.material = material;
+ this.colour = color;
+ this.defaultValue = defaultValue;
+ }
+
+ public boolean getDefault() {
+ return this.defaultValue;
+ }
+
+ public String friendlyName() {
+ return this.friendlyName;
+ }
+
+ public Material material() {
+ return this.material;
+ }
+
+ public TextColor colour() {
+ return this.colour;
+ }
+
+ public String basicName() {
+ return this.name().replace("_", "").toLowerCase(Locale.ROOT);
+ }
+ }
+
+ public enum State {
+ ENABLED("Enabled", Material.GREEN_STAINED_GLASS_PANE, NamedTextColor.GREEN),
+ MODIFIED("Modified", Material.MAGENTA_STAINED_GLASS_PANE, NamedTextColor.LIGHT_PURPLE),
+ DISABLED("Disabled", Material.RED_STAINED_GLASS_PANE, NamedTextColor.RED);
+
+ private final String title;
+ private final Material material;
+ private final TextColor colour;
+
+ State(String name, Material material, TextColor colour) {
+ this.title = name;
+ this.material = material;
+ this.colour = colour;
+ }
+
+ public String title() {
+ return this.title;
+ }
+
+ public Material material() {
+ return material;
+ }
+
+ public TextColor colour() {
+ return colour;
+ }
+ }
+
+}
diff --git a/src/main/java/org/bukkit/entity/Player.java b/src/main/java/org/bukkit/entity/Player.java
index d048ae07cc33fd77d128cc1ebf88b0804969fa3c..af8ec9dca850dc921fd33c393f1b606ce90c21db 100644
--- a/src/main/java/org/bukkit/entity/Player.java
+++ b/src/main/java/org/bukkit/entity/Player.java
@@ -56,6 +56,15 @@ import org.jetbrains.annotations.Nullable;
*/
public interface Player extends HumanEntity, Conversable, OfflinePlayer, PluginMessageRecipient, net.kyori.adventure.identity.Identified, net.kyori.adventure.bossbar.BossBarViewer, com.destroystokyo.paper.network.NetworkClient { // Paper
+ // Sakura start
+ /**
+ * Visibility API for FPS settings.
+ *
+ * @return visibility api
+ */
+ me.samsuik.sakura.player.visibility.Visibility getVisibility();
+ // Sakura end
+
// Paper start
@Override
default net.kyori.adventure.identity.@NotNull Identity identity() {