/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package de.steamwar.bausystem;
import de.steamwar.inventory.SWItem;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.UtilityClass;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;
import java.util.function.Function;
@UtilityClass
public class SWUtils {
@Getter
@Setter
private static Plugin bausystem;
public static void giveItemToPlayer(Player player, ItemStack itemStack) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return;
}
for (int i = 0; i < player.getInventory().getSize(); i++) {
ItemStack current = player.getInventory().getItem(i);
if (current != null && current.isSimilar(itemStack)) {
player.getInventory().setItem(i, null);
itemStack = current;
break;
}
}
ItemStack current = player.getInventory().getItemInMainHand();
player.getInventory().setItemInMainHand(itemStack);
if (current.getType() != Material.AIR) {
player.getInventory().addItem(current);
}
}
public static SWItem setCustomModelData(SWItem item, int customModelData) {
ItemMeta itemMeta = item.getItemMeta();
itemMeta.setCustomModelData(customModelData);
item.setItemMeta(itemMeta);
return item;
}
public static void sendToActionbar(Player p, String message) {
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));
}
public static void actionBar(Function message) {
Bukkit.getOnlinePlayers().forEach(player -> {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message.apply(player)));
});
}
public static void message(Function message) {
Bukkit.getOnlinePlayers().forEach(player -> {
player.sendMessage(message.apply(player));
});
}
public static NamespacedKey getNamespaceKey(String name) {
return new NamespacedKey(getBausystem(), name);
}
}