Moved head shop logic to its own listener

pull/18/head
Joey Hines 2021-08-09 19:19:28 -06:00
parent a673eadd2e
commit 006a3a2c0d
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
3 changed files with 69 additions and 40 deletions

View File

@ -102,6 +102,9 @@ public class QoL extends JavaPlugin {
// Shopkeepers Hook
if (Bukkit.getPluginManager().isPluginEnabled("Shopkeepers")) {
shopkeepersAPI = ShopkeepersPlugin.getInstance();
new HeadShopListener(this);
log("Hooked in Shopkeepers for the head shop");
}

View File

@ -0,0 +1,66 @@
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.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 java.util.ArrayList;
import java.util.List;
public class HeadShopListener implements Listener{
private QoL plugin;
public HeadShopListener(QoL plugin) {
this.plugin = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer();
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);
}
}
}
}
}

View File

@ -1,18 +1,11 @@
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;
@ -62,39 +55,6 @@ public class JoinListener implements Listener {
}
}
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);
}