Rework listeners

This commit is contained in:
Len 2022-07-08 11:00:31 +02:00
parent efebd4422c
commit b608668cae
3 changed files with 81 additions and 0 deletions

View File

@ -24,9 +24,17 @@ public class Config extends AbstractConfiguration {
}
public static int shopLimit = 100;
public static boolean usePermissionShopLimit = false;
private static void shopSettings() {
String path = "shop-settings.";
shopLimit = config.getInt(path + "player-shop-limit", shopLimit);
usePermissionShopLimit = config.getBoolean(path + "use-permission-based-shop-limit", usePermissionShopLimit);
}
public static String shopLimitPermission = "shop.buildlimit";
private static void permissionSettings() {
String path = "permission.";
shopLimitPermission = config.getString(path + "build-limit", shopLimitPermission);
}
}

View File

@ -1,4 +1,68 @@
package com.alttd.playershops.listener;
import com.alttd.playershops.PlayerShops;
import com.alttd.playershops.api.ShopHandler;
import com.alttd.playershops.config.Config;
import com.alttd.playershops.shop.AbstractShop;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.permissions.PermissionAttachmentInfo;
import java.util.UUID;
public class PlayerListener extends EventListener {
private final PlayerShops plugin;
public PlayerListener(PlayerShops plugin) {
this.plugin = plugin;
this.register(this.plugin);
}
@EventHandler(ignoreCancelled = true)
public void onPlayerJoin(PlayerJoinEvent event) {
if(!this.isRegistered || !Config.usePermissionShopLimit) return;
Player player = event.getPlayer();
if (!player.hasPermission(Config.shopLimitPermission)) return;
ShopHandler shopHandler = plugin.getShopHandler();
UUID uuid = player.getUniqueId();
// early return to not check this all the time, if this changes by rankup etc handle it in another event
if (shopHandler.getShopLimit(uuid) != Config.shopLimit) return;
int buildPermissionNumber = -1;
for (PermissionAttachmentInfo permInfo : player.getEffectivePermissions()) {
if (permInfo.getPermission().contains("shop.buildlimit.")) {
try {
int tempNum = Integer.parseInt(permInfo.getPermission().substring(permInfo.getPermission().lastIndexOf(".") + 1));
if (tempNum > buildPermissionNumber) {
buildPermissionNumber = tempNum;
}
} catch (Exception ignore) {}
}
}
if (buildPermissionNumber == -1)
shopHandler.addPlayerLimit(player.getUniqueId(), buildPermissionNumber);
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onSignChange(SignChangeEvent event) {
Block block = event.getBlock();
if (!(block.getState() instanceof Sign)) return;
AbstractShop shop = plugin.getShopHandler().getShop(block.getLocation());
if(shop == null) return;
if(shop.isInitialized()) event.setCancelled(true);
}
}

View File

@ -1,4 +1,13 @@
package com.alttd.playershops.listener;
import com.alttd.playershops.PlayerShops;
public class ShopListener extends EventListener {
private final PlayerShops plugin;
public ShopListener(PlayerShops plugin) {
this.plugin = plugin;
this.register(this.plugin);
}
}