Skip to content

Commit

Permalink
Disallow kicking offline players - Fixes #91
Browse files Browse the repository at this point in the history
  • Loading branch information
A248 committed Nov 27, 2021
1 parent a119e57 commit a4b378e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public PunishmentType parseType(String arg) {
}

@Override
public CentralisedFuture<Victim> parseVictim(CmdSender sender, String targetArg) {
public CentralisedFuture<Victim> parseVictim(CmdSender sender, String targetArg, PunishmentType type) {
return argumentParser().parseAddressVictim(sender, targetArg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public PunishmentType parseType(String arg) {
}

@Override
public CentralisedFuture<Victim> parseVictim(CmdSender sender, String targetArg) {
public CentralisedFuture<Victim> parseVictim(CmdSender sender, String targetArg, PunishmentType type) {
return argumentParser().parseAddressVictim(sender, targetArg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import space.arim.libertybans.api.PlayerVictim;
import space.arim.libertybans.core.commands.extra.TabCompletion;
import space.arim.libertybans.core.env.EnvUserResolver;
import space.arim.omnibus.util.concurrent.CentralisedFuture;

import space.arim.libertybans.api.PunishmentType;
Expand All @@ -32,15 +34,21 @@
import space.arim.libertybans.core.env.EnvEnforcer;
import space.arim.libertybans.core.punish.MiscUtil;

import java.util.UUID;

@Singleton
public class PlayerPunishCommands extends PunishCommands {

private final EnvUserResolver envUserResolver;

@Inject
public PlayerPunishCommands(Dependencies dependencies,
PunishmentDrafter drafter, InternalFormatter formatter,
EnvEnforcer<?> envEnforcer, TabCompletion tabCompletion) {
EnvEnforcer<?> envEnforcer, TabCompletion tabCompletion,
EnvUserResolver envUserResolver) {
super(dependencies, MiscUtil.punishmentTypes().stream().map(PunishmentType::toString),
drafter, formatter, envEnforcer, tabCompletion);
this.envUserResolver = envUserResolver;
}

@Override
Expand All @@ -49,8 +57,28 @@ public PunishmentType parseType(String arg) {
}

@Override
public CentralisedFuture<Victim> parseVictim(CmdSender sender, String targetArg) {
return argumentParser().parseVictimByName(sender, targetArg);
public CentralisedFuture<Victim> parseVictim(CmdSender sender, String targetArg, PunishmentType type) {
var victimFuture = argumentParser().parseVictimByName(sender, targetArg);
switch (type) {
case BAN:
case MUTE:
case WARN:
return victimFuture;
case KICK:
// For kicks, users need to be screened to check that they are online
return victimFuture.thenApply((victim) -> {
if (victim instanceof PlayerVictim) {
UUID uuid = ((PlayerVictim) victim).getUUID();
if (envUserResolver.lookupName(uuid).isEmpty()) {
sender.sendMessage(messages().additions().kicks().mustBeOnline().replaceText("%TARGET%", targetArg));
return null;
}
}
return victim;
});
default:
throw MiscUtil.unknownType(type);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public PunishmentType parseType(String arg) {
}

@Override
public CentralisedFuture<Victim> parseVictim(CmdSender sender, String targetArg) {
public CentralisedFuture<Victim> parseVictim(CmdSender sender, String targetArg, PunishmentType type) {
return argumentParser().parseVictimByName(sender, targetArg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void execute() {

private void execute0() {
String targetArg = command().next();
CentralisedFuture<?> future = parseVictim(sender(), targetArg).thenCompose((victim) -> {
CentralisedFuture<?> future = parseVictim(sender(), targetArg, type()).thenCompose((victim) -> {
if (victim == null) {
return completedFuture(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ interface PunishUnpunishCommands {

PunishmentType parseType(String arg);

CentralisedFuture<Victim> parseVictim(CmdSender sender, String targetArg);
CentralisedFuture<Victim> parseVictim(CmdSender sender, String targetArg, PunishmentType type);

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void execute() {

private void execute0() {
String targetArg = command().next();
CentralisedFuture<?> future = parseVictim(sender(), targetArg).thenCompose((victim) -> {
CentralisedFuture<?> future = parseVictim(sender(), targetArg, type()).thenCompose((victim) -> {
if (victim == null) {
return completedFuture(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import jakarta.inject.Inject;

import space.arim.libertybans.core.config.MessagesConfig;
import space.arim.omnibus.util.concurrent.CentralisedFuture;
import space.arim.omnibus.util.concurrent.FactoryOfTheFuture;

Expand Down Expand Up @@ -52,6 +53,10 @@ public StandardArgumentParser(FactoryOfTheFuture futuresFactory, Configs configs
private <T> CentralisedFuture<T> completedFuture(T value) {
return futuresFactory.completedFuture(value);
}

private MessagesConfig.All.NotFound notFound() {
return configs.getMessagesConfig().all().notFound();
}

// UUID from name

Expand All @@ -62,7 +67,7 @@ private CentralisedFuture<UUID> parseOrLookupUUID(CmdSender sender, String targe
try {
uuid = UUID.fromString(targetArg);
} catch (IllegalArgumentException ex) {
sender.sendMessage(configs.getMessagesConfig().all().notFound().uuid().replaceText("%TARGET%", targetArg));
sender.sendMessage(notFound().uuid().replaceText("%TARGET%", targetArg));
return completedFuture(null);
}
return completedFuture(uuid);
Expand All @@ -73,7 +78,7 @@ private CentralisedFuture<UUID> parseOrLookupUUID(CmdSender sender, String targe
mostSigBits = Long.parseUnsignedLong(targetArg.substring(0, 16), 16);
leastSigBits = Long.parseUnsignedLong(targetArg.substring(16, 32), 16);
} catch (NumberFormatException ex) {
sender.sendMessage(configs.getMessagesConfig().all().notFound().uuid().replaceText("%TARGET%", targetArg));
sender.sendMessage(notFound().uuid().replaceText("%TARGET%", targetArg));
return completedFuture(null);
}
return completedFuture(new UUID(mostSigBits, leastSigBits));
Expand All @@ -82,7 +87,7 @@ private CentralisedFuture<UUID> parseOrLookupUUID(CmdSender sender, String targe
}
return uuidManager.lookupUUID(targetArg).thenApply((uuid) -> {
if (uuid.isEmpty()) {
sender.sendMessage(configs.getMessagesConfig().all().notFound().player().replaceText("%TARGET%", targetArg));
sender.sendMessage(notFound().player().replaceText("%TARGET%", targetArg));
return null;
}
return uuid.get();
Expand Down Expand Up @@ -118,7 +123,7 @@ public CentralisedFuture<Victim> parseAddressVictim(CmdSender sender, String tar
}
return uuidManager.lookupAddress(targetArg).thenApply((address) -> {
if (address == null) {
sender.sendMessage(configs.getMessagesConfig().all().notFound().playerOrAddress().replaceText("%TARGET%", targetArg));
sender.sendMessage(notFound().playerOrAddress().replaceText("%TARGET%", targetArg));
return null;
}
return AddressVictim.of(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ interface KickAddition extends PunishmentAddition {
"&c&lReason",
"&7%REASON%"})
ComponentText layout();


@ConfKey("must-be-online")
@DefaultString("&c&o%TARGET%&r&7 must be online.")
ComponentText mustBeOnline();

}

@SubSection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import space.arim.libertybans.core.config.MessagesConfig;
import space.arim.libertybans.core.env.CmdSender;
import space.arim.libertybans.core.env.EnvEnforcer;
import space.arim.libertybans.core.env.EnvUserResolver;
import space.arim.omnibus.util.concurrent.impl.IndifferentFactoryOfTheFuture;

import java.util.Set;
Expand Down Expand Up @@ -80,7 +81,8 @@ public UnspecifiedReasonsTest(@Mock PunishmentDrafter drafter, @Mock InternalFor

@BeforeEach
public void setConfigSection(AbstractSubCommandGroup.Dependencies dependencies,
/* Mock */ Configs configs, /* Mock */ ArgumentParser argParser) {
/* Mock */ Configs configs, /* Mock */ ArgumentParser argParser,
@Mock EnvUserResolver envUserResolver) {
{
MainConfig mainConfig = mock(MainConfig.class);
when(configs.getMainConfig()).thenReturn(mainConfig);
Expand All @@ -102,7 +104,8 @@ public void setConfigSection(AbstractSubCommandGroup.Dependencies dependencies,
}
when(argParser.parseVictimByName(any(), eq("A248"))).thenReturn(
new IndifferentFactoryOfTheFuture().completedFuture(PlayerVictim.of(UUID.randomUUID())));
punishCommands = new PlayerPunishCommands(dependencies, drafter, formatter, envEnforcer, tabCompletion);

punishCommands = new PlayerPunishCommands(dependencies, drafter, formatter, envEnforcer, tabCompletion, envUserResolver);
}

private void executeBan(CmdSender sender) {
Expand Down

0 comments on commit a4b378e

Please sign in to comment.