mirror of
https://github.com/CivMC/Civ.git
synced 2026-07-18 00:20:44 +00:00
use anvil guis
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package net.civmc.kitpvp;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import net.civmc.kitpvp.anvil.AnvilGui;
|
||||
import net.civmc.kitpvp.arena.ArenaCommand;
|
||||
import net.civmc.kitpvp.arena.ArenaManager;
|
||||
import net.civmc.kitpvp.arena.MysqlLoader;
|
||||
import net.civmc.kitpvp.arena.RespawnListener;
|
||||
import net.civmc.kitpvp.arena.data.SqlArenaDao;
|
||||
import net.civmc.kitpvp.warp.WarpMain;
|
||||
import net.civmc.kitpvp.command.ClearCommand;
|
||||
import net.civmc.kitpvp.command.KitCommand;
|
||||
import net.civmc.kitpvp.snapshot.DeathListener;
|
||||
@@ -12,24 +14,27 @@ import net.civmc.kitpvp.snapshot.InventorySnapshotManager;
|
||||
import net.civmc.kitpvp.snapshot.ViewInventorySnapshotCommand;
|
||||
import net.civmc.kitpvp.spawn.SetSpawnCommand;
|
||||
import net.civmc.kitpvp.spawn.SpawnCommand;
|
||||
import net.civmc.kitpvp.spawn.SpawnListener;
|
||||
import net.civmc.kitpvp.spawn.SpawnProvider;
|
||||
import net.civmc.kitpvp.spawn.SqlSpawnProvider;
|
||||
import net.civmc.kitpvp.sql.SqlKitPvpDao;
|
||||
import net.civmc.kitpvp.warp.WarpMain;
|
||||
import org.bukkit.Bukkit;
|
||||
import vg.civcraft.mc.civmodcore.ACivMod;
|
||||
import vg.civcraft.mc.civmodcore.dao.DatabaseCredentials;
|
||||
import vg.civcraft.mc.civmodcore.dao.ManagedDatasource;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class KitPvpPlugin extends ACivMod {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
AnvilGui anvilGui = new AnvilGui();
|
||||
getServer().getPluginManager().registerEvents(anvilGui, this);
|
||||
|
||||
saveDefaultConfig();
|
||||
DatabaseCredentials credentials = (DatabaseCredentials) getConfig().get("database");
|
||||
ManagedDatasource source = ManagedDatasource.construct(this, credentials);
|
||||
getCommand("kit").setExecutor(new KitCommand(new SqlKitPvpDao(source)));
|
||||
getCommand("kit").setExecutor(new KitCommand(new SqlKitPvpDao(source), anvilGui));
|
||||
WarpMain warpMain = new WarpMain(this, source);
|
||||
getCommand("clear").setExecutor(new ClearCommand());
|
||||
|
||||
@@ -44,10 +49,12 @@ public class KitPvpPlugin extends ACivMod {
|
||||
SpawnProvider spawnProvider = new SqlSpawnProvider(source);
|
||||
getCommand("spawn").setExecutor(new SpawnCommand(spawnProvider));
|
||||
getCommand("setspawn").setExecutor(new SetSpawnCommand(spawnProvider));
|
||||
getServer().getPluginManager().registerEvents(new SpawnListener(spawnProvider), this);
|
||||
|
||||
try {
|
||||
ArenaManager manager = new ArenaManager(this, spawnProvider, new MysqlLoader(source));
|
||||
getCommand("arena").setExecutor(new ArenaCommand(this, new SqlArenaDao(source), manager));
|
||||
getServer().getPluginManager().registerEvents(new RespawnListener(manager, spawnProvider), this);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package net.civmc.kitpvp.anvil;
|
||||
|
||||
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.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryView;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.view.AnvilView;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class AnvilGui implements Listener {
|
||||
private final Map<Player, Inventory> anvils = new HashMap<>();
|
||||
private final Map<Player, AnvilGuiListener> listeners = new HashMap<>();
|
||||
|
||||
public void open(Player player, AnvilGuiListener listener) {
|
||||
InventoryView view = player.openAnvil(null, true);
|
||||
anvils.put(player, view.getTopInventory());
|
||||
listeners.put(player, listener);
|
||||
|
||||
view.getTopInventory().setItem(0, new ItemStack(Material.WRITABLE_BOOK));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(PlayerQuitEvent event) {
|
||||
Inventory inventory = anvils.remove(event.getPlayer());
|
||||
if (inventory != null) {
|
||||
inventory.clear();
|
||||
listeners.remove(event.getPlayer()).onClose();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(InventoryCloseEvent event) {
|
||||
Inventory inventory = anvils.remove(event.getPlayer());
|
||||
if (inventory != null) {
|
||||
inventory.clear();
|
||||
listeners.remove(event.getPlayer()).onClose();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(InventoryClickEvent event) {
|
||||
if (anvils.get(event.getWhoClicked()) != event.getInventory()) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.setCancelled(true);
|
||||
if (event.getClickedInventory() != event.getInventory()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getSlot() == 2) {
|
||||
String text = ((AnvilView) event.getView()).getRenameText();
|
||||
if (text == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (listeners.get(event.getWhoClicked()).onRename(text)) {
|
||||
listeners.remove(event.getWhoClicked());
|
||||
anvils.remove(event.getWhoClicked()).clear();
|
||||
if (event.getWhoClicked().getOpenInventory().getTopInventory() == event.getInventory()) {
|
||||
event.getWhoClicked().closeInventory();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package net.civmc.kitpvp.anvil;
|
||||
|
||||
public interface AnvilGuiListener {
|
||||
void onClose();
|
||||
boolean onRename(String name);
|
||||
}
|
||||
@@ -82,6 +82,10 @@ public class ArenaManager {
|
||||
return "dynamicarena." + owner.getName() + "." + arena;
|
||||
}
|
||||
|
||||
public boolean isArena(String worldName) {
|
||||
return worldName.startsWith("dynamicarena.");
|
||||
}
|
||||
|
||||
public void createArena(Player player, Arena arena) {
|
||||
AdvancedSlimePaperAPI api = AdvancedSlimePaperAPI.instance();
|
||||
SlimeWorld slimeWorld;
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package net.civmc.kitpvp.arena;
|
||||
|
||||
import net.civmc.kitpvp.spawn.SpawnProvider;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerRespawnEvent;
|
||||
|
||||
public class RespawnListener implements Listener {
|
||||
|
||||
private final ArenaManager manager;
|
||||
private final SpawnProvider provider;
|
||||
|
||||
public RespawnListener(ArenaManager manager, SpawnProvider provider) {
|
||||
this.manager = manager;
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
|
||||
@EventHandler
|
||||
public void on(PlayerRespawnEvent event) {
|
||||
if (manager.isArena(event.getPlayer().getWorld().getName())) {
|
||||
Location spawn = this.provider.getSpawn();
|
||||
if (spawn != null) {
|
||||
event.setRespawnLocation(spawn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package net.civmc.kitpvp.command;
|
||||
import java.util.logging.Level;
|
||||
import net.civmc.kitpvp.KitApplier;
|
||||
import net.civmc.kitpvp.KitPvpPlugin;
|
||||
import net.civmc.kitpvp.anvil.AnvilGui;
|
||||
import net.civmc.kitpvp.data.Kit;
|
||||
import net.civmc.kitpvp.data.KitPvpDao;
|
||||
import net.civmc.kitpvp.gui.KitListGui;
|
||||
@@ -20,9 +21,11 @@ import org.jetbrains.annotations.NotNull;
|
||||
public class KitCommand implements CommandExecutor {
|
||||
|
||||
private final KitPvpDao dao;
|
||||
private final AnvilGui anvilGui;
|
||||
|
||||
public KitCommand(KitPvpDao dao) {
|
||||
public KitCommand(KitPvpDao dao, AnvilGui anvilGui) {
|
||||
this.dao = dao;
|
||||
this.anvilGui = anvilGui;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -110,7 +113,7 @@ public class KitCommand implements CommandExecutor {
|
||||
});
|
||||
return true;
|
||||
} else if (args.length == 0) {
|
||||
new KitListGui(dao, player);
|
||||
new KitListGui(dao, anvilGui, player);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -5,6 +5,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import net.civmc.kitpvp.KitPvpPlugin;
|
||||
import net.civmc.kitpvp.anvil.AnvilGui;
|
||||
import net.civmc.kitpvp.anvil.AnvilGuiListener;
|
||||
import net.civmc.kitpvp.data.Kit;
|
||||
import net.civmc.kitpvp.data.KitPvpDao;
|
||||
import net.civmc.kitpvp.gui.selection.ArmourSlotSelectionGui;
|
||||
@@ -16,18 +18,12 @@ import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.conversations.ConversationContext;
|
||||
import org.bukkit.conversations.ConversationFactory;
|
||||
import org.bukkit.conversations.Prompt;
|
||||
import org.bukkit.conversations.ValidatingPrompt;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import vg.civcraft.mc.civmodcore.inventory.gui.Clickable;
|
||||
import vg.civcraft.mc.civmodcore.inventory.gui.ClickableInventory;
|
||||
import vg.civcraft.mc.civmodcore.inventory.gui.DecorationStack;
|
||||
@@ -35,14 +31,16 @@ import vg.civcraft.mc.civmodcore.inventory.gui.DecorationStack;
|
||||
public class EditKitGui {
|
||||
|
||||
private final KitPvpDao dao;
|
||||
private final AnvilGui anvilGui;
|
||||
private final Player player;
|
||||
private Kit kit;
|
||||
private ItemStack lastItem;
|
||||
private final KitListGui gui;
|
||||
private final boolean canEdit;
|
||||
|
||||
public EditKitGui(KitPvpDao dao, Player player, Kit kit, KitListGui gui) {
|
||||
public EditKitGui(KitPvpDao dao, AnvilGui anvilGui, Player player, Kit kit, KitListGui gui) {
|
||||
this.dao = dao;
|
||||
this.anvilGui = anvilGui;
|
||||
this.player = player;
|
||||
this.kit = kit;
|
||||
this.gui = gui;
|
||||
@@ -108,54 +106,40 @@ public class EditKitGui {
|
||||
protected void clicked(@NotNull Player clicker) {
|
||||
inventory.setOnClose(null);
|
||||
player.closeInventory();
|
||||
clicker.beginConversation(new ConversationFactory(JavaPlugin.getPlugin(KitPvpPlugin.class))
|
||||
.withFirstPrompt(new ValidatingPrompt() {
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public @NotNull String getPromptText(@NotNull ConversationContext context) {
|
||||
return ChatColor.GOLD + "Enter name to rename kit to or 'cancel' to cancel:";
|
||||
}
|
||||
anvilGui.open(player, new AnvilGuiListener() {
|
||||
@Override
|
||||
public void onClose() {
|
||||
JavaPlugin plugin = JavaPlugin.getProvidingPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTask(plugin, EditKitGui.this::open);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Prompt acceptValidatedInput(@NotNull ConversationContext context, @NotNull String input) {
|
||||
JavaPlugin plugin = JavaPlugin.getPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
Kit renamedKit;
|
||||
try {
|
||||
renamedKit = dao.renameKit(kit.id(), input);
|
||||
} catch (Exception e) {
|
||||
JavaPlugin.getPlugin(KitPvpPlugin.class).getLogger().log(Level.WARNING, "Error renaming kit", e);
|
||||
return;
|
||||
}
|
||||
if (renamedKit == null) {
|
||||
player.sendMessage(Component.text("A kit with that name already exists", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
player.sendMessage(Component.text("Renamed kit to: %s".formatted(renamedKit.name()), NamedTextColor.GOLD));
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
updateKit(renamedKit);
|
||||
open();
|
||||
});
|
||||
@Override
|
||||
public boolean onRename(String name) {
|
||||
if (!Kit.checkValidName(player, name)) {
|
||||
return false;
|
||||
}
|
||||
JavaPlugin plugin = JavaPlugin.getPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
Kit renamedKit;
|
||||
try {
|
||||
renamedKit = dao.renameKit(kit.id(), name);
|
||||
} catch (Exception e) {
|
||||
JavaPlugin.getPlugin(KitPvpPlugin.class).getLogger().log(Level.WARNING, "Error renaming kit", e);
|
||||
return;
|
||||
}
|
||||
if (renamedKit == null) {
|
||||
player.sendMessage(Component.text("A kit with that name already exists", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
player.sendMessage(Component.text("Renamed kit to: %s".formatted(renamedKit.name()), NamedTextColor.GOLD));
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
updateKit(renamedKit);
|
||||
open();
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isInputValid(@NotNull ConversationContext context, @NotNull String input) {
|
||||
return Kit.checkValidName(player, input);
|
||||
}
|
||||
})
|
||||
.addConversationAbandonedListener(abandonedEvent -> {
|
||||
if (!abandonedEvent.gracefulExit()) {
|
||||
player.sendMessage(Component.text("Cancelled renaming kit", NamedTextColor.GOLD));
|
||||
open();
|
||||
}
|
||||
})
|
||||
.withTimeout(30)
|
||||
.withModality(false)
|
||||
.withLocalEcho(false)
|
||||
.withEscapeSequence("cancel")
|
||||
.buildConversation(clicker));
|
||||
});
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 7);
|
||||
|
||||
@@ -277,12 +261,12 @@ public class EditKitGui {
|
||||
if (kitItem.getMaxStackSize() > 1) {
|
||||
Bukkit.getScheduler().runTask(JavaPlugin.getProvidingPlugin(KitPvpPlugin.class), () -> {
|
||||
inventory.setOnClose(null);
|
||||
new CountSelectionGui(dao, player, itemIndex, kit, EditKitGui.this).open();
|
||||
new CountSelectionGui(dao, anvilGui, player, itemIndex, kit, EditKitGui.this).open();
|
||||
});
|
||||
} else if (!kitItem.isEmpty()) {
|
||||
Bukkit.getScheduler().runTask(JavaPlugin.getProvidingPlugin(KitPvpPlugin.class), () -> {
|
||||
inventory.setOnClose(null);
|
||||
new EnchantmentGui(dao, player, itemIndex, kit, EditKitGui.this).open();
|
||||
new EnchantmentGui(dao, anvilGui, player, itemIndex, kit, EditKitGui.this).open();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,26 @@
|
||||
package net.civmc.kitpvp.gui;
|
||||
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Multimaps;
|
||||
import net.civmc.kitpvp.KitApplier;
|
||||
import net.civmc.kitpvp.KitPvpPlugin;
|
||||
import net.civmc.kitpvp.anvil.AnvilGui;
|
||||
import net.civmc.kitpvp.anvil.AnvilGuiListener;
|
||||
import net.civmc.kitpvp.data.Kit;
|
||||
import net.civmc.kitpvp.data.KitPvpDao;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.conversations.ConversationContext;
|
||||
import org.bukkit.conversations.ConversationFactory;
|
||||
import org.bukkit.conversations.Prompt;
|
||||
import org.bukkit.conversations.ValidatingPrompt;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import vg.civcraft.mc.civmodcore.inventory.gui.Clickable;
|
||||
import vg.civcraft.mc.civmodcore.inventory.gui.FastMultiPageView;
|
||||
import vg.civcraft.mc.civmodcore.inventory.gui.IClickable;
|
||||
@@ -44,6 +37,7 @@ public class KitListGui {
|
||||
}
|
||||
|
||||
private final KitPvpDao dao;
|
||||
private final AnvilGui anvilGui;
|
||||
private final Player player;
|
||||
private final List<Kit> kits = new ArrayList<>();
|
||||
private final FastMultiPageView view;
|
||||
@@ -51,8 +45,9 @@ public class KitListGui {
|
||||
private boolean ready = false;
|
||||
private boolean openWhenReady = true;
|
||||
|
||||
public KitListGui(KitPvpDao dao, Player player) {
|
||||
public KitListGui(KitPvpDao dao, AnvilGui anvilGui, Player player) {
|
||||
this.dao = dao;
|
||||
this.anvilGui = anvilGui;
|
||||
this.player = player;
|
||||
this.view = new FastMultiPageView(player, this::kitSupplier, "Kits", 6);
|
||||
this.view.setMenuSlot(new Clickable(CREATE_KIT_ITEM) {
|
||||
@@ -73,53 +68,41 @@ public class KitListGui {
|
||||
}
|
||||
|
||||
clicker.closeInventory();
|
||||
clicker.beginConversation(new ConversationFactory(JavaPlugin.getPlugin(KitPvpPlugin.class))
|
||||
.withFirstPrompt(new ValidatingPrompt() {
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public @NotNull String getPromptText(@NotNull ConversationContext context) {
|
||||
return ChatColor.GOLD + "Enter name to create kit to or 'cancel' to cancel:";
|
||||
}
|
||||
clicker.sendMessage(Component.text("Enter kit name to create", NamedTextColor.GOLD));
|
||||
anvilGui.open(player, new AnvilGuiListener() {
|
||||
@Override
|
||||
public void onClose() {
|
||||
JavaPlugin plugin = JavaPlugin.getProvidingPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTask(plugin, KitListGui.this::open);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Prompt acceptValidatedInput(@NotNull ConversationContext context, @NotNull String input) {
|
||||
JavaPlugin plugin = JavaPlugin.getPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
Kit createdKit;
|
||||
try {
|
||||
createdKit = dao.createKit(input, player.getUniqueId());
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Error creating kit", e);
|
||||
return;
|
||||
}
|
||||
if (createdKit == null) {
|
||||
player.sendMessage(Component.text("A kit with that name already exists", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
invalidate();
|
||||
new EditKitGui(KitListGui.this.dao, clicker, createdKit, KitListGui.this);
|
||||
});
|
||||
@Override
|
||||
public boolean onRename(String name) {
|
||||
if (!Kit.checkValidName(player, name)) {
|
||||
return false;
|
||||
}
|
||||
JavaPlugin plugin = JavaPlugin.getPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
|
||||
Kit createdKit;
|
||||
try {
|
||||
createdKit = dao.createKit(name, player.getUniqueId());
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Error creating kit", e);
|
||||
return;
|
||||
}
|
||||
if (createdKit == null) {
|
||||
player.sendMessage(Component.text("A kit with that name already exists", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
invalidate();
|
||||
new EditKitGui(KitListGui.this.dao, anvilGui, clicker, createdKit, KitListGui.this);
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isInputValid(@NotNull ConversationContext context, @NotNull String input) {
|
||||
return Kit.checkValidName(player, input);
|
||||
}
|
||||
})
|
||||
.addConversationAbandonedListener(abandonedEvent -> {
|
||||
if (!abandonedEvent.gracefulExit()) {
|
||||
player.sendMessage(Component.text("Cancelled creating kit", NamedTextColor.GOLD));
|
||||
open();
|
||||
}
|
||||
})
|
||||
.withTimeout(30)
|
||||
.withModality(false)
|
||||
.withLocalEcho(false)
|
||||
.withEscapeSequence("cancel")
|
||||
.buildConversation(clicker));
|
||||
});
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 0);
|
||||
|
||||
@@ -198,7 +181,7 @@ public class KitListGui {
|
||||
|
||||
@Override
|
||||
protected void onRightClick(@NotNull Player clicker) {
|
||||
new EditKitGui(KitListGui.this.dao, clicker, kit, KitListGui.this);
|
||||
new EditKitGui(KitListGui.this.dao, KitListGui.this.anvilGui, clicker, kit, KitListGui.this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,32 +1,30 @@
|
||||
package net.civmc.kitpvp.gui.selection;
|
||||
|
||||
import net.civmc.kitpvp.KitPvpPlugin;
|
||||
import net.civmc.kitpvp.anvil.AnvilGui;
|
||||
import net.civmc.kitpvp.anvil.AnvilGuiListener;
|
||||
import net.civmc.kitpvp.data.Kit;
|
||||
import net.civmc.kitpvp.data.KitPvpDao;
|
||||
import net.civmc.kitpvp.gui.EditKitGui;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.conversations.ConversationContext;
|
||||
import org.bukkit.conversations.ConversationFactory;
|
||||
import org.bukkit.conversations.Prompt;
|
||||
import org.bukkit.conversations.ValidatingPrompt;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import vg.civcraft.mc.civmodcore.inventory.gui.Clickable;
|
||||
import vg.civcraft.mc.civmodcore.inventory.gui.ClickableInventory;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class CountSelectionGui extends ItemSelectionGui {
|
||||
|
||||
public CountSelectionGui(KitPvpDao dao, Player player, int slot, Kit kit, EditKitGui gui) {
|
||||
private final AnvilGui anvilGui;
|
||||
|
||||
public CountSelectionGui(KitPvpDao dao, AnvilGui anvilGui, Player player, int slot, Kit kit, EditKitGui gui) {
|
||||
super(dao, "Change item count", player, slot, kit, gui::open, gui);
|
||||
this.anvilGui = anvilGui;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,63 +39,46 @@ public class CountSelectionGui extends ItemSelectionGui {
|
||||
protected void clicked(@NotNull Player clicker) {
|
||||
inventory.setOnClose(null);
|
||||
clicker.closeInventory();
|
||||
clicker.beginConversation(new ConversationFactory(JavaPlugin.getPlugin(KitPvpPlugin.class))
|
||||
.withFirstPrompt(new ValidatingPrompt() {
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public @NotNull String getPromptText(@NotNull ConversationContext context) {
|
||||
return ChatColor.GOLD + "Enter item count or 'cancel' to cancel:";
|
||||
}
|
||||
anvilGui.open(clicker, new AnvilGuiListener() {
|
||||
@Override
|
||||
public void onClose() {
|
||||
JavaPlugin plugin = JavaPlugin.getProvidingPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTask(plugin, CountSelectionGui.this::open);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Prompt acceptValidatedInput(@NotNull ConversationContext context, @NotNull String input) {
|
||||
int num = Integer.parseInt(input);
|
||||
|
||||
ItemStack[] items = kit.items().clone();
|
||||
ItemStack item = kit.items()[slot];
|
||||
item.setAmount(num);
|
||||
gui.setLastItem(item);
|
||||
items[slot] = item;
|
||||
|
||||
JavaPlugin plugin = JavaPlugin.getProvidingPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
Kit updatedKit = dao.updateKit(kit.id(), kit.icon(), items);
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
gui.updateKit(updatedKit);
|
||||
inventory.setOnClose(null);
|
||||
gui.open();
|
||||
});
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isInputValid(@NotNull ConversationContext context, @NotNull String input) {
|
||||
try {
|
||||
int num = Integer.parseInt(input);
|
||||
if (num >= 1 && num <= kitItem.getMaxStackSize()) {
|
||||
return true;
|
||||
} else {
|
||||
clicker.sendMessage(Component.text("Must be between 1 and " + kitItem.getMaxStackSize(), NamedTextColor.RED));
|
||||
return false;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
clicker.sendMessage(Component.text("Must be a number", NamedTextColor.RED));
|
||||
@Override
|
||||
public boolean onRename(String name) {
|
||||
int num;
|
||||
try {
|
||||
num = Integer.parseInt(name);
|
||||
if (num < 1 || num > kitItem.getMaxStackSize()) {
|
||||
clicker.sendMessage(Component.text("Must be between 1 and " + kitItem.getMaxStackSize(), NamedTextColor.RED));
|
||||
open();
|
||||
return false;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
clicker.sendMessage(Component.text("Must be a number", NamedTextColor.RED));
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.addConversationAbandonedListener(abandonedEvent -> {
|
||||
if (!abandonedEvent.gracefulExit()) {
|
||||
clicker.sendMessage(Component.text("Cancelled updating item count", NamedTextColor.GOLD));
|
||||
open();
|
||||
}
|
||||
})
|
||||
.withTimeout(30)
|
||||
.withModality(false)
|
||||
.withLocalEcho(false)
|
||||
.withEscapeSequence("cancel")
|
||||
.buildConversation(clicker));
|
||||
|
||||
ItemStack[] items = kit.items().clone();
|
||||
ItemStack item = kit.items()[slot];
|
||||
item.setAmount(num);
|
||||
gui.setLastItem(item);
|
||||
items[slot] = item;
|
||||
|
||||
JavaPlugin plugin = JavaPlugin.getProvidingPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
Kit updatedKit = dao.updateKit(kit.id(), kit.icon(), items);
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
gui.updateKit(updatedKit);
|
||||
inventory.setOnClose(null);
|
||||
gui.open();
|
||||
});
|
||||
});
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 18);
|
||||
if (kitItem.getMaxStackSize() == 64) {
|
||||
|
||||
@@ -6,6 +6,8 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import net.civmc.kitpvp.KitPvpPlugin;
|
||||
import net.civmc.kitpvp.anvil.AnvilGui;
|
||||
import net.civmc.kitpvp.anvil.AnvilGuiListener;
|
||||
import net.civmc.kitpvp.data.Kit;
|
||||
import net.civmc.kitpvp.data.KitPvpDao;
|
||||
import net.civmc.kitpvp.gui.EditKitGui;
|
||||
@@ -13,12 +15,7 @@ import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.minecraft.core.component.DataComponents;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.conversations.ConversationContext;
|
||||
import org.bukkit.conversations.ConversationFactory;
|
||||
import org.bukkit.conversations.Prompt;
|
||||
import org.bukkit.conversations.ValidatingPrompt;
|
||||
import org.bukkit.craftbukkit.inventory.CraftItemStack;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -27,7 +24,6 @@ import org.bukkit.inventory.meta.Damageable;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import vg.civcraft.mc.civmodcore.inventory.gui.Clickable;
|
||||
import vg.civcraft.mc.civmodcore.inventory.gui.ClickableInventory;
|
||||
|
||||
@@ -35,8 +31,11 @@ public class EnchantmentGui extends ItemSelectionGui {
|
||||
|
||||
private static final int[] ENCHANT_START_SLOTS = new int[]{0, 9, 18, 27, 36, 5, 14, 23, 32, 41};
|
||||
|
||||
public EnchantmentGui(KitPvpDao dao, Player player, int slot, Kit kit, EditKitGui gui) {
|
||||
private final AnvilGui anvilGui;
|
||||
|
||||
public EnchantmentGui(KitPvpDao dao, AnvilGui anvilGui, Player player, int slot, Kit kit, EditKitGui gui) {
|
||||
super(dao, "Enchant", player, slot, kit, gui::open, gui);
|
||||
this.anvilGui = anvilGui;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -118,69 +117,51 @@ public class EnchantmentGui extends ItemSelectionGui {
|
||||
protected void clicked(@NotNull Player clicker) {
|
||||
inventory.setOnClose(null);
|
||||
clicker.closeInventory();
|
||||
clicker.beginConversation(new ConversationFactory(JavaPlugin.getPlugin(KitPvpPlugin.class))
|
||||
.withFirstPrompt(new ValidatingPrompt() {
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public @NotNull String getPromptText(@NotNull ConversationContext context) {
|
||||
return ChatColor.GOLD + "Enter durability (between %s and %s) or 'cancel' to cancel:".formatted(1, maxDamage);
|
||||
}
|
||||
anvilGui.open(clicker, new AnvilGuiListener() {
|
||||
@Override
|
||||
public void onClose() {
|
||||
JavaPlugin plugin = JavaPlugin.getProvidingPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTask(plugin, EnchantmentGui.this::open);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Prompt acceptValidatedInput(@NotNull ConversationContext context, @NotNull String input) {
|
||||
try {
|
||||
int num = Integer.parseInt(input);
|
||||
|
||||
ItemStack[] items = kit.items().clone();
|
||||
ItemStack item = kit.items()[EnchantmentGui.this.slot];
|
||||
Damageable meta = (Damageable) item.getItemMeta();
|
||||
meta.setDamage(maxDamage - num);
|
||||
item.setItemMeta(meta);
|
||||
items[EnchantmentGui.this.slot] = item;
|
||||
gui.setLastItem(item);
|
||||
|
||||
JavaPlugin plugin = JavaPlugin.getProvidingPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
Kit updatedKit = dao.updateKit(kit.id(), kit.icon(), items);
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
gui.updateKit(updatedKit);
|
||||
inventory.setOnClose(null);
|
||||
gui.open();
|
||||
});
|
||||
});
|
||||
} catch (Exception e) {
|
||||
JavaPlugin.getPlugin(KitPvpPlugin.class).getLogger().log(Level.WARNING, "Error setting durability", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isInputValid(@NotNull ConversationContext context, @NotNull String input) {
|
||||
try {
|
||||
int num = Integer.parseInt(input);
|
||||
if (num >= 1 && num <= maxDamage) {
|
||||
return true;
|
||||
} else {
|
||||
clicker.sendMessage(Component.text("Must be between 1 and " + maxDamage, NamedTextColor.RED));
|
||||
return false;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
clicker.sendMessage(Component.text("Must be a number", NamedTextColor.RED));
|
||||
@Override
|
||||
public boolean onRename(String name) {
|
||||
int num;
|
||||
try {
|
||||
num = Integer.parseInt(name);
|
||||
if (num < 1 || num > maxDamage) {
|
||||
clicker.sendMessage(Component.text("Must be between 1 and " + maxDamage, NamedTextColor.RED));
|
||||
return false;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
clicker.sendMessage(Component.text("Must be a number", NamedTextColor.RED));
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.addConversationAbandonedListener(abandonedEvent -> {
|
||||
if (!abandonedEvent.gracefulExit()) {
|
||||
clicker.sendMessage(Component.text("Cancelled updating durability", NamedTextColor.GOLD));
|
||||
open();
|
||||
|
||||
try {
|
||||
ItemStack[] items = kit.items().clone();
|
||||
ItemStack item = kit.items()[EnchantmentGui.this.slot];
|
||||
Damageable meta = (Damageable) item.getItemMeta();
|
||||
meta.setDamage(maxDamage - num);
|
||||
item.setItemMeta(meta);
|
||||
items[EnchantmentGui.this.slot] = item;
|
||||
gui.setLastItem(item);
|
||||
|
||||
JavaPlugin plugin = JavaPlugin.getProvidingPlugin(KitPvpPlugin.class);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
Kit updatedKit = dao.updateKit(kit.id(), kit.icon(), items);
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
gui.updateKit(updatedKit);
|
||||
inventory.setOnClose(null);
|
||||
gui.open();
|
||||
});
|
||||
});
|
||||
} catch (Exception e) {
|
||||
JavaPlugin.getPlugin(KitPvpPlugin.class).getLogger().log(Level.WARNING, "Error setting durability", e);
|
||||
}
|
||||
})
|
||||
.withTimeout(30)
|
||||
.withModality(false)
|
||||
.withLocalEcho(false)
|
||||
.withEscapeSequence("cancel")
|
||||
.buildConversation(clicker));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 53);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package net.civmc.kitpvp.spawn;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerRespawnEvent;
|
||||
import org.spigotmc.event.player.PlayerSpawnLocationEvent;
|
||||
|
||||
public class SpawnListener implements Listener {
|
||||
|
||||
private final SpawnProvider provider;
|
||||
|
||||
public SpawnListener(SpawnProvider provider) {
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(PlayerJoinEvent event) {
|
||||
if (!event.getPlayer().hasPlayedBefore()) {
|
||||
Location spawn = this.provider.getSpawn();
|
||||
if (spawn != null) {
|
||||
event.getPlayer().teleport(spawn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(PlayerSpawnLocationEvent event) {
|
||||
// handles players joining after their arena was deleted
|
||||
if (event.getPlayer().getLocation().distanceSquared(Bukkit.getWorld("world").getSpawnLocation()) <= 1) {
|
||||
Location spawn = this.provider.getSpawn();
|
||||
if (spawn != null) {
|
||||
event.setSpawnLocation(spawn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(PlayerRespawnEvent event) {
|
||||
if (event.getPlayer().getWorld().getName().equals("world")) {
|
||||
Location spawn = this.provider.getSpawn();
|
||||
if (spawn != null) {
|
||||
event.setRespawnLocation(spawn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user