Reduce code duplication

This commit is contained in:
Telesphoreo 2022-05-10 00:00:48 -05:00
parent 9284ec3409
commit 770fe65f98
No known key found for this signature in database
GPG Key ID: 5ACFFC4682CF849B
4 changed files with 24 additions and 32 deletions

View File

@ -12,8 +12,11 @@ import dev.plex.player.PlexPlayer;
import dev.plex.punishment.Punishment; import dev.plex.punishment.Punishment;
import dev.plex.punishment.PunishmentType; import dev.plex.punishment.PunishmentType;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.util.*; import dev.plex.util.BungeeUtil;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import dev.plex.util.TimeUtils;
import dev.plex.util.WebUtils;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.List; import java.util.List;
@ -97,13 +100,8 @@ public class BanCMD extends PlexCommand
Bukkit.getScheduler().runTask(Plex.get(), () -> Bukkit.getScheduler().runTask(Plex.get(), () ->
{ {
if (player != null) if (player != null)
{
if (BungeeUtil.isBungeeCord() || BungeeUtil.isVelocity())
{ {
BungeeUtil.kickPlayer(player, Punishment.generateBanMessage(punishment)); BungeeUtil.kickPlayer(player, Punishment.generateBanMessage(punishment));
} else {
player.kick(Punishment.generateBanMessage(punishment));
}
} }
}); });
PlexLog.debug("(From /ban command) PunishedPlayer UUID: " + plexPlayer.getUuid()); PlexLog.debug("(From /ban command) PunishedPlayer UUID: " + plexPlayer.getUuid());

View File

@ -67,13 +67,7 @@ public class KickCMD extends PlexCommand
punishment.setIp(player.getAddress().getAddress().getHostAddress().trim()); punishment.setIp(player.getAddress().getAddress().getHostAddress().trim());
plugin.getPunishmentManager().punish(plexPlayer, punishment); plugin.getPunishmentManager().punish(plexPlayer, punishment);
PlexUtils.broadcast(messageComponent("kickedPlayer", sender.getName(), plexPlayer.getName())); PlexUtils.broadcast(messageComponent("kickedPlayer", sender.getName(), plexPlayer.getName()));
if (BungeeUtil.isBungeeCord() || BungeeUtil.isVelocity())
{
BungeeUtil.kickPlayer(player, Punishment.generateBanMessage(punishment)); BungeeUtil.kickPlayer(player, Punishment.generateBanMessage(punishment));
} else
{
player.kick(Punishment.generateBanMessage(punishment));
}
return null; return null;
} }
} }

View File

@ -15,6 +15,8 @@ import dev.plex.util.BungeeUtil;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import dev.plex.util.TimeUtils; import dev.plex.util.TimeUtils;
import dev.plex.util.WebUtils; import dev.plex.util.WebUtils;
import java.util.List;
import java.util.UUID;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -23,9 +25,6 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.UUID;
@CommandParameters(name = "tempban", usage = "/<command> <player> <time> [reason]", description = "Temporarily ban a player") @CommandParameters(name = "tempban", usage = "/<command> <player> <time> [reason]", description = "Temporarily ban a player")
@CommandPermissions(level = Rank.ADMIN, permission = "plex.tempban", source = RequiredCommandSource.ANY) @CommandPermissions(level = Rank.ADMIN, permission = "plex.tempban", source = RequiredCommandSource.ANY)
@ -73,7 +72,8 @@ public class TempbanCMD extends PlexCommand
{ {
reason = StringUtils.join(args, " ", 2, args.length); reason = StringUtils.join(args, " ", 2, args.length);
punishment.setReason(reason); punishment.setReason(reason);
} else }
else
{ {
punishment.setReason("No reason provided."); punishment.setReason("No reason provided.");
} }
@ -88,14 +88,8 @@ public class TempbanCMD extends PlexCommand
plugin.getPunishmentManager().punish(plexPlayer, punishment); plugin.getPunishmentManager().punish(plexPlayer, punishment);
PlexUtils.broadcast(messageComponent("banningPlayer", sender.getName(), plexPlayer.getName())); PlexUtils.broadcast(messageComponent("banningPlayer", sender.getName(), plexPlayer.getName()));
if (player != null) if (player != null)
{
if (BungeeUtil.isBungeeCord() || BungeeUtil.isVelocity())
{ {
BungeeUtil.kickPlayer(player, Punishment.generateBanMessage(punishment)); BungeeUtil.kickPlayer(player, Punishment.generateBanMessage(punishment));
} else
{
player.kick(Punishment.generateBanMessage(punishment));
}
} }
return null; return null;
} }

View File

@ -4,7 +4,6 @@ import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import dev.plex.Plex; import dev.plex.Plex;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -22,6 +21,8 @@ public class BungeeUtil
} }
public static void kickPlayer(Player player, Component message) public static void kickPlayer(Player player, Component message)
{
if (isBungeeCord() || isVelocity())
{ {
ByteArrayDataOutput out = ByteStreams.newDataOutput(); ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("KickPlayer"); out.writeUTF("KickPlayer");
@ -29,4 +30,9 @@ public class BungeeUtil
out.writeUTF(LegacyComponentSerializer.legacySection().serialize(message)); out.writeUTF(LegacyComponentSerializer.legacySection().serialize(message));
player.sendPluginMessage(Plex.get(), "BungeeCord", out.toByteArray()); player.sendPluginMessage(Plex.get(), "BungeeCord", out.toByteArray());
} }
else
{
player.kick(message);
}
}
} }