io.github.dailystruggle.rtp.common.commands.info.InfoCmd Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of RTP Show documentation
Show all versions of RTP Show documentation
a random teleport plugin
package io.github.dailystruggle.rtp.common.commands.info;
import io.github.dailystruggle.commandsapi.common.CommandsAPICommand;
import io.github.dailystruggle.rtp.common.RTP;
import io.github.dailystruggle.rtp.common.commands.BaseRTPCmdImpl;
import io.github.dailystruggle.rtp.common.commands.parameters.RegionParameter;
import io.github.dailystruggle.rtp.common.commands.parameters.WorldParameter;
import io.github.dailystruggle.rtp.common.configuration.ConfigParser;
import io.github.dailystruggle.rtp.common.configuration.MultiConfigParser;
import io.github.dailystruggle.rtp.common.configuration.enums.MessagesKeys;
import io.github.dailystruggle.rtp.common.configuration.enums.RegionKeys;
import io.github.dailystruggle.rtp.common.configuration.enums.WorldKeys;
import io.github.dailystruggle.rtp.common.selection.region.Region;
import io.github.dailystruggle.rtp.common.serverSide.substitutions.RTPWorld;
import io.github.dailystruggle.rtp.common.tools.ParseString;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
public class InfoCmd extends BaseRTPCmdImpl {
private static final Map> worldDataLookup = new ConcurrentHashMap<>();
private static final Map> regionDataLookup = new ConcurrentHashMap<>();
static {
worldDataLookup.put("world", RTPWorld::name);
worldDataLookup.put("region", world -> RTP.selectionAPI.getRegion(world).name);
worldDataLookup.put("requirePermission", world -> {
MultiConfigParser worlds = (MultiConfigParser) RTP.configs.getParser(WorldKeys.class);
ConfigParser parser = worlds.getParser(world.name());
return parser.getConfigValue(WorldKeys.requirePermission, false).toString();
});
worldDataLookup.put("override", world -> {
MultiConfigParser worlds = (MultiConfigParser) RTP.configs.getParser(WorldKeys.class);
ConfigParser parser = worlds.getParser(world.name());
return parser.getConfigValue(WorldKeys.override, "[0]").toString();
});
regionDataLookup.put("region", region -> region.name);
regionDataLookup.put("world", region -> region.getWorld().name());
regionDataLookup.put("shape", region -> region.getShape().name);
regionDataLookup.put("cacheCap", region -> region.getNumber(RegionKeys.cacheCap, 0).toString());
regionDataLookup.put("cached", region -> String.valueOf(region.getPublicQueueLength()));
regionDataLookup.put("worldBorderOverride", region -> {
boolean wbo = false;
EnumMap data = region.getData();
Object o = data.getOrDefault(RegionKeys.worldBorderOverride, false);
if (o instanceof Boolean) wbo = (Boolean) o;
else if (o instanceof String) {
wbo = Boolean.parseBoolean((String) o);
data.put(RegionKeys.worldBorderOverride, wbo);
}
return String.valueOf(wbo);
});
regionDataLookup.put("requirePermission", region -> {
boolean req = false;
EnumMap data = region.getData();
Object o = data.getOrDefault(RegionKeys.requirePermission, false);
if (o instanceof Boolean) req = (Boolean) o;
else if (o instanceof String) {
req = Boolean.parseBoolean((String) o);
data.put(RegionKeys.requirePermission, req);
}
return String.valueOf(req);
});
}
public InfoCmd(@Nullable CommandsAPICommand parent) {
super(parent);
addParameter("world", new WorldParameter("rtp.info", "check on a world's configuration", (uuid, s) -> true));
addParameter("region", new RegionParameter("rtp.info", "check on a region's state and configuration", (uuid, s) -> true));
}
@Override
public String name() {
return "info";
}
@Override
public String permission() {
return "rtp.info";
}
@Override
public String description() {
return "check the current state of the plugin";
}
@Override
public boolean onCommand(UUID callerId, Map> parameterValues, CommandsAPICommand nextCommand) {
ConfigParser lang = (ConfigParser) RTP.configs.getParser(MessagesKeys.class);
if (parameterValues.size() == 0) {
String title = lang.getConfigValue(MessagesKeys.infoTitle, "").toString();
String chunks = lang.getConfigValue(MessagesKeys.infoChunks, "").toString();
String worldHeader = lang.getConfigValue(MessagesKeys.infoWorldHeader, "").toString();
String worlds = lang.getConfigValue(MessagesKeys.infoWorld, "").toString();
String regionHeader = lang.getConfigValue(MessagesKeys.infoRegionHeader, "").toString();
String regions = lang.getConfigValue(MessagesKeys.infoRegion, "").toString();
RTP.serverAccessor.sendMessage(callerId, title);
RTP.serverAccessor.sendMessage(callerId, chunks);
RTP.serverAccessor.sendMessage(callerId, worldHeader);
for (RTPWorld world : RTP.serverAccessor.getRTPWorlds()) {
String msg = worlds.replaceAll("\\[world]", world.name());
RTP.serverAccessor.sendMessageAndSuggest(callerId, msg, "rtp info world:" + world.name());
}
RTP.serverAccessor.sendMessage(callerId, regionHeader);
RTP.selectionAPI.permRegionLookup.values().forEach(region -> {
String msg = regions.replaceAll("\\[region]", region.name);
RTP.serverAccessor.sendMessageAndSuggest(callerId, msg, "rtp info region:" + region.name);
});
return true;
}
Set front = new HashSet<>(Arrays.asList('[', '%'));
Set back = new HashSet<>(Arrays.asList(']', '%'));
List worldNames = parameterValues.get("world");
if (worldNames != null) {
Object worldInfoObj = lang.getConfigValue(MessagesKeys.worldInfo, "");
if (!(worldInfoObj instanceof List)) return true;
List worldInfo = ((List>) worldInfoObj).stream().map(String::valueOf).collect(Collectors.toList());
for (String worldName : worldNames) {
RTPWorld rtpWorld = RTP.serverAccessor.getRTPWorld(worldName);
if (rtpWorld == null || !rtpWorld.isActive()) continue;
ArrayList worldInfoCopy = new ArrayList<>(worldInfo);
List strings = worldInfoCopy.stream().map(s -> {
Set keywords = ParseString.keywords(s, worldDataLookup.keySet(), front, back);
Map placeholders = new HashMap<>();
worldDataLookup.forEach((s1, rtpWorldStringFunction) -> {
if (!keywords.contains(s1)) return;
placeholders.put(s1, rtpWorldStringFunction.apply(rtpWorld));
});
//replace all placeholders with their respective string function results
for (Map.Entry e : placeholders.entrySet()) {
s = s.replaceAll("\\[" + e.getKey() + "]", e.getValue());
}
for (Map.Entry e : placeholders.entrySet()) {
s = s.replaceAll("%" + e.getKey() + "%", e.getValue());
}
return s;
}).collect(Collectors.toList());
strings.forEach(s -> RTP.serverAccessor.sendMessage(callerId, s));
}
}
List regionNames = parameterValues.get("region");
if (regionNames != null) {
Object regionInfoObj = lang.getConfigValue(MessagesKeys.regionInfo, "");
if (!(regionInfoObj instanceof List)) return true;
List regionInfo = ((List>) regionInfoObj).stream().map(String::valueOf).collect(Collectors.toList());
for (String regionName : regionNames) {
Region region = RTP.selectionAPI.getRegion(regionName);
if (region == null) continue;
ArrayList regionInfoCopy = new ArrayList<>(regionInfo);
List strings = regionInfoCopy.stream().map(s -> {
Set keywords = ParseString.keywords(s, regionDataLookup.keySet(), front, back);
Map placeholders = new HashMap<>();
regionDataLookup.forEach((s1, rtpRegionStringFunction) -> {
if (!keywords.contains(s1)) return;
placeholders.put(s1, rtpRegionStringFunction.apply(region));
});
for (Map.Entry e : placeholders.entrySet()) {
s = s.replaceAll("\\[" + e.getKey() + "]", e.getValue());
}
for (Map.Entry e : placeholders.entrySet()) {
s = s.replaceAll("%" + e.getKey() + "%", e.getValue());
}
return s;
}).collect(Collectors.toList());
strings.forEach(s -> RTP.serverAccessor.sendMessage(callerId, s));
}
}
return true;
}
}