Initializing repository
parent
87f7b06c29
commit
92db00c277
@ -0,0 +1 @@
|
||||
target/
|
||||
@ -0,0 +1,43 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.zivilon</groupId>
|
||||
<artifactId>ItemSalvage</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>ItemSalvage</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.bukkit</groupId>
|
||||
<artifactId>bukkit</artifactId>
|
||||
<version>1.7.10-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.flinbein</groupId>
|
||||
<artifactId>PowerNBT</artifactId>
|
||||
<version>0.8.9.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@ -0,0 +1,34 @@
|
||||
package com.zivilon.itemsalvage;
|
||||
|
||||
import com.zivilon.itemsalvage.utils.dust_handler;
|
||||
import com.zivilon.itemsalvage.listeners.enchantment_table_listener;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
|
||||
public class ItemSalvage extends JavaPlugin {
|
||||
private FileConfiguration item_config;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getServer().getPluginManager().registerEvents(new enchantment_table_listener(this), this);
|
||||
this.item_config = loadConfiguration("items.yml");
|
||||
}
|
||||
|
||||
private FileConfiguration loadConfiguration(String filename) {
|
||||
File file = new File(getDataFolder(), filename);
|
||||
if (!file.exists()) {
|
||||
saveResource(filename, false);
|
||||
}
|
||||
|
||||
return YamlConfiguration.loadConfiguration(file);
|
||||
}
|
||||
|
||||
public FileConfiguration get_item_config() {
|
||||
return item_config;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,371 @@
|
||||
package com.zivilon.itemsalvage.listeners;
|
||||
|
||||
import com.zivilon.itemsalvage.ItemSalvage;
|
||||
import com.zivilon.itemsalvage.utils.dust_handler;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
|
||||
import me.dpohvar.powernbt.PowerNBT;
|
||||
import me.dpohvar.powernbt.api.NBTCompound;
|
||||
import me.dpohvar.powernbt.api.NBTManager;
|
||||
import me.dpohvar.powernbt.api.NBTList;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class enchantment_table_listener implements Listener {
|
||||
|
||||
private final ItemSalvage plugin;
|
||||
private static final String LOG_FILE = "plugins/ItemSalvage/salvage.log";
|
||||
|
||||
public enchantment_table_listener(ItemSalvage plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public Map<String, Object> get_config_item_data(ItemStack held_item) {
|
||||
// Getting the plugin config and items section
|
||||
FileConfiguration config = plugin.get_item_config();
|
||||
ConfigurationSection items_section = config.getConfigurationSection("items");
|
||||
|
||||
// Getting NBT data from held item
|
||||
NBTManager nbtManager = me.dpohvar.powernbt.PowerNBT.getApi();
|
||||
NBTCompound itemNBT = nbtManager.read(held_item);
|
||||
if (itemNBT == null) return null;
|
||||
byte lotrRandomEnch = itemNBT.getByte("LOTRRandomEnch");
|
||||
int lotrRepairCost = itemNBT.getInt("LOTRRepairCost");
|
||||
NBTList lotrEnchNBT = itemNBT.getList("LOTREnch");
|
||||
List<String> lotrEnch = new ArrayList<>();
|
||||
if (lotrEnchNBT != null) {
|
||||
for (Object obj : lotrEnchNBT) {
|
||||
lotrEnch.add(obj.toString());
|
||||
}
|
||||
}
|
||||
NBTList lotrEnchProgressNBT = itemNBT.getList("LOTREnchProgress");
|
||||
List<Map<String, Object>> lotrEnchProgress = new ArrayList<>();
|
||||
if (lotrEnchProgressNBT != null) {
|
||||
for (Object obj : lotrEnchProgressNBT) {
|
||||
NBTCompound enchProgressCompound = (NBTCompound) obj;
|
||||
Map<String, Object> enchProgressMap = new HashMap<>();
|
||||
enchProgressMap.put("Name", enchProgressCompound.getString("Name"));
|
||||
enchProgressMap.put("Kills", enchProgressCompound.getInt("Kills"));
|
||||
enchProgressMap.put("KillsRequired", enchProgressCompound.getInt("KillsRequired"));
|
||||
lotrEnchProgress.add(enchProgressMap);
|
||||
}
|
||||
}
|
||||
|
||||
// Iterating over items in config
|
||||
for (String key : items_section.getKeys(false)) {
|
||||
ConfigurationSection item_section = items_section.getConfigurationSection(key);
|
||||
Material config_item_material = Material.matchMaterial(item_section.getString("id"));
|
||||
String config_item_name = ChatColor.translateAlternateColorCodes('&', item_section.getString("name"));
|
||||
List<String> config_item_lore = item_section.getStringList("lore").stream()
|
||||
.map(line -> ChatColor.translateAlternateColorCodes('&', line))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Map<Enchantment, Integer> config_enchantments = new HashMap<>();
|
||||
if (item_section.contains("enchantments")) {
|
||||
ConfigurationSection enchantmentSection = item_section.getConfigurationSection("enchantments");
|
||||
for (String enchantmentName : enchantmentSection.getKeys(false)) {
|
||||
Enchantment enchantment = Enchantment.getByName(enchantmentName);
|
||||
int level = enchantmentSection.getInt(enchantmentName + ".lvl");
|
||||
config_enchantments.put(enchantment, level);
|
||||
}
|
||||
}
|
||||
|
||||
ItemMeta held_item_meta = held_item.getItemMeta();
|
||||
|
||||
if (held_item.getType() == config_item_material
|
||||
&& held_item.getEnchantments().equals(config_enchantments)) {
|
||||
|
||||
// Check if the held item has a lore that matches the config
|
||||
boolean lore_matches = (held_item_meta.hasLore() && held_item_meta.getLore().equals(config_item_lore)) || (!held_item_meta.hasLore() && config_item_lore.equals(Collections.emptyList()));
|
||||
boolean name_matches = held_item_meta.hasDisplayName() && held_item_meta.getDisplayName().equals(config_item_name);
|
||||
|
||||
// If item matches, prepare and return item properties
|
||||
if (lore_matches) {
|
||||
Map<String, Object> item_data = new HashMap<>();
|
||||
item_data.put("item_id", key);
|
||||
item_data.put("original_name", held_item_meta.getDisplayName());
|
||||
item_data.put("name", config_item_name);
|
||||
item_data.put("lore", config_item_lore);
|
||||
item_data.put("enchantments", config_enchantments);
|
||||
item_data.put("salvage_value", item_section.getInt("salvage_value", 0));
|
||||
item_data.put("upgrade_cost", item_section.getInt("upgrade_cost", 0));
|
||||
item_data.put("default_name", !name_matches);
|
||||
item_data.put("LOTRRandomEnch", lotrRandomEnch);
|
||||
item_data.put("LOTRRepairCost", item_section.getInt("LOTRRepairCost", lotrRepairCost)); // Override if found in config
|
||||
item_data.put("LOTREnch", lotrEnch);
|
||||
item_data.put("LOTREnchProgress", lotrEnchProgress);
|
||||
item_data.put("upgrades_to", item_section.getString("upgrades_to", null));
|
||||
item_data.put("upgrade_mode", item_section.getString("upgrade_mode", null));
|
||||
|
||||
return item_data;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInteract(PlayerInteractEvent event) {
|
||||
|
||||
if (event.getClickedBlock() == null || event.getClickedBlock().getType() != Material.ENCHANTMENT_TABLE) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = event.getPlayer();
|
||||
ItemStack held_item = player.getInventory().getItemInHand();
|
||||
|
||||
if (player.isSneaking()) {
|
||||
salvage_item(player, held_item, event);
|
||||
return;
|
||||
}
|
||||
standard_interact(player, held_item, event); // Line 135
|
||||
}
|
||||
|
||||
private void log_salvage_event(String username, String item_id) {
|
||||
String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||
String log_entry = timestamp + " - " + username + " - " + item_id;
|
||||
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(LOG_FILE, true))) {
|
||||
writer.write(log_entry);
|
||||
writer.newLine();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); // Handle the exception as needed
|
||||
}
|
||||
}
|
||||
|
||||
private void salvage_item(Player player, ItemStack held_item, PlayerInteractEvent event) {
|
||||
if (held_item.getType().toString().equals("LOTR_ITEMITHILDIN") && !held_item.hasItemMeta()) {
|
||||
event.setCancelled(true);
|
||||
int randomNum = ThreadLocalRandom.current().nextInt(0, 4);
|
||||
// Remove held item first
|
||||
held_item.setAmount(held_item.getAmount() - 1);
|
||||
player.getInventory().setItemInHand(held_item);
|
||||
if (randomNum == 1) {
|
||||
dust_handler.give_dust_to_player(player, 1);
|
||||
player.updateInventory();
|
||||
log_salvage_event(player.getName(), "ithildin");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (held_item == null) return;
|
||||
Map<String, Object> item_data = null;
|
||||
if (held_item.hasItemMeta()) item_data = get_config_item_data(held_item); // Line 196
|
||||
|
||||
if (item_data != null) {
|
||||
event.setCancelled(true);
|
||||
|
||||
int salvage_value = (Integer) item_data.get("salvage_value");
|
||||
|
||||
if (salvage_value != 0) {
|
||||
int space = dust_handler.calculate_dust_space(player);
|
||||
if (salvage_value > (space + 64)) {
|
||||
player.sendMessage(ChatColor.RED + "You do not have inventory space to salvage this item.");
|
||||
} else {
|
||||
// Remove held item first
|
||||
held_item.setAmount(held_item.getAmount() - 1);
|
||||
player.getInventory().setItemInHand(held_item);
|
||||
|
||||
// Now add the dust
|
||||
dust_handler.give_dust_to_player(player, salvage_value);
|
||||
|
||||
// Force an inventory update
|
||||
player.updateInventory();
|
||||
|
||||
String original_name = (String) item_data.get("original_name");
|
||||
player.sendMessage(ChatColor.GREEN + "You salvaged your " + original_name + ChatColor.GREEN + " and received " + salvage_value + ChatColor.AQUA + " Arcane Dust" + ChatColor.GREEN + ".");
|
||||
log_salvage_event(player.getName(), (String) item_data.get("item_id"));
|
||||
|
||||
}
|
||||
} else {
|
||||
player.sendMessage(ChatColor.RED + "This item cannot be salvaged.");
|
||||
}
|
||||
} else {
|
||||
player.sendMessage(ChatColor.RED + "This item cannot be salvaged");
|
||||
}
|
||||
}
|
||||
|
||||
private void standard_interact(Player player, ItemStack held_item, PlayerInteractEvent event) {
|
||||
if (held_item.getType().toString().equals("LOTR_ITEMITHILDIN") && !held_item.hasItemMeta()) {
|
||||
event.setCancelled(true);
|
||||
player.sendMessage(ChatColor.GREEN + "This item can be salvaged for 1/4 Arcane Dust.");
|
||||
return;
|
||||
}
|
||||
if (held_item == null) return;
|
||||
Map<String, Object> item_data = null;
|
||||
if (held_item.hasItemMeta()) item_data = get_config_item_data(held_item);
|
||||
if (item_data != null) {
|
||||
event.setCancelled(true);
|
||||
|
||||
|
||||
int salvage_value = (Integer) item_data.get("salvage_value");
|
||||
int upgrade_cost = (Integer) item_data.get("upgrade_cost");
|
||||
String upgrade_mode = (String) item_data.get("upgrade_mode");
|
||||
|
||||
if (salvage_value == 0 && upgrade_cost == 0) {
|
||||
player.sendMessage(ChatColor.AQUA + "Enchantment Table");
|
||||
player.sendMessage(ChatColor.YELLOW + "To upgrade items, right-click with the item in hand.");
|
||||
player.sendMessage(ChatColor.YELLOW + "To salvage items, sneak and right-click with the item in hand.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (upgrade_cost != 0 && dust_handler.count_dust_in_inventory(player) >= upgrade_cost) {
|
||||
dust_handler.remove_dust_from_inventory(player, upgrade_cost);
|
||||
upgrade_item(player, held_item, item_data);
|
||||
String original_name = (String) item_data.get("original_name");
|
||||
player.sendMessage(ChatColor.GREEN + "Your " + original_name + ChatColor.GREEN + " has been upgraded for " + upgrade_cost + ChatColor.AQUA + " Arcane Dust" + ChatColor.GREEN + ".");
|
||||
return;
|
||||
}
|
||||
|
||||
if (upgrade_cost != 0) {
|
||||
player.sendMessage(ChatColor.GREEN + "This item can be upgraded for " + upgrade_cost + " Arcane Dust.");
|
||||
}
|
||||
|
||||
if (salvage_value != 0) {
|
||||
player.sendMessage(ChatColor.GREEN + "This item can be salvaged for " + salvage_value + " Arcane Dust.");
|
||||
}
|
||||
if (upgrade_cost != 0) {
|
||||
player.sendMessage(ChatColor.GREEN + "Upgrade type: " + ChatColor.GRAY + upgrade_mode);
|
||||
}
|
||||
|
||||
} else {
|
||||
player.sendMessage(ChatColor.AQUA + "Enchantment Table");
|
||||
player.sendMessage(ChatColor.YELLOW + "To upgrade items, right-click with the item in hand.");
|
||||
player.sendMessage(ChatColor.YELLOW + "To salvage items, sneak and right-click with the item in hand.");
|
||||
}
|
||||
}
|
||||
|
||||
private void upgrade_item(Player player, ItemStack held_item, Map<String, Object> item_data) {
|
||||
// Fetch the original item data
|
||||
Map<String, Object> original_item_data = item_data;
|
||||
|
||||
// Get the upgraded item name
|
||||
String upgradeTo = (String) original_item_data.get("upgrades_to");
|
||||
|
||||
// Fetch the upgraded item data directly from the config
|
||||
FileConfiguration config = plugin.get_item_config();
|
||||
ConfigurationSection item_section = config.getConfigurationSection("items." + upgradeTo);
|
||||
|
||||
// Get the id, name, and lore from the upgraded item data
|
||||
String id = item_section.getString("id");
|
||||
String name = item_section.getString("name");
|
||||
String original_name = (String) original_item_data.get("original_name");
|
||||
List<String> lore = item_section.getStringList("lore");
|
||||
|
||||
// Create a new item stack for the upgraded item
|
||||
ItemStack item = new ItemStack(Material.valueOf(id), held_item.getAmount());
|
||||
|
||||
boolean default_name = (boolean) original_item_data.get("default_name");
|
||||
|
||||
// Add meta to the upgraded item
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (!default_name) {
|
||||
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
|
||||
} else {
|
||||
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', original_name));
|
||||
}
|
||||
lore = lore.stream().map(s -> ChatColor.translateAlternateColorCodes('&', s)).collect(Collectors.toList());
|
||||
meta.setLore(lore);
|
||||
item.setItemMeta(meta);
|
||||
|
||||
// Fetch the enchantments directly from the upgraded item data and add to the item
|
||||
ConfigurationSection enchantment_section = item_section.getConfigurationSection("enchantments");
|
||||
for (String key : enchantment_section.getKeys(false)) {
|
||||
Enchantment ench = Enchantment.getByName(key);
|
||||
int level = enchantment_section.getConfigurationSection(key).getInt("lvl");
|
||||
item.addUnsafeEnchantment(ench, level);
|
||||
}
|
||||
|
||||
// Remove the original item by setting its amount to 0 and set the upgraded one as the currently held item
|
||||
player.getInventory().setItemInHand(item);
|
||||
player.updateInventory(); // Ensure the inventory update is immediate
|
||||
|
||||
|
||||
// Add NBT tags to the upgraded item
|
||||
item = player.getInventory().getItemInHand();
|
||||
NBTManager nbtManager = PowerNBT.getApi();
|
||||
NBTCompound item_nbt = nbtManager.read(item);
|
||||
if (item_nbt == null) {
|
||||
item_nbt = new NBTCompound();
|
||||
}
|
||||
|
||||
byte lotrRandomEnch = (byte) original_item_data.get("LOTRRandomEnch");
|
||||
List<String> lotrEnch = (List<String>) original_item_data.get("LOTREnch");
|
||||
// Add this after getting the 'lotrEnch' list
|
||||
if ("modifier".equals(original_item_data.get("upgrade_mode"))) {
|
||||
for (int i = 0; i < lotrEnch.size(); i++) {
|
||||
String ench = lotrEnch.get(i);
|
||||
String upgradedEnch = ench; // Default to original enchantment
|
||||
if ("meleeSpeed1".equals(ench)) {
|
||||
upgradedEnch = "meleeSpeed2";
|
||||
} else if ("meleeReach1".equals(ench)) {
|
||||
upgradedEnch = "meleeReach2";
|
||||
} else if ("strong1".equals(ench)) {
|
||||
upgradedEnch = "strong2";
|
||||
} else if ("strong2".equals(ench)) {
|
||||
upgradedEnch = "strong3";
|
||||
} else if ("strong3".equals(ench)) {
|
||||
upgradedEnch = "strong4";
|
||||
} else if ("knockback1".equals(ench)) {
|
||||
upgradedEnch = "knockback2";
|
||||
} else if ("rangedKnockback1".equals(ench)) {
|
||||
upgradedEnch = "rangedKnockback2";
|
||||
} else if ("rangedStrong1".equals(ench)) {
|
||||
upgradedEnch = "rangedStrong2";
|
||||
} else if ("rangedStrong2".equals(ench)) {
|
||||
upgradedEnch = "rangedStrong3";
|
||||
} else if ("rangedStrong3".equals(ench)) {
|
||||
upgradedEnch = "rangedStrong4";
|
||||
}
|
||||
lotrEnch.set(i, upgradedEnch);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int lotrRepairCost = (int) original_item_data.get("LOTRRepairCost");
|
||||
List<Map<String, Object>> lotrEnchProgress = (List<Map<String, Object>>) original_item_data.get("LOTREnchProgress");
|
||||
|
||||
item_nbt.put("LOTRRepairCost", Integer.valueOf(lotrRepairCost));
|
||||
|
||||
// Convert lotrEnchProgress to NBTList of NBTCompound
|
||||
NBTList nbtListEnchProgress = new NBTList();
|
||||
for (Map<String, Object> enchProgress : lotrEnchProgress) {
|
||||
NBTCompound compound = new NBTCompound();
|
||||
compound.put("Kills", Integer.valueOf((int) enchProgress.get("Kills")));
|
||||
compound.put("KillsRequired", Integer.valueOf((int) enchProgress.get("KillsRequired")));
|
||||
compound.put("Name", String.valueOf(enchProgress.get("Name")));
|
||||
nbtListEnchProgress.add(compound);
|
||||
}
|
||||
|
||||
// Add NBT tags to the upgraded item
|
||||
item_nbt.put("LOTRRandomEnch", Byte.valueOf((byte) lotrRandomEnch));
|
||||
item_nbt.put("LOTREnch", new NBTList(lotrEnch));
|
||||
|
||||
item_nbt.put("LOTREnchProgress", nbtListEnchProgress);
|
||||
|
||||
nbtManager.write(item, item_nbt);
|
||||
player.updateInventory();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,203 @@
|
||||
package com.zivilon.itemsalvage.utils;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class dust_handler {
|
||||
public static void remove_dust_from_inventory(Player player, int amount) {
|
||||
Plugin plugin = Bukkit.getPluginManager().getPlugin("ItemSalvage");
|
||||
|
||||
// Create the plugin directory if it doesn't exist
|
||||
if (!plugin.getDataFolder().exists()) {
|
||||
plugin.getDataFolder().mkdirs();
|
||||
}
|
||||
|
||||
File config_file = new File(plugin.getDataFolder(), "salvage.yml");
|
||||
|
||||
if (!config_file.exists()) {
|
||||
try (InputStream in = plugin.getResource("salvage.yml")) {
|
||||
Files.copy(in, config_file.toPath());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(config_file);
|
||||
int remaining = amount;
|
||||
|
||||
String dust_item_name = replace_color_codes(config.getString("salvage.name"));
|
||||
|
||||
Material dust_material = Material.matchMaterial(config.getString("salvage.item"));
|
||||
if (dust_material == null) {
|
||||
Bukkit.getLogger().warning("Could not find material with name: " + config.getString("salvage.item"));
|
||||
return;
|
||||
}
|
||||
|
||||
ItemStack[] inventory = player.getInventory().getContents();
|
||||
for (int i = 0; i < inventory.length; i++) {
|
||||
ItemStack item = inventory[i];
|
||||
if (item != null && item.hasItemMeta()) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (item.getType() == dust_material
|
||||
&& meta.hasDisplayName() && meta.getDisplayName().equals(dust_item_name)
|
||||
&& meta.hasLore()) {
|
||||
// Load the list of lore lines from the config
|
||||
List<String> config_lore = config.getStringList("salvage.lore").stream()
|
||||
.map(dust_handler::replace_color_codes)
|
||||
.collect(Collectors.toList());
|
||||
// Check if all lore lines are present on the item
|
||||
boolean lore_matches = meta.getLore().equals(config_lore);
|
||||
|
||||
if (lore_matches) {
|
||||
int total_dust = item.getAmount();
|
||||
if (total_dust > remaining) {
|
||||
item.setAmount(total_dust - remaining);
|
||||
remaining = 0;
|
||||
break;
|
||||
} else {
|
||||
remaining -= total_dust;
|
||||
inventory[i] = null;
|
||||
}
|
||||
if (remaining == 0) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
player.getInventory().setContents(inventory);
|
||||
}
|
||||
|
||||
public static String replace_color_codes(String text) {
|
||||
return text.replaceAll("&([0-9a-fA-Fk-oK-OrR])", "§$1");
|
||||
}
|
||||
|
||||
public static int count_dust_in_inventory(Player player) {
|
||||
File config_file = new File(Bukkit.getPluginManager().getPlugin("ItemSalvage").getDataFolder(), "salvage.yml");
|
||||
if (!config_file.exists()) {
|
||||
Bukkit.getLogger().warning("Could not find salvage.yml file");
|
||||
return 0;
|
||||
}
|
||||
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(config_file);
|
||||
|
||||
String dust_item_name = replace_color_codes(config.getString("salvage.name"));
|
||||
|
||||
Material dust_material = Material.matchMaterial(config.getString("salvage.item"));
|
||||
if (dust_material == null) {
|
||||
Bukkit.getLogger().warning("Could not find material with name: " + config.getString("salvage.item"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
List<String> config_lore = config.getStringList("salvage.lore").stream()
|
||||
.map(dust_handler::replace_color_codes)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
int total_dust = 0;
|
||||
ItemStack[] inventory = player.getInventory().getContents();
|
||||
for (int i = 0; i < inventory.length; i++) {
|
||||
ItemStack item = inventory[i];
|
||||
if (item != null && item.hasItemMeta()) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (item.getType() == dust_material) {
|
||||
if (meta.hasDisplayName() && meta.getDisplayName().equals(dust_item_name)
|
||||
&& meta.hasLore() && meta.getLore().equals(config_lore)) {
|
||||
total_dust += item.getAmount();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return total_dust;
|
||||
}
|
||||
|
||||
public static void give_dust_to_player(Player player, int amount) {
|
||||
File config_file = new File(Bukkit.getPluginManager().getPlugin("ItemSalvage").getDataFolder(), "salvage.yml");
|
||||
if (!config_file.exists()) {
|
||||
Bukkit.getLogger().warning("Could not find salvage.yml file");
|
||||
return;
|
||||
}
|
||||
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(config_file);
|
||||
|
||||
String dust_item_name = replace_color_codes(config.getString("salvage.name"));
|
||||
|
||||
Material dust_material = Material.matchMaterial(config.getString("salvage.item"));
|
||||
if (dust_material == null) {
|
||||
Bukkit.getLogger().warning("Could not find material with name: " + config.getString("salvage.item"));
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> config_lore = config.getStringList("salvage.lore").stream()
|
||||
.map(dust_handler::replace_color_codes)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// Give items one by one because otherwise the hidden initial stack size value prevents mixing these with other identical item stacks.
|
||||
for (int i = 0; i < amount; i++) {
|
||||
ItemStack dust = new ItemStack(dust_material, 1);
|
||||
ItemMeta dust_meta = dust.getItemMeta();
|
||||
|
||||
dust_meta.setDisplayName(dust_item_name);
|
||||
dust_meta.setLore(config_lore);
|
||||
dust.setItemMeta(dust_meta);
|
||||
|
||||
player.getInventory().addItem(dust);
|
||||
}
|
||||
}
|
||||
|
||||
public static int calculate_dust_space(Player player) {
|
||||
File config_file = new File(Bukkit.getPluginManager().getPlugin("ItemSalvage").getDataFolder(), "salvage.yml");
|
||||
if (!config_file.exists()) {
|
||||
Bukkit.getLogger().warning("Could not find salvage.yml file");
|
||||
return 0;
|
||||
}
|
||||
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(config_file);
|
||||
|
||||
String dust_item_name = replace_color_codes(config.getString("salvage.name"));
|
||||
|
||||
Material dust_material = Material.matchMaterial(config.getString("salvage.item"));
|
||||
if (dust_material == null) {
|
||||
Bukkit.getLogger().warning("Could not find material with name: " + config.getString("salvage.item"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
List<String> config_lore = config.getStringList("salvage.lore").stream()
|
||||
.map(dust_handler::replace_color_codes)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
int total_space = 0;
|
||||
ItemStack[] inventory = player.getInventory().getContents();
|
||||
|
||||
for (int i = 0; i < 36; i++) { // consider only the main inventory, excluding armor and off-hand slots
|
||||
ItemStack item = inventory[i];
|
||||
if (item == null) {
|
||||
total_space += 64; // Empty slot, can hold a full stack
|
||||
} else if (item.hasItemMeta()) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (item.getType() == dust_material
|
||||
&& meta.hasDisplayName() && meta.getDisplayName().equals(dust_item_name)
|
||||
&& meta.hasLore() && meta.getLore().equals(config_lore)) {
|
||||
total_space += (64 - item.getAmount()); // Partially filled slot
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return total_space;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,4 @@
|
||||
main: com.zivilon.itemsalvage.ItemSalvage
|
||||
name: ItemSalvage
|
||||
author: Shinare
|
||||
version: 1.0
|
||||
@ -0,0 +1,6 @@
|
||||
salvage:
|
||||
item: LOTR_ITEMITHILDIN
|
||||
name: &bArcane Dust
|
||||
lore:
|
||||
- &5This is lore text
|
||||
- &5But this time we have a second line
|
||||
Loading…
Reference in New Issue