From b024d24a93865655b20de1118a42f90c4e7c482d Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 12 Dec 2023 23:21:16 +0100 Subject: [PATCH] Add TokensCommand --- CommonCore | 2 +- src/de/steamwar/bungeecore/BungeeCore.java | 1 + .../bungeecore/commands/TokensCommand.java | 116 ++++++++++++++++++ .../steamwar/messages/BungeeCore.properties | 13 ++ .../messages/BungeeCore_de.properties | 15 ++- 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 src/de/steamwar/bungeecore/commands/TokensCommand.java diff --git a/CommonCore b/CommonCore index e664c6c..b66c18b 160000 --- a/CommonCore +++ b/CommonCore @@ -1 +1 @@ -Subproject commit e664c6cf4e3e9a056918cf15030c247e7bc6fe19 +Subproject commit b66c18b8b3f1faa3fe97fd8c3f16ce19259dc0d2 diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 643921c..f389b65 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -155,6 +155,7 @@ public class BungeeCore extends Plugin { new LocalCommand(); new SetLocaleCommand(); new BuilderCloudCommand(); + new TokensCommand(); new ModCommand(); diff --git a/src/de/steamwar/bungeecore/commands/TokensCommand.java b/src/de/steamwar/bungeecore/commands/TokensCommand.java new file mode 100644 index 0000000..00e01a9 --- /dev/null +++ b/src/de/steamwar/bungeecore/commands/TokensCommand.java @@ -0,0 +1,116 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bungeecore.commands; + +import de.steamwar.bungeecore.BungeeCore; +import de.steamwar.bungeecore.Message; +import de.steamwar.bungeecore.inventory.SWInventory; +import de.steamwar.bungeecore.inventory.SWItem; +import de.steamwar.bungeecore.inventory.SWListInv; +import de.steamwar.command.SWCommand; +import de.steamwar.sql.SteamwarUser; +import de.steamwar.sql.Token; +import lombok.SneakyThrows; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.security.MessageDigest; +import java.util.Base64; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; + +public class TokensCommand extends SWCommand { + public TokensCommand() { + super("tokens", null, "token"); + } + + private static final Random random = new Random(); + + @SneakyThrows + private static String generateCode() { + byte[] hash = new byte[16]; + for(int i = 0; i < hash.length; i++) { + hash[i] = (byte) (random.nextInt(256) - 128); + } + return Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-1").digest(hash)); + } + + @SneakyThrows + private static String generateHash(String code) { + MessageDigest md = MessageDigest.getInstance("SHA-512"); + return Base64.getEncoder().encodeToString(md.digest(code.getBytes())); + } + + @Register + public void genericCommand(ProxiedPlayer p) { + SteamwarUser user = SteamwarUser.get(p.getUniqueId()); + List tokens = Token.listUser(user); + + List> entries = tokens + .stream() + .map(token -> new SWListInv.SWListEntry<>(new SWItem("STONE", token.getName()), token)) + .collect(Collectors.toList()); + + SWListInv inv = new SWListInv<>(p, Message.parse("TOKENS_TITLE", p), entries, (click, element) -> { + SWInventory deleteInv = new SWInventory(p, 9, Message.parse("TOKENS_DELETE", p)); + deleteInv.addItem(0, new SWItem(Message.parse("TOKENS_DELETE_CONFIRM", p), 2), deleteClick -> { + Token.delete(element); + Message.send("TOKENS_DELETED", p); + deleteInv.close(); + }); + + deleteInv.addItem(8, new SWItem(Message.parse("TOKENS_DELETE_CANCEL", p), 1), deleteClick -> { + deleteInv.close(); + }); + + deleteInv.open(); + }); + + inv.open(); + } + + @Register("create") + public void createToken(ProxiedPlayer p, String name) { + SteamwarUser user = SteamwarUser.get(p.getUniqueId()); + + if(name.length() > 32) { + Message.send("TOKENS_NAME_TOO_LONG", p); + return; + } + + if(name.length() < 3) { + Message.send("TOKENS_NAME_TOO_SHORT", p); + return; + } + + if(Token.listUser(user).stream().anyMatch(token -> token.getName().equalsIgnoreCase(name))) { + Message.send("TOKENS_NAME_ALREADY_EXISTS", p); + return; + } + + String code = generateCode(); + String hash = generateHash(code); + + Token token = Token.create(name, user, hash); + + Message.send("TOKENS_NAME_CREATED", p, "TOKENS_NAME_CREATED_HOVER", new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, code)); + } +} diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index 0575535..e9f0c92 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -702,3 +702,16 @@ MOD_FORBIDDEN=§eForbidden MOD_AUTOBAN=§cAutoban MOD_YT=§5YT Only MOD_ITEM_BACK=§7Back + +#Tokens Command +TOKENS_TITLE=Tokens +TOKENS_DELETE=Delete Token +TOKENS_DELETE_CONFIRM=§aConfirm +TOKENS_DELETE_CANCEL=§cCancel +TOKENS_DELETED=§aThe token was successfully deleted. + +TOKENS_NAME_TOO_LONG=§cThe name cannot be longer than 32 characters. +TOKENS_NAME_TOO_SHORT=§cThe name cannot be shorter than 3 characters. +TOKENS_NAME_ALREADY_EXISTS=§cA token with this name already exists. +TOKENS_NAME_CREATED=§aThe token was successfully created. +TOKENS_NAME_CREATED_HOVER=§eClick to copy \ No newline at end of file diff --git a/src/de/steamwar/messages/BungeeCore_de.properties b/src/de/steamwar/messages/BungeeCore_de.properties index 7e5566a..ab99180 100644 --- a/src/de/steamwar/messages/BungeeCore_de.properties +++ b/src/de/steamwar/messages/BungeeCore_de.properties @@ -657,4 +657,17 @@ ADVENT_CALENDAR_TITLE=§eAdventskalender ADVENT_CALENDAR_DAY=§7Tag§8: §e{0} ADVENT_CALENDAR_MESSAGE=§eHast du heute schon dein Geschenk geholt? ADVENT_CALENDAR_MESSAGE_HOVER=§eKlicken zum öffnen! -ADVENT_CALENDAR_OPEN=§7Du hast §e{0}§7 aus dem Adventskalender erhalten! \ No newline at end of file +ADVENT_CALENDAR_OPEN=§7Du hast §e{0}§7 aus dem Adventskalender erhalten! + +# Tokens Command +TOKENS_TITLE=Tokens +TOKENS_DELETE=Token löschen +TOKENS_DELETE_CONFIRM=§aLöschen +TOKENS_DELETE_CANCEL=§cAbbrechen +TOKENS_DELETED=§aDer Token wurde gelöscht. + +TOKENS_NAME_TOO_LONG=§cDer Name darf nicht länger als 32 Zeichen sein. +TOKENS_NAME_TOO_SHORT=§cDer Name muss mindestens 3 Zeichen lang sein. +TOKENS_NAME_ALREADY_EXISTS=§cDer Name existiert bereits. +TOKENS_NAME_CREATED=§aDer Token wurde erstellt. +TOKENS_NAME_CREATED_HOVER=§eKlicke zum kopieren! \ No newline at end of file