Compare commits

...

3 commits

9 changed files with 80 additions and 11 deletions

View file

@ -29,6 +29,13 @@ public class CustomChatCommand {
.executes(CustomChatCommand::enabled)
)
)
.then(
literal("onlyUseWhenNecessary")
.then(
argument("boolean", bool())
.executes(CustomChatCommand::onlyUseWhenNecessary)
)
)
.then(
literal("format")
.then(
@ -48,6 +55,17 @@ public class CustomChatCommand {
return Command.SINGLE_SUCCESS;
}
public static int onlyUseWhenNecessary (final CommandContext<FabricClientCommandSource> context) {
final FabricClientCommandSource source = context.getSource();
final boolean bool = getBool(context, "boolean");
CustomChat.INSTANCE.onlyUseWhenNecessary = bool;
if (bool) source.sendFeedback(Text.literal("Custom chat now only gets used when necessary"));
else source.sendFeedback(Text.literal("Custom chat now always get used"));
return Command.SINGLE_SUCCESS;
}
public static int setFormat (final CommandContext<FabricClientCommandSource> context) {
final FabricClientCommandSource source = context.getSource();
final String format = getString(context, "format");

View file

@ -5,6 +5,7 @@ import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.modules.CommandCore;
import land.chipmunk.chipmunkmod.modules.custom_chat.CustomChat;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.text.Text;
@ -25,12 +26,14 @@ public class ReloadConfigCommand {
try {
ChipmunkMod.CONFIG = ChipmunkMod.loadConfig();
CommandCore.INSTANCE.reloadRelativeArea();
CustomChat.INSTANCE.reloadFromConfig();
source.sendFeedback(Text.literal("Successfully reloaded the config"));
} catch (final IOException e) {
source.sendError(Text.literal("Could not load config, check the logs for stacktrace"));
e.printStackTrace();
ChipmunkMod.LOGGER.error("Could not load the config!", e);
}
return Command.SINGLE_SUCCESS;

View file

@ -79,6 +79,9 @@ public class Configuration {
@ConfigSerializable
public static class CustomChat {
public boolean enabled = true;
public boolean onlyUseWhenNecessary = false;
public @NotNull Component format =
// chipmunk's custom chat format
// §8[§7Chat§8] §aUSERNAME§8 §7MESSAGE

View file

@ -6,7 +6,7 @@ import org.spongepowered.configurate.transformation.TransformAction;
import static org.spongepowered.configurate.NodePath.path;
public class MigrationV2 implements ConfigMigration {
public final class MigrationV2 implements ConfigMigration {
@Override
public int version () {
return 2;

View file

@ -6,7 +6,7 @@ import org.spongepowered.configurate.transformation.TransformAction;
import static org.spongepowered.configurate.NodePath.path;
public class MigrationV3 implements ConfigMigration {
public final class MigrationV3 implements ConfigMigration {
@Override
public int version () {
return 3;

View file

@ -1,5 +1,6 @@
package land.chipmunk.chipmunkmod.mixin;
import land.chipmunk.chipmunkmod.modules.custom_chat.CustomChat;
import net.minecraft.util.StringHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
@ -10,6 +11,11 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
public class StringHelperMixin {
@Inject(method = "isValidChar", at = @At("RETURN"), cancellable = true)
private static void isValidChar (final char c, final CallbackInfoReturnable<Boolean> cir) {
if (!CustomChat.SHOULD_IGNORE_INVALID_CHAR.get()) {
CustomChat.SHOULD_IGNORE_INVALID_CHAR.set(true);
return;
}
// very legal [NUL] [LF] § Allowance.
cir.setReturnValue(true);
}

View file

@ -27,7 +27,10 @@ import net.minecraft.state.property.Property;
import net.minecraft.util.Hand;
import net.minecraft.util.Util;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.*;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.dimension.DimensionType;
@ -451,7 +454,7 @@ public class CommandCore implements Listener {
for (final Property<?> property : oldBlockState.getProperties()) {
command.append(property.getName())
.append('=')
.append(Util.getValueAsString(property, property.getType()))
.append(Util.getValueAsString(property, property.getValues().getLast())) // water[level=15] instead of water[level=0,level=1,....,level=15]
.append(',');
}

View file

@ -62,7 +62,16 @@ public class SelfCare implements Listener {
else if (!player.isInCreativeMode() && gamemodeEnabled)
networkHandler.sendChatCommand("gamemode creative");
else if (!cspy && cspyEnabled && serverHasCommand("c"))
if (
!CommandCore.INSTANCE.ready
|| !CommandCore.INSTANCE.runFillCommand
|| !player.isInCreativeMode()
|| !player.hasPermissionLevel(2)
) {
networkHandler.sendChatCommand("c on");
} else {
CommandCore.INSTANCE.run("c " + player.getUuidAsString() + " on");
}
else if (!hasSkin && !skin.equalsIgnoreCase("off"))
networkHandler.sendChatCommand("skin " + skin);
}

View file

@ -13,6 +13,7 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.util.StringHelper;
import java.util.Map;
import java.util.regex.Pattern;
@ -51,19 +52,30 @@ public class CustomChat {
.build();
private final MinecraftClient client;
public boolean enabled = true;
public static final ThreadLocal<Boolean> SHOULD_IGNORE_INVALID_CHAR = ThreadLocal.withInitial(() -> true);
public Component format = ChipmunkMod.CONFIG.customChat.format;
private final MinecraftClient client;
public boolean enabled;
public boolean onlyUseWhenNecessary;
public Component format;
public CustomChat (final MinecraftClient client) {
this.client = client;
reloadFromConfig();
}
public void reloadFromConfig () {
this.format = ChipmunkMod.CONFIG.customChat.format;
this.enabled = ChipmunkMod.CONFIG.customChat.enabled;
this.onlyUseWhenNecessary = ChipmunkMod.CONFIG.customChat.onlyUseWhenNecessary;
}
public void chat (final String message) {
final ClientPlayerEntity player = client.player;
if (player == null) return;
if (!enabled || !player.hasPermissionLevel(2) || !player.isCreative()) {
if (shouldUsePlayerChat(player, message)) {
Chat.sendChatMessage(message, true);
return;
}
@ -80,4 +92,19 @@ public class CustomChat {
CommandCore.INSTANCE.run((KaboomCheck.INSTANCE.isKaboom ? "minecraft:tellraw @a " : "tellraw @a ") + json);
}
private boolean shouldUsePlayerChat (final ClientPlayerEntity player, final String message) {
return !enabled || isUnnecessary(message) || !player.hasPermissionLevel(2) || !player.isCreative();
}
private boolean isUnnecessary (final String message) {
if (!onlyUseWhenNecessary || message.isEmpty() || message.length() > 256) return false;
for (final char character : message.toCharArray()) {
SHOULD_IGNORE_INVALID_CHAR.set(false);
if (!StringHelper.isValidChar(character)) return false;
}
return true;
}
}