From 19e1127c8e5f9222ea9718bb3e5509243aea9e9f Mon Sep 17 00:00:00 2001 From: glisco Date: Mon, 27 Dec 2021 15:55:32 +0100 Subject: [PATCH] only actually initialize item groups on the client to avoid crashing servers and close #9, allow agreeing to the eula in the server console instead of restarting --- gradle.properties | 5 +- .../owo/command/EnumArgumentType.java | 44 +++++++++++++++-- .../io/wispforest/owo/itemgroup/Icon.java | 1 - .../owo/itemgroup/OwoItemGroup.java | 6 ++- .../wispforest/owo/mixin/EulaReaderMixin.java | 49 +++++++++++++++++++ src/main/resources/owo.mixins.json | 1 + 6 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 src/main/java/io/wispforest/owo/mixin/EulaReaderMixin.java diff --git a/gradle.properties b/gradle.properties index a317afb9..d20a746e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,7 +7,7 @@ minecraft_version=1.18.1 yarn_mappings=1.18.1+build.7 loader_version=0.12.12 # Mod Properties -mod_version=0.3.10 +mod_version=0.3.11 maven_group=io.wispforest archives_base_name=owo-lib # Dependencies @@ -15,5 +15,4 @@ archives_base_name=owo-lib fabric_version=0.44.0+1.18 # https://www.curseforge.com/minecraft/mc-mods/roughly-enough-items/files -rei_version=7.1.356 - +rei_version=7.1.356 \ No newline at end of file diff --git a/src/main/java/io/wispforest/owo/command/EnumArgumentType.java b/src/main/java/io/wispforest/owo/command/EnumArgumentType.java index 60d447e5..103d8ea7 100644 --- a/src/main/java/io/wispforest/owo/command/EnumArgumentType.java +++ b/src/main/java/io/wispforest/owo/command/EnumArgumentType.java @@ -1,5 +1,6 @@ package io.wispforest.owo.command; +import com.google.gson.JsonObject; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; @@ -8,6 +9,9 @@ import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.command.CommandSource; +import net.minecraft.command.argument.ArgumentTypes; +import net.minecraft.command.argument.serialize.ArgumentSerializer; +import net.minecraft.network.PacketByteBuf; import net.minecraft.text.LiteralText; import java.util.Arrays; @@ -24,28 +28,35 @@ public class EnumArgumentType> implements ArgumentType> { private final DynamicCommandExceptionType noValueException; + private final String noElementMessage; private final Class enumClass; private EnumArgumentType(Class enumClass, String noElementMessage) { this.enumClass = enumClass; - this.noValueException = new DynamicCommandExceptionType(o -> new LiteralText(noElementMessage.replace("{}", o.toString()))); + this.noElementMessage = noElementMessage; + this.noValueException = new DynamicCommandExceptionType(o -> new LiteralText(this.noElementMessage.replace("{}", o.toString()))); } /** * Creates a new instance that uses {@code Invalid enum value '{}'} as the - * error message if an invalid value is supplied + * error message if an invalid value is supplied. This must be called + * on both server and client so the serializer can be registered correctly * * @param enumClass The {@code enum} type to parse for * @param The {@code enum} type to parse for * @return A new argument type that can parse instances of {@code T} */ + @SuppressWarnings({"unchecked", "rawtypes"}) public static > EnumArgumentType create(Class enumClass) { - return new EnumArgumentType<>(enumClass, "Invalid enum value '{}'"); + final var type = new EnumArgumentType<>(enumClass, "Invalid enum value '{}'"); + ArgumentTypes.register("owo-enum_" + enumClass.getName().toLowerCase(), type.getClass(), new Serializer(type)); + return type; } /** * Creates a new instance that uses {@code noElementMessage} as the - * error message if an invalid value is supplied + * error message if an invalid value is supplied. This must be called + * on both server and client so the serializer can be registered correctly * * @param enumClass The {@code enum} type to parse for * @param noElementMessage The error message to send if an invalid value is @@ -54,8 +65,11 @@ public static > EnumArgumentType create(Class enumClass) * @param The {@code enum} type to parse for * @return A new argument type that can parse instances of {@code T} */ + @SuppressWarnings({"unchecked", "rawtypes"}) public static > EnumArgumentType create(Class enumClass, String noElementMessage) { - return new EnumArgumentType<>(enumClass, noElementMessage); + final var type = new EnumArgumentType<>(enumClass, noElementMessage); + ArgumentTypes.register("owo-enum_" + enumClass.getName().toLowerCase(), type.getClass(), new Serializer(type)); + return type; } public T get(CommandContext context, String name) { @@ -76,4 +90,24 @@ public T parse(StringReader reader) throws CommandSyntaxException { throw noValueException.create(name); } } + + public static final class Serializer, TypeInstance extends EnumArgumentType> implements ArgumentSerializer { + + private final TypeInstance instance; + + public Serializer(TypeInstance instance) { + this.instance = instance; + } + + @Override + public void toPacket(TypeInstance type, PacketByteBuf buf) {} + + @Override + public TypeInstance fromPacket(PacketByteBuf buf) { + return instance; + } + + @Override + public void toJson(TypeInstance type, JsonObject json) {} + } } diff --git a/src/main/java/io/wispforest/owo/itemgroup/Icon.java b/src/main/java/io/wispforest/owo/itemgroup/Icon.java index 239cdf13..926b65d2 100644 --- a/src/main/java/io/wispforest/owo/itemgroup/Icon.java +++ b/src/main/java/io/wispforest/owo/itemgroup/Icon.java @@ -16,7 +16,6 @@ *

