Improve NoClipCommand

Improve SelectBauGuiItem
This commit is contained in:
2025-11-07 14:43:52 +01:00
parent b69adcd362
commit 40ac7b2139
3 changed files with 42 additions and 47 deletions
@@ -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);
}
}
@@ -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<Player> NOCLIPS = new ArrayList<>();
private static final Map<Player, Long> 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<Player, Object, Object> 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<Player, Object, Object> 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<Player, Object, Object> 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);
@@ -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<Player, LastSelect> 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