mirror of
https://gitlab.com/phoenix-dvpmt/mmocore.git
synced 2025-02-22 14:41:51 +01:00
Added input cancel message
This commit is contained in:
parent
bd28cc01df
commit
93d7824590
@ -1,6 +1,7 @@
|
|||||||
package net.Indyuce.mmocore.api.util.input;
|
package net.Indyuce.mmocore.api.util.input;
|
||||||
|
|
||||||
import net.Indyuce.mmocore.MMOCore;
|
import net.Indyuce.mmocore.MMOCore;
|
||||||
|
import net.Indyuce.mmocore.gui.api.PluginInventory;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@ -8,36 +9,63 @@ import org.bukkit.event.EventPriority;
|
|||||||
import org.bukkit.event.inventory.InventoryOpenEvent;
|
import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||||
import org.bukkit.util.Consumer;
|
import org.bukkit.util.Consumer;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
|
||||||
public class ChatInput extends PlayerInput {
|
public class ChatInput extends PlayerInput {
|
||||||
public ChatInput(Player player, InputType type, Consumer<String> output) {
|
private final InputType inputType;
|
||||||
super(player, output);
|
private final PluginInventory lastOpened;
|
||||||
|
|
||||||
player.closeInventory();
|
@Deprecated
|
||||||
MMOCore.plugin.configManager.getSimpleMessage("player-input.chat." + type.getLowerCaseName()).send(player);
|
public ChatInput(@NotNull Player player, @NotNull InputType inputType, @NotNull Consumer<String> output) {
|
||||||
MMOCore.plugin.configManager.getSimpleMessage("player-input.chat.cancel").send(player);
|
this(player, inputType, null, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public void close() {
|
* Have a player input a string in the global chat
|
||||||
AsyncPlayerChatEvent.getHandlerList().unregister(this);
|
*
|
||||||
InventoryOpenEvent.getHandlerList().unregister(this);
|
* @param player Player requesting chat input
|
||||||
}
|
* @param inputType Type of chat input
|
||||||
|
* @param lastOpened Inventory opened again if 'cancel' is input. Set to null to disable
|
||||||
|
* @param output What to do when input is detected
|
||||||
|
*/
|
||||||
|
public ChatInput(@NotNull Player player, @NotNull InputType inputType, @Nullable PluginInventory lastOpened, @NotNull Consumer<String> output) {
|
||||||
|
super(player, output);
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
this.inputType = inputType;
|
||||||
public void a(AsyncPlayerChatEvent event) {
|
this.lastOpened = lastOpened;
|
||||||
if (event.getPlayer().equals(getPlayer())) {
|
|
||||||
close();
|
|
||||||
event.setCancelled(true);
|
|
||||||
|
|
||||||
if (!event.getMessage().equals("cancel"))
|
player.closeInventory();
|
||||||
Bukkit.getScheduler().scheduleSyncDelayedTask(MMOCore.plugin, () -> output(event.getMessage()));
|
MMOCore.plugin.configManager.getSimpleMessage("player-input.chat." + inputType.getLowerCaseName()).send(player);
|
||||||
}
|
MMOCore.plugin.configManager.getSimpleMessage("player-input.chat.cancel").send(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@Override
|
||||||
public void b(InventoryOpenEvent event) {
|
public void close() {
|
||||||
if (event.getPlayer().equals(getPlayer()))
|
AsyncPlayerChatEvent.getHandlerList().unregister(this);
|
||||||
close();
|
InventoryOpenEvent.getHandlerList().unregister(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
|
||||||
|
public void registerInput(AsyncPlayerChatEvent event) {
|
||||||
|
if (event.getPlayer().equals(getPlayer())) {
|
||||||
|
close();
|
||||||
|
event.setCancelled(true);
|
||||||
|
|
||||||
|
if (event.getMessage().equals("cancel")) {
|
||||||
|
if (lastOpened != null)
|
||||||
|
Bukkit.getScheduler().runTask(MMOCore.plugin, () -> lastOpened.open());
|
||||||
|
MMOCore.plugin.configManager.getSimpleMessage("player-input.chat." + inputType.getLowerCaseName() + "-cancel").send(getPlayer());
|
||||||
|
} else
|
||||||
|
// Run sync
|
||||||
|
Bukkit.getScheduler().runTask(MMOCore.plugin, () -> output(event.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void b(InventoryOpenEvent event) {
|
||||||
|
if (event.getPlayer().equals(getPlayer()))
|
||||||
|
close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,29 +9,27 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
public class InventoryClickContext {
|
public class InventoryClickContext {
|
||||||
private final int slot;
|
private final int slot;
|
||||||
|
private final ClickType clickType;
|
||||||
|
private final Cancellable event;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private final ItemStack currentItem;
|
private final ItemStack currentItem;
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private final ClickType clickType;
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private final Cancellable event;
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private final Inventory inv;
|
private final Inventory inv;
|
||||||
|
private final PluginInventory invHolder;
|
||||||
|
|
||||||
public InventoryClickContext(int slot, ItemStack currentItem, ClickType clickType, Cancellable event) {
|
public InventoryClickContext(int slot, ItemStack currentItem, ClickType clickType, Cancellable event) {
|
||||||
this(slot, currentItem, clickType, event, null);
|
this(slot, currentItem, clickType, event, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public InventoryClickContext(int slot, ItemStack currentItem, ClickType clickType, Cancellable event, Inventory inv) {
|
public InventoryClickContext(int slot, @Nullable ItemStack currentItem, ClickType clickType, Cancellable event, @Nullable Inventory inv, @Nullable PluginInventory invHolder) {
|
||||||
this.slot = slot;
|
this.slot = slot;
|
||||||
this.currentItem = currentItem;
|
this.currentItem = currentItem;
|
||||||
this.clickType = clickType;
|
this.clickType = clickType;
|
||||||
this.event = event;
|
this.event = event;
|
||||||
this.inv = inv;
|
this.inv = inv;
|
||||||
|
this.invHolder = invHolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCancelled(boolean val) {
|
public void setCancelled(boolean val) {
|
||||||
@ -66,6 +64,11 @@ public class InventoryClickContext {
|
|||||||
return inv;
|
return inv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public PluginInventory getInventoryHolder() {
|
||||||
|
return invHolder;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isClassic() {
|
public boolean isClassic() {
|
||||||
return inv != null;
|
return inv != null;
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,8 @@ public abstract class PluginInventory implements InventoryHolder {
|
|||||||
|
|
||||||
public PluginInventory(Player player) {
|
public PluginInventory(Player player) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.playerData = player.getOpenInventory() != null && player.getOpenInventory().getTopInventory().getHolder() instanceof PluginInventory ? ((PluginInventory) player.getOpenInventory().getTopInventory().getHolder()).playerData : PlayerData.get(player);
|
this.playerData = player.getOpenInventory() != null && player.getOpenInventory().getTopInventory().getHolder() instanceof PluginInventory ?
|
||||||
|
((PluginInventory) player.getOpenInventory().getTopInventory().getHolder()).playerData : PlayerData.get(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PluginInventory(PlayerData playerData) {
|
public PluginInventory(PlayerData playerData) {
|
||||||
@ -34,5 +35,6 @@ public abstract class PluginInventory implements InventoryHolder {
|
|||||||
public abstract void whenClicked(InventoryClickContext context);
|
public abstract void whenClicked(InventoryClickContext context);
|
||||||
|
|
||||||
public void whenClosed(InventoryCloseEvent event) {
|
public void whenClosed(InventoryCloseEvent event) {
|
||||||
|
// Nothing by default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -192,7 +192,7 @@ public class EditableFriendList extends EditableInventory {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new ChatInput(player, InputType.FRIEND_REQUEST, (input) -> {
|
new ChatInput(player, InputType.FRIEND_REQUEST, context.getInventoryHolder(), input -> {
|
||||||
Player target = Bukkit.getPlayer(input);
|
Player target = Bukkit.getPlayer(input);
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
MMOCore.plugin.configManager.getSimpleMessage("not-online-player", "player", input).send(player);
|
MMOCore.plugin.configManager.getSimpleMessage("not-online-player", "player", input).send(player);
|
||||||
|
@ -152,7 +152,7 @@ public class EditableGuildAdmin extends EditableInventory {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new ChatInput(player, PlayerInput.InputType.GUILD_INVITE, (input) -> {
|
new ChatInput(player, PlayerInput.InputType.GUILD_INVITE, context.getInventoryHolder(), input -> {
|
||||||
Player target = Bukkit.getPlayer(input);
|
Player target = Bukkit.getPlayer(input);
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
MMOCore.plugin.configManager.getSimpleMessage("not-online-player", "player", input).send(player);
|
MMOCore.plugin.configManager.getSimpleMessage("not-online-player", "player", input).send(player);
|
||||||
|
@ -38,14 +38,14 @@ public class EditableGuildCreation extends EditableInventory {
|
|||||||
public void whenClicked(InventoryClickContext context, InventoryItem item) {
|
public void whenClicked(InventoryClickContext context, InventoryItem item) {
|
||||||
|
|
||||||
if (item.getFunction().equals("create")) {
|
if (item.getFunction().equals("create")) {
|
||||||
new ChatInput(player, PlayerInput.InputType.GUILD_CREATION_TAG, (input) -> {
|
new ChatInput(player, PlayerInput.InputType.GUILD_CREATION_TAG, context.getInventoryHolder(), input -> {
|
||||||
if(MMOCore.plugin.dataProvider.getGuildManager().getConfig().shouldUppercaseTags())
|
if(MMOCore.plugin.dataProvider.getGuildManager().getConfig().shouldUppercaseTags())
|
||||||
input = input.toUpperCase();
|
input = input.toUpperCase();
|
||||||
|
|
||||||
if(check(player, input, MMOCore.plugin.dataProvider.getGuildManager().getConfig().getTagRules())) {
|
if(check(player, input, MMOCore.plugin.dataProvider.getGuildManager().getConfig().getTagRules())) {
|
||||||
String tag = input;
|
String tag = input;
|
||||||
|
|
||||||
new ChatInput(player, PlayerInput.InputType.GUILD_CREATION_NAME, (name) -> {
|
new ChatInput(player, PlayerInput.InputType.GUILD_CREATION_NAME, context.getInventoryHolder(), name -> {
|
||||||
if(check(player, name, MMOCore.plugin.dataProvider.getGuildManager().getConfig().getNameRules())) {
|
if(check(player, name, MMOCore.plugin.dataProvider.getGuildManager().getConfig().getNameRules())) {
|
||||||
MMOCore.plugin.dataProvider.getGuildManager().newRegisteredGuild(playerData.getUniqueId(), name, tag);
|
MMOCore.plugin.dataProvider.getGuildManager().newRegisteredGuild(playerData.getUniqueId(), name, tag);
|
||||||
MMOCore.plugin.dataProvider.getGuildManager().getGuild(tag.toLowerCase()).addMember(playerData.getUniqueId());
|
MMOCore.plugin.dataProvider.getGuildManager().getGuild(tag.toLowerCase()).addMember(playerData.getUniqueId());
|
||||||
|
@ -201,7 +201,7 @@ public class EditableGuildView extends EditableInventory {
|
|||||||
* Sound.ENTITY_VILLAGER_NO, 1, 1); return; }
|
* Sound.ENTITY_VILLAGER_NO, 1, 1); return; }
|
||||||
*/
|
*/
|
||||||
|
|
||||||
new ChatInput(player, PlayerInput.InputType.GUILD_INVITE, (input) -> {
|
new ChatInput(player, PlayerInput.InputType.GUILD_INVITE, context.getInventoryHolder(), input -> {
|
||||||
Player target = Bukkit.getPlayer(input);
|
Player target = Bukkit.getPlayer(input);
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
MMOCore.plugin.configManager.getSimpleMessage("not-online-player", "player", input).send(player);
|
MMOCore.plugin.configManager.getSimpleMessage("not-online-player", "player", input).send(player);
|
||||||
|
@ -149,7 +149,7 @@ public class EditablePartyView extends EditableInventory {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new ChatInput(player, PlayerInput.InputType.PARTY_INVITE, input -> {
|
new ChatInput(player, PlayerInput.InputType.PARTY_INVITE, context.getInventoryHolder(), input -> {
|
||||||
Player target = Bukkit.getPlayer(input);
|
Player target = Bukkit.getPlayer(input);
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
MMOCore.plugin.configManager.getSimpleMessage("not-online-player", "player", input).send(player);
|
MMOCore.plugin.configManager.getSimpleMessage("not-online-player", "player", input).send(player);
|
||||||
|
@ -139,7 +139,7 @@ public class ConfigManager {
|
|||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public PlayerInput newPlayerInput(Player player, InputType type, Consumer<String> output) {
|
public PlayerInput newPlayerInput(Player player, InputType type, Consumer<String> output) {
|
||||||
return new ChatInput(player, type, output);
|
return new ChatInput(player, type, null, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadDefaultFile(String name) {
|
public void loadDefaultFile(String name) {
|
||||||
|
@ -38,7 +38,7 @@ public class PlayerListener implements Listener {
|
|||||||
@EventHandler
|
@EventHandler
|
||||||
public void b(InventoryClickEvent event) {
|
public void b(InventoryClickEvent event) {
|
||||||
if (event.getInventory().getHolder() instanceof PluginInventory)
|
if (event.getInventory().getHolder() instanceof PluginInventory)
|
||||||
((PluginInventory) event.getInventory().getHolder()).whenClicked(new InventoryClickContext(event.getRawSlot(), event.getCurrentItem(), event.getClick(), event, event.getInventory()));
|
((PluginInventory) event.getInventory().getHolder()).whenClicked(new InventoryClickContext(event.getRawSlot(), event.getCurrentItem(), event.getClick(), event, event.getInventory(), (PluginInventory) event.getInventory().getHolder()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user