Add gui mirror, fix prompt errors, add prompt cancel

This commit is contained in:
jascotty2 2019-08-29 11:04:32 -05:00
parent 00279b9137
commit 72f8375e7b
10 changed files with 77 additions and 10 deletions

10
pom.xml
View File

@ -83,6 +83,16 @@
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>

View File

@ -172,4 +172,17 @@ public class GuiUtils {
item.setItemMeta(meta);
return item;
}
public static void mirrorFill(Gui gui, int row, int col, boolean mirrorRow, boolean mirrorCol, ItemStack item) {
gui.setItem(row, col, item);
if (mirrorRow) {
gui.setItem(gui.rows - row - 1, col, item);
}
if (mirrorCol) {
gui.setItem(row, 8 - col, item);
}
if (mirrorRow && mirrorCol) {
gui.setItem(gui.rows - row - 1, 8 - col, item);
}
}
}

View File

@ -10,6 +10,9 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.plugin.Plugin;
public class ChatPrompt implements Listener {
@ -18,6 +21,7 @@ public class ChatPrompt implements Listener {
private final ChatConfirmHandler handler;
private OnClose onClose = null;
private OnCancel onCancel = null;
private Listener listener;
private ChatPrompt(Player player, ChatConfirmHandler hander) {
@ -54,9 +58,14 @@ public class ChatPrompt implements Listener {
return this;
}
public ChatPrompt setOnCancel(OnCancel onCancel) {
this.onCancel = onCancel;
return this;
}
private void startListener(Plugin plugin) {
this.listener = new Listener() {
@EventHandler
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = false)
public void onChat(AsyncPlayerChatEvent event) {
Player player = event.getPlayer();
if (!ChatPrompt.isRegistered(player)) return;
@ -66,7 +75,11 @@ public class ChatPrompt implements Listener {
ChatConfirmEvent chatConfirmEvent = new ChatConfirmEvent(player, event.getMessage());
handler.onChat(chatConfirmEvent);
try {
handler.onChat(chatConfirmEvent);
} catch (Throwable t) {
plugin.getLogger().log(Level.SEVERE, "Failed to process chat prompt", t);
}
if (onClose != null) {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () ->
@ -74,6 +87,25 @@ public class ChatPrompt implements Listener {
}
HandlerList.unregisterAll(listener);
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = false)
public void onCancel(PlayerCommandPreprocessEvent event) {
Player player = event.getPlayer();
if (!ChatPrompt.isRegistered(player)) return;
ChatPrompt.unregister(player);
if(event.getMessage().toLowerCase().startsWith("/cancel"))
event.setCancelled(true);
if (onCancel != null) {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () ->
onCancel.onCancel(), 0L);
} else if (onClose != null) {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () ->
onClose.onClose(), 0L);
}
HandlerList.unregisterAll(listener);
}
};
Bukkit.getPluginManager().registerEvents(listener, plugin);
@ -87,6 +119,10 @@ public class ChatPrompt implements Listener {
void onClose();
}
public static interface OnCancel {
void onCancel();
}
public static class ChatConfirmEvent {
private final Player player;

View File

@ -301,6 +301,7 @@ public class Config {
if (!plugin.getDataFolder().exists())
plugin.getDataFolder().mkdir();
BufferedWriter writer = new BufferedWriter(new FileWriter(configFile));
writer.write(config.toString());
writer.flush();
writer.close();

View File

@ -14,6 +14,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import us.myles.viaversion.libs.bungeecordchat.api.ChatColor;
public class ConfigEditorGUI extends AbstractGUI {
@ -69,12 +70,16 @@ public class ConfigEditorGUI extends AbstractGUI {
material = LegacyMaterials.CLOCK.getMaterial();
registerClickable(j, ((player1, inventory1, cursor, slot, type) -> {
ChatPrompt prompt = ChatPrompt.showPrompt(plugin, player, "Enter your new value.", event -> {
if (config.isInt(setting.getCompleteKey())) {
config.set(setting.getCompleteKey(), Integer.parseInt(event.getMessage().trim()));
} else if (config.isDouble(setting.getCompleteKey())) {
config.set(setting.getCompleteKey(), Double.parseDouble(event.getMessage().trim()));
} else if (config.isLong(setting.getCompleteKey())) {
config.set(setting.getCompleteKey(), Long.parseLong(event.getMessage().trim()));
try {
if (config.isInt(setting.getCompleteKey())) {
config.set(setting.getCompleteKey(), Integer.parseInt(event.getMessage().trim()));
} else if (config.isDouble(setting.getCompleteKey())) {
config.set(setting.getCompleteKey(), Double.parseDouble(event.getMessage().trim()));
} else if (config.isLong(setting.getCompleteKey())) {
config.set(setting.getCompleteKey(), Long.parseLong(event.getMessage().trim()));
}
} catch (NumberFormatException e) {
player1.sendMessage(ChatColor.RED + "Error: \"" + event.getMessage().trim() + "\" is not a number!");
}
});
prompt.setOnClose(() -> init("Settings Editor", inventory.getSize()));

View File

@ -4,7 +4,6 @@ import com.songoda.core.input.ChatPrompt;
import com.songoda.core.compatibility.LegacyMaterials;
import com.songoda.core.settings.Setting;
import com.songoda.core.utils.gui.AbstractGUI;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

View File

@ -20,6 +20,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.util.*;
@Deprecated
public abstract class AbstractGUI implements Listener {
private static boolean listenersInitialized = false;

View File

@ -5,6 +5,7 @@ import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@Deprecated
public interface Clickable {
void Clickable(Player player, Inventory inventory, ItemStack cursor, int slot, ClickType type);

View File

@ -3,6 +3,7 @@ package com.songoda.core.utils.gui;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
@Deprecated
public interface OnClose {
void OnClose(Player player, Inventory inventory);

View File

@ -1,10 +1,10 @@
package com.songoda.core.utils.gui;
import com.songoda.core.SongodaCore;
import com.songoda.core.compatibility.ServerVersion;
import org.bukkit.Sound;
import org.bukkit.event.inventory.ClickType;
@Deprecated
public class Range {
private int min;