From 8bd43aa1eb06511fa197c4cfc22a19acb7951fc9 Mon Sep 17 00:00:00 2001 From: Brian <32250137+TehBrian@users.noreply.github.com> Date: Thu, 16 Sep 2021 07:07:34 -0400 Subject: [PATCH] Only no-clip players if block is solid (#50) --- .../buildersutilities/BuildersUtilities.java | 7 +- .../buildersutilities/NoClipManager.java | 100 +++++++++--------- 2 files changed, 55 insertions(+), 52 deletions(-) diff --git a/src/main/java/net/arcaniax/buildersutilities/BuildersUtilities.java b/src/main/java/net/arcaniax/buildersutilities/BuildersUtilities.java index d0188dd..f4782ad 100644 --- a/src/main/java/net/arcaniax/buildersutilities/BuildersUtilities.java +++ b/src/main/java/net/arcaniax/buildersutilities/BuildersUtilities.java @@ -71,10 +71,15 @@ public final class BuildersUtilities extends JavaPlugin { instance = this; this.settings = new Settings(new CustomConfig(this, "config.yml")); - this.noClipManager = new NoClipManager(); + + this.noClipManager = new NoClipManager(this); + this.noClipManager.start(); + this.nmsManager = new NmsManager(); + this.inventoryManager = new InventoryManager(this); this.inventoryManager.init(); + BannerUtil.addColors(); BannerUtil.addPatterns(); diff --git a/src/main/java/net/arcaniax/buildersutilities/NoClipManager.java b/src/main/java/net/arcaniax/buildersutilities/NoClipManager.java index f3ed032..e225b57 100644 --- a/src/main/java/net/arcaniax/buildersutilities/NoClipManager.java +++ b/src/main/java/net/arcaniax/buildersutilities/NoClipManager.java @@ -26,8 +26,9 @@ package net.arcaniax.buildersutilities; import org.bukkit.Bukkit; import org.bukkit.GameMode; -import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.checkerframework.checker.nullness.qual.NonNull; import java.util.HashSet; import java.util.Set; @@ -35,64 +36,61 @@ import java.util.UUID; public class NoClipManager { - public static Set noClipPlayerIds; + public static Set noClipPlayerIds = new HashSet<>(); + private final JavaPlugin javaPlugin; - public NoClipManager() { - noClipPlayerIds = new HashSet<>(); - Bukkit.getScheduler().runTaskTimer(BuildersUtilities.getInstance(), this::checkForBlocks, 1L, 1L); + public NoClipManager( + final @NonNull JavaPlugin javaPlugin + ) { + this.javaPlugin = javaPlugin; + } + + public void start() { + Bukkit.getScheduler().runTaskTimer(javaPlugin, this::checkForBlocks, 1L, 1L); } private void checkForBlocks() { - for (UUID name : noClipPlayerIds) { - Player player = Bukkit.getPlayer(name); - if (player != null && player.isOnline()) { - if (player.getGameMode().equals(GameMode.CREATIVE)) { - boolean noClip; - if (player.getLocation().add(0, -0.1, 0).getBlock().getType() != Material.AIR && player - .isSneaking()) { - noClip = true; - } else { - noClip = isNoClip(player); - } - if (noClip) { - player.setGameMode(GameMode.SPECTATOR); - } - } else if (player.getGameMode().equals(GameMode.SPECTATOR)) { - boolean noClip; - if (player.getLocation().add(0, -0.1, 0).getBlock().getType() != Material.AIR) { - noClip = true; - } else { - noClip = isNoClip(player); - } - if (!noClip) { - player.setGameMode(GameMode.CREATIVE); - } + for (final UUID id : noClipPlayerIds) { + final Player p = javaPlugin.getServer().getPlayer(id); + + if (p == null || !p.isOnline()) { + continue; + } + + final boolean noClip; + if (p.getGameMode() == GameMode.CREATIVE) { + if (p.getLocation().add(0, -0.1, 0).getBlock().getType().isSolid() && p.isSneaking()) { + noClip = true; + } else { + noClip = this.shouldNoClip(p); + } + + if (noClip) { + p.setGameMode(GameMode.SPECTATOR); + } + } else if (p.getGameMode() == GameMode.SPECTATOR) { + if (p.getLocation().add(0, -0.1, 0).getBlock().getType().isSolid()) { + noClip = true; + } else { + noClip = this.shouldNoClip(p); + } + + if (!noClip) { + p.setGameMode(GameMode.CREATIVE); } } } } - private boolean isNoClip(Player player) { - boolean noClip = false; - if (player.getLocation().add(+0.4, 0, 0).getBlock().getType() != Material.AIR) { - noClip = true; - } else if (player.getLocation().add(-0.4, 0, 0).getBlock().getType() != Material.AIR) { - noClip = true; - } else if (player.getLocation().add(0, 0, +0.4).getBlock().getType() != Material.AIR) { - noClip = true; - } else if (player.getLocation().add(0, 0, -0.4).getBlock().getType() != Material.AIR) { - noClip = true; - } else if (player.getLocation().add(+0.4, 1, 0).getBlock().getType() != Material.AIR) { - noClip = true; - } else if (player.getLocation().add(-0.4, 1, 0).getBlock().getType() != Material.AIR) { - noClip = true; - } else if (player.getLocation().add(0, 1, +0.4).getBlock().getType() != Material.AIR) { - noClip = true; - } else if (player.getLocation().add(0, 1, -0.4).getBlock().getType() != Material.AIR) { - noClip = true; - } else if (player.getLocation().add(0, +1.9, 0).getBlock().getType() != Material.AIR) { - noClip = true; - } - return noClip; + private boolean shouldNoClip(final Player player) { + return player.getLocation().add(+0.4, 0, 0).getBlock().getType().isSolid() + || player.getLocation().add(-0.4, 0, 0).getBlock().getType().isSolid() + || player.getLocation().add(0, 0, +0.4).getBlock().getType().isSolid() + || player.getLocation().add(0, 0, -0.4).getBlock().getType().isSolid() + || player.getLocation().add(+0.4, 1, 0).getBlock().getType().isSolid() + || player.getLocation().add(-0.4, 1, 0).getBlock().getType().isSolid() + || player.getLocation().add(0, 1, +0.4).getBlock().getType().isSolid() + || player.getLocation().add(0, 1, -0.4).getBlock().getType().isSolid() + || player.getLocation().add(0, +1.9, 0).getBlock().getType().isSolid(); } }