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,15 +17,18 @@ 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;
/**
* 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;
public DepositMenu(Player player) { public DepositMenu(Player player) {
super(player); super(player);
} }
@ -29,27 +36,12 @@ public class DepositMenu extends PluginInventory {
@Override @Override
public Inventory getInventory() { public Inventory getInventory() {
Inventory inv = Bukkit.createInventory(this, 27, "Deposit"); Inventory inv = Bukkit.createInventory(this, 27, "Deposit");
inv.setItem(26, depositItem = new SimpleItemBuilder("DEPOSIT_ITEM").addPlaceholders("worth", "0").build());
new BukkitRunnable() {
@Override
public void run() {
if (inv.getViewers().size() < 1) {
cancel();
return;
}
updateDeposit(inv); updateDeposit(inv);
}
}.runTaskTimer(MMOCore.plugin, 0, 20);
return inv; return inv;
} }
@Override @Override
public void whenClicked(InventoryClickEvent event) { public void whenClicked(InventoryClickEvent event) {
// event.setCancelled(true);
if (event.getCurrentItem() == null || event.getCurrentItem().getType() == Material.AIR) if (event.getCurrentItem() == null || event.getCurrentItem().getType() == Material.AIR)
return; return;
@ -67,44 +59,25 @@ public class DepositMenu extends PluginInventory {
event.getInventory().clear(); event.getInventory().clear();
player.closeInventory(); player.closeInventory();
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 2); player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 2);
MMOCore.plugin.configManager.getSimpleMessage("deposit", "worth", "" + deposit).send(player); MMOCore.plugin.configManager.getSimpleMessage("deposit", "worth", String.valueOf(deposit)).send(player);
return; return;
} }
int worth = NBTItem.get(event.getCurrentItem()).getInteger("RpgWorth"); int worth = NBTItem.get(event.getCurrentItem()).getInteger("RpgWorth");
if (worth < 1) { if (worth < 1)
event.setCancelled(true); event.setCancelled(true);
} else
scheduleUpdate(event.getInventory());
// in deposit menu
// if (event.getRawSlot() < 27) {
// 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
// int empty = event.getInventory().firstEmpty();
// if (empty < 0)
// 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 @Override
public void whenClosed(InventoryCloseEvent event) { public void whenClosed(InventoryCloseEvent event) {
// Cancel runnable
if (updateRunnable != null)
updateRunnable.cancel();
// Give all items back
SmartGive smart = new SmartGive(player); SmartGive smart = new SmartGive(player);
for (int j = 0; j < 26; j++) { for (int j = 0; j < 26; j++) {
ItemStack item = event.getInventory().getItem(j); ItemStack item = event.getInventory().getItem(j);
@ -113,8 +86,38 @@ public class DepositMenu extends PluginInventory {
} }
} }
private BukkitRunnable newUpdateRunnable(Inventory inv) {
return new BukkitRunnable() {
@Override
public void run() {
updateDeposit(inv);
}
};
}
private void scheduleUpdate(Inventory inv) {
if (updateRunnable != null)
updateRunnable.cancel();
updateRunnable = new BukkitRunnable() {
@Override
public void run() {
updateRunnable = null;
updateDeposit(inv);
}
};
updateRunnable.runTaskLater(MMOCore.plugin, 10);
}
private void updateDeposit(Inventory inv) { private void updateDeposit(Inventory inv) {
if (updateRunnable != null) {
updateRunnable.cancel();
updateRunnable = null;
}
deposit = MMOCoreUtils.getWorth(inv.getContents()); deposit = MMOCoreUtils.getWorth(inv.getContents());
inv.setItem(26, depositItem = new SimpleItemBuilder("DEPOSIT_ITEM").addPlaceholders("worth", "" + deposit).build()); inv.setItem(26, depositItem = new SimpleItemBuilder("DEPOSIT_ITEM").addPlaceholders("worth", String.valueOf(deposit)).build());
} }
} }