102 lines
4.2 KiB
Java
102 lines
4.2 KiB
Java
package xyz.etztech.qol.listeners;
|
|
|
|
import com.nisovin.shopkeepers.api.ShopkeepersPlugin;
|
|
import com.nisovin.shopkeepers.api.shopkeeper.ShopkeeperRegistry;
|
|
import com.nisovin.shopkeepers.api.shopkeeper.admin.regular.RegularAdminShopkeeper;
|
|
import com.nisovin.shopkeepers.api.shopkeeper.offers.TradeOffer;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.meta.SkullMeta;
|
|
import xyz.etztech.qol.QoL;
|
|
import xyz.etztech.qol.commands.CheckupCommand;
|
|
|
|
import java.util.*;
|
|
|
|
public class JoinListener implements Listener {
|
|
|
|
private QoL plugin;
|
|
|
|
public JoinListener(QoL plugin) {
|
|
this.plugin = plugin;
|
|
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
|
}
|
|
|
|
@EventHandler
|
|
public void onJoin(PlayerJoinEvent event) {
|
|
|
|
// First login
|
|
final Player player = event.getPlayer();
|
|
if (!player.hasPlayedBefore()) {
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
|
|
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
|
|
if (player != p) {
|
|
p.sendMessage(ChatColor.GREEN + player.getName() + " is new here! Be sure to welcome them!");
|
|
} else {
|
|
p.sendMessage(ChatColor.GREEN + "Welcome, " + player.getName() + "! Check out /rules and then /apply!");
|
|
}
|
|
}
|
|
}, 30); // ~1.5 seconds
|
|
}
|
|
|
|
// Queue commands
|
|
List<?> queues = plugin.getConfig().getList("queue");
|
|
|
|
if (queues != null) {
|
|
for (int i = 0; i < queues.size(); i++) {
|
|
try {
|
|
LinkedHashMap queue = (LinkedHashMap) queues.get(i);
|
|
String permission = (String) queue.get("permission");
|
|
if (player.hasPermission(permission)) {
|
|
for (String command : (List<String>) queue.get("commands")) {
|
|
plugin.runTask(command.replace("<player>", player.getName()));
|
|
}
|
|
}
|
|
} catch (Exception ignored) {
|
|
}
|
|
}
|
|
}
|
|
|
|
ShopkeepersPlugin shopkeepersAPI = plugin.getShopkeepersAPI();
|
|
if (shopkeepersAPI != null && player.hasPermission("qol.head_shop")) {
|
|
int shopKeeperID = plugin.getConfig().getInt("head_shop.id", -1);
|
|
|
|
if (shopKeeperID != -1) {
|
|
ShopkeeperRegistry shopkeeperRegistry = shopkeepersAPI.getShopkeeperRegistry();
|
|
RegularAdminShopkeeper donorHeadShop = (RegularAdminShopkeeper) shopkeeperRegistry.getShopkeeperById(shopKeeperID);
|
|
|
|
if (donorHeadShop != null) {
|
|
// Remove the head if it already exists. This is done to refresh the skin of the head.
|
|
List<? extends TradeOffer> tradeOffers = new ArrayList<>(donorHeadShop.getOffers());
|
|
tradeOffers.removeIf(tradeOffer -> {
|
|
SkullMeta itemOfferMeta = (SkullMeta)tradeOffer.getResultItem().getItemMeta();
|
|
return itemOfferMeta.getOwningPlayer().getUniqueId() == player.getUniqueId();
|
|
});
|
|
|
|
donorHeadShop.setOffers(tradeOffers);
|
|
|
|
// Add the head to the shop
|
|
int diamondPrice = plugin.getConfig().getInt("head_shop.price", 2);
|
|
ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD);
|
|
SkullMeta skullMeta = (SkullMeta) playerHead.getItemMeta();
|
|
|
|
skullMeta.setOwningPlayer(player);
|
|
skullMeta.setDisplayName(player.getDisplayName());
|
|
playerHead.setItemMeta(skullMeta);
|
|
|
|
TradeOffer donorHeadTrade = TradeOffer.create(playerHead, new ItemStack(Material.DIAMOND, diamondPrice), null);
|
|
|
|
donorHeadShop.addOffer(donorHeadTrade);
|
|
}
|
|
}
|
|
}
|
|
CheckupCommand.join(player);
|
|
}
|
|
|
|
}
|