Improved implementation of deposit menu updates

This commit is contained in:
Indyuce 2022-07-07 15:05:37 +02:00
parent 23f6854f79
commit 2300a7fc13

View File

@ -1,6 +1,10 @@
package net.Indyuce.mmocore.gui.eco; package net.Indyuce.mmocore.gui.eco;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.api.util.SmartGive;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.gui.api.PluginInventory;
import net.Indyuce.mmocore.util.item.SimpleItemBuilder; import net.Indyuce.mmocore.util.item.SimpleItemBuilder;
import net.milkbowl.vault.economy.EconomyResponse; import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -13,108 +17,107 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.gui.api.PluginInventory;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.api.util.SmartGive;
public class DepositMenu extends PluginInventory { public class DepositMenu extends PluginInventory {
private ItemStack depositItem; private ItemStack depositItem;
private int deposit; private int deposit;
public DepositMenu(Player player) { /**
super(player); * Every time an item is clicked in the inventory, an inventory
} * update is scheduled. If nothing happens for the next 10 ticks
* then the update is processed. If another item is clicked within
* this delay the task is cancelled and scheduled for later
*/
private BukkitRunnable updateRunnable;
@Override public DepositMenu(Player player) {
public Inventory getInventory() { super(player);
Inventory inv = Bukkit.createInventory(this, 27, "Deposit"); }
inv.setItem(26, depositItem = new SimpleItemBuilder("DEPOSIT_ITEM").addPlaceholders("worth", "0").build()); @Override
public Inventory getInventory() {
Inventory inv = Bukkit.createInventory(this, 27, "Deposit");
updateDeposit(inv);
return inv;
}
new BukkitRunnable() { @Override
public void whenClicked(InventoryClickEvent event) {
if (event.getCurrentItem() == null || event.getCurrentItem().getType() == Material.AIR)
return;
@Override if (event.getCurrentItem().isSimilar(depositItem)) {
public void run() { event.setCancelled(true);
if (inv.getViewers().size() < 1) {
cancel();
return;
}
updateDeposit(inv); updateDeposit(event.getInventory());
} if (deposit <= 0)
}.runTaskTimer(MMOCore.plugin, 0, 20); return;
return inv;
}
@Override EconomyResponse response = MMOCore.plugin.economy.getEconomy().depositPlayer(player, deposit);
public void whenClicked(InventoryClickEvent event) { if (!response.transactionSuccess())
// event.setCancelled(true); return;
if (event.getCurrentItem() == null || event.getCurrentItem().getType() == Material.AIR)
return;
if (event.getCurrentItem().isSimilar(depositItem)) { event.getInventory().clear();
event.setCancelled(true); player.closeInventory();
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 2);
MMOCore.plugin.configManager.getSimpleMessage("deposit", "worth", String.valueOf(deposit)).send(player);
return;
}
updateDeposit(event.getInventory()); int worth = NBTItem.get(event.getCurrentItem()).getInteger("RpgWorth");
if (deposit <= 0) if (worth < 1)
return; event.setCancelled(true);
else
scheduleUpdate(event.getInventory());
}
EconomyResponse response = MMOCore.plugin.economy.getEconomy().depositPlayer(player, deposit); @Override
if (!response.transactionSuccess()) public void whenClosed(InventoryCloseEvent event) {
return;
event.getInventory().clear(); // Cancel runnable
player.closeInventory(); if (updateRunnable != null)
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 2); updateRunnable.cancel();
MMOCore.plugin.configManager.getSimpleMessage("deposit", "worth", "" + deposit).send(player);
return;
}
int worth = NBTItem.get(event.getCurrentItem()).getInteger("RpgWorth"); // Give all items back
if (worth < 1) { SmartGive smart = new SmartGive(player);
event.setCancelled(true); for (int j = 0; j < 26; j++) {
} ItemStack item = event.getInventory().getItem(j);
if (item != null)
smart.give(item);
}
}
// in deposit menu private BukkitRunnable newUpdateRunnable(Inventory inv) {
// if (event.getRawSlot() < 27) { return new BukkitRunnable() {
// int empty = player.getInventory().firstEmpty();
// if (empty < 0)
// return;
//
// player.playSound(player.getLocation(), Sound.ENTITY_SHULKER_TELEPORT,
// 1, 2);
// player.getInventory().addItem(event.getCurrentItem());
// event.setCurrentItem(null);
// updateDeposit(event.getInventory());
// return;
// }
// in player inventory @Override
// int empty = event.getInventory().firstEmpty(); public void run() {
// if (empty < 0) updateDeposit(inv);
// return; }
// };
// player.playSound(player.getLocation(), }
// Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 2);
// event.getInventory().addItem(event.getCurrentItem());
// event.setCurrentItem(null);
// updateDeposit(event.getInventory());
// return;
}
@Override private void scheduleUpdate(Inventory inv) {
public void whenClosed(InventoryCloseEvent event) { if (updateRunnable != null)
SmartGive smart = new SmartGive(player); updateRunnable.cancel();
for (int j = 0; j < 26; j++) {
ItemStack item = event.getInventory().getItem(j);
if (item != null)
smart.give(item);
}
}
private void updateDeposit(Inventory inv) { updateRunnable = new BukkitRunnable() {
deposit = MMOCoreUtils.getWorth(inv.getContents());
inv.setItem(26, depositItem = new SimpleItemBuilder("DEPOSIT_ITEM").addPlaceholders("worth", "" + deposit).build()); @Override
} public void run() {
updateRunnable = null;
updateDeposit(inv);
}
};
updateRunnable.runTaskLater(MMOCore.plugin, 10);
}
private void updateDeposit(Inventory inv) {
if (updateRunnable != null) {
updateRunnable.cancel();
updateRunnable = null;
}
deposit = MMOCoreUtils.getWorth(inv.getContents());
inv.setItem(26, depositItem = new SimpleItemBuilder("DEPOSIT_ITEM").addPlaceholders("worth", String.valueOf(deposit)).build());
}
} }