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.event.Listener;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.Objects; import java.util.*;
public class BuyItemTags implements Listener { public class BuyItemTags implements Listener {
CommandPanels plugin; CommandPanels plugin;
@ -25,13 +25,13 @@ public class BuyItemTags implements Listener {
public void commandTag(CommandTagEvent e){ public void commandTag(CommandTagEvent e){
if(e.name.equalsIgnoreCase("buy=")){ if(e.name.equalsIgnoreCase("buy=")){
e.commandTagUsed(); 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 { try {
if (plugin.econ != null) { if (plugin.econ != null) {
if (plugin.econ.getBalance(e.p) >= Double.parseDouble(e.args[0])) { if (plugin.econ.getBalance(e.p) >= Double.parseDouble(e.args[0])) {
plugin.econ.withdrawPlayer(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])); 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 { } else {
plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.currency.failure"))); 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=")) { if(e.name.equalsIgnoreCase("tokenbuy=")) {
e.commandTagUsed(); 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 { try {
if (plugin.getServer().getPluginManager().isPluginEnabled("TokenManager")) { if (plugin.getServer().getPluginManager().isPluginEnabled("TokenManager")) {
final TokenManager api = (TokenManager) Bukkit.getServer().getPluginManager().getPlugin("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.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])); 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 { } else {
plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.tokens.failure"))); 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") @SuppressWarnings("deprecation")
private void giveItem(Player p, String[] args){ private void giveItem(Player p, CommandTagEvent e){
String[] args = e.args;
//legacy ID //legacy ID
byte id = 0; byte id = 0;
if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_15)) { if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_15)) {
for (String argsTemp : args) { for (String arg : args) {
if (argsTemp.startsWith("id:")) { if (arg.startsWith("id:")) {
id = Byte.parseByte(argsTemp.replace("id:", "")); id = Byte.parseByte(arg.replace("id:", ""));
break; 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.CommandPanels;
import me.rockyhawk.commandpanels.commandtags.CommandTagEvent; import me.rockyhawk.commandpanels.commandtags.CommandTagEvent;
import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition; import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition;
import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -22,14 +23,27 @@ public class ItemTags implements Listener {
public void commandTag(CommandTagEvent e){ public void commandTag(CommandTagEvent e){
if(e.name.equalsIgnoreCase("give-item=")){ if(e.name.equalsIgnoreCase("give-item=")){
e.commandTagUsed(); 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(e.args.length == 2){ if (Material.matchMaterial(e.args[0]) == null) {
try{ itm = plugin.itemCreate.makeCustomItemFromConfig(null,e.pos,e.panel.getConfig().getConfigurationSection("custom-item." + e.args[0]), e.p, true, true, false);
itm.setAmount(Integer.parseInt(e.args[1])); if(e.args.length == 2){
} catch (Exception err){ try{
plugin.debug(err,e.p); itm.setAmount(Integer.parseInt(e.args[1]));
} catch (Exception err){
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); plugin.inventorySaver.addItem(e.p,itm);
return; return;
} }

View File

@ -143,7 +143,7 @@ public class InventorySaver implements Listener {
p.getInventory().addItem(item); p.getInventory().addItem(item);
return; return;
} }
}else { } else {
List<ItemStack> cont = new ArrayList<>(Arrays.asList(getNormalInventory(p))); List<ItemStack> cont = new ArrayList<>(Arrays.asList(getNormalInventory(p)));
boolean found = false; boolean found = false;
for (int i = 0; 36 > i; i++){ for (int i = 0; 36 > i; i++){