Skip to content

Commit

Permalink
Ported rule fakePlayerRemoteSpawning
Browse files Browse the repository at this point in the history
and it's condition is stricter
stop bot abuse
  • Loading branch information
Fallen-Breath committed May 28, 2023
1 parent 2e2e7bd commit 91a6dff
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
17 changes: 17 additions & 0 deletions src/main/java/carpet/commands/PlayerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import carpet.patches.EntityPlayerMPFake;
import carpet.settings.CarpetSettings;
import carpet.settings.SettingsManager;
import carpet.utils.CommandUtil;
import carpet.utils.Messenger;
import com.google.common.collect.Sets;
import com.mojang.brigadier.CommandDispatcher;
Expand Down Expand Up @@ -479,6 +480,22 @@ private static int spawn(CommandContext<CommandSource> context) throws CommandSy
catch (CommandSyntaxException ignored)
{
}

// TISCM rule fakePlayerRemoteSpawning
if (!SettingsManager.canUseCommand(context.getSource(), CarpetSettings.fakePlayerRemoteSpawning))
{
final int MAX_ALLOWED_REMOTE_RANGE = 16;
Vec3d sourcePos = context.getSource().getPos();
DimensionType sourceDimension = context.getSource().getWorld().getDimension().getType();

// only allow remote bot spawning iif. the bot pos is closed enough to the player
if (!sourceDimension.equals(dim) || pos.distanceTo(sourcePos) >= MAX_ALLOWED_REMOTE_RANGE)
{
Messenger.m(context.getSource(), "rb Remote player spawning is not allowed");
return 0;
}
}

EntityPlayer p = EntityPlayerMPFake.createFake(
getString(context,"player"),
context.getSource().getServer(),
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/carpet/settings/CarpetSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import carpet.utils.Messenger;
import carpet.utils.TISCMConfig;
import carpet.utils.Translations;
import com.google.common.collect.ImmutableList;
import net.minecraft.command.CommandSource;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.dedicated.DedicatedServer;
Expand Down Expand Up @@ -866,6 +867,26 @@ public String description()
)
public static boolean playerCheckLightDisabled = false;

@Rule(
desc = "The permission requirement for spawning remotely a fake player with `/player` command",
extra = "Here \"remotely\" means spawning a fake player at more than 16m away, or in other dimension",
options = {"true", "false", "ops", "0", "1", "2", "3", "4"},
category = SURVIVAL,
validate = PermissionLevelValidator.class
)
public static String fakePlayerRemoteSpawning = "true";

public static class PermissionLevelValidator extends Validator<String>
{
public static final ImmutableList<String> OPTIONS = ImmutableList.of("true", "false", "ops", "0", "1", "2", "3", "4");

@Override
public String validate(CommandSource source, ParsedRule<String> currentRule, String newValue, String string)
{
return OPTIONS.contains(newValue.toLowerCase()) ? newValue : null;
}
}


// /$$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$
//|__ $$__/|_ $$_/ /$$__ $$ /$$__ $$| $$$ /$$$
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carpet/settings/ParsedRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
}
else if (this.type == String.class && categories.contains(RuleCategory.COMMAND))
{
this.options = ImmutableList.of("true", "false", "ops");
this.options = ImmutableList.of("true", "false", "ops", "0", "1", "2", "3", "4");
}
else if (this.type.isEnum())
{
Expand Down
32 changes: 25 additions & 7 deletions src/main/java/carpet/utils/CommandUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.command.CommandSource;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;

import java.util.Optional;

public class CommandUtil
{
public static boolean isConsoleCommandSource(CommandSource commandSource)
Expand All @@ -15,20 +18,35 @@ public static boolean isConsoleCommandSource(CommandSource commandSource)
return false;
}

public static boolean isPlayerCommandSource(CommandSource commandSource)
public static Optional<EntityPlayerMP> getPlayer(CommandSource source)
{
if (commandSource != null)
if (source != null)
{
try
{
commandSource.asPlayer();
return true;
return Optional.of(source.asPlayer());
}
catch (CommandSyntaxException e)
catch (CommandSyntaxException ignored)
{
return false;
}
}
return false;
return Optional.empty();
}

public static boolean isPlayerCommandSource(CommandSource source)
{
return getPlayer(source).isPresent();
}

public static boolean isCreativePlayer(CommandSource source)
{
return getPlayer(source).
map(EntityPlayerMP::isCreative).
orElse(false);
}

public static boolean canCheat(CommandSource source)
{
return source.hasPermissionLevel(2); // commonly used in cheaty commands
}
}

0 comments on commit 91a6dff

Please sign in to comment.