88 lines
3.2 KiB
Java
Executable File
88 lines
3.2 KiB
Java
Executable File
package ru.themixray.itemeconomy;
|
|
|
|
import com.google.common.io.ByteArrayDataInput;
|
|
import net.milkbowl.vault.economy.Economy;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.plugin.ServicePriority;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
|
|
public class Main extends JavaPlugin {
|
|
public static HashMap<String, List<ItemStack>> addItems = new HashMap<>();
|
|
|
|
@Override
|
|
public void onEnable() {
|
|
Config.loadConfig(new UnrealConfig(this, getDataFolder(), "config.yml"));
|
|
Bukkit.getServicesManager().register(Economy.class, new VaultLayer(), this, ServicePriority.High);
|
|
new BukkitRunnable() {
|
|
public void run() {
|
|
new HashMap<>(addItems).forEach((k, v) -> {
|
|
Player player = Bukkit.getPlayerExact(k);
|
|
|
|
if (player != null) {
|
|
for (ItemStack item : v) {
|
|
addItems(player, item);
|
|
}
|
|
|
|
addItems.remove(k);
|
|
}
|
|
});
|
|
}
|
|
}.runTaskTimer(this, 20, 20);
|
|
}
|
|
|
|
@Override
|
|
public void onDisable() {
|
|
super.onDisable();
|
|
}
|
|
|
|
public static List<ItemStack> getInventory(Player player) {
|
|
return Arrays.stream(player.getInventory().getContents()).map(o -> o == null ? new ItemStack(Material.AIR) : o).toList();
|
|
}
|
|
|
|
public static boolean removeItems(String playerName, Material type, int amount) {
|
|
System.out.println("try remove item "+type+" "+amount+" from "+playerName);
|
|
ItemStack item = new ItemStack(type, amount);
|
|
|
|
Player player = Bukkit.getPlayerExact(playerName);
|
|
|
|
if (player == null) {
|
|
return false;
|
|
} else {
|
|
return removeItems(player, item);
|
|
}
|
|
}
|
|
|
|
public static void addItems(String playerName, Material type, int amount) {
|
|
System.out.println("try add item "+type+" "+amount+" to "+playerName);
|
|
ItemStack item = new ItemStack(type, amount);
|
|
|
|
Player player = Bukkit.getPlayerExact(playerName);
|
|
|
|
if (player == null) {
|
|
List<ItemStack> items = addItems.getOrDefault(playerName, new ArrayList<>());
|
|
items.add(item);
|
|
addItems.put(playerName, items);
|
|
} else {
|
|
addItems(player, item);
|
|
}
|
|
}
|
|
|
|
private static boolean removeItems(Player player, ItemStack item) {
|
|
System.out.println("remove item "+item.getType()+" "+item.getAmount()+" from "+player.getName());
|
|
if (player.getInventory().all(item.getType()).values().stream().mapToInt(ItemStack::getAmount).sum() < item.getAmount()) return false;
|
|
return player.getInventory().removeItem(item).isEmpty();
|
|
}
|
|
|
|
private static void addItems(Player player, ItemStack item) {
|
|
System.out.println("add item "+item.getType()+" "+item.getAmount()+" from "+player.getName());
|
|
HashMap<Integer, ItemStack> nope = player.getInventory().addItem(item);
|
|
for (ItemStack v:nope.values()) player.getWorld().dropItemNaturally(player.getLocation(), v);
|
|
}
|
|
} |