* Default implementations provided for textures and itemstacks */ -@SuppressWarnings("ClassCanBeRecord") public interface Icon { void render(MatrixStack matrixStack, int x, int y, int mouseX, int mouseY, float delta); diff --git a/src/main/java/io/wispforest/owo/itemgroup/OwoItemGroup.java b/src/main/java/io/wispforest/owo/itemgroup/OwoItemGroup.java index af10b4d1..ded44bc2 100644 --- a/src/main/java/io/wispforest/owo/itemgroup/OwoItemGroup.java +++ b/src/main/java/io/wispforest/owo/itemgroup/OwoItemGroup.java @@ -4,7 +4,9 @@ import io.wispforest.owo.itemgroup.gui.ItemGroupButtonWidget; import io.wispforest.owo.itemgroup.gui.ItemGroupTab; import io.wispforest.owo.itemgroup.json.WrapperGroup; +import net.fabricmc.api.EnvType; import net.fabricmc.fabric.impl.item.group.ItemGroupExtensions; +import net.fabricmc.loader.api.FabricLoader; import net.minecraft.item.*; import net.minecraft.tag.Tag; import net.minecraft.util.Identifier; @@ -72,7 +74,7 @@ protected OwoItemGroup(int index, String name) { public void initialize() { if (initialized) return; - setup(); + if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) setup(); if (tabs.size() == 0) this.addTab(Icon.of(Items.AIR), "based_placeholder_tab", ItemGroupTab.EMPTY); this.initialized = true; } @@ -107,7 +109,7 @@ protected void addTab(Icon icon, String name, Tag contentTag, Identifier t * Adds a new tab to this group, using the default button texture * * @param icon The icon to use - * @param name The name of the, used for the translation key + * @param name The name of the tab, used for the translation key * @param contentTag The tag used for filling this tab * @see Icon#of(ItemConvertible) */ diff --git a/src/main/java/io/wispforest/owo/mixin/EulaReaderMixin.java b/src/main/java/io/wispforest/owo/mixin/EulaReaderMixin.java new file mode 100644 index 00000000..3572b2e8 --- /dev/null +++ b/src/main/java/io/wispforest/owo/mixin/EulaReaderMixin.java @@ -0,0 +1,49 @@ +package io.wispforest.owo.mixin; + +import net.minecraft.server.dedicated.EulaReader; +import org.apache.logging.log4j.Logger; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; +import java.util.Scanner; + +@Mixin(EulaReader.class) +public class EulaReaderMixin { + + @Shadow + @Final + private static Logger LOGGER; + + @Shadow + @Final + private Path eulaFile; + + @Inject(method = "checkEulaAgreement", at = @At(value = "TAIL"), cancellable = true) + private void overrideEulaAgreement(CallbackInfoReturnable cir) { + var scanner = new Scanner(System.in); + LOGGER.info("By answering 'true' to this prompt you are indicating your agreement to Minecraft's EULA (https://account.mojang.com/documents/minecraft_eula)\nEULA:"); + + var input = scanner.next(); + if (!input.equalsIgnoreCase("true")) return; + + try (var inStream = Files.newInputStream(this.eulaFile); var outStream = Files.newOutputStream(this.eulaFile)) { + var properties = new Properties(); + properties.load(inStream); + properties.setProperty("eula", "true"); + properties.store(outStream, "By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula)."); + } catch (IOException e) { + LOGGER.info("Could not accept eula", e); + } + + LOGGER.info("EULA accepted"); + cir.setReturnValue(true); + } +} diff --git a/src/main/resources/owo.mixins.json b/src/main/resources/owo.mixins.json index 7d728853..4631d0f5 100644 --- a/src/main/resources/owo.mixins.json +++ b/src/main/resources/owo.mixins.json @@ -4,6 +4,7 @@ "package": "io.wispforest.owo.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ + "EulaReaderMixin", "FabricItemGroupBuilderMixin", "ItemMixin", "ItemSettingsMixin",