Skip to content

Commit

Permalink
no way commands actually work??!!!
Browse files Browse the repository at this point in the history
also disable server checking for now
  • Loading branch information
rhysdh540 committed Feb 12, 2024
1 parent 386e439 commit 537e606
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 129 deletions.
1 change: 1 addition & 0 deletions buildSrc/src/main/java/SubprojectsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ void manifold(DependencyHandler deps, String module) {
String location = "systems.manifold:manifold-" + module + ":" + getProperty("manifold_version");
deps.add("annotationProcessor", location);
deps.add("compileOnly", location);
deps.add("localRuntime", location);
}

void dependencies(Consumer<DependencyHandler> deps) {
Expand Down
6 changes: 0 additions & 6 deletions common/src/main/java/dev/rdh/createunlimited/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import dev.architectury.injectables.annotations.ExpectPlatform;

import dev.rdh.createunlimited.config.CUConfigs;

import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -65,11 +64,6 @@ public static Attribute getReachAttribute() {
throw new AssertionError();
}

@ExpectPlatform
public static String remapMethod(Class<?> clazz, String name, Class<?>... parameterTypes) {
throw new AssertionError();
}

public static Supplier<Multimap<Attribute, AttributeModifier>> singleRange() {
AttributeModifier am = new AttributeModifier(UUID.fromString("7f7dbdb2-0d0d-458a-aa40-ac7633691f66"), "Range modifier",
CUConfigs.server.singleExtendoGripRange.get(), AttributeModifier.Operation.ADDITION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,76 @@
import com.mojang.brigadier.CommandDispatcher;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;


import dev.rdh.createunlimited.CreateUnlimited;

import com.simibubi.create.foundation.utility.Components;
import com.simibubi.create.infrastructure.command.AllCommands;

import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;

import java.util.Collections;
import java.util.List;

public class CUCommands {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, boolean dedicated) {
LiteralArgumentBuilder<CommandSourceStack> base = Commands.literal(CreateUnlimited.ID);
// .then(CUConfigCommand.register()); - does not work in prod because obfuscation :(
List<MutableComponent> links = List.of(
link("https://github.com/rhysdh540/create-unlimited", "GitHub", ChatFormatting.GRAY),
link("https://modrinth.com/mod/create-unlimited", "Modrinth", ChatFormatting.GREEN),
link("https://curseforge.com/minecraft/mc-mods/create-unlimited", "CurseForge", ChatFormatting.GOLD),
link("https://discord.gg/GeGm3DRDWY", "Discord", ChatFormatting.BLUE)
);
LiteralArgumentBuilder<CommandSourceStack> base = Commands.literal(CreateUnlimited.ID)
.executes(context -> {
message(context, CreateUnlimited.NAME + " v" + CreateUnlimited.VERSION + " by rdh\nVisit us on:");
MutableComponent link = MutableComponent.create(CommonComponents.EMPTY.getContents());
links.forEach(a -> link.append(a).append(Component.literal(" ")));
message(context, link);
return 1;
})
.then(CUConfigCommand.register());

LiteralCommandNode<CommandSourceStack> root = dispatcher.register(base);

CommandNode<CommandSourceStack> cu = dispatcher.findNode(Collections.singleton("cu"));
if(cu != null) return;
dispatcher.getRoot().addChild(AllCommands.buildRedirect("cu", root));
}

protected static MutableComponent link(String link, String display, ChatFormatting color) {
return ComponentUtils.wrapInSquareBrackets(Component.nullToEmpty(display))
.withStyle(color)
.withStyle(style -> style
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, link))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Components.literal("Click to open " + display + " page")))
.withUnderlined(false));
}

protected static void message(CommandContext<CommandSourceStack> context, Component message) {
context.getSource().sendSystemMessage(message);
}

protected static void message(CommandContext<CommandSourceStack> context, String message) {
message(context, Component.nullToEmpty(message));
}

protected static void error(CommandContext<CommandSourceStack> context, Component message) {
context.getSource().sendFailure(message);
}

