From e27fbaf9dec39502635575d19b7e29ab1ec8a0bf Mon Sep 17 00:00:00 2001 From: Paldiu Date: Tue, 9 Feb 2021 18:13:30 -0600 Subject: [PATCH] Updated. --- .../paldiu/simplexcore/banning/Ban.java | 81 +++++++++++++ .../simplexcore/banning/BanFactory.java | 109 ++++++++++++++++++ .../paldiu/simplexcore/banning/BanType.java | 26 +++++ .../paldiu/simplexcore/banning/IBan.java | 21 ++++ .../paldiu/simplexcore/chat/Messages.java | 4 +- .../paldiu/simplexcore/utils/Utilities.java | 19 ++- 6 files changed, 254 insertions(+), 6 deletions(-) create mode 100644 src/main/java/io/github/paldiu/simplexcore/banning/Ban.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/banning/BanFactory.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/banning/BanType.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/banning/IBan.java diff --git a/src/main/java/io/github/paldiu/simplexcore/banning/Ban.java b/src/main/java/io/github/paldiu/simplexcore/banning/Ban.java new file mode 100644 index 0000000..e708452 --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/banning/Ban.java @@ -0,0 +1,81 @@ +package io.github.paldiu.simplexcore.banning; + +import io.github.paldiu.simplexcore.chat.Messages; +import io.github.paldiu.simplexcore.config.Yaml; +import io.github.paldiu.simplexcore.config.YamlFactory; +import io.github.paldiu.simplexcore.utils.Constants; +import io.github.paldiu.simplexcore.utils.Utilities; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; + +import java.io.File; +import java.io.IOException; +import java.util.Date; + +/** + * This class provides a way for you to handle your own banning. + * Simply extend this class and create a new instance of the subclass. + * Alternatively, you may use the + * + * @link BanFactory + * Use this in synchrony with SimplexListener to process bans on player login/join. + * Use this in synchrony with YamlFactory to create a new yaml file to store your bans, or to create an individual yaml file per user ban. + */ +public abstract class Ban implements IBan { + private final Player player; + private final CommandSender sender; + private final BanType type; + private final Date banDate; + private final long banDuration; + + private final String banId; + private final String banReason; + + public Ban(Player player, CommandSender sender) { + this(player, sender, BanType.TEMPORARY); + } + + public Ban(Player player, CommandSender sender, BanType type) { + this(player, sender, type, Constants.getTimeValues().DAY()); + } + + public Ban(Player player, CommandSender sender, BanType type, long banDuration) { + this(player, sender, type, Utilities.generateBanId(type), Messages.BAN.getMessage(), new Date(), banDuration); + } + + public Ban(Player player, CommandSender sender, BanType type, String banId, String banReason, Date banDate, long banDuration) { + this.player = player; + this.sender = sender; + this.type = type; + this.banId = banId; + this.banReason = banReason; + this.banDuration = banDuration; + this.banDate = banDate; + } + + public void writeToFile(boolean separateFiles) { + File fileLocation = new File(Constants.getPlugin().getDataFolder(), "bans"); + + if (separateFiles) { + Yaml yaml = new YamlFactory(Constants.getPlugin()).setPathways(null, fileLocation, player.getName() + ".yml"); + yaml.getConfig().createSection(getOffender().toString()); + ConfigurationSection section = yaml.getConfigurationSection(getOffender().toString()); + section.set("name", player.getName()); + section.set("ban_id", banId); + section.set("sender", sender.getName()); + section.set("reason", banReason); + section.set("duration", banDuration); + section.set("date", banDate.getTime()); + section.set("type", type.toString()); + try { + yaml.save(); + } catch (IOException e) { + Constants.getLogger().severe(e.getMessage()); + } + yaml.reload(); + } else { + // TODO: Write to a single file as separate sections per UUID. + } + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/banning/BanFactory.java b/src/main/java/io/github/paldiu/simplexcore/banning/BanFactory.java new file mode 100644 index 0000000..0250f4a --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/banning/BanFactory.java @@ -0,0 +1,109 @@ +package io.github.paldiu.simplexcore.banning; + +import io.github.paldiu.simplexcore.chat.Messages; +import io.github.paldiu.simplexcore.functional.Guard; +import io.github.paldiu.simplexcore.utils.Constants; +import io.github.paldiu.simplexcore.utils.Utilities; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.Date; +import java.util.UUID; + +public final class BanFactory { + private final Player player; + private final CommandSender sender; + private final Date banDate; + private final BanType type; + private final String banId; + private String banReason; + private long banDuration; + + private BanFactory(Player player, CommandSender sender, Date banDate, BanType type) { + this.player = player; + this.sender = sender; + this.banDate = banDate; + this.type = type; + + this.banReason = Messages.BAN.getMessage(); + assignBanDuration().verify(); + + banId = createBanId(); + } + + public static BanFactory define(Player player, CommandSender sender, Date banDate, BanType type) { + return new BanFactory(player, sender, banDate, type); + } + + /** + * The values here are optional to define. They are defined by default, and this does not need to be used. + * + * @param banDuration The duration of the ban. Use Constants.getTimeValues() for respective amounts of time. + * @param banReason The reason for the ban. By default, this uses Messages#BAN for the message. + * @return The current instance of BanFactory. + */ + public BanFactory defineOptional(long banDuration, String banReason) { + this.banDuration = banDuration; + this.banReason = banReason; + return this; + } + + public Ban create() { + return new Ban(player, sender, type, banDuration) { + @Override + public UUID getOffender() { + return player.getUniqueId(); + } + + @Override + public String getSender() { + return sender.getName(); + } + + @Override + public String getBanReason() { + return banReason; + } + + @Override + public String getBanId() { + return banId; + } + + @Override + public Date getDate() { + return banDate; + } + + @Override + public long getBanDuration() { + return banDuration; + } + + @Override + public BanType getBanType() { + return type; + } + }; + } + + public void deleteBan(IBan ban) { + + } + + private Guard assignBanDuration() { + return () -> { + if (type.equals(BanType.PERMANENT)) { + banDuration = Constants.getTimeValues().YEAR() * 99; + } else if (type.equals(BanType.TEMPORARY)) { + banDuration = Constants.getTimeValues().DAY(); + } else { + banDuration = Constants.getTimeValues().MINUTE() * 5; + } + }; + } + + private String createBanId() { + return Utilities.generateBanId(type); + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/banning/BanType.java b/src/main/java/io/github/paldiu/simplexcore/banning/BanType.java new file mode 100644 index 0000000..0a66ed8 --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/banning/BanType.java @@ -0,0 +1,26 @@ +package io.github.paldiu.simplexcore.banning; + +public enum BanType { + PERMANENT("P-"), + TEMPORARY("T-"); + + private final String prefix; + + BanType(String prefix) { + this.prefix = prefix; + } + + public String getPrefix() { + return prefix; + } + + public static String value(BanType type) { + if (type.equals(PERMANENT)) { + return "Permanent"; + } else if (type.equals(TEMPORARY)) { + return "Temporary"; + } else { + return "Unknown"; + } + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/banning/IBan.java b/src/main/java/io/github/paldiu/simplexcore/banning/IBan.java new file mode 100644 index 0000000..fce08ed --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/banning/IBan.java @@ -0,0 +1,21 @@ +package io.github.paldiu.simplexcore.banning; + +import java.util.Date; +import java.util.SplittableRandom; +import java.util.UUID; + +public interface IBan { + UUID getOffender(); + + String getSender(); + + String getBanReason(); + + String getBanId(); + + Date getDate(); + + long getBanDuration(); + + BanType getBanType(); +} diff --git a/src/main/java/io/github/paldiu/simplexcore/chat/Messages.java b/src/main/java/io/github/paldiu/simplexcore/chat/Messages.java index d0c7eef..66c6785 100644 --- a/src/main/java/io/github/paldiu/simplexcore/chat/Messages.java +++ b/src/main/java/io/github/paldiu/simplexcore/chat/Messages.java @@ -2,8 +2,8 @@ package io.github.paldiu.simplexcore.chat; public enum Messages { NO_PERMS("You do not have permission to use this command!"), - DISCORD("https://discord.gg/"), - BAN("You have been permanently banned from this server."), + DISCORD("https://discord.gg/Rumx5dTJuf"), + BAN("You have been banned from this server."), KICK("You have been kicked by a moderator."), AFK_KICK("You were kicked to ensure space for active players."); diff --git a/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java b/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java index 1819b7c..82deefd 100644 --- a/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java +++ b/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java @@ -1,12 +1,10 @@ package io.github.paldiu.simplexcore.utils; +import io.github.paldiu.simplexcore.banning.BanType; import io.github.paldiu.simplexcore.functional.Guard; import io.github.paldiu.simplexcore.functional.Validate; -import java.util.Arrays; -import java.util.Collection; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -32,4 +30,17 @@ public final class Utilities { public static void mapFE(Map map, BiConsumer actions) { map.forEach(actions); } + + public static String generateBanId(BanType type) { + String charList = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; + int length = charList.length(); + + StringBuilder sb = new StringBuilder(); + SplittableRandom random = new SplittableRandom(); + + for (int x = 0; x <= 8; x++) { + sb.append(charList.indexOf(random.nextInt(length - 1))); + } + return sb.toString(); + } }