UltimateStacker/src/main/java/com/songoda/ultimatestacker/commands/CommandRemoveAll.java

98 lines
3.4 KiB
Java
Raw Normal View History

2019-09-03 22:38:00 +02:00
package com.songoda.ultimatestacker.commands;
2018-11-10 21:54:41 +01:00
2019-09-03 22:38:00 +02:00
import com.songoda.core.commands.AbstractCommand;
import com.songoda.core.utils.TextUtils;
2018-11-10 21:54:41 +01:00
import com.songoda.ultimatestacker.UltimateStacker;
2020-08-25 01:01:11 +02:00
import com.songoda.ultimatestacker.stackable.entity.EntityStackManager;
2019-01-10 21:27:01 +01:00
import com.songoda.ultimatestacker.utils.Methods;
2018-11-10 21:54:41 +01:00
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
2020-08-25 01:01:11 +02:00
import org.bukkit.entity.LivingEntity;
2018-11-10 21:54:41 +01:00
import org.bukkit.entity.Player;
2019-08-19 17:08:33 +02:00
import java.util.Arrays;
import java.util.Collections;
2019-08-19 17:07:10 +02:00
import java.util.List;
2018-11-10 21:54:41 +01:00
public class CommandRemoveAll extends AbstractCommand {
2019-09-03 22:38:00 +02:00
UltimateStacker instance;
public CommandRemoveAll() {
super(false, "removeall");
instance = UltimateStacker.getInstance();
2018-11-10 21:54:41 +01:00
}
@Override
2019-09-03 22:38:00 +02:00
protected ReturnType runCommand(CommandSender sender, String... args) {
if (args.length < 1) return ReturnType.SYNTAX_ERROR;
2018-11-10 21:54:41 +01:00
2019-09-03 22:38:00 +02:00
String type = args[0];
2018-11-10 21:54:41 +01:00
2019-09-03 22:38:00 +02:00
boolean all = args.length == 2;
2018-11-10 21:54:41 +01:00
if (!type.equalsIgnoreCase("entities")
&& !type.equalsIgnoreCase("items")) {
return ReturnType.SYNTAX_ERROR;
}
int amountRemoved = 0;
EntityStackManager stackManager = instance.getEntityStackManager();
for (World world : Bukkit.getWorlds()) {
for (Entity entityO : world.getEntities()) {
2020-08-25 01:01:11 +02:00
if (entityO instanceof Player || !(entityO instanceof LivingEntity)) continue;
2018-11-10 21:54:41 +01:00
2020-08-25 01:01:11 +02:00
if (entityO.getType() != EntityType.DROPPED_ITEM && (stackManager.isStackedAndLoaded((LivingEntity)entityO) || all) && type.equalsIgnoreCase("entities")) {
entityO.remove();
amountRemoved++;
} else if (entityO.getType() == EntityType.DROPPED_ITEM && type.equalsIgnoreCase("items")) {
2019-09-03 22:38:00 +02:00
if (entityO.isCustomNameVisible() && !entityO.getCustomName().contains(TextUtils.convertToInvisibleString("IS")) || all)
2018-11-10 21:54:41 +01:00
continue;
entityO.remove();
amountRemoved++;
}
2018-11-10 21:54:41 +01:00
}
}
if (type.equalsIgnoreCase("entities") && amountRemoved == 1) type = "Entity";
if (type.equalsIgnoreCase("items") && amountRemoved == 1) type = "Item";
if (amountRemoved == 0) {
2019-09-03 22:38:00 +02:00
instance.getLocale().newMessage("&7No" + (all ? " " : " stacked ")
+ type + " exist that could be removed.").sendPrefixedMessage(sender);
2018-11-10 21:54:41 +01:00
} else {
2019-09-03 22:38:00 +02:00
instance.getLocale().newMessage("&7Removed &6" + amountRemoved + (all ? " " : " stacked ")
+ Methods.formatText(type.toLowerCase(), true) + " &7Successfully.").sendPrefixedMessage(sender);
2018-11-10 21:54:41 +01:00
}
return ReturnType.SUCCESS;
}
2019-08-19 17:07:10 +02:00
@Override
2019-09-03 22:38:00 +02:00
protected List<String> onTab(CommandSender sender, String... args) {
if (args.length == 1)
return Arrays.asList("entities", "items");
2019-09-03 22:38:00 +02:00
else if (args.length == 2)
return Collections.singletonList("all");
return null;
2019-08-19 17:07:10 +02:00
}
2018-11-10 21:54:41 +01:00
@Override
public String getPermissionNode() {
return "ultimatestacker.admin";
}
@Override
public String getSyntax() {
2020-06-28 04:12:13 +02:00
return "removeall <entities/items> [all]";
2018-11-10 21:54:41 +01:00
}
@Override
public String getDescription() {
return "Remove all stacked entites or items from the world.";
}
2019-09-03 22:38:00 +02:00
2018-11-10 21:54:41 +01:00
}