Buy command tag rework. Give-item not supports normal mats.

This commit is contained in:
TinyTank800 2024-04-11 12:41:26 -07:00
parent ae207f0dc8
commit 3072414868
3 changed files with 41 additions and 17 deletions

View File

@ -13,7 +13,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import java.util.Objects;
import java.util.*;
public class BuyItemTags implements Listener {
CommandPanels plugin;
@ -25,13 +25,13 @@ public class BuyItemTags implements Listener {
public void commandTag(CommandTagEvent e){
if(e.name.equalsIgnoreCase("buy=")){
e.commandTagUsed();
//if player uses buy= it will be eg. buy= <price> <item> <amount of item> <ID>
//if player uses buy= it will be eg. buy= <price> <item> <amount of item> [id:#]
try {
if (plugin.econ != null) {
if (plugin.econ.getBalance(e.p) >= Double.parseDouble(e.args[0])) {
plugin.econ.withdrawPlayer(e.p, Double.parseDouble(e.args[0]));
plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.currency.success")).replaceAll("%cp-args%", e.args[0]));
giveItem(e.p, e.args);
giveItem(e.p, e);
} else {
plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.currency.failure")));
@ -47,7 +47,7 @@ public class BuyItemTags implements Listener {
}
if(e.name.equalsIgnoreCase("tokenbuy=")) {
e.commandTagUsed();
//if player uses tokenbuy= it will be eg. tokenbuy= <price> <item> <amount of item> <ID>
//if player uses tokenbuy= it will be eg. tokenbuy= <price> <item> <amount of item> [id:#]
try {
if (plugin.getServer().getPluginManager().isPluginEnabled("TokenManager")) {
final TokenManager api = (TokenManager) Bukkit.getServer().getPluginManager().getPlugin("TokenManager");
@ -58,7 +58,7 @@ public class BuyItemTags implements Listener {
plugin.tex.sendMessage(e.p, Objects.requireNonNull(plugin.config.getString("purchase.tokens.success")).replaceAll("%cp-args%", e.args[0]));
plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.tokens.success")).replaceAll("%cp-args%", e.args[0]));
giveItem(e.p,e.args);
giveItem(e.p, e);
} else {
plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.tokens.failure")));
@ -74,17 +74,27 @@ public class BuyItemTags implements Listener {
}
@SuppressWarnings("deprecation")
private void giveItem(Player p, String[] args){
private void giveItem(Player p, CommandTagEvent e){
String[] args = e.args;
//legacy ID
byte id = 0;
if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_15)) {
for (String argsTemp : args) {
if (argsTemp.startsWith("id:")) {
id = Byte.parseByte(argsTemp.replace("id:", ""));
for (String arg : args) {
if (arg.startsWith("id:")) {
id = Byte.parseByte(arg.replace("id:", ""));
break;
}
}
}
plugin.inventorySaver.addItem(p,new ItemStack(Objects.requireNonNull(Material.matchMaterial(args[1])), Integer.parseInt(args[2]),id));
ItemStack buyItem;
if (Material.matchMaterial(args[1]) == null) {
buyItem = plugin.itemCreate.makeCustomItemFromConfig(e.panel, PanelPosition.Top, e.panel.getConfig().getConfigurationSection("custom-item." + args[1]), e.p, true, true, false);
buyItem.setAmount(Integer.parseInt(args[2]));
} else {
buyItem = new ItemStack(Objects.requireNonNull(Material.matchMaterial(args[1])), Integer.parseInt(args[2]), id);
}
plugin.inventorySaver.addItem(p,buyItem);
}
}

View File

@ -3,6 +3,7 @@ package me.rockyhawk.commandpanels.commandtags.tags.standard;
import me.rockyhawk.commandpanels.CommandPanels;
import me.rockyhawk.commandpanels.commandtags.CommandTagEvent;
import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler;
@ -22,7 +23,9 @@ public class ItemTags implements Listener {
public void commandTag(CommandTagEvent e){
if(e.name.equalsIgnoreCase("give-item=")){
e.commandTagUsed();
ItemStack itm = plugin.itemCreate.makeCustomItemFromConfig(null,e.pos,e.panel.getConfig().getConfigurationSection("custom-item." + e.args[0]), e.p, true, true, false);
ItemStack itm;
if (Material.matchMaterial(e.args[0]) == null) {
itm = plugin.itemCreate.makeCustomItemFromConfig(null,e.pos,e.panel.getConfig().getConfigurationSection("custom-item." + e.args[0]), e.p, true, true, false);
if(e.args.length == 2){
try{
itm.setAmount(Integer.parseInt(e.args[1]));
@ -30,6 +33,17 @@ public class ItemTags implements Listener {
plugin.debug(err,e.p);
}
}
} else {
itm = new ItemStack(Objects.requireNonNull(Material.matchMaterial(e.args[0])));
if(e.args.length == 2){
try{
itm.setAmount(Integer.parseInt(e.args[1]));
} catch (Exception err){
plugin.debug(err,e.p);
}
}
}
plugin.inventorySaver.addItem(e.p,itm);
return;
}