protected static void error(CommandContext<CommandSourceStack> context, String message) {
error(context, Component.nullToEmpty(message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,32 @@
import com.mojang.brigadier.builder.ArgumentBuilder;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;

import dev.rdh.createunlimited.CreateUnlimited;
import dev.rdh.createunlimited.Util;
import dev.rdh.createunlimited.config.CUConfigs;
import dev.rdh.createunlimited.config.CUServer;
import dev.rdh.createunlimited.mixin.accessor.CValueAccessor;

import com.simibubi.create.foundation.config.ConfigBase.CValue;
import com.simibubi.create.foundation.config.ConfigBase.ConfigGroup;
import com.simibubi.create.foundation.utility.Components;

import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import net.minecraftforge.common.ForgeConfigSpec.ConfigValue;
import net.minecraftforge.common.ForgeConfigSpec.DoubleValue;
import net.minecraftforge.common.ForgeConfigSpec.EnumValue;
import net.minecraftforge.common.ForgeConfigSpec.IntValue;

import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.entity.Entity;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.util.List;
import java.util.function.Supplier;

import static dev.rdh.createunlimited.multiversion.SupportedMinecraftVersion.*;
import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;

public class CUConfigCommand {
public class CUConfigCommand extends CUCommands {
public static ArgumentBuilder<CommandSourceStack, ?> register() {
List<MutableComponent> links = List.of(
link("https://github.com/rhysdh540/create-unlimited", "GitHub", ChatFormatting.GRAY),
link("https://modrinth.com/mod/create-unlimited", "Modrinth", ChatFormatting.GREEN),
link("https://curseforge.com/minecraft/mc-mods/create-unlimited", "CurseForge", ChatFormatting.GOLD),
link("https://discord.gg/GeGm3DRDWY", "Discord", ChatFormatting.BLUE)
);
LiteralArgumentBuilder<CommandSourceStack> base = literal("config")
.executes(context -> {
sendSuccess(context, CreateUnlimited.NAME + " v" + CreateUnlimited.VERSION + " by rdh\nVisit us on:");
MutableComponent link = MutableComponent.create(CommonComponents.EMPTY.getContents());
links.forEach(a -> link.append(a).append(Component.literal(" ")));
sendSuccess(context, link);
return 1;
});
LiteralArgumentBuilder<CommandSourceStack> base = literal("config");

LiteralArgumentBuilder<CommandSourceStack> category = null;

Expand All @@ -77,9 +48,8 @@ public class CUConfigCommand {
category = literal(name);

// add description for category
assert base != null;
base.then(literal(field.getName()).executes(context -> {
sendSuccess(context, CUServer.getComment(name));
message(context, CUServer.getComment(name));
return Command.SINGLE_SUCCESS;
}));

Expand Down Expand Up @@ -134,9 +104,9 @@ private static boolean perms(Object o) {
private static <T> void gdr(LiteralArgumentBuilder<CommandSourceStack> category, String name, ConfigValue<T> value) {
category.then(literal(name)
.executes(context -> {
sendSuccess(context, name + ": " + CUServer.getComment(name));
sendSuccess(context, "Current value: " + value.get());
sendSuccess(context, "Default value: " + value.getDefault());
message(context, name + ": " + CUServer.getComment(name));
message(context, "Current value: " + value.get());
message(context, "Default value: " + value.getDefault());
return Command.SINGLE_SUCCESS;
})
.then(literal("reset").requires(CUConfigCommand::perms)
Expand All @@ -146,7 +116,7 @@ private static <T> void gdr(LiteralArgumentBuilder<CommandSourceStack> category,
return 0;
}
value.set(value.getDefault());
sendSuccess(context, name + " reset to: " + value.get());
message(context, name + " reset to: " + value.get());
return Command.SINGLE_SUCCESS;
})
)
Expand All @@ -163,7 +133,7 @@ private static void setBoolean(LiteralArgumentBuilder<CommandSourceStack> catego
return 0;
}
value.set(set);
sendSuccess(context, name + " set to: " + set);
message(context, name + " set to: " + set);
return Command.SINGLE_SUCCESS;
})
)
Expand All @@ -180,7 +150,7 @@ private static void setInt(LiteralArgumentBuilder<CommandSourceStack> category,
return 0;
}
value.set(set);
sendSuccess(context, name + " set to: " + set);
message(context, name + " set to: " + set);
return Command.SINGLE_SUCCESS;
})
)
Expand All @@ -197,7 +167,7 @@ private static void setDouble(LiteralArgumentBuilder<CommandSourceStack> categor
return 0;
}
value.set(set);
sendSuccess(context, name + " set to: " + set);
message(context, name + " set to: " + set);
return Command.SINGLE_SUCCESS;
})
)
Expand All @@ -216,64 +186,10 @@ private static <T extends Enum<T>> void setEnum(LiteralArgumentBuilder<CommandSo
return 0;
}
value.set(set);
sendSuccess(context, name + " set to: " + set.name().toLowerCase());
message(context, name + " set to: " + set.name().toLowerCase());
return Command.SINGLE_SUCCESS;
})
)
);
}

private static MutableComponent link(String link, String display, ChatFormatting color) {
return ComponentUtils.wrapInSquareBrackets(Component.nullToEmpty(display))
.withStyle(color)
.withStyle(style -> style
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, link))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Components.literal("Click to open " + display + " page")))
.withUnderlined(false));
}

