forked from SteamWar/SteamWar
Improve NoClipCommand
Improve SelectBauGuiItem
This commit is contained in:
+7
-7
@@ -20,6 +20,7 @@ package de.steamwar.bausystem.features.util;
|
|||||||
|
|
||||||
import de.steamwar.bausystem.BauSystem;
|
import de.steamwar.bausystem.BauSystem;
|
||||||
import de.steamwar.command.SWCommand;
|
import de.steamwar.command.SWCommand;
|
||||||
|
import de.steamwar.core.SWPlayer;
|
||||||
import de.steamwar.linkage.Linked;
|
import de.steamwar.linkage.Linked;
|
||||||
import org.bukkit.GameMode;
|
import org.bukkit.GameMode;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@@ -39,10 +40,11 @@ public class GamemodeCommand extends SWCommand {
|
|||||||
|
|
||||||
@Register
|
@Register
|
||||||
public void genericCommand(final Player p) {
|
public void genericCommand(final Player p) {
|
||||||
if (NoClipCommand.getNOCLIPS().contains(p)) {
|
SWPlayer swPlayer = SWPlayer.of(p);
|
||||||
p.performCommand("noclip");
|
if (swPlayer.hasComponent(NoClipCommand.NoClipData.class)) {
|
||||||
return;
|
swPlayer.removeComponent(NoClipCommand.NoClipData.class);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
if (p.getGameMode() == GameMode.CREATIVE) {
|
if (p.getGameMode() == GameMode.CREATIVE) {
|
||||||
p.setGameMode(GameMode.SPECTATOR);
|
p.setGameMode(GameMode.SPECTATOR);
|
||||||
} else {
|
} else {
|
||||||
@@ -52,9 +54,7 @@ public class GamemodeCommand extends SWCommand {
|
|||||||
|
|
||||||
@Register
|
@Register
|
||||||
public void gamemodeCommand(final Player p, final GameMode gameMode) {
|
public void gamemodeCommand(final Player p, final GameMode gameMode) {
|
||||||
if (NoClipCommand.getNOCLIPS().contains(p)) {
|
SWPlayer.of(p).removeComponent(NoClipCommand.NoClipData.class);
|
||||||
p.performCommand("noclip");
|
|
||||||
}
|
|
||||||
p.setGameMode(gameMode);
|
p.setGameMode(gameMode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+30
-32
@@ -19,17 +19,17 @@
|
|||||||
|
|
||||||
package de.steamwar.bausystem.features.util;
|
package de.steamwar.bausystem.features.util;
|
||||||
|
|
||||||
import de.steamwar.Reflection;
|
|
||||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||||
import com.mojang.authlib.GameProfile;
|
import com.mojang.authlib.GameProfile;
|
||||||
|
import de.steamwar.Reflection;
|
||||||
import de.steamwar.bausystem.BauSystem;
|
import de.steamwar.bausystem.BauSystem;
|
||||||
import de.steamwar.bausystem.features.tpslimit.TPSUtils;
|
import de.steamwar.bausystem.features.tpslimit.TPSUtils;
|
||||||
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
|
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
|
||||||
import de.steamwar.bausystem.utils.NMSWrapper;
|
import de.steamwar.bausystem.utils.NMSWrapper;
|
||||||
import de.steamwar.command.SWCommand;
|
import de.steamwar.command.SWCommand;
|
||||||
import de.steamwar.core.ProtocolWrapper;
|
import de.steamwar.core.ProtocolWrapper;
|
||||||
|
import de.steamwar.core.SWPlayer;
|
||||||
import de.steamwar.linkage.Linked;
|
import de.steamwar.linkage.Linked;
|
||||||
import lombok.Getter;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.GameMode;
|
import org.bukkit.GameMode;
|
||||||
import org.bukkit.entity.Player;
|
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.PlayerTeleportEvent;
|
||||||
import org.bukkit.event.player.PlayerToggleFlightEvent;
|
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;
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
@Linked
|
@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<?> windowClick = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundContainerClickPacket");
|
||||||
private static final Class<?> setSlotStack = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundSetCreativeModeSlotPacket");
|
private static final Class<?> setSlotStack = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundSetCreativeModeSlotPacket");
|
||||||
|
|
||||||
@Getter
|
public static class NoClipData implements SWPlayer.Component {
|
||||||
private static final List<Player> NOCLIPS = new ArrayList<>();
|
private long lastTick = -1;
|
||||||
private static final Map<Player, Long> LAST_TICKS = new HashMap<>();
|
|
||||||
|
@Override
|
||||||
|
public void onUnmount(SWPlayer player) {
|
||||||
|
player.setGameMode(GameMode.CREATIVE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public NoClipCommand() {
|
public NoClipCommand() {
|
||||||
super("noclip", "nc");
|
super("noclip", "nc");
|
||||||
|
|
||||||
BiFunction<Player, Object, Object> first = (player, o) -> {
|
BiFunction<Player, Object, Object> first = (player, o) -> {
|
||||||
if (NOCLIPS.contains(player)) {
|
NoClipData noClipData = SWPlayer.of(player).getComponent(NoClipData.class).orElse(null);
|
||||||
if (LAST_TICKS.getOrDefault(player, -1L).equals(TPSUtils.currentTick.get())) return o;
|
if (noClipData == null) return o;
|
||||||
NMSWrapper.impl.setInternalGameMode(player, GameMode.SPECTATOR);
|
if (noClipData.lastTick == TPSUtils.currentTick.get()) return o;
|
||||||
LAST_TICKS.put(player, TPSUtils.currentTick.get());
|
NMSWrapper.impl.setInternalGameMode(player, GameMode.SPECTATOR);
|
||||||
}
|
noClipData.lastTick = TPSUtils.currentTick.get();
|
||||||
return o;
|
return o;
|
||||||
};
|
};
|
||||||
TinyProtocol.instance.addFilter(position, first);
|
TinyProtocol.instance.addFilter(position, first);
|
||||||
TinyProtocol.instance.addFilter(positionLook, first);
|
TinyProtocol.instance.addFilter(positionLook, first);
|
||||||
|
|
||||||
BiFunction<Player, Object, Object> second = (player, o) -> {
|
BiFunction<Player, Object, Object> second = (player, o) -> {
|
||||||
if (NOCLIPS.contains(player)) {
|
NoClipData noClipData = SWPlayer.of(player).getComponent(NoClipData.class).orElse(null);
|
||||||
NMSWrapper.impl.setInternalGameMode(player, GameMode.CREATIVE);
|
if (noClipData == null) return o;
|
||||||
LAST_TICKS.put(player, TPSUtils.currentTick.get());
|
NMSWrapper.impl.setInternalGameMode(player, GameMode.CREATIVE);
|
||||||
}
|
noClipData.lastTick = TPSUtils.currentTick.get();
|
||||||
return o;
|
return o;
|
||||||
};
|
};
|
||||||
TinyProtocol.instance.addFilter(useItem, second);
|
TinyProtocol.instance.addFilter(useItem, second);
|
||||||
@@ -89,7 +90,7 @@ public class NoClipCommand extends SWCommand implements Listener {
|
|||||||
TinyProtocol.instance.addFilter(windowClick, second);
|
TinyProtocol.instance.addFilter(windowClick, second);
|
||||||
|
|
||||||
BiFunction<Player, Object, Object> third = (player, o) -> {
|
BiFunction<Player, Object, Object> third = (player, o) -> {
|
||||||
if (NOCLIPS.contains(player)) {
|
if (SWPlayer.of(player).hasComponent(NoClipData.class)) {
|
||||||
NMSWrapper.impl.setSlotToItemStack(player, o);
|
NMSWrapper.impl.setSlotToItemStack(player, o);
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
@@ -99,9 +100,9 @@ public class NoClipCommand extends SWCommand implements Listener {
|
|||||||
|
|
||||||
@Register(help = true)
|
@Register(help = true)
|
||||||
public void genericCommand(@Validator Player player) {
|
public void genericCommand(@Validator Player player) {
|
||||||
if (NOCLIPS.contains(player)) {
|
SWPlayer swPlayer = SWPlayer.of(player);
|
||||||
NOCLIPS.remove(player);
|
if (swPlayer.hasComponent(NoClipData.class)) {
|
||||||
player.setGameMode(GameMode.CREATIVE);
|
swPlayer.removeComponent(NoClipData.class);
|
||||||
} else {
|
} else {
|
||||||
player.setGameMode(GameMode.SPECTATOR);
|
player.setGameMode(GameMode.SPECTATOR);
|
||||||
NMSWrapper.impl.setPlayerBuildAbilities(player);
|
NMSWrapper.impl.setPlayerBuildAbilities(player);
|
||||||
@@ -109,8 +110,8 @@ public class NoClipCommand extends SWCommand implements Listener {
|
|||||||
Object gameStateChangeObject = Reflection.newInstance(gameStateChange);
|
Object gameStateChangeObject = Reflection.newInstance(gameStateChange);
|
||||||
NMSWrapper.impl.setGameStateChangeReason(gameStateChangeObject);
|
NMSWrapper.impl.setGameStateChangeReason(gameStateChangeObject);
|
||||||
floatFieldAccessor.set(gameStateChangeObject, 1F);
|
floatFieldAccessor.set(gameStateChangeObject, 1F);
|
||||||
|
|
||||||
NOCLIPS.add(player);
|
swPlayer.setComponent(new NoClipData());
|
||||||
BauSystem.MESSAGE.send("OTHER_NOCLIP_SLOT_INFO", player);
|
BauSystem.MESSAGE.send("OTHER_NOCLIP_SLOT_INFO", player);
|
||||||
TinyProtocol.instance.sendPacket(player, gameStateChangeObject);
|
TinyProtocol.instance.sendPacket(player, gameStateChangeObject);
|
||||||
pseudoGameMode(player, GameMode.SPECTATOR);
|
pseudoGameMode(player, GameMode.SPECTATOR);
|
||||||
@@ -120,30 +121,27 @@ public class NoClipCommand extends SWCommand implements Listener {
|
|||||||
@EventHandler
|
@EventHandler
|
||||||
public void onBauMemberUpdate(BauMemberUpdateEvent event) {
|
public void onBauMemberUpdate(BauMemberUpdateEvent event) {
|
||||||
event.getNewSpectator().forEach(player -> {
|
event.getNewSpectator().forEach(player -> {
|
||||||
if (NOCLIPS.contains(player)) {
|
SWPlayer.of(player).removeComponent(NoClipData.class);
|
||||||
NOCLIPS.remove(player);
|
|
||||||
player.setGameMode(GameMode.CREATIVE);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true)
|
@EventHandler(ignoreCancelled = true)
|
||||||
public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) {
|
public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) {
|
||||||
if (NOCLIPS.contains(event.getPlayer())) {
|
if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true)
|
@EventHandler(ignoreCancelled = true)
|
||||||
public void onBlock(BlockCanBuildEvent event) {
|
public void onBlock(BlockCanBuildEvent event) {
|
||||||
if (NOCLIPS.contains(event.getPlayer())) {
|
if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) {
|
||||||
event.setBuildable(true);
|
event.setBuildable(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true)
|
@EventHandler(ignoreCancelled = true)
|
||||||
public void onPlayerToggleFlight(PlayerToggleFlightEvent event) {
|
public void onPlayerToggleFlight(PlayerToggleFlightEvent event) {
|
||||||
if (NOCLIPS.contains(event.getPlayer())) {
|
if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
event.getPlayer().setFlying(true);
|
event.getPlayer().setFlying(true);
|
||||||
}
|
}
|
||||||
@@ -154,7 +152,7 @@ public class NoClipCommand extends SWCommand implements Listener {
|
|||||||
if (event.getCause() != PlayerTeleportEvent.TeleportCause.SPECTATE) {
|
if (event.getCause() != PlayerTeleportEvent.TeleportCause.SPECTATE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (NOCLIPS.contains(event.getPlayer())) {
|
if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
|
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
|
||||||
event.getPlayer().setSpectatorTarget(null);
|
event.getPlayer().setSpectatorTarget(null);
|
||||||
|
|||||||
+5
-8
@@ -24,6 +24,7 @@ import de.steamwar.bausystem.Permission;
|
|||||||
import de.steamwar.bausystem.linkage.BauGuiItem;
|
import de.steamwar.bausystem.linkage.BauGuiItem;
|
||||||
import de.steamwar.bausystem.region.utils.RegionExtensionType;
|
import de.steamwar.bausystem.region.utils.RegionExtensionType;
|
||||||
import de.steamwar.bausystem.region.utils.RegionType;
|
import de.steamwar.bausystem.region.utils.RegionType;
|
||||||
|
import de.steamwar.core.SWPlayer;
|
||||||
import de.steamwar.inventory.SWInventory;
|
import de.steamwar.inventory.SWInventory;
|
||||||
import de.steamwar.inventory.SWItem;
|
import de.steamwar.inventory.SWItem;
|
||||||
import de.steamwar.linkage.Linked;
|
import de.steamwar.linkage.Linked;
|
||||||
@@ -35,15 +36,11 @@ import org.bukkit.event.inventory.ClickType;
|
|||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Linked
|
@Linked
|
||||||
public class SelectBauGuiItem extends BauGuiItem {
|
public class SelectBauGuiItem extends BauGuiItem {
|
||||||
|
|
||||||
private static final Map<Player, LastSelect> LAST_SELECT_MAP = new HashMap<>();
|
|
||||||
|
|
||||||
public SelectBauGuiItem() {
|
public SelectBauGuiItem() {
|
||||||
super(13);
|
super(13);
|
||||||
}
|
}
|
||||||
@@ -58,13 +55,13 @@ public class SelectBauGuiItem extends BauGuiItem {
|
|||||||
|
|
||||||
private static void selectFinish(Player p, RegionType type, RegionExtensionType extensionType) {
|
private static void selectFinish(Player p, RegionType type, RegionExtensionType extensionType) {
|
||||||
p.closeInventory();
|
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());
|
p.performCommand("select " + type.name() + " " + extensionType.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack getItem(Player player) {
|
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 -> {
|
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();
|
}).getItemStack();
|
||||||
}
|
}
|
||||||
@@ -80,7 +77,7 @@ public class SelectBauGuiItem extends BauGuiItem {
|
|||||||
inv.open();
|
inv.open();
|
||||||
} else {
|
} else {
|
||||||
p.closeInventory();
|
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());
|
p.performCommand("select " + last.getType().name() + " " + last.getExtensionType().toString());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -92,7 +89,7 @@ public class SelectBauGuiItem extends BauGuiItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
private static class LastSelect {
|
private static class LastSelect implements SWPlayer.Component {
|
||||||
@Getter
|
@Getter
|
||||||
private final RegionType type;
|
private final RegionType type;
|
||||||
@Getter
|
@Getter
|
||||||
|
|||||||
Reference in New Issue
Block a user