Last active
February 23, 2020 15:07
-
-
Save noobmobile/a8a300596c08905b463302ab3a4c53ca to your computer and use it in GitHub Desktop.
Crie um inventário para confirmar a ação de um player, utilizando o pattern builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.dont.empregos.Terminal; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Material; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.inventory.InventoryClickEvent; | |
import org.bukkit.inventory.Inventory; | |
import org.bukkit.inventory.InventoryHolder; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import java.util.function.Consumer; | |
/** | |
author: dont | |
*/ | |
public class Confirmar { | |
static { | |
Bukkit.getPluginManager().registerEvents(new Listener() { | |
@EventHandler | |
public void onClick(InventoryClickEvent e){ | |
if (e.getInventory().getHolder() instanceof ConfirmarHolder){ | |
e.setCancelled(true); | |
ConfirmarHolder holder = (ConfirmarHolder) e.getInventory().getHolder(); | |
Player player = (Player) e.getWhoClicked(); | |
if (holder.get().getConfirmarSlot() == e.getSlot()){ | |
player.closeInventory(); | |
holder.get().getOnConfirmar().accept(player); | |
} else if (holder.get().getRecusarSlot() == e.getSlot()){ | |
player.closeInventory(); | |
holder.get().getOnRecusar().accept(player); | |
} | |
} | |
} | |
}, Terminal.getPlugin(Terminal.class)); //sua main | |
} | |
private Consumer<Player> recusar, confirmar; | |
private ItemStack middleItem; | |
private int recusarSlot, confirmarSlot, middleSlot; | |
private Inventory inventory; | |
public Confirmar(Builder builder) { | |
this.recusar = builder.recusar; | |
this.confirmar = builder.confirmar; | |
this.middleItem = builder.middleItem; | |
this.recusarSlot = builder.recusarSlot; | |
this.confirmarSlot = builder.confirmarSlot; | |
this.middleSlot = builder.middleSlot; | |
createInventory(); | |
} | |
private void createInventory(){ | |
inventory = Bukkit.createInventory(new ConfirmarHolder(this), 27, "Confirmar"); | |
inventory.setItem(middleSlot, middleItem); | |
inventory.setItem(recusarSlot, createItem("§cCancelar",14)); | |
inventory.setItem(confirmarSlot, createItem("§aConfirmar",5)); | |
} | |
private ItemStack createItem(String name, int durability){ | |
ItemStack item = new ItemStack(Material.STAINED_CLAY, 1, (short) durability); | |
ItemMeta meta = item.getItemMeta(); | |
meta.setDisplayName(name); | |
item.setItemMeta(meta); | |
return item; | |
} | |
public void open(Player player){ | |
player.openInventory(inventory); | |
} | |
public int getRecusarSlot() { | |
return recusarSlot; | |
} | |
public int getConfirmarSlot() { | |
return confirmarSlot; | |
} | |
public Consumer<Player> getOnRecusar() { | |
return recusar; | |
} | |
public Consumer<Player> getOnConfirmar() { | |
return confirmar; | |
} | |
public class ConfirmarHolder implements InventoryHolder { | |
private Confirmar confirmar; | |
public ConfirmarHolder(Confirmar confirmar) { | |
this.confirmar = confirmar; | |
} | |
public Confirmar get() { | |
return confirmar; | |
} | |
@Override | |
public Inventory getInventory() { | |
return null; | |
} | |
} | |
public static class Builder { | |
private Consumer<Player> recusar, confirmar; | |
private ItemStack middleItem; | |
private int recusarSlot, confirmarSlot, middleSlot; | |
public Builder() { | |
recusar = p -> {}; | |
confirmar = p -> {}; | |
middleItem = new ItemStack(Material.AIR); | |
recusarSlot = 11; | |
middleSlot = 13; | |
confirmarSlot = 15; | |
} | |
public Builder onRecusar(Consumer<Player> recusar){ | |
this.recusar = recusar; | |
return this; | |
} | |
public Builder onConfirmar(Consumer<Player> confirmar){ | |
this.confirmar = confirmar; | |
return this; | |
} | |
public Builder withMiddleItem(ItemStack middleItem){ | |
this.middleItem = middleItem; | |
return this; | |
} | |
public Builder withSlots(int recusarSlot, int confirmarSlot, int middleSlot){ | |
this.recusarSlot = recusarSlot; | |
this.confirmarSlot = confirmarSlot; | |
this.middleSlot = middleSlot; | |
return this; | |
} | |
public Confirmar build(){ | |
return new Confirmar(this); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// exemplo real de uso | |
private final Confirmar QUIT = new Confirmar.Builder().onConfirmar(player -> { | |
User user = (User) manager.cache.get(player.getName()); | |
user.setEmpregoAtual(null); | |
user.setLastChange(System.currentTimeMillis()); | |
player.sendMessage("§eVocê saiu do emprego com sucesso."); | |
player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1, 1); | |
main.getEmpregoManager().open(player); //abre o menu de empregos de volta | |
}).onRecusar(player -> { | |
main.getEmpregoManager().open(player); //abre o menu de empregos de volta | |
}).build(); | |
@EventHandler | |
public void onClick(InventoryClickEvent e){ | |
if (e.getInventory().getName().equals("Empregos")){ | |
e.setCancelled(true); | |
Player player = (Player) e.getWhoClicked(); | |
User user = (User) manager.cache.get(player.getName()); | |
if (e.getSlot() == 8){ | |
if (user.getEmpregoAtual() == null){ | |
player.sendMessage("§eVocê já está desempregado."); | |
player.playSound(player.getLocation(), Sound.VILLAGER_NO, 1, 1); | |
return; | |
} | |
QUIT.open(player); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment