From c380bd82d3211a74c274f4f66e1834789a319208 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 22 Mar 2022 11:46:34 +0100 Subject: [PATCH] Tutorial deletion option Signed-off-by: Lixfel --- .../bungeecore/commands/TutorialCommand.java | 130 +++++++++--------- src/de/steamwar/bungeecore/sql/Tutorial.java | 15 ++ .../steamwar/messages/BungeeCore.properties | 1 + 3 files changed, 82 insertions(+), 64 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/TutorialCommand.java b/src/de/steamwar/bungeecore/commands/TutorialCommand.java index d855715d..3a031e5d 100644 --- a/src/de/steamwar/bungeecore/commands/TutorialCommand.java +++ b/src/de/steamwar/bungeecore/commands/TutorialCommand.java @@ -26,14 +26,15 @@ import de.steamwar.bungeecore.inventory.SWListInv; import de.steamwar.bungeecore.inventory.SWStreamInv; import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.Tutorial; -import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.ProxiedPlayer; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.stream.Collectors; @@ -49,57 +50,54 @@ public class TutorialCommand extends BasicCommand { return; ProxiedPlayer player = (ProxiedPlayer) sender; - if(args.length != 0) { - switch(args[0].toLowerCase()) { - case "rate": - if(args.length < 2) { - player.chat("/tutorial rate"); - return; - } - - int id; - try { - id = Integer.parseInt(args[1]); - } catch (NumberFormatException e) { - BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with non number: " + args[1]); - return; - } - - Tutorial tutorial = Tutorial.get(id); - if(tutorial == null) { - BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with nonexistent id: " + id); - return; - } - - rate(player, tutorial); - break; - case "create": - if(args.length < 3) { - Message.send("TUTORIAL_CREATE_HELP", player); - return; - } - - String material = args[1].toUpperCase(); - String name = Arrays.stream(args).skip(2).collect(Collectors.joining(" ")); - create(player, name, material); - break; - case "own": - openInventory(player, false, true); - break; - - case "unreleased": - if (SteamwarUser.get(player.getUniqueId()).getUserGroup().isTeamGroup()) { - openInventory(player, false, false); - break; - } - default: - Message.send("TUTORIAL_CREATE_HELP", player); - Message.send("TUTORIAL_OWN_HELP", player); - } + if(args.length == 0) { + openInventory(player, true, false); return; } - openInventory(player, true, false); + switch(args[0].toLowerCase()) { + case "rate": + if(args.length < 2) { + player.chat("/tutorial rate"); + return; + } + + int id; + try { + id = Integer.parseInt(args[1]); + } catch (NumberFormatException e) { + BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with non number: " + args[1]); + return; + } + + Tutorial tutorial = Tutorial.get(id); + if(tutorial == null) { + BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with nonexistent id: " + id); + return; + } + + rate(player, tutorial); + break; + case "create": + if(args.length < 3) { + Message.send("TUTORIAL_CREATE_HELP", player); + return; + } + + create(player, Arrays.stream(args).skip(2).collect(Collectors.joining(" ")), args[1].toUpperCase()); + break; + case "own": + openInventory(player, false, true); + break; + case "unreleased": + if (SteamwarUser.get(player.getUniqueId()).getUserGroup().isTeamGroup()) { + openInventory(player, false, false); + break; + } + default: + Message.send("TUTORIAL_CREATE_HELP", player); + Message.send("TUTORIAL_OWN_HELP", player); + } } private void openInventory(ProxiedPlayer player, boolean released, boolean own) { @@ -111,6 +109,11 @@ public class TutorialCommand extends BasicCommand { (click, tutorial) -> { if(!released && click.isShiftClick() && user.getUserGroup().isTeamGroup() && user != tutorial.creator()) { tutorial.release(); + openInventory(player, released, own); + return; + } else if(own && click.isShiftClick() && click.isRightClick()) { + tutorial.delete(); + openInventory(player, released, own); return; } @@ -120,16 +123,20 @@ public class TutorialCommand extends BasicCommand { ).open(); } - private SWItem getTutorialItem(ProxiedPlayer player, Tutorial tutorial, boolean highlightReleased) { + private SWItem getTutorialItem(ProxiedPlayer player, Tutorial tutorial, boolean personalHighlights) { SWItem item = new SWItem(tutorial.item(), Message.parse("TUTORIAL_NAME", player, tutorial.name())); item.setHideAttributes(true); List lore = new ArrayList<>(); lore.add(Message.parse("TUTORIAL_BY", player, tutorial.creator().getUserName())); lore.add(Message.parse("TUTORIAL_STARS", player, tutorial.stars())); + + if (personalHighlights) + lore.add(Message.parse("TUTORIAL_DELETE", player)); + item.setLore(lore); - if (highlightReleased && tutorial.released()) + if (personalHighlights && tutorial.released()) item.setEnchanted(true); return item; @@ -156,19 +163,14 @@ public class TutorialCommand extends BasicCommand { } subserver.execute("save-all"); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - BungeeCore.get().getLogger().log(Level.WARNING, "Tutorial creation interrupted", e); - Thread.currentThread().interrupt(); - } + ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), () -> { + Tutorial tutorial = Tutorial.create(user, name, item); + File tutorialWorld = tutorial.world(); - Tutorial tutorial = Tutorial.create(user, name, item); - File tutorialWorld = new File(ServerStarter.TUTORIAL_PATH, String.valueOf(tutorial.id())); - - if (tutorialWorld.exists()) - SubserverSystem.deleteFolder(Node.local, tutorialWorld.getPath()); - ServerStarter.copyWorld(Node.local, tempWorld.getPath(), tutorialWorld.getPath()); - Message.send("TUTORIAL_CREATED", player); + if (tutorialWorld.exists()) + SubserverSystem.deleteFolder(Node.local, tutorialWorld.getPath()); + ServerStarter.copyWorld(Node.local, tempWorld.getPath(), tutorialWorld.getPath()); + Message.send("TUTORIAL_CREATED", player); + }, 1, TimeUnit.SECONDS); } } diff --git a/src/de/steamwar/bungeecore/sql/Tutorial.java b/src/de/steamwar/bungeecore/sql/Tutorial.java index 4bbe7c43..75916f2e 100644 --- a/src/de/steamwar/bungeecore/sql/Tutorial.java +++ b/src/de/steamwar/bungeecore/sql/Tutorial.java @@ -19,6 +19,11 @@ package de.steamwar.bungeecore.sql; +import de.steamwar.bungeecore.Node; +import de.steamwar.bungeecore.ServerStarter; +import de.steamwar.bungeecore.SubserverSystem; + +import java.io.File; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -34,6 +39,7 @@ public class Tutorial { private static final Statement rate = new Statement("INSERT INTO TutorialRating (TutorialID, UserID, Stars) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Stars = VALUES(Stars)"); private static final Statement create = new Statement("INSERT INTO Tutorial (Creator, Name, Item) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Item = VALUES(Item), Released = 0"); private static final Statement release = new Statement("UPDATE Tutorial SET Released = 1 WHERE TutorialID = ?"); + private static final Statement delete = new Statement("DELETE FROM Tutorial WHERE TutorialID = ?"); public static List getPage(int page, int elementsPerPage, boolean released) { List tutorials = by_popularity.select(rs -> { @@ -116,6 +122,15 @@ public class Tutorial { release.update(id); } + public void delete() { + delete.update(id); + SubserverSystem.deleteFolder(Node.local, world().getPath()); + } + + public File world() { + return new File(ServerStarter.TUTORIAL_PATH, String.valueOf(id)); + } + public void rate(SteamwarUser user, int rating) { rate.update(id, user.getId(), rating); } diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index 4d66ebb2..19226ef8 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -340,6 +340,7 @@ TUTORIAL_BY=§8von §7{0} TUTORIAL_STARS=§e{0} §7Sterne TUTORIAL_RATE_TITLE=Tutorial bewerten TUTORIAL_RATE=§e{0} §7Stern(e) +TUTORIAL_DELETE=§cMit Shift+Rechtsklick löschen TUTORIAL_CREATE_HELP=§8/§7tutorial create §8[§eMaterial§8] §8[§eName§8] TUTORIAL_CREATE_MISSING=§cEin Tutorial kann nur von einem Tutorialserver aus erstellt werden! TUTORIAL_CREATED=§7Das Tutorial wurde erstellt§8.