Refresh player head on login

+ Cleaned up imports
pull/18/head
Joey Hines 2021-08-06 22:36:33 -06:00
parent 1b5c8625de
commit e66969f039
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
1 changed files with 17 additions and 26 deletions

View File

@ -1,26 +1,18 @@
package xyz.etztech.qol.listeners;
import com.nisovin.shopkeepers.api.ShopkeepersAPI;
import com.nisovin.shopkeepers.api.ShopkeepersPlugin;
import com.nisovin.shopkeepers.api.shopkeeper.Shopkeeper;
import com.nisovin.shopkeepers.api.shopkeeper.ShopkeeperRegistry;
import com.nisovin.shopkeepers.api.shopkeeper.admin.regular.RegularAdminShopkeeper;
import com.nisovin.shopkeepers.api.shopkeeper.offers.PriceOffer;
import com.nisovin.shopkeepers.api.shopkeeper.offers.TradeOffer;
import com.nisovin.shopkeepers.api.shopobjects.ShopObject;
import com.nisovin.shopkeepers.api.storage.ShopkeeperStorage;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.dynmap.markers.Marker;
import xyz.etztech.qol.QoL;
import xyz.etztech.qol.commands.CheckupCommand;
@ -79,28 +71,27 @@ public class JoinListener implements Listener {
RegularAdminShopkeeper donorHeadShop = (RegularAdminShopkeeper) shopkeeperRegistry.getShopkeeperById(shopKeeperID);
if (donorHeadShop != null) {
boolean donorFound = false;
for (TradeOffer tradeOffer: donorHeadShop.getOffers()) {
// 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();
if (itemOfferMeta.getOwningPlayer().getUniqueId() == player.getUniqueId()) {
donorFound = true;
break;
}
}
return itemOfferMeta.getOwningPlayer().getUniqueId() == player.getUniqueId();
});
if (!donorFound) {
int diamondPrice = plugin.getConfig().getInt("head_shop.price", 2);
ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD);
SkullMeta skullMeta = (SkullMeta) playerHead.getItemMeta();
donorHeadShop.setOffers(tradeOffers);
skullMeta.setOwningPlayer(player);
skullMeta.setDisplayName(player.getDisplayName());
playerHead.setItemMeta(skullMeta);
// 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();
TradeOffer donorHeadTrade = TradeOffer.create(playerHead, new ItemStack(Material.DIAMOND, diamondPrice), null);
donorHeadShop.addOffer(donorHeadTrade);
}
skullMeta.setOwningPlayer(player);
skullMeta.setDisplayName(player.getDisplayName());
playerHead.setItemMeta(skullMeta);
TradeOffer donorHeadTrade = TradeOffer.create(playerHead, new ItemStack(Material.DIAMOND, diamondPrice), null);
donorHeadShop.addOffer(donorHeadTrade);
}
}