mirror of
https://github.com/SimplexDevelopment/SimplexCore.git
synced 2024-12-22 08:37:37 +00:00
Updated.
This commit is contained in:
parent
d645e818e9
commit
e27fbaf9de
81
src/main/java/io/github/paldiu/simplexcore/banning/Ban.java
Normal file
81
src/main/java/io/github/paldiu/simplexcore/banning/Ban.java
Normal file
@ -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.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
21
src/main/java/io/github/paldiu/simplexcore/banning/IBan.java
Normal file
21
src/main/java/io/github/paldiu/simplexcore/banning/IBan.java
Normal file
@ -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();
|
||||||
|
}
|
@ -2,8 +2,8 @@ package io.github.paldiu.simplexcore.chat;
|
|||||||
|
|
||||||
public enum Messages {
|
public enum Messages {
|
||||||
NO_PERMS("You do not have permission to use this command!"),
|
NO_PERMS("You do not have permission to use this command!"),
|
||||||
DISCORD("https://discord.gg/"),
|
DISCORD("https://discord.gg/Rumx5dTJuf"),
|
||||||
BAN("You have been permanently banned from this server."),
|
BAN("You have been banned from this server."),
|
||||||
KICK("You have been kicked by a moderator."),
|
KICK("You have been kicked by a moderator."),
|
||||||
AFK_KICK("You were kicked to ensure space for active players.");
|
AFK_KICK("You were kicked to ensure space for active players.");
|
||||||
|
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
package io.github.paldiu.simplexcore.utils;
|
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.Guard;
|
||||||
import io.github.paldiu.simplexcore.functional.Validate;
|
import io.github.paldiu.simplexcore.functional.Validate;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -32,4 +30,17 @@ public final class Utilities {
|
|||||||
public static <K, V> void mapFE(Map<K, V> map, BiConsumer<K, V> actions) {
|
public static <K, V> void mapFE(Map<K, V> map, BiConsumer<K, V> actions) {
|
||||||
map.forEach(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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user