From 40ac7b21392bc1220ae610616fd42bf1f7d26136 Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Fri, 7 Nov 2025 14:43:52 +0100 Subject: [PATCH] Improve NoClipCommand Improve SelectBauGuiItem --- .../features/util/GamemodeCommand.java | 14 ++--- .../features/util/NoClipCommand.java | 62 +++++++++---------- .../features/util/items/SelectBauGuiItem.java | 13 ++-- 3 files changed, 42 insertions(+), 47 deletions(-) diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/GamemodeCommand.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/GamemodeCommand.java index 9e3da950..86ecf6ca 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/GamemodeCommand.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/GamemodeCommand.java @@ -20,6 +20,7 @@ package de.steamwar.bausystem.features.util; import de.steamwar.bausystem.BauSystem; import de.steamwar.command.SWCommand; +import de.steamwar.core.SWPlayer; import de.steamwar.linkage.Linked; import org.bukkit.GameMode; import org.bukkit.entity.Player; @@ -39,10 +40,11 @@ public class GamemodeCommand extends SWCommand { @Register public void genericCommand(final Player p) { - if (NoClipCommand.getNOCLIPS().contains(p)) { - p.performCommand("noclip"); - return; - } + SWPlayer swPlayer = SWPlayer.of(p); + if (swPlayer.hasComponent(NoClipCommand.NoClipData.class)) { + swPlayer.removeComponent(NoClipCommand.NoClipData.class); + return; + } if (p.getGameMode() == GameMode.CREATIVE) { p.setGameMode(GameMode.SPECTATOR); } else { @@ -52,9 +54,7 @@ public class GamemodeCommand extends SWCommand { @Register public void gamemodeCommand(final Player p, final GameMode gameMode) { - if (NoClipCommand.getNOCLIPS().contains(p)) { - p.performCommand("noclip"); - } + SWPlayer.of(p).removeComponent(NoClipCommand.NoClipData.class); p.setGameMode(gameMode); } } \ No newline at end of file diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java index ad97dac0..61fdecdc 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java @@ -19,17 +19,17 @@ package de.steamwar.bausystem.features.util; -import de.steamwar.Reflection; import com.comphenix.tinyprotocol.TinyProtocol; import com.mojang.authlib.GameProfile; +import de.steamwar.Reflection; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.tpslimit.TPSUtils; import de.steamwar.bausystem.utils.BauMemberUpdateEvent; import de.steamwar.bausystem.utils.NMSWrapper; import de.steamwar.command.SWCommand; import de.steamwar.core.ProtocolWrapper; +import de.steamwar.core.SWPlayer; import de.steamwar.linkage.Linked; -import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.entity.Player; @@ -40,10 +40,6 @@ import org.bukkit.event.player.PlayerGameModeChangeEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerToggleFlightEvent; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import java.util.function.BiFunction; @Linked @@ -59,29 +55,34 @@ public class NoClipCommand extends SWCommand implements Listener { private static final Class windowClick = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundContainerClickPacket"); private static final Class setSlotStack = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundSetCreativeModeSlotPacket"); - @Getter - private static final List NOCLIPS = new ArrayList<>(); - private static final Map LAST_TICKS = new HashMap<>(); + public static class NoClipData implements SWPlayer.Component { + private long lastTick = -1; + + @Override + public void onUnmount(SWPlayer player) { + player.setGameMode(GameMode.CREATIVE); + } + } public NoClipCommand() { super("noclip", "nc"); BiFunction first = (player, o) -> { - if (NOCLIPS.contains(player)) { - if (LAST_TICKS.getOrDefault(player, -1L).equals(TPSUtils.currentTick.get())) return o; - NMSWrapper.impl.setInternalGameMode(player, GameMode.SPECTATOR); - LAST_TICKS.put(player, TPSUtils.currentTick.get()); - } + NoClipData noClipData = SWPlayer.of(player).getComponent(NoClipData.class).orElse(null); + if (noClipData == null) return o; + if (noClipData.lastTick == TPSUtils.currentTick.get()) return o; + NMSWrapper.impl.setInternalGameMode(player, GameMode.SPECTATOR); + noClipData.lastTick = TPSUtils.currentTick.get(); return o; }; TinyProtocol.instance.addFilter(position, first); TinyProtocol.instance.addFilter(positionLook, first); BiFunction second = (player, o) -> { - if (NOCLIPS.contains(player)) { - NMSWrapper.impl.setInternalGameMode(player, GameMode.CREATIVE); - LAST_TICKS.put(player, TPSUtils.currentTick.get()); - } + NoClipData noClipData = SWPlayer.of(player).getComponent(NoClipData.class).orElse(null); + if (noClipData == null) return o; + NMSWrapper.impl.setInternalGameMode(player, GameMode.CREATIVE); + noClipData.lastTick = TPSUtils.currentTick.get(); return o; }; TinyProtocol.instance.addFilter(useItem, second); @@ -89,7 +90,7 @@ public class NoClipCommand extends SWCommand implements Listener { TinyProtocol.instance.addFilter(windowClick, second); BiFunction third = (player, o) -> { - if (NOCLIPS.contains(player)) { + if (SWPlayer.of(player).hasComponent(NoClipData.class)) { NMSWrapper.impl.setSlotToItemStack(player, o); } return o; @@ -99,9 +100,9 @@ public class NoClipCommand extends SWCommand implements Listener { @Register(help = true) public void genericCommand(@Validator Player player) { - if (NOCLIPS.contains(player)) { - NOCLIPS.remove(player); - player.setGameMode(GameMode.CREATIVE); + SWPlayer swPlayer = SWPlayer.of(player); + if (swPlayer.hasComponent(NoClipData.class)) { + swPlayer.removeComponent(NoClipData.class); } else { player.setGameMode(GameMode.SPECTATOR); NMSWrapper.impl.setPlayerBuildAbilities(player); @@ -109,8 +110,8 @@ public class NoClipCommand extends SWCommand implements Listener { Object gameStateChangeObject = Reflection.newInstance(gameStateChange); NMSWrapper.impl.setGameStateChangeReason(gameStateChangeObject); floatFieldAccessor.set(gameStateChangeObject, 1F); - - NOCLIPS.add(player); + + swPlayer.setComponent(new NoClipData()); BauSystem.MESSAGE.send("OTHER_NOCLIP_SLOT_INFO", player); TinyProtocol.instance.sendPacket(player, gameStateChangeObject); pseudoGameMode(player, GameMode.SPECTATOR); @@ -120,30 +121,27 @@ public class NoClipCommand extends SWCommand implements Listener { @EventHandler public void onBauMemberUpdate(BauMemberUpdateEvent event) { event.getNewSpectator().forEach(player -> { - if (NOCLIPS.contains(player)) { - NOCLIPS.remove(player); - player.setGameMode(GameMode.CREATIVE); - } + SWPlayer.of(player).removeComponent(NoClipData.class); }); } @EventHandler(ignoreCancelled = true) public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) { - if (NOCLIPS.contains(event.getPlayer())) { + if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) { event.setCancelled(true); } } @EventHandler(ignoreCancelled = true) public void onBlock(BlockCanBuildEvent event) { - if (NOCLIPS.contains(event.getPlayer())) { + if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) { event.setBuildable(true); } } @EventHandler(ignoreCancelled = true) public void onPlayerToggleFlight(PlayerToggleFlightEvent event) { - if (NOCLIPS.contains(event.getPlayer())) { + if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) { event.setCancelled(true); event.getPlayer().setFlying(true); } @@ -154,7 +152,7 @@ public class NoClipCommand extends SWCommand implements Listener { if (event.getCause() != PlayerTeleportEvent.TeleportCause.SPECTATE) { return; } - if (NOCLIPS.contains(event.getPlayer())) { + if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) { event.setCancelled(true); Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { event.getPlayer().setSpectatorTarget(null); diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/SelectBauGuiItem.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/SelectBauGuiItem.java index 78669a8d..43b9cb85 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/SelectBauGuiItem.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/SelectBauGuiItem.java @@ -24,6 +24,7 @@ import de.steamwar.bausystem.Permission; import de.steamwar.bausystem.linkage.BauGuiItem; import de.steamwar.bausystem.region.utils.RegionExtensionType; import de.steamwar.bausystem.region.utils.RegionType; +import de.steamwar.core.SWPlayer; import de.steamwar.inventory.SWInventory; import de.steamwar.inventory.SWItem; import de.steamwar.linkage.Linked; @@ -35,15 +36,11 @@ import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.ItemStack; import java.util.Arrays; -import java.util.HashMap; import java.util.Locale; -import java.util.Map; @Linked public class SelectBauGuiItem extends BauGuiItem { - private static final Map LAST_SELECT_MAP = new HashMap<>(); - public SelectBauGuiItem() { super(13); } @@ -58,13 +55,13 @@ public class SelectBauGuiItem extends BauGuiItem { private static void selectFinish(Player p, RegionType type, RegionExtensionType extensionType) { p.closeInventory(); - LAST_SELECT_MAP.put(p, new LastSelect(type, extensionType)); + SWPlayer.of(p).setComponent(new LastSelect(type, extensionType)); p.performCommand("select " + type.name() + " " + extensionType.toString()); } @Override public ItemStack getItem(Player player) { - LastSelect last = LAST_SELECT_MAP.getOrDefault(player, new LastSelect(RegionType.BUILD, RegionExtensionType.NORMAL)); + LastSelect last = SWPlayer.of(player).getComponentOrDefault(LastSelect.class, () -> new LastSelect(RegionType.BUILD, RegionExtensionType.NORMAL)); return new SWItem(Material.SCAFFOLDING, BauSystem.MESSAGE.parse("SELECT_ITEM_SELECT", player), Arrays.asList(BauSystem.MESSAGE.parse("SELECT_ITEM_AUSWAHL", player, BauSystem.MESSAGE.parse(last.type.getChatValue(), player), last.extensionType.name()), BauSystem.MESSAGE.parse("SELECT_ITEM_RIGHT_CLICK", player)), false, clickType -> { }).getItemStack(); } @@ -80,7 +77,7 @@ public class SelectBauGuiItem extends BauGuiItem { inv.open(); } else { p.closeInventory(); - LastSelect last = LAST_SELECT_MAP.getOrDefault(p, new LastSelect(RegionType.BUILD, RegionExtensionType.NORMAL)); + LastSelect last = SWPlayer.of(p).getComponentOrDefault(LastSelect.class, () -> new LastSelect(RegionType.BUILD, RegionExtensionType.NORMAL)); p.performCommand("select " + last.getType().name() + " " + last.getExtensionType().toString()); } return false; @@ -92,7 +89,7 @@ public class SelectBauGuiItem extends BauGuiItem { } @AllArgsConstructor - private static class LastSelect { + private static class LastSelect implements SWPlayer.Component { @Getter private final RegionType type; @Getter