249 lines
13 KiB
Java
249 lines
13 KiB
Java
package de.jatitv.commandguiv2.Spigot.Listener;
|
|
|
|
import de.jatitv.commandguiv2.Spigot.Listener.UseItem_Listener.Events;
|
|
import de.jatitv.commandguiv2.Spigot.Objekte.Slot;
|
|
import de.jatitv.commandguiv2.Spigot.cmdManagement.Commands;
|
|
import de.jatitv.commandguiv2.Spigot.system.Bungee_Sender_Reciver;
|
|
import de.jatitv.commandguiv2.Spigot.config.languages.SelectMessages;
|
|
import de.jatitv.commandguiv2.Spigot.Objekte.Object;
|
|
import de.jatitv.commandguiv2.Spigot.gui.OpenGUI;
|
|
import de.jatitv.commandguiv2.Spigot.config.config.SelectConfig;
|
|
import de.jatitv.commandguiv2.Spigot.Main;
|
|
import de.jatitv.commandguiv2.Util;
|
|
import net.t2code.lib.Spigot.Lib.items.ItemVersion;
|
|
import net.t2code.lib.Spigot.Lib.messages.send;
|
|
import net.t2code.lib.Spigot.Lib.replace.Replace;
|
|
import net.t2code.lib.Spigot.Lib.vault.Vault;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.Sound;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.inventory.ClickType;
|
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
|
import org.bukkit.plugin.Plugin;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
|
|
public class GUI_Listener implements Listener {
|
|
|
|
private static String prefix = Util.getPrefix();
|
|
public static String GUICode;
|
|
private static Plugin plugin = Main.plugin;
|
|
|
|
@EventHandler
|
|
public void onInventoryClick(InventoryClickEvent e) {
|
|
Player player = (Player) e.getWhoClicked();
|
|
JavaPlugin plugin = Main.plugin;
|
|
if (e.getInventory() == null) return;
|
|
if (e.getCurrentItem() == null) return;
|
|
for (Object gui : Main.guiHashMap.values()) {
|
|
if (player.getOpenInventory().getTitle().equals(Replace.replace(prefix, GUICode + gui.GUI_Name))
|
|
|| (Main.PaPi && player.getOpenInventory().getTitle().equals(Replace.replace(prefix, player, GUICode + gui.GUI_Name)))) {
|
|
e.setCancelled(true);
|
|
for (Slot slot : gui.GUI_Slots) {
|
|
if (e.getSlot() != slot.slot) continue;
|
|
if (e.getCurrentItem().getType() == ItemVersion.getHead() || e.getCurrentItem().getType() == ItemVersion.getGreenWool().getType() || e.getCurrentItem().getType() == ItemVersion.getRedWool().getType() || e.getCurrentItem().getType() == Material.valueOf(slot.item.toUpperCase().replace(".", "_"))) {
|
|
if (!slot.perm
|
|
|| player.hasPermission("commandgui.gui." + gui.Command_Command + ".slot." + (slot.slot + 1))
|
|
|| player.hasPermission("commandgui.gui." + gui.Command_Command + ".slot." + (slot.slot + 1) + ".use")
|
|
|| player.hasPermission("commandgui.admin")) {
|
|
if (!slot.enable) continue;
|
|
if (!e.getCurrentItem().getItemMeta().getDisplayName().equals(Replace.replace(prefix, slot.name))) continue;
|
|
if (slot.cost_Enable) {
|
|
if (slot.command_Enable || slot.message_Enable || slot.openGUI_Enable || slot.serverChange) {
|
|
if (!Vault.buy(prefix, player, slot.price)) {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
player.closeInventory();
|
|
}
|
|
}.runTaskLater(plugin, 1L);
|
|
player.sendMessage(SelectMessages.No_money);
|
|
if (SelectConfig.Sound_NoMoney_Enable && SelectConfig.Sound_Enable)
|
|
player.playSound(player.getLocation(), SelectConfig.Sound_NoMoney, 3, 1);
|
|
} else {
|
|
player.sendMessage(SelectMessages.Buy_msg.replace("[itemname]", Replace.replace(prefix, slot.name))
|
|
.replace("[price]", slot.price + " " + SelectConfig.Currency));
|
|
execute(slot, player, e, gui);
|
|
}
|
|
}
|
|
} else execute(slot, player, e, gui);
|
|
} else player.sendMessage(SelectMessages.NoPermissionForItem.replace("[item]", Replace.replace(prefix, slot.name))
|
|
.replace("[perm]", "commandgui.gui." + gui.Command_Command + ".slot." + (slot.slot + 1)));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void execute(Slot slot, Player player, InventoryClickEvent e, Object gui) {
|
|
if (slot.togglePermission) togglePerm(slot, player);
|
|
if (slot.toggleUseItem) toggleUseItem(player);
|
|
if (slot.command_Enable) command(slot, player);
|
|
if (slot.openGUI_Enable) openGUI(slot, player);
|
|
if (slot.message_Enable) message(slot, player);
|
|
if (slot.serverChange) serverChange(slot, player);
|
|
if (slot.setConfigEnable) setConfig(slot, player, e);
|
|
if (SelectConfig.Sound_Enable && SelectConfig.Sound_Click_Enable) sound(slot, player, gui);
|
|
}
|
|
|
|
private static void command(Slot slot, Player player) {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
player.closeInventory();
|
|
}
|
|
}.runTaskLater(plugin, 1L);
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
if (slot.command_BungeeCommand) {
|
|
if (SelectConfig.Bungee) {
|
|
for (String cmd : slot.command) {
|
|
Bungee_Sender_Reciver.sendToBungee(player, cmd.replace("[player]", player.getName()), slot.commandAsConsole);
|
|
}
|
|
} else {
|
|
send.console(prefix + " §4To use bungee commands, enable the Bungee option in the config.");
|
|
send.player(player, prefix + " §4To use bungee commands, enable the Bungee option in the config.");
|
|
}
|
|
} else {
|
|
for (String cmd : slot.command) {
|
|
if (slot.commandAsConsole) {
|
|
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), cmd.replace("[player]", player.getName()));
|
|
} else player.chat("/" + cmd.replace("[player]", player.getName()));
|
|
}
|
|
}
|
|
}
|
|
}.runTaskLater(plugin, 2L);
|
|
}
|
|
|
|
private static void openGUI(Slot slot, Player player) {
|
|
player.closeInventory();
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
OpenGUI.openGUI(player, slot.openGUI);
|
|
}
|
|
}.runTaskLater(plugin, 1L);
|
|
}
|
|
|
|
private static void message(Slot slot, Player player) {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
player.closeInventory();
|
|
}
|
|
}.runTaskLater(plugin, 1L);
|
|
for (String msg : slot.message) {
|
|
if (Main.PaPi) {
|
|
if (slot.cost_Enable) {
|
|
player.sendMessage(Replace.replacePrice(prefix, player, msg, slot.price + " " + SelectConfig.Currency));
|
|
} else player.sendMessage(Replace.replace(prefix, player, msg.replace("[prefix]", prefix)));
|
|
} else {
|
|
if (slot.cost_Enable) {
|
|
player.sendMessage(Replace.replacePrice(prefix, msg, slot.price + " " + SelectConfig.Currency));
|
|
} else player.sendMessage(Replace.replace(prefix, msg.replace("[prefix]", prefix)));
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void serverChange(Slot slot, Player player) {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
player.closeInventory();
|
|
}
|
|
}.runTaskLater(plugin, 1L);
|
|
send.player(player, SelectMessages.onServerChange.replace("[server]", slot.serverChangeServer));
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
ServerChange.send(player, slot.serverChangeServer);
|
|
}
|
|
}.runTaskLater(Main.plugin, 20L);
|
|
}
|
|
|
|
private static void setConfig(Slot slot, Player player, InventoryClickEvent e) {
|
|
File config = new File(slot.configFilePath);
|
|
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(config);
|
|
|
|
if (e.getClick() == ClickType.LEFT) {
|
|
if (slot.configOptionPremat.equals("String")) {
|
|
yamlConfiguration.set(slot.configOptionPath.replace("/", "."), slot.configStringValueLeft);
|
|
} else if (slot.configOptionPremat.equals("Boolean")) {
|
|
yamlConfiguration.set(slot.configOptionPath.replace("/", "."), slot.configBooleanValueLeft);
|
|
} else if (slot.configOptionPremat.equals("Integer")) {
|
|
yamlConfiguration.set(slot.configOptionPath.replace("/", "."), slot.configIntegerValueLeft);
|
|
} else if (slot.configOptionPremat.equals("Double")) {
|
|
yamlConfiguration.set(slot.configOptionPath.replace("/", "."), slot.configDoubleValueLeft);
|
|
} else if (slot.configOptionPremat.equals("List")) {
|
|
yamlConfiguration.set(slot.configOptionPath.replace("/", "."), slot.configListValueLeft);
|
|
} else {
|
|
player.sendMessage("§cCheck the Option §6SetConfig/Option/Premat"); //todo
|
|
}
|
|
}
|
|
if (e.getClick() == ClickType.RIGHT) {
|
|
if (slot.configOptionPremat.equals("String")) {
|
|
yamlConfiguration.set(slot.configOptionPath.replace("/", "."), slot.configStringValueRight);
|
|
} else if (slot.configOptionPremat.equals("Boolean")) {
|
|
yamlConfiguration.set(slot.configOptionPath.replace("/", "."), slot.configBooleanValueRight);
|
|
} else if (slot.configOptionPremat.equals("Integer")) {
|
|
yamlConfiguration.set(slot.configOptionPath.replace("/", "."), slot.configIntegerValueRight);
|
|
} else if (slot.configOptionPremat.equals("Double")) {
|
|
yamlConfiguration.set(slot.configOptionPath.replace("/", "."), slot.configDoubleValueRight);
|
|
} else if (slot.configOptionPremat.equals("List")) {
|
|
yamlConfiguration.set(slot.configOptionPath.replace("/", "."), slot.configListValueRight);
|
|
} else {
|
|
player.sendMessage("§cCheck the Option §6SetConfig/Option/Premat"); //todo
|
|
}
|
|
}
|
|
try {
|
|
yamlConfiguration.save(config);
|
|
} catch (IOException tac) {
|
|
tac.printStackTrace();
|
|
}
|
|
if (slot.pluginReloadEnable) {
|
|
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), slot.pluginReloadCommand);
|
|
}
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
player.closeInventory();
|
|
}
|
|
}.runTaskLater(plugin, 1L);
|
|
}
|
|
|
|
private static void sound(Slot slot, Player player, Object gui) {
|
|
if (slot.customSound_Enable) {
|
|
if (!slot.customSound_NoSound) {
|
|
try {
|
|
player.playSound(player.getLocation(), Sound.valueOf(slot.customSound_Sound.toUpperCase().replace(".", "_")), 3, 1);
|
|
|
|
} catch (Exception e1) {
|
|
send.console("§4\n§4\n§4\n" + SelectMessages.SoundNotFound.replace("[prefix]", prefix)
|
|
.replace("[sound]", "§6GUI: §e" + Replace.replace(prefix, gui.GUI_Name) + "§r §6Slot: §e" + slot.slot + " §6CustomSound: §9" + slot.customSound_Sound));
|
|
player.playSound(player.getLocation(), SelectConfig.Sound_Click, 3, 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void togglePerm(Slot slot, Player player) {
|
|
if (player.hasPermission(slot.togglePermissionPerm)) {
|
|
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), SelectConfig.togglePermFalse.replace("[player]", player.getName()).replace("[perm]", slot.togglePermissionPerm));
|
|
} else
|
|
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), SelectConfig.togglePermTrue.replace("[player]", player.getName()).replace("[perm]", slot.togglePermissionPerm));
|
|
}
|
|
|
|
private static void toggleUseItem(Player player) {
|
|
if (Events.useItemHashMap.get(player)) {
|
|
Commands.itemOff(player);
|
|
} else Commands.itemOn(player);
|
|
}
|
|
} |