forked from SteamWar/SteamWar
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e0314472b |
+77
-69
@@ -19,10 +19,12 @@
|
||||
|
||||
package de.steamwar.bausystem.features.smartplace;
|
||||
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
import de.steamwar.bausystem.BauSystem;
|
||||
import de.steamwar.bausystem.Permission;
|
||||
import de.steamwar.bausystem.configplayer.Config;
|
||||
import de.steamwar.linkage.Linked;
|
||||
import net.minecraft.network.protocol.game.ServerboundUseItemOnPacket;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
@@ -32,98 +34,104 @@ import org.bukkit.block.data.*;
|
||||
import org.bukkit.block.data.type.*;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.RayTraceResult;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Linked
|
||||
public class SmartPlaceListener implements Listener {
|
||||
|
||||
private static final Set<Material> IGNORED = Set.of(Material.TNT, Material.REDSTONE_ORE, Material.BEEHIVE, Material.SEA_PICKLE);
|
||||
private static final Set<Material> CONTAINERS = new HashSet<>();
|
||||
private static final Set<Material> IGNORED = new HashSet<>();
|
||||
|
||||
private static final Set<UUID> FAKED_SNEAK = new HashSet<>();
|
||||
|
||||
private static boolean isContainer(Block block) {
|
||||
if (block.getType() == Material.COMPARATOR) return false;
|
||||
if (block.getType() == Material.GRINDSTONE) return true;
|
||||
return block.getState() instanceof TileState || block.getBlockData() instanceof Stairs;
|
||||
}
|
||||
|
||||
private static boolean isReallySneaking(Player player) {
|
||||
return player.isSneaking() && !FAKED_SNEAK.contains(player.getUniqueId());
|
||||
}
|
||||
|
||||
private static boolean shouldSmartPlace(Block block, BlockFace blockFace, Material heldType) {
|
||||
boolean smartPlace = false;
|
||||
if (block.getType().isInteractable() || block.getType() == Material.NOTE_BLOCK) {
|
||||
smartPlace = true;
|
||||
static {
|
||||
World world = Bukkit.getWorlds().get(0);
|
||||
Block block = world.getBlockAt(0, 0, 0);
|
||||
block.setType(Material.AIR);
|
||||
BlockState state = block.getState();
|
||||
for (Material material : Material.values()) {
|
||||
if (material.isLegacy()) continue;
|
||||
if (!material.isInteractable() && !material.isBlock()) continue;
|
||||
BlockData blockData = material.createBlockData();
|
||||
block.setBlockData(blockData);
|
||||
if (block.getState() instanceof TileState) {
|
||||
CONTAINERS.add(material);
|
||||
} else if (blockData instanceof Stairs) {
|
||||
CONTAINERS.add(material);
|
||||
}
|
||||
state.update(true, false);
|
||||
}
|
||||
if (isContainer(block)) {
|
||||
if (heldType == Material.TNT) {
|
||||
if (block.getType() == Material.CHEST || block.getType() == Material.BARREL || block.getType().name().endsWith("SHULKER_BOX")) {
|
||||
smartPlace = false;
|
||||
CONTAINERS.add(Material.GRINDSTONE);
|
||||
CONTAINERS.remove(Material.COMPARATOR);
|
||||
state.update(true, false);
|
||||
|
||||
IGNORED.add(Material.TNT);
|
||||
IGNORED.add(Material.REDSTONE_ORE);
|
||||
IGNORED.add(Material.BEEHIVE);
|
||||
IGNORED.add(Material.SEA_PICKLE);
|
||||
IGNORED.remove(Material.STONE);
|
||||
IGNORED.remove(Material.BARRIER);
|
||||
}
|
||||
|
||||
private static final Set<Player> SMART_PLACING = new HashSet<>();
|
||||
private static final Set<Player> WAS_EXECUTED = new HashSet<>();
|
||||
|
||||
public SmartPlaceListener() {
|
||||
TinyProtocol.instance.addFilter(ServerboundUseItemOnPacket.class, (player, packet) -> {
|
||||
if (!Permission.BUILD.hasPermission(player)) return packet;
|
||||
if (!Config.getInstance().get(player).getPlainValueOrDefault("smartPlace", false)) return packet;
|
||||
RayTraceResult rayTraceResult = player.rayTraceBlocks(6);
|
||||
Block block = rayTraceResult != null ? rayTraceResult.getHitBlock() : null;
|
||||
BlockFace blockFace = rayTraceResult != null ? rayTraceResult.getHitBlockFace() : null;
|
||||
boolean shouldSneak = false;
|
||||
if (block != null) {
|
||||
ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
if (block.getType().isInteractable() || block.getType() == Material.NOTE_BLOCK) {
|
||||
shouldSneak = true;
|
||||
}
|
||||
if (CONTAINERS.contains(block.getType())) {
|
||||
if (itemStack.getType() == Material.TNT) {
|
||||
if (block.getType() == Material.CHEST || block.getType() == Material.BARREL || block.getType().name().endsWith("SHULKER_BOX")) {
|
||||
shouldSneak = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
shouldSneak = false;
|
||||
}
|
||||
if (IGNORED.contains(block.getType())) {
|
||||
shouldSneak = false;
|
||||
}
|
||||
if (blockFace == BlockFace.DOWN && (block.getType() == Material.REPEATER || block.getType() == Material.COMPARATOR) && itemStack.getType().isSolid()) {
|
||||
shouldSneak = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
smartPlace = false;
|
||||
}
|
||||
if (IGNORED.contains(block.getType())) {
|
||||
smartPlace = false;
|
||||
}
|
||||
if (blockFace == BlockFace.DOWN && (block.getType() == Material.REPEATER || block.getType() == Material.COMPARATOR) && heldType.isSolid()) {
|
||||
smartPlace = true;
|
||||
}
|
||||
return smartPlace;
|
||||
}
|
||||
|
||||
private static boolean smartPlaceEnabled(Player player) {
|
||||
return Config.getInstance().get(player).getPlainValueOrDefault("smartPlace", false);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW)
|
||||
public void onSmartPlace(PlayerInteractEvent event) {
|
||||
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
|
||||
Player player = event.getPlayer();
|
||||
if (player.getGameMode() == GameMode.SPECTATOR) return;
|
||||
if (!Permission.BUILD.hasPermission(player)) return;
|
||||
if (!smartPlaceEnabled(player)) return;
|
||||
if (player.isSneaking()) return;
|
||||
|
||||
ItemStack itemStack = event.getItem();
|
||||
if (itemStack == null || itemStack.getType().isAir()) return;
|
||||
if (!shouldSmartPlace(event.getClickedBlock(), event.getBlockFace(), itemStack.getType())) return;
|
||||
|
||||
player.setSneaking(true);
|
||||
FAKED_SNEAK.add(player.getUniqueId());
|
||||
Bukkit.getScheduler().runTask(BauSystem.getInstance(), () -> {
|
||||
if (FAKED_SNEAK.remove(player.getUniqueId())) {
|
||||
player.setSneaking(false);
|
||||
}
|
||||
boolean sneaking = player.isSneaking();
|
||||
if (sneaking) SMART_PLACING.add(player);
|
||||
player.setSneaking(shouldSneak || sneaking);
|
||||
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
|
||||
SMART_PLACING.remove(player);
|
||||
player.setSneaking(sneaking);
|
||||
}, 0);
|
||||
return packet;
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onQuit(PlayerQuitEvent event) {
|
||||
FAKED_SNEAK.remove(event.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
if (!Permission.BUILD.hasPermission(event.getPlayer())) return;
|
||||
if (!smartPlaceEnabled(event.getPlayer())) return;
|
||||
if (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("smartPlace", false)) return;
|
||||
WAS_EXECUTED.add(event.getPlayer());
|
||||
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
|
||||
if (event.getPlayer().getGameMode() == GameMode.SPECTATOR) return;
|
||||
if (!isReallySneaking(event.getPlayer())) return;
|
||||
if (!event.getPlayer().isSneaking()) return;
|
||||
if (event.getClickedBlock().getType() != Material.REPEATER) return;
|
||||
if ((event.getItem() == null || event.getItem().getType() == Material.REPEATER)) {
|
||||
Repeater repeater = (Repeater) event.getClickedBlock().getBlockData();
|
||||
@@ -138,9 +146,9 @@ public class SmartPlaceListener implements Listener {
|
||||
@EventHandler
|
||||
public void onBlockPlace(BlockPlaceEvent event) {
|
||||
if (!Permission.BUILD.hasPermission(event.getPlayer())) return;
|
||||
if (!smartPlaceEnabled(event.getPlayer())) return;
|
||||
if (!isReallySneaking(event.getPlayer())) {
|
||||
if (isContainer(event.getBlockAgainst())) {
|
||||
if (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("smartPlace", false)) return;
|
||||
if (!SMART_PLACING.contains(event.getPlayer())) {
|
||||
if (CONTAINERS.contains(event.getBlockAgainst().getType())) {
|
||||
SoundGroup soundGroup = event.getBlockPlaced().getBlockData().getSoundGroup();
|
||||
event.getPlayer().playSound(event.getBlockPlaced().getLocation(), soundGroup.getPlaceSound(), soundGroup.getVolume() * 0.8F, soundGroup.getPitch() * 0.8F);
|
||||
}
|
||||
|
||||
+87
-29
@@ -22,71 +22,122 @@ package de.steamwar.bausystem.features.util;
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import de.steamwar.bausystem.BauSystem;
|
||||
import de.steamwar.bausystem.features.tpslimit.TPSUtils;
|
||||
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
|
||||
import de.steamwar.command.SWCommand;
|
||||
import de.steamwar.core.ProtocolWrapper;
|
||||
import de.steamwar.core.SWPlayer;
|
||||
import de.steamwar.linkage.Linked;
|
||||
import io.papermc.paper.event.player.PlayerFailMoveEvent;
|
||||
import net.minecraft.network.protocol.game.ClientboundGameEventPacket;
|
||||
import net.minecraft.network.protocol.game.*;
|
||||
import net.minecraft.world.entity.player.Abilities;
|
||||
import net.minecraft.world.level.GameType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.craftbukkit.entity.CraftPlayer;
|
||||
import org.bukkit.craftbukkit.inventory.CraftItemStack;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockCanBuildEvent;
|
||||
import org.bukkit.event.player.PlayerGameModeChangeEvent;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
import org.bukkit.event.player.PlayerToggleFlightEvent;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
@Linked
|
||||
public class NoClipCommand extends SWCommand implements Listener {
|
||||
|
||||
public static class NoClipData implements SWPlayer.Component {
|
||||
private long lastTick = -1;
|
||||
|
||||
@Override
|
||||
public void onUnmount(SWPlayer swPlayer) {
|
||||
Player player = swPlayer.getPlayer();
|
||||
showClientGameMode(player, GameMode.CREATIVE, GameMode.CREATIVE);
|
||||
public void onUnmount(SWPlayer player) {
|
||||
player.getPlayer().setGameMode(GameMode.CREATIVE);
|
||||
}
|
||||
}
|
||||
|
||||
public NoClipCommand() {
|
||||
super("noclip", "nc");
|
||||
|
||||
BiFunction<Player, ServerboundMovePlayerPacket, Object> first = (player, o) -> {
|
||||
NoClipData noClipData = SWPlayer.of(player).getComponent(NoClipData.class).orElse(null);
|
||||
if (noClipData == null) return o;
|
||||
if (noClipData.lastTick == TPSUtils.currentTick.get()) return o;
|
||||
setInternalGameMode(player, GameMode.SPECTATOR);
|
||||
noClipData.lastTick = TPSUtils.currentTick.get();
|
||||
return o;
|
||||
};
|
||||
TinyProtocol.instance.addFilter(ServerboundMovePlayerPacket.Pos.class, first);
|
||||
TinyProtocol.instance.addFilter(ServerboundMovePlayerPacket.PosRot.class, first);
|
||||
|
||||
BiFunction<Player, Object, Object> second = (player, o) -> {
|
||||
NoClipData noClipData = SWPlayer.of(player).getComponent(NoClipData.class).orElse(null);
|
||||
if (noClipData == null) return o;
|
||||
setInternalGameMode(player, GameMode.CREATIVE);
|
||||
noClipData.lastTick = TPSUtils.currentTick.get();
|
||||
return o;
|
||||
};
|
||||
TinyProtocol.instance.addFilter(ServerboundUseItemOnPacket.class, second);
|
||||
TinyProtocol.instance.addFilter(ServerboundPlayerActionPacket.class, second);
|
||||
TinyProtocol.instance.addFilter(ServerboundContainerClickPacket.class, second);
|
||||
|
||||
BiFunction<Player, ServerboundSetCreativeModeSlotPacket, Object> third = (player, o) -> {
|
||||
if (SWPlayer.of(player).hasComponent(NoClipData.class)) {
|
||||
int index = o.slotNum();
|
||||
if (index >= 36 && index <= 44) {
|
||||
index -= 36;
|
||||
} else if (index > 44) {
|
||||
index -= 5;
|
||||
} else if (index <= 8) {
|
||||
index = index - 8 + 36;
|
||||
}
|
||||
player.getInventory().setItem(index, CraftItemStack.asBukkitCopy(o.itemStack()));
|
||||
if (index < 9) player.getInventory().setHeldItemSlot(index);
|
||||
player.updateInventory();
|
||||
}
|
||||
return o;
|
||||
};
|
||||
TinyProtocol.instance.addFilter(ServerboundSetCreativeModeSlotPacket.class, third);
|
||||
}
|
||||
|
||||
@Register
|
||||
private void setInternalGameMode(Player player, GameMode gameMode) {
|
||||
((CraftPlayer) player).getHandle().gameMode.gameModeForPlayer = GameType.byId(gameMode.getValue());
|
||||
}
|
||||
|
||||
@Register(help = true)
|
||||
public void genericCommand(@Validator Player player) {
|
||||
SWPlayer swPlayer = SWPlayer.of(player);
|
||||
if (swPlayer.hasComponent(NoClipData.class)) {
|
||||
swPlayer.removeComponent(NoClipData.class);
|
||||
} else {
|
||||
player.setGameMode(GameMode.CREATIVE);
|
||||
player.setAllowFlight(true);
|
||||
player.setFlying(true);
|
||||
player.setGameMode(GameMode.SPECTATOR);
|
||||
Abilities abilities = (((CraftPlayer) player).getHandle()).getAbilities();
|
||||
abilities.mayBuild = true;
|
||||
abilities.mayfly = true;
|
||||
|
||||
swPlayer.setComponent(new NoClipData());
|
||||
BauSystem.MESSAGE.send("OTHER_NOCLIP_SLOT_INFO", player);
|
||||
showClientGameMode(player, GameMode.CREATIVE, GameMode.SPECTATOR);
|
||||
TinyProtocol.instance.sendPacket(player, new ClientboundGameEventPacket(ClientboundGameEventPacket.CHANGE_GAME_MODE, 1F));
|
||||
pseudoGameMode(player, GameMode.SPECTATOR);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerFailMove(PlayerFailMoveEvent event) {
|
||||
if (event.getFailReason() != PlayerFailMoveEvent.FailReason.CLIPPED_INTO_BLOCK) return;
|
||||
if (!SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) return;
|
||||
event.setAllowed(true);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onBauMemberUpdate(BauMemberUpdateEvent event) {
|
||||
event.getNewSpectator().forEach(player -> SWPlayer.of(player).removeComponent(NoClipData.class));
|
||||
event.getNewSpectator().forEach(player -> {
|
||||
SWPlayer.of(player).removeComponent(NoClipData.class);
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) {
|
||||
if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onBlock(BlockCanBuildEvent event) {
|
||||
if (event.getPlayer() == null) return;
|
||||
if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) {
|
||||
@@ -94,7 +145,7 @@ public class NoClipCommand extends SWCommand implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerToggleFlight(PlayerToggleFlightEvent event) {
|
||||
if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) {
|
||||
event.setCancelled(true);
|
||||
@@ -102,13 +153,20 @@ public class NoClipCommand extends SWCommand implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param player player
|
||||
* @param localMode GameMode for breaking and placing
|
||||
* @param listedMode GameMode for noPhysics
|
||||
*/
|
||||
private static void showClientGameMode(Player player, GameMode localMode, GameMode listedMode) {
|
||||
TinyProtocol.instance.sendPacket(player, new ClientboundGameEventPacket(ClientboundGameEventPacket.CHANGE_GAME_MODE, localMode.getValue()));
|
||||
TinyProtocol.instance.sendPacket(player, ProtocolWrapper.playerInfoPacketConstructor(ProtocolWrapper.PlayerInfoAction.GAMEMODE, new GameProfile(player.getUniqueId(), player.getName()), listedMode));
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerTeleport(PlayerTeleportEvent event) {
|
||||
if (event.getCause() != PlayerTeleportEvent.TeleportCause.SPECTATE) {
|
||||
return;
|
||||
}
|
||||
if (SWPlayer.of(event.getPlayer()).hasComponent(NoClipData.class)) {
|
||||
event.setCancelled(true);
|
||||
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
|
||||
event.getPlayer().setSpectatorTarget(null);
|
||||
}, 1L);
|
||||
}
|
||||
}
|
||||
|
||||
private static void pseudoGameMode(Player player, GameMode gameMode) {
|
||||
TinyProtocol.instance.sendPacket(player, ProtocolWrapper.playerInfoPacketConstructor(ProtocolWrapper.PlayerInfoAction.GAMEMODE, new GameProfile(player.getUniqueId(), player.getName()), gameMode));
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ import net.kyori.adventure.text.Component;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@Linked
|
||||
//@Linked
|
||||
public class FML2 extends BasicListener {
|
||||
// FML2: https://wiki.vg/Minecraft_Forge_Handshake#FML2_protocol_.281.13_-_Current.29
|
||||
// FML3: https://github.com/adde0109/Ambassador/tree/non-api/src/main/java/org/adde0109/ambassador/forge
|
||||
|
||||
Reference in New Issue
Block a user