Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions API/src/main/java/fr/maxlego08/essentials/api/utils/Warp.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fr.maxlego08.essentials.api.utils;

import fr.maxlego08.essentials.api.commands.Permission;
import org.bukkit.Location;
import org.bukkit.permissions.Permissible;

import java.util.Collection;
import java.util.Locale;

/**
* Represents a warp location.
* This record encapsulates data related to a warp, including its name and location.
Expand All @@ -17,6 +19,24 @@ public record Warp(String name, SafeLocation location) {
* @return true if the permissible entity has permission, false otherwise.
*/
public boolean hasPermission(Permissible permissible) {
return permissible.hasPermission(Permission.ESSENTIALS_WARP_.asPermission(this.name));
if (permissible.hasPermission(Permission.ESSENTIALS_WARP.asPermission())) {
return true;
}
return permissible.hasPermission(Permission.ESSENTIALS_WARP_.asPermission(this.name.toLowerCase(Locale.ROOT)));
}

/**
* Checks if the permissible can use at least one warp or the global warp permission.
*/
public static boolean canAccessAnyWarp(Permissible permissible, Collection<Warp> warps) {
if (permissible.hasPermission(Permission.ESSENTIALS_WARP.asPermission())) {
return true;
}
for (Warp warp : warps) {
if (warp.hasPermission(permissible)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public boolean hasPermission() {
@Override
public boolean checkPermission(Player player, InventoryEngine inventory, Placeholders placeholders) {
Optional<Warp> optional = plugin.getWarp(this.warpName);
return super.checkPermission(player, inventory, placeholders) && optional.map(warp -> warp.hasPermission(player)).orElse(false);
if (optional.isEmpty() || !optional.get().hasPermission(player)) {
return false;
}
return super.checkPermission(player, inventory, placeholders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public boolean checkPermission(Player player, InventoryEngine inventory, Placeho
User user = this.plugin.getUser(player.getUniqueId());
if (user == null) return false;
Optional<Kit> optional = this.plugin.getKit(this.kitName);
return optional.filter(kit -> super.checkPermission(player, inventory, placeholders) && user.isKitCooldown(kit)).isPresent();
if (optional.isEmpty() || !optional.get().hasPermission(player) || !user.isKitCooldown(optional.get())) {
return false;
}
return super.checkPermission(player, inventory, placeholders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public boolean checkPermission(Player player, InventoryEngine inventory, Placeho
User user = this.plugin.getUser(player.getUniqueId());
if (user == null) return false;
Optional<Kit> optional = this.plugin.getKit(this.kitName);
return optional.filter(kit -> super.checkPermission(player, inventory, placeholders) && kit.hasPermission(player)).isPresent();
if (optional.isEmpty() || !optional.get().hasPermission(player)) {
return false;
}
return super.checkPermission(player, inventory, placeholders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import fr.maxlego08.essentials.api.EssentialsPlugin;
import fr.maxlego08.essentials.api.commands.CommandResultType;
import fr.maxlego08.essentials.api.commands.Permission;
import fr.maxlego08.essentials.api.messages.Message;
import fr.maxlego08.essentials.api.utils.Warp;
import fr.maxlego08.essentials.module.modules.WarpModule;
import fr.maxlego08.essentials.zutils.utils.commands.VCommand;
import org.apache.logging.log4j.util.Strings;
import org.bukkit.command.CommandSender;

import java.util.List;

Expand All @@ -16,7 +16,6 @@ public class CommandWarp extends VCommand {
public CommandWarp(EssentialsPlugin plugin) {
super(plugin);
this.setModule(WarpModule.class);
this.setPermission(Permission.ESSENTIALS_WARP);
this.setDescription(Message.DESCRIPTION_WARP_USE);
this.addOptionalArg("name", (sender, args) -> {
List<Warp> warps = plugin.getWarps();
Expand All @@ -25,6 +24,14 @@ public CommandWarp(EssentialsPlugin plugin) {
this.onlyPlayers();
}

@Override
public CommandResultType prePerform(EssentialsPlugin plugin, CommandSender commandSender, String[] args) {
if (!Warp.canAccessAnyWarp(commandSender, plugin.getWarps())) {
return CommandResultType.NO_PERMISSION;
}
return super.prePerform(plugin, commandSender, args);
}

@Override
protected CommandResultType perform(EssentialsPlugin plugin) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import fr.maxlego08.essentials.api.user.Option;
import fr.maxlego08.essentials.api.user.User;
import fr.maxlego08.essentials.api.utils.DynamicCooldown;
import fr.maxlego08.essentials.storage.ConfigStorage;
import fr.maxlego08.essentials.zutils.utils.FoliaJoinHelper;
import fr.maxlego08.essentials.zutils.utils.TimerBuilder;
import fr.maxlego08.essentials.zutils.utils.ZUtils;
import org.bukkit.Material;
Expand Down Expand Up @@ -190,11 +190,7 @@ public void onJoin(PlayerJoinEvent event) {
if (user != null) user.startCurrentSessionPlayTime();

if (user != null && user.isFirstJoin()) {
if (ConfigStorage.firstSpawnLocation != null && ConfigStorage.firstSpawnLocation.isValid()) {
this.plugin.getScheduler().teleportAsync(player, ConfigStorage.firstSpawnLocation.getLocation());
} else if (ConfigStorage.spawnLocation != null && ConfigStorage.spawnLocation.isValid()) {
this.plugin.getScheduler().teleportAsync(player, ConfigStorage.spawnLocation.getLocation());
}
FoliaJoinHelper.teleportFirstSpawnAfterJoin(this.plugin, player);
}

if (user != null && user.getOption(Option.VANISH)) {
Expand Down Expand Up @@ -225,7 +221,7 @@ public void onJoin(PlayerJoinEvent event) {
player.setFlySpeed(0.1f);
player.setWalkSpeed(0.2f);
}
}, 1);
}, FoliaJoinHelper.SAFE_LOGIN_DELAY_TICKS);
}

@EventHandler(priority = EventPriority.LOWEST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import fr.maxlego08.essentials.listener.paper.ChatListener;
import fr.maxlego08.essentials.module.ZModule;
import fr.maxlego08.essentials.user.ZUser;
import fr.maxlego08.essentials.zutils.utils.FoliaJoinHelper;
import fr.maxlego08.essentials.zutils.utils.TimerBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand Down Expand Up @@ -446,7 +447,7 @@ public void onJoin(PlayerJoinEvent event) {
player.setAllowFlight(true);
player.setFlying(true);
player.setFlySpeed(0f);
this.plugin.getScheduler().teleportAsync(player, player.getLocation().add(0, 0.1, 0));
FoliaJoinHelper.teleportAfterJoin(this.plugin, player, player.getLocation().add(0, 0.1, 0));
}
this.plugin.getEssentialsServer().sendMessage(user.getUniqueId(), Message.MESSAGE_FREEZE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fr.maxlego08.essentials.api.user.User;
import fr.maxlego08.essentials.module.ZModule;
import fr.maxlego08.essentials.storage.ConfigStorage;
import fr.maxlego08.essentials.zutils.utils.FoliaJoinHelper;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -69,7 +70,9 @@ public void loadConfiguration() {
var player = playerJoinEvent.getPlayer();
if (ConfigStorage.spawnLocation != null && ConfigStorage.spawnLocation.isValid()) {
Location spawnLoc = ConfigStorage.spawnLocation.getLocation();
if (spawnLoc != null) player.teleport(spawnLoc);
if (spawnLoc != null) {
FoliaJoinHelper.teleportAfterJoin(spawnModule.plugin, player, spawnLoc, true);
}
}
}
}, this.plugin);
Expand Down Expand Up @@ -135,14 +138,4 @@ private void onRespawn(PlayerRespawnEvent event, Player player) {
event.setRespawnLocation(ConfigStorage.spawnLocation.getLocation());
}

public void onPlayerFirstJoin(Player player) {

if (!this.isEnable) return;

if (ConfigStorage.firstSpawnLocation != null && ConfigStorage.firstSpawnLocation.isValid()) {
player.teleport(ConfigStorage.firstSpawnLocation.getLocation());
} else if (ConfigStorage.spawnLocation != null && ConfigStorage.spawnLocation.isValid()) {
player.teleport(ConfigStorage.spawnLocation.getLocation());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -116,7 +117,7 @@ private void loadKit(File file) {

String name = configuration.getString("name");
String displayName = configuration.getString("display-name", name);
String permission = configuration.getString("permission", Permission.ESSENTIALS_KIT_.asPermission(name));
String permission = configuration.getString("permission", Permission.ESSENTIALS_KIT_.asPermission(name.toLowerCase(Locale.ROOT)));
String category = configuration.getString("category", null);
String subCategory = configuration.getString("sub-category", null);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.maxlego08.essentials.module.modules.kit;

import fr.maxlego08.essentials.api.EssentialsPlugin;
import fr.maxlego08.essentials.api.commands.Permission;
import fr.maxlego08.essentials.api.kit.Kit;
import fr.maxlego08.essentials.zutils.utils.ZUtils;
import fr.maxlego08.menu.api.MenuItemStack;
Expand All @@ -12,6 +13,7 @@

import java.io.File;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class ZKit extends ZUtils implements Kit {
Expand Down Expand Up @@ -125,7 +127,16 @@ public List<Action> getActions() {

@Override
public boolean hasPermission(Permissible permissible) {
return permissible.hasPermission(this.permission);
if (this.permission == null || this.permission.isEmpty()) {
return true;
}
if (permissible.hasPermission(Permission.ESSENTIALS_KIT.asPermission())) {
return true;
}
if (permissible.hasPermission(this.permission)) {
return true;
}
return permissible.hasPermission(Permission.ESSENTIALS_KIT_.asPermission(this.name.toLowerCase(Locale.ROOT)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import fr.maxlego08.essentials.api.storage.IStorage;
import fr.maxlego08.essentials.api.storage.StorageManager;
import fr.maxlego08.essentials.api.storage.StorageType;
import fr.maxlego08.essentials.module.modules.SpawnModule;
import fr.maxlego08.essentials.storage.storages.JsonStorage;
import fr.maxlego08.essentials.storage.storages.SqlStorage;
import fr.maxlego08.essentials.zutils.utils.TimerBuilder;
Expand Down Expand Up @@ -88,10 +87,6 @@ public void onLogin(PlayerLoginEvent event) {
var user = this.iStorage.createOrLoad(playerUuid, playerName);
user.setAddress(event.getAddress().getHostAddress());

if (user.isFirstJoin()){
this.plugin.getModuleManager().getModule(SpawnModule.class).onPlayerFirstJoin(event.getPlayer());
}

var userEvent = new UserJoinEvent(user);
this.plugin.getScheduler().runNextTick(wrappedTask -> userEvent.callEvent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public void accept() {
private void teleport(TeleportationModule teleportationModule) {
Location playerLocation = fromUser.getPlayer().getLocation();
Location location = toUser.getPlayer().isFlying() ? playerLocation : teleportationModule.isTeleportSafety() ? toSafeLocation(playerLocation) : playerLocation;
if (location == null) {
location = playerLocation;
}

if (teleportationModule.isTeleportToCenter()) {
location = location.getBlock().getLocation().add(0.5, 0, 0.5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public void accept() {
private void teleport(TeleportationModule teleportationModule) {
Location playerLocation = toUser.getPlayer().getLocation();
Location location = fromUser.getPlayer().isFlying() ? playerLocation : teleportationModule.isTeleportSafety() ? toSafeLocation(playerLocation) : playerLocation;
if (location == null) {
location = playerLocation;
}

if (teleportationModule.isTeleportToCenter()) {
location = location.getBlock().getLocation().add(0.5, 0, 0.5);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/fr/maxlego08/essentials/user/ZUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ private void teleport(TeleportationModule teleportationModule, Location toLocati
if (player == null) return;

Location location = player.isFlying() ? toLocation : teleportationModule.isTeleportSafety() ? toSafeLocation(toLocation) : toLocation;
if (location == null) {
location = toLocation;
}

if (teleportationModule.isTeleportToCenter()) {
location = location.getBlock().getLocation().add(0.5, 0, 0.5);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package fr.maxlego08.essentials.zutils.utils;

import fr.maxlego08.essentials.api.EssentialsPlugin;
import fr.maxlego08.essentials.storage.ConfigStorage;
import org.bukkit.Location;
import org.bukkit.entity.Player;

/**
* Folia-safe helpers for player join flows.
* <p>
* Teleports must not run during {@link org.bukkit.event.player.PlayerJoinEvent}
* or {@link org.bukkit.event.player.PlayerLoginEvent}; the player is still being placed in the world.
*/
public final class FoliaJoinHelper {

private static final double SPAWN_TOLERANCE_SQUARED = 1.0;
private static final long JOIN_TELEPORT_DELAY_TICKS = 1L;
public static final long SAFE_LOGIN_DELAY_TICKS = 5L;

private FoliaJoinHelper() {
}

public static Location resolveFirstSpawnLocation() {
if (ConfigStorage.firstSpawnLocation != null && ConfigStorage.firstSpawnLocation.isValid()) {
return ConfigStorage.firstSpawnLocation.getLocation();
}
if (ConfigStorage.spawnLocation != null && ConfigStorage.spawnLocation.isValid()) {
return ConfigStorage.spawnLocation.getLocation();
}
return null;
}

public static void teleportFirstSpawnAfterJoin(EssentialsPlugin plugin, Player player) {
teleportAfterJoin(plugin, player, resolveFirstSpawnLocation(), true);
}

public static void teleportAfterJoin(EssentialsPlugin plugin, Player player, Location target) {
teleportAfterJoin(plugin, player, target, false);
}

public static void teleportAfterJoin(EssentialsPlugin plugin, Player player, Location target, boolean skipWhenAlreadyThere) {
if (player == null || target == null || target.getWorld() == null) {
return;
}

Location destination = target;
plugin.getScheduler().runAtLocationLater(player.getLocation(), () -> {
if (!player.isOnline()) {
return;
}

if (skipWhenAlreadyThere) {
Location current = player.getLocation();
if (current.getWorld().equals(destination.getWorld())
&& current.distanceSquared(destination) < SPAWN_TOLERANCE_SQUARED) {
return;
}
}

plugin.getScheduler().teleportAsync(player, destination);
}, JOIN_TELEPORT_DELAY_TICKS);
}
}
Loading