This commit is contained in:
Telesphoreo 2022-04-06 18:01:34 -05:00
commit 035b579fc1
4 changed files with 122 additions and 3 deletions

View File

@ -0,0 +1,99 @@
package dev.plex.command.impl;
import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.source.RequiredCommandSource;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@CommandPermissions(level = Rank.ADMIN, permission = "plex.entitywipe", source = RequiredCommandSource.ANY)
@CommandParameters(name = "entitywipe", description = "Remove various server entities that may cause lag, such as dropped items, minecarts, and boats.", usage = "/<command> [name | -a]", aliases = "ew,rd")
public class EntityWipeCMD extends PlexCommand
{
@Override
protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args)
{
List<String> entityBlacklist = plugin.config.getStringList("entitywipe_list");
List<String> entityWhitelist = new LinkedList<>(Arrays.asList(args));
EntityType[] entityTypes = EntityType.values();
entityWhitelist.removeIf(name -> {
boolean res = Arrays.stream(entityTypes).noneMatch(entityType -> entityType.name().equalsIgnoreCase(name));
if (res)
{
sender.sendMessage(messageComponent("invalidEntityType", name));
}
return res;
});
boolean useBlacklist = args.length == 0;
HashMap<String, Integer> entityCounts = new HashMap<>();
for (World world : Bukkit.getWorlds())
{
for (Entity entity : world.getEntities())
{
if (entity.getType() != EntityType.PLAYER)
{
String type = entity.getType().name();
if (useBlacklist ? entityBlacklist.stream().noneMatch(entityName -> entityName.equalsIgnoreCase(type)) : entityWhitelist.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(type)))
{
Location loc = entity.getLocation();
loc.setY(-500);
entity.teleportAsync(loc);
entity.remove();
if (!entityCounts.containsKey(type))
{
entityCounts.put(type,0);
}
entityCounts.put(type,entityCounts.get(type)+1);
}
}
}
}
int entityCount = entityCounts.values().stream().mapToInt(a -> a).sum();
if (useBlacklist)
{
PlexUtils.broadcast(messageComponent("removedEntities", sender.getName(), entityCount));
}
else
{
if (entityCount == 0)
{
sender.sendMessage(messageComponent("noRemovedEntities"));
return null;
}
String list = String.join(", ", entityCounts.keySet());
list = list.replaceAll("(, )(?!.*\1)", (list.indexOf(", ") == list.lastIndexOf(", ") ? "" : ",") + " and ");
PlexUtils.broadcast(messageComponent("removedEntitiesOfTypes", sender.getName(), entityCount, list));
}
entityCounts.forEach((entityName, numRemoved) -> {
sender.sendMessage(messageComponent("removedEntitiesOfType", sender.getName(), numRemoved, entityName));
});
return null;
}
}

View File

@ -33,6 +33,7 @@ import java.util.stream.Collectors;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -133,7 +134,7 @@ public class PlexUtils extends PlexBase
public static Component messageComponent(String entry, Object... objects)
{
return MiniMessage.miniMessage().deserialize(LegacyComponentSerializer.legacySection().serialize(LegacyComponentSerializer.legacyAmpersand().deserialize(messageString(entry, objects))));
return MiniMessage.miniMessage().deserialize(messageString(entry, objects));
}
public static String messageString(String entry, Object... objects)
@ -149,7 +150,7 @@ public class PlexUtils extends PlexBase
}*/
for (int i = 0; i < objects.length; i++)
{
f = f.replace("{" + i + "}", String.valueOf(objects[i]));
f = f.replace("{" + i + "}", PlainTextComponentSerializer.plainText().serialize(MiniMessage.miniMessage().deserialize(String.valueOf(objects[i]))));
}
return f;
}

View File

@ -62,6 +62,11 @@ global_gamerules:
- "showDeathMessages;false"
- "sendCommandFeedback;false"
# Mob limiter/Entity wiping config
# All entities listed here will NOT be wiped upon wiping entities
entitywipe_list:
- "item_frame"
worlds:
flatlands:
name: "Flatlands"

View File

@ -143,3 +143,17 @@ yourEditsHaveBeenUnblocked: "<gray>Your block modification abilities have been r
editsBlocked: "<gray>Blocked block modification abilities for {0}"
# 0 - The player name
editsUnblocked: "<gray>Unblocked block modification abilities for {0}"
# 0 - The command sender
# 1 - Number of entities removed
removedEntities: "<red>{0} - Removed {1} entities"
# 0 - The command sender
# 1 - Number of entities removed
# 2 - Entity type(s) removed
removedEntitiesOfTypes: "<red>{0} - Removed {1} entities of type(s) {2}"
# 0 - The command sender
# 1 - Number of entities removed
# 2 - Entity type removed
removedEntitiesOfType: "<gray>Removed {1} {2}"
# 0 - Entity type that is invalid
invalidEntityType: "<gray>Notice: Entity type {0} is invalid!"
noRemovedEntities: "<gray>No entities were removed."