private static final MethodHandle SEND_SUCCESS;
static {
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
MethodType type;
if(CURRENT <= v1_19_2) {
type = MethodType.methodType(void.class, Component.class, boolean.class);
} else if(CURRENT >= v1_20_1) {
type = MethodType.methodType(void.class, Supplier.class, boolean.class);
} else {
throw new RuntimeException("Unsupported Minecraft version: " + CURRENT);
}

String remapped = Util.remapMethod(CommandSourceStack.class, "sendSuccess", type.parameterArray());
try {
SEND_SUCCESS = lookup.findVirtual(CommandSourceStack.class, remapped, type);
} catch(NoSuchMethodException | IllegalAccessException e) {
throw unchecked(e);
}
}

private static void sendSuccess(CommandContext<CommandSourceStack> context, Component message) {
try {
if(CURRENT <= v1_19_2)
SEND_SUCCESS.invokeExact(context.getSource(), message, false);
else if(CURRENT >= v1_20_1)
SEND_SUCCESS.invoke(context.getSource(), (Supplier<Component>) () -> message, false);
else
throw new RuntimeException("Unsupported Minecraft version: " + CURRENT);
} catch(Throwable t) {
throw unchecked(t);
}
}

private static void sendSuccess(CommandContext<CommandSourceStack> context, String message) {
sendSuccess(context, Component.nullToEmpty(message));
}

private static void error(CommandContext<CommandSourceStack> context, Component message) {
context.getSource().sendFailure(message);
}

private static void error(CommandContext<CommandSourceStack> context, String message) {
error(context, Component.nullToEmpty(message));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.rdh.createunlimited.mixin;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
Expand Down Expand Up @@ -29,7 +32,7 @@ public class ClientHandshakePacketListenerImplMixin {
private void onAuthenticateServer(String serverHash, CallbackInfoReturnable<Component> cir) {
CreateUnlimited.LOGGER.info("Checking if Create Unlimited is installed on the server...");
try {
// this will fail if unlimited is not installed on the server
// this always fails - make it only fail if the server doesn't have the mod installed
CUConfigs.server.allowAllCopycatBlocks.get();
} catch (IllegalStateException e) {
Component c = Component.literal("Create Unlimited is not installed on the server!" +
Expand Down
15 changes: 0 additions & 15 deletions fabric/src/main/java/dev/rdh/createunlimited/fabric/UtilImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;

import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.MappingResolver;

import net.minecraft.commands.CommandSourceStack;

Expand Down Expand Up @@ -105,18 +104,4 @@ public static Attribute getReachAttribute() {
public static String platformName() {
return FabricLoader.getInstance().isModLoaded("quilt_loader") ? "Quilt" : "Fabric";
}

public static String remapMethod(Class<?> clazz, String name, Class<?>... parameterTypes) {
MappingResolver resolver = FabricLoader.getInstance().getMappingResolver();
String namespace = resolver.getCurrentRuntimeNamespace();
return resolver.mapMethodName(namespace, clazz.getName(), name, makeDescriptor(parameterTypes));
}

static String makeDescriptor(Class<?>... parameterTypes) {
StringBuilder builder = new StringBuilder("(");
for(Class<?> parameterType : parameterTypes) {
builder.append(parameterType.getName());
}
return builder.append(")").toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@
import net.minecraftforge.fml.config.IConfigSpec;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.fml.util.ObfuscationReflectionHelper;
import net.minecraftforge.forgespi.language.IModInfo;
import net.minecraftforge.registries.RegistryObject;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cpw.mods.modlauncher.api.INameMappingService.Domain;

import static dev.rdh.createunlimited.multiversion.SupportedMinecraftVersion.*;

@SuppressWarnings({"UnstableApiUsage", "RedundantSuppression"})
Expand Down Expand Up @@ -93,8 +90,4 @@ public static Attribute getReachAttribute() {
public static String platformName() {
return "Forge";
}

public static String remapMethod(Class<?> clazz, String name, Class<?>... parameterTypes) {
return ObfuscationReflectionHelper.remapName(Domain.METHOD, clazz.getName() + "/" + name);
}
}
6 changes: 3 additions & 3 deletions forge/src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ logoFile = "icon.png"
modId = "forge"
mandatory = true
versionRange = "[1,)"
ordering = "NONE"
ordering = "AFTER"
side = "BOTH"

[[dependencies.createunlimited]]
modId = "minecraft"
mandatory = true
versionRange = "${minecraft}"
ordering = "NONE"
ordering = "AFTER"
side = "BOTH"

[[dependencies.createunlimited]]
modId="create"
mandatory=true
versionRange="[${create},)"
ordering="NONE"
ordering="AFTER"
side="BOTH"

0 comments on commit 537e606

Please sign in to comment.