Add a gui and a menu system and some more shit

This commit is contained in:
Trixkz 2021-11-21 21:36:37 -05:00
parent 3eed8f07ef
commit 0285358607
7 changed files with 439 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import rip.tilly.bedwars.listeners.GameStartListener;
import rip.tilly.bedwars.listeners.InteractListener;
@ -21,6 +22,7 @@ import rip.tilly.bedwars.managers.arena.chunk.ChunkManager;
import rip.tilly.bedwars.managers.hotbar.HotbarManager;
import rip.tilly.bedwars.managers.mongo.MongoManager;
import rip.tilly.bedwars.managers.party.PartyManager;
import rip.tilly.bedwars.menusystem.PlayerMenuUtil;
import rip.tilly.bedwars.providers.scoreboard.ScoreboardProvider;
import rip.tilly.bedwars.utils.CC;
import rip.tilly.bedwars.utils.assemble.Assemble;
@ -28,6 +30,7 @@ import rip.tilly.bedwars.utils.assemble.AssembleStyle;
import rip.tilly.bedwars.utils.config.file.Config;
import java.util.Arrays;
import java.util.HashMap;
/**
* Created by Lucanius & Trixkz
@ -51,6 +54,8 @@ public final class BedWars extends JavaPlugin {
private GameManager gameManager;
private PartyManager partyManager;
private HashMap<Player, PlayerMenuUtil> playerMenuUtilMap = new HashMap<Player, PlayerMenuUtil>();
@Override
public void onEnable() {
instance = this;
@ -121,4 +126,18 @@ public final class BedWars extends JavaPlugin {
new PlayerDataListener(), new RandomListeners(), new InteractListener(), new GameStartListener()
).forEach(listener -> this.getServer().getPluginManager().registerEvents(listener, this));
}
public PlayerMenuUtil getPlayerMenuUtil(Player player) {
PlayerMenuUtil playerMenuUtil;
if (playerMenuUtilMap.containsKey(player)) {
return playerMenuUtilMap.get(player);
} else {
playerMenuUtil = new PlayerMenuUtil(player);
playerMenuUtilMap.put(player, playerMenuUtil);
return playerMenuUtil;
}
}
}

View File

@ -11,6 +11,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.material.TrapDoor;
import rip.tilly.bedwars.BedWars;
import rip.tilly.bedwars.managers.hotbar.impl.HotbarItem;
import rip.tilly.bedwars.menusystem.menu.PlayAGameMenu;
import rip.tilly.bedwars.player.PlayerData;
import rip.tilly.bedwars.player.PlayerState;
@ -72,13 +73,15 @@ public class InteractListener implements Listener {
case SPAWN:
switch (hotbarItem.getActionType()) {
case QUEUE_MENU:
// open queue menu
new PlayAGameMenu(this.plugin.getPlayerMenuUtil(player)).open();
break;
case SETTINGS_MENU:
// open settings menu
break;
case CREATE_PARTY:
this.plugin.getPartyManager().createParty(player);
break;
case COSMETICS_MENU:
// open cosmetics menu
@ -95,6 +98,7 @@ public class InteractListener implements Listener {
break;
case PARTY_LEAVE:
this.plugin.getPartyManager().leaveParty(player);
break;
}
break;

View File

@ -0,0 +1,63 @@
package rip.tilly.bedwars.menusystem;
import org.bukkit.Bukkit;
import org.bukkit.Material;
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 rip.tilly.bedwars.utils.ItemBuilder;
import java.util.Arrays;
public abstract class Menu implements InventoryHolder {
protected PlayerMenuUtil playerMenuUtil;
protected Inventory inventory;
protected ItemStack FILLER_GLASS = new ItemBuilder(Material.STAINED_GLASS_PANE).durability(7).name("").build();
public Menu(PlayerMenuUtil playerMenuUtil) {
this.playerMenuUtil = playerMenuUtil;
}
public abstract String getMenuName();
public abstract int getSlots();
public abstract void handleMenu(InventoryClickEvent event);
public abstract void setMenuItems();
public void open() {
inventory = Bukkit.createInventory(this, getSlots(), getMenuName());
this.setMenuItems();
playerMenuUtil.getOwner().openInventory(inventory);
}
@Override
public Inventory getInventory() {
return inventory;
}
public void setFillerGlass(){
for (int i = 0; i < this.getSlots(); i++) {
if (this.inventory.getItem(i) == null){
this.inventory.setItem(i, this.FILLER_GLASS);
}
}
}
public ItemStack makeItem(Material material, String displayName, String... lore) {
ItemStack item = new ItemStack(material);
ItemMeta itemMeta = item.getItemMeta();
itemMeta.setDisplayName(displayName);
itemMeta.setLore(Arrays.asList(lore));
item.setItemMeta(itemMeta);
return item;
}
}

View File

@ -0,0 +1,44 @@
package rip.tilly.bedwars.menusystem;
import org.bukkit.ChatColor;
import org.bukkit.Material;
public abstract class PaginatedMenu extends Menu {
protected int page = 0;
protected int maxItemsPerPage = 28;
protected int index = 0;
public PaginatedMenu(PlayerMenuUtil playerMenuUtil) {
super(playerMenuUtil);
}
public void addMenuBorder() {
this.inventory.setItem(48, makeItem(Material.WOOD_BUTTON, ChatColor.GREEN + "Left"));
this.inventory.setItem(49, makeItem(Material.BARRIER, ChatColor.DARK_RED + "Close"));
this.inventory.setItem(50, makeItem(Material.STONE_BUTTON, ChatColor.GREEN + "Right"));
for (int i = 0; i < 10; i++) {
if (this.inventory.getItem(i) == null) {
this.inventory.setItem(i, this.FILLER_GLASS);
}
}
this.inventory.setItem(17, this.FILLER_GLASS);
this.inventory.setItem(18, this.FILLER_GLASS);
this.inventory.setItem(26, this.FILLER_GLASS);
this.inventory.setItem(27, this.FILLER_GLASS);
this.inventory.setItem(35, this.FILLER_GLASS);
this.inventory.setItem(36, this.FILLER_GLASS);
for (int i = 44; i < 54; i++) {
if (this.inventory.getItem(i) == null) {
this.inventory.setItem(i, this.FILLER_GLASS);
}
}
}
public int getMaxItemsPerPage() {
return maxItemsPerPage;
}
}

View File

@ -0,0 +1,20 @@
package rip.tilly.bedwars.menusystem;
import org.bukkit.entity.Player;
public class PlayerMenuUtil {
private Player owner;
public PlayerMenuUtil(Player owner) {
this.owner = owner;
}
public Player getOwner() {
return owner;
}
public void setOwner(Player owner) {
this.owner = owner;
}
}

View File

@ -0,0 +1,90 @@
package rip.tilly.bedwars.menusystem.menu;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import rip.tilly.bedwars.menusystem.Menu;
import rip.tilly.bedwars.menusystem.PlayerMenuUtil;
import rip.tilly.bedwars.utils.CC;
import rip.tilly.bedwars.utils.ItemBuilder;
import java.util.ArrayList;
import java.util.List;
public class PlayAGameMenu extends Menu {
public PlayAGameMenu(PlayerMenuUtil playerMenuUtil) {
super(playerMenuUtil);
}
@Override
public String getMenuName() {
return CC.translate("&eSelect a game to play...");
}
@Override
public int getSlots() {
return 27;
}
@Override
public void handleMenu(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
if (event.getView().getTitle().equalsIgnoreCase(CC.translate("&eSelect a game to play..."))) {
switch (event.getCurrentItem().getType()) {
case WOOD_SWORD:
break;
case STONE_SWORD:
break;
case IRON_SWORD:
break;
case DIAMOND_SWORD:
break;
}
player.closeInventory();
}
}
@Override
public void setMenuItems() {
List<String> bedWars1v1Lore = new ArrayList<String>();
bedWars1v1Lore.add(CC.translate(""));
bedWars1v1Lore.add(CC.translate("&9You have to start somewhere,"));
bedWars1v1Lore.add(CC.translate("&9so why not start here?"));
ItemStack bedWars1v1ItemStack = new ItemBuilder(Material.WOOD_SWORD).name(CC.translate("&dBedWars 1v1")).lore(bedWars1v1Lore).build();
List<String> bedWars2v2Lore = new ArrayList<String>();
bedWars2v2Lore.add(CC.translate(""));
bedWars2v2Lore.add(CC.translate("&9Adapt, overcome, and conquer,"));
bedWars2v2Lore.add(CC.translate("&9you are advancing rapidly"));
ItemStack bedWars2v2ItemStack = new ItemBuilder(Material.STONE_SWORD).name(CC.translate("&dBedWars 2v2")).lore(bedWars2v2Lore).build();
List<String> bedWars3v3Lore = new ArrayList<String>();
bedWars3v3Lore.add(CC.translate(""));
bedWars3v3Lore.add(CC.translate("&9How crazy is this,"));
bedWars3v3Lore.add(CC.translate("&9it seems like it was just yesterday"));
bedWars3v3Lore.add(CC.translate("&9since you started"));
ItemStack bedWars3v3ItemStack = new ItemBuilder(Material.IRON_SWORD).name(CC.translate("&dBedWars 3v3")).lore(bedWars3v3Lore).build();
List<String> bedWars4v4Lore = new ArrayList<String>();
bedWars4v4Lore.add(CC.translate(""));
bedWars4v4Lore.add(CC.translate("&9You are now a master,"));
bedWars4v4Lore.add(CC.translate("&9you can go up against"));
bedWars4v4Lore.add(CC.translate("&9the undefeated PvP bot &7(Coming soon)"));
ItemStack bedWars4v4ItemStack = new ItemBuilder(Material.DIAMOND_SWORD).name(CC.translate("&dBedWars 4v4")).lore(bedWars4v4Lore).build();
this.inventory.setItem(10, bedWars1v1ItemStack);
this.inventory.setItem(12, bedWars2v2ItemStack);
this.inventory.setItem(14, bedWars3v3ItemStack);
this.inventory.setItem(16, bedWars4v4ItemStack);
this.setFillerGlass();
}
}

View File

@ -0,0 +1,198 @@
package rip.tilly.bedwars.utils;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import org.apache.commons.codec.binary.Base64;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.material.MaterialData;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class ItemBuilder implements Listener {
private final ItemStack is;
public ItemBuilder(final Material mat) {
is = new ItemStack(mat);
}
public ItemBuilder(final ItemStack is) {
this.is = is;
}
public ItemBuilder amount(final int amount) {
is.setAmount(amount);
return this;
}
public ItemBuilder name(final String name) {
final ItemMeta meta = is.getItemMeta();
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
is.setItemMeta(meta);
return this;
}
public ItemBuilder lore(final String name) {
final ItemMeta meta = is.getItemMeta();
List<String> lore = meta.getLore();
if (lore == null) {
lore = new ArrayList<>();
}
lore.add(name);
meta.setLore(lore);
is.setItemMeta(meta);
return this;
}
public ItemBuilder lore(final List<String> lore) {
List<String> toSet = new ArrayList<>();
ItemMeta meta = is.getItemMeta();
for (String string : lore) {
toSet.add(ChatColor.translateAlternateColorCodes('&', string));
}
meta.setLore(toSet);
is.setItemMeta(meta);
return this;
}
public ItemBuilder durability(final int durability) {
is.setDurability((short) durability);
return this;
}
public ItemBuilder head(String url) {
SkullMeta headMeta = (SkullMeta) is.getItemMeta();
GameProfile profile = new GameProfile(UUID.randomUUID(), null);
byte[] encodedData = Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", url).getBytes());
profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
Field profileField;
try {
profileField = headMeta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profileField.set(headMeta, profile);
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e1) {
e1.printStackTrace();
}
is.setItemMeta(headMeta);
return this;
}
public ItemBuilder owner(String owner) {
if (this.is.getType() == Material.SKULL_ITEM) {
SkullMeta meta = (SkullMeta) this.is.getItemMeta();
meta.setOwner(owner);
this.is.setItemMeta(meta);
return this;
}
throw new IllegalArgumentException("setOwner() only applicable for Skull Item");
}
@SuppressWarnings("deprecation")
public ItemBuilder data(final int data) {
is.setData(new MaterialData(is.getType(), (byte) data));
return this;
}
public ItemBuilder enchantment(final Enchantment enchantment, final int level) {
is.addUnsafeEnchantment(enchantment, level);
return this;
}
public ItemBuilder enchantment(final Enchantment enchantment) {
is.addUnsafeEnchantment(enchantment, 1);
return this;
}
public ItemBuilder hideFlags() {
final ItemMeta meta = is.getItemMeta();
meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_ATTRIBUTES);
is.setItemMeta(meta);
return this;
}
public ItemBuilder hideEnchants() {
final ItemMeta meta = is.getItemMeta();
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
is.setItemMeta(meta);
return this;
}
public ItemBuilder hideUnbreakable() {
final ItemMeta meta = is.getItemMeta();
meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
is.setItemMeta(meta);
return this;
}
public ItemBuilder addUnbreakable() {
final ItemMeta meta = is.getItemMeta();
meta.spigot().setUnbreakable(true);
is.setItemMeta(meta);
return this;
}
public ItemBuilder type(final Material material) {
is.setType(material);
return this;
}
public ItemBuilder clearLore() {
final ItemMeta meta = is.getItemMeta();
meta.setLore(new ArrayList<>());
is.setItemMeta(meta);
return this;
}
public ItemBuilder clearEnchantments() {
for (final Enchantment e : is.getEnchantments().keySet()) {
is.removeEnchantment(e);
}
return this;
}
public ItemBuilder color(Color color) {
if (is.getType() == Material.LEATHER_BOOTS || is.getType() == Material.LEATHER_CHESTPLATE
|| is.getType() == Material.LEATHER_HELMET || is.getType() == Material.LEATHER_LEGGINGS) {
LeatherArmorMeta meta = (LeatherArmorMeta) is.getItemMeta();
meta.setColor(color);
is.setItemMeta(meta);
return this;
} else {
throw new IllegalArgumentException("color() only applicable for leather armor!");
}
}
public ItemStack build() {
return is;
}
}