SteamWar/BungeeCore
Archiviert
13
2

Add TokensCommand
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
Chaoscaot 2023-12-12 23:21:16 +01:00
Ursprung 3d67919f0a
Commit b024d24a93
5 geänderte Dateien mit 145 neuen und 2 gelöschten Zeilen

@ -1 +1 @@
Subproject commit e664c6cf4e3e9a056918cf15030c247e7bc6fe19
Subproject commit b66c18b8b3f1faa3fe97fd8c3f16ce19259dc0d2

Datei anzeigen

@ -155,6 +155,7 @@ public class BungeeCore extends Plugin {
new LocalCommand();
new SetLocaleCommand();
new BuilderCloudCommand();
new TokensCommand();
new ModCommand();

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Token> tokens = Token.listUser(user);
List<SWListInv.SWListEntry<Token>> entries = tokens
.stream()
.map(token -> new SWListInv.SWListEntry<>(new SWItem("STONE", token.getName()), token))
.collect(Collectors.toList());
SWListInv<Token> 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));
}
}

Datei anzeigen

@ -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

Datei anzeigen

@ -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!
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!