Code cleanup

This commit is contained in:
Telesphoreo 2022-01-30 15:03:47 -06:00
parent d4761d4386
commit 4a05d90c17
25 changed files with 191 additions and 97 deletions

View File

@ -1,2 +1,3 @@
# Plex
A new freedom plugin.

View File

@ -61,7 +61,9 @@ public class DataUtils
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
Plex.get().getMongoPlayerData().save(plexPlayer);
} else {
}
else
{
Plex.get().getSqlPlayerData().insert(plexPlayer);
}
}

View File

@ -54,7 +54,9 @@ public class SQLPlayerData
String rankName = set.getString("rank").toUpperCase();
long coins = set.getLong("coins");
boolean vanished = set.getBoolean("vanished");
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>(){}.getType());
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>()
{
}.getType());
plexPlayer.setName(name);
plexPlayer.setLoginMSG(loginMSG);
plexPlayer.setPrefix(prefix);

View File

@ -5,6 +5,7 @@ import dev.plex.rank.enums.Rank;
public @interface CheckPermission
{
String permission() default "";
Rank rank() default Rank.IMPOSTOR;
}

View File

@ -44,7 +44,8 @@ public class NameHistoryCMD extends PlexCommand
if (offlinePlayer != null)
{
uuid = offlinePlayer.getUniqueId();
} else
}
else
{
uuid = MojangUtils.getUUID(username);
}
@ -68,7 +69,8 @@ public class NameHistoryCMD extends PlexCommand
.append(Component.text("-").color(NamedTextColor.DARK_GRAY))
.append(Component.space())
.append(Component.text(DATE_FORMAT.format(entry.getValue())).color(NamedTextColor.GOLD)));
} else
}
else
{
historyList.add(
Component.text(entry.getKey()).color(NamedTextColor.GOLD)

View File

@ -12,7 +12,6 @@ import java.util.List;
import net.kyori.adventure.text.Component;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@CommandPermissions(level = Rank.OP, permission = "plex.plex", source = RequiredCommandSource.ANY)

View File

@ -14,10 +14,7 @@ import dev.plex.util.PlexUtils;
import java.util.List;
import java.util.UUID;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@CommandParameters(name = "unban", usage = "/<command> <player>", description = "Unbans a player, offline or online")

View File

@ -1,13 +1,9 @@
package dev.plex.listener.impl;
import dev.plex.listener.PlexListener;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.jetbrains.annotations.NotNull;
public class LoginListener extends PlexListener
{

View File

@ -25,7 +25,8 @@ public class PunishedPlayerMenu extends AbstractMenu
private final List<Inventory> inventories = Lists.newArrayList();
public PunishedPlayerMenu(PunishedPlayer player) {
public PunishedPlayerMenu(PunishedPlayer player)
{
super("§c§lPunishments");
this.punishedPlayer = player;
for (int i = 0; i <= punishedPlayer.getPunishments().size() / 53; i++)
@ -53,11 +54,13 @@ public class PunishedPlayerMenu extends AbstractMenu
}
}
public List<Inventory> getInventory() {
public List<Inventory> getInventory()
{
return inventories;
}
public void openInv(Player player, int index) {
public void openInv(Player player, int index)
{
int currentItemIndex = 0;
int currentInvIndex = 0;
for (Punishment punishment : punishedPlayer.getPunishments())
@ -68,7 +71,8 @@ public class PunishedPlayerMenu extends AbstractMenu
break;
}
if (currentItemIndex == inv.getSize() - 1) {
if (currentItemIndex == inv.getSize() - 1)
{
currentInvIndex++;
currentItemIndex = 0;
inv = inventories.get(currentInvIndex);
@ -91,32 +95,59 @@ public class PunishedPlayerMenu extends AbstractMenu
@EventHandler
public void onClick(InventoryClickEvent event)
{
if (event.getClickedInventory() == null) return;
if (event.getClickedInventory() == null)
{
return;
}
Inventory inv = event.getClickedInventory();
if (!isValidInventory(inv)) return;
if (event.getCurrentItem() == null) return;
if (!isValidInventory(inv))
{
return;
}
if (event.getCurrentItem() == null)
{
return;
}
ItemStack item = event.getCurrentItem();
event.setCancelled(true);
if (item.getType() == Material.FEATHER)
{
if (item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "Next Page"))
{
if (getCurrentInventoryIndex(inv) + 1 > inventories.size() - 1) return;
if (inventories.get(getCurrentInventoryIndex(inv) + 1).getContents().length == 0) return;
openInv((Player) event.getWhoClicked(), getCurrentInventoryIndex(inv) + 1);
} else if (item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "Previous Page"))
if (getCurrentInventoryIndex(inv) + 1 > inventories.size() - 1)
{
if (getCurrentInventoryIndex(inv) - 1 < 0) return;
if (getCurrentInventoryIndex(inv) - 1 > inventories.size() - 1) return;
if (inventories.get(getCurrentInventoryIndex(inv) - 1).getContents().length == 0) return;
return;
}
if (inventories.get(getCurrentInventoryIndex(inv) + 1).getContents().length == 0)
{
return;
}
openInv((Player)event.getWhoClicked(), getCurrentInventoryIndex(inv) + 1);
}
else if (item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "Previous Page"))
{
if (getCurrentInventoryIndex(inv) - 1 < 0)
{
return;
}
if (getCurrentInventoryIndex(inv) - 1 > inventories.size() - 1)
{
return;
}
if (inventories.get(getCurrentInventoryIndex(inv) - 1).getContents().length == 0)
{
return;
}
openInv((Player)event.getWhoClicked(), getCurrentInventoryIndex(inv) - 1);
}
} else if (item.getType() == Material.BARRIER)
}
else if (item.getType() == Material.BARRIER)
{
new PunishmentMenu().openInv((Player)event.getWhoClicked(), 0);
} else if (item.getType() == Material.PLAYER_HEAD)
}
else if (item.getType() == Material.PLAYER_HEAD)
{
SkullMeta meta = (SkullMeta)item.getItemMeta();
OfflinePlayer player = meta.getOwningPlayer();

View File

@ -22,7 +22,8 @@ public class PunishmentMenu extends AbstractMenu
private List<Inventory> inventories = Lists.newArrayList();
public PunishmentMenu() {
public PunishmentMenu()
{
super("§c§lPunishments");
for (int i = 0; i <= Bukkit.getOnlinePlayers().size() / 53; i++)
{
@ -43,11 +44,13 @@ public class PunishmentMenu extends AbstractMenu
}
}
public List<Inventory> getInventory() {
public List<Inventory> getInventory()
{
return inventories;
}
public void openInv(Player player, int index) {
public void openInv(Player player, int index)
{
int currentItemIndex = 0;
int currentInvIndex = 0;
for (Player players : Bukkit.getOnlinePlayers())
@ -58,7 +61,8 @@ public class PunishmentMenu extends AbstractMenu
break;
}
if (currentItemIndex == inv.getSize() - 1) {
if (currentItemIndex == inv.getSize() - 1)
{
currentInvIndex++;
currentItemIndex = 0;
inv = inventories.get(currentInvIndex);
@ -81,29 +85,55 @@ public class PunishmentMenu extends AbstractMenu
@EventHandler
public void onClick(InventoryClickEvent event)
{
if (event.getClickedInventory() == null) return;
if (event.getClickedInventory() == null)
{
return;
}
Inventory inv = event.getClickedInventory();
if (!isValidInventory(inv)) return;
if (event.getCurrentItem() == null) return;
if (!isValidInventory(inv))
{
return;
}
if (event.getCurrentItem() == null)
{
return;
}
ItemStack item = event.getCurrentItem();
event.setCancelled(true);
if (item.getType() == Material.FEATHER)
{
if (item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "Next Page"))
{
if (getCurrentInventoryIndex(inv) + 1 > inventories.size() - 1) return;
if (inventories.get(getCurrentInventoryIndex(inv) + 1).getContents().length == 0) return;
openInv((Player) event.getWhoClicked(), getCurrentInventoryIndex(inv) + 1);
} else if (item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "Previous Page"))
if (getCurrentInventoryIndex(inv) + 1 > inventories.size() - 1)
{
if (getCurrentInventoryIndex(inv) - 1 < 0) return;
if (getCurrentInventoryIndex(inv) - 1 > inventories.size() - 1) return;
if (inventories.get(getCurrentInventoryIndex(inv) - 1).getContents().length == 0) return;
return;
}
if (inventories.get(getCurrentInventoryIndex(inv) + 1).getContents().length == 0)
{
return;
}
openInv((Player)event.getWhoClicked(), getCurrentInventoryIndex(inv) + 1);
}
else if (item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "Previous Page"))
{
if (getCurrentInventoryIndex(inv) - 1 < 0)
{
return;
}
if (getCurrentInventoryIndex(inv) - 1 > inventories.size() - 1)
{
return;
}
if (inventories.get(getCurrentInventoryIndex(inv) - 1).getContents().length == 0)
{
return;
}
openInv((Player)event.getWhoClicked(), getCurrentInventoryIndex(inv) - 1);
}
} else if (item.getType() == Material.PLAYER_HEAD)
}
else if (item.getType() == Material.PLAYER_HEAD)
{
SkullMeta meta = (SkullMeta)item.getItemMeta();
OfflinePlayer player = meta.getOwningPlayer();
@ -119,7 +149,6 @@ public class PunishmentMenu extends AbstractMenu
}
}
public int getCurrentInventoryIndex(Inventory inventory)

View File

@ -69,7 +69,8 @@ public class PlexPlayer
this.rank = "";
}
public String displayName() {
public String displayName()
{
return PlainTextComponentSerializer.plainText().serialize(player.displayName());
}

View File

@ -68,9 +68,12 @@ public class PunishedPlayer
File file = new File(folder, getUuid() + ".json");
if (!file.exists())
{
try {
try
{
file.createNewFile();
} catch (IOException e) {
}
catch (IOException e)
{
e.printStackTrace();
}
}
@ -86,24 +89,32 @@ public class PunishedPlayer
if (isNotEmpty(file))
{
try {
try
{
JSONTokener tokener = new JSONTokener(new FileInputStream(file));
JSONObject object = new JSONObject(tokener);
object.getJSONObject(getUuid()).getJSONArray("punishments").forEach(obj -> {
object.getJSONObject(getUuid()).getJSONArray("punishments").forEach(obj ->
{
Punishment punishment = Punishment.fromJson(obj.toString());
punishments.add(punishment);
});
} catch (FileNotFoundException e) {
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
return punishments;
}
private boolean isNotEmpty(File file) {
try {
private boolean isNotEmpty(File file)
{
try
{
return !FileUtils.readFileToString(file, StandardCharsets.UTF_8).trim().isEmpty();
} catch (IOException e) {
}
catch (IOException e)
{
e.printStackTrace();
}
return false;

View File

@ -29,7 +29,8 @@ public class PunishmentManager
{
File file = player.getPunishmentsFile();
try {
try
{
if (isNotEmpty(file))
{
JSONTokener tokener = new JSONTokener(new FileInputStream(file));
@ -40,7 +41,9 @@ public class PunishmentManager
writer.append(object.toString(8));
writer.flush();
writer.close();
} else {
}
else
{
JSONObject object = new JSONObject();
Map<String, List<String>> punishments = Maps.newHashMap();
@ -56,15 +59,21 @@ public class PunishmentManager
writer.close();
}
} catch (IOException e) {
}
catch (IOException e)
{
e.printStackTrace();
}
}
private boolean isNotEmpty(File file) {
try {
private boolean isNotEmpty(File file)
{
try
{
return !FileUtils.readFileToString(file, StandardCharsets.UTF_8).trim().isEmpty();
} catch (IOException e) {
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
@ -76,15 +85,18 @@ public class PunishmentManager
{
Ban ban = new Ban(punishment.getPunished(), (punishment.getPunisher() == null ? null : punishment.getPunisher()), "", punishment.getReason(), punishment.getEndDate());
Plex.get().getBanManager().executeBan(ban);
} else if (punishment.getType() == PunishmentType.FREEZE)
}
else if (punishment.getType() == PunishmentType.FREEZE)
{
player.setFrozen(true);
Date now = new Date();
Date then = punishment.getEndDate();
long seconds = TimeUnit.MILLISECONDS.toSeconds(then.getTime() - now.getTime());
new BukkitRunnable() {
new BukkitRunnable()
{
@Override
public void run() {
public void run()
{
if (!player.isFrozen())
{
this.cancel();
@ -97,16 +109,18 @@ public class PunishmentManager
}.runTaskLater(Plex.get(), 20 * seconds);
} else if (punishment.getType() == PunishmentType.MUTE)
}
else if (punishment.getType() == PunishmentType.MUTE)
{
player.setMuted(true);
Date now = new Date();
Date then = punishment.getEndDate();
long seconds = TimeUnit.MILLISECONDS.toSeconds(then.getTime() - now.getTime());
new BukkitRunnable() {
new BukkitRunnable()
{
@Override
public void run() {
public void run()
{
player.setMuted(false);
}
}.runTaskLater(Plex.get(), 20 * seconds);

View File

@ -11,11 +11,13 @@ public abstract class AbstractService implements IService
this.asynchronous = async;
}
public boolean isRepeating() {
public boolean isRepeating()
{
return repeating;
}
public boolean isAsynchronous() {
public boolean isAsynchronous()
{
return asynchronous;
}
}

View File

@ -2,7 +2,6 @@ package dev.plex.services;
import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.services.impl.BanService;
import dev.plex.services.impl.GameRuleService;
import java.util.List;
import org.bukkit.Bukkit;

View File

@ -8,12 +8,14 @@ import org.bukkit.Bukkit;
public class BanService extends AbstractService
{
public BanService() {
public BanService()
{
super(true, true);
}
@Override
public void run() {
public void run()
{
for (Ban ban : Plex.get().getBanManager().getActiveBans())
{
if (new Date().after(ban.getEndDate()))
@ -25,7 +27,8 @@ public class BanService extends AbstractService
}
@Override
public int repeatInSeconds() {
public int repeatInSeconds()
{
return 10;
}
}

View File

@ -35,7 +35,8 @@ public class MojangUtils
.insert(13, "-")
.insert(18, "-")
.insert(23, "-").toString());
} catch (IOException e)
}
catch (IOException e)
{
e.printStackTrace();
return null;
@ -63,12 +64,14 @@ public class MojangUtils
Instant instant = Instant.ofEpochMilli(dateTime);
LocalDateTime time = LocalDateTime.ofInstant(instant, ZoneId.of("America/Los_Angeles"));
names.put(name, time);
} else
}
else
{
names.put(name, null);
}
});
} catch (IOException e)
}
catch (IOException e)
{
e.printStackTrace();
}

View File

@ -14,7 +14,8 @@ public abstract class AbstractMenu implements Listener
Plex.get().getServer().getPluginManager().registerEvents(this, Plex.get());
}
public String getName() {
public String getName()
{
return name;
}
}