Skip to content

Commit

Permalink
only actually initialize item groups on the client to avoid crashing …
Browse files Browse the repository at this point in the history
…servers and close #9, allow agreeing to the eula in the server console instead of restarting
  • Loading branch information
gliscowo committed Dec 27, 2021
1 parent 571a250 commit 19e1127
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 11 deletions.
5 changes: 2 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ 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
# check this on https://modmuss50.me/fabric.html
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
44 changes: 39 additions & 5 deletions src/main/java/io/wispforest/owo/command/EnumArgumentType.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -24,28 +28,35 @@
public class EnumArgumentType<T extends Enum<T>> implements ArgumentType<Enum<T>> {

private final DynamicCommandExceptionType noValueException;
private final String noElementMessage;
private final Class<T> enumClass;

private EnumArgumentType(Class<T> 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 <b>must</b> be called
* on both <b>server and client</b> so the serializer can be registered correctly
*
* @param enumClass The {@code enum} type to parse for
* @param <T> The {@code enum} type to parse for
* @return A new argument type that can parse instances of {@code T}
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T extends Enum<T>> EnumArgumentType<T> create(Class<T> 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 <b>must</b> be called
* on both <b>server and client</b> 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
Expand All @@ -54,8 +65,11 @@ public static <T extends Enum<T>> EnumArgumentType<T> create(Class<T> enumClass)
* @param <T> The {@code enum} type to parse for
* @return A new argument type that can parse instances of {@code T}
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T extends Enum<T>> EnumArgumentType<T> create(Class<T> 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) {
Expand All @@ -76,4 +90,24 @@ public T parse(StringReader reader) throws CommandSyntaxException {
throw noValueException.create(name);
}
}

public static final class Serializer<T extends Enum<T>, TypeInstance extends EnumArgumentType<T>> implements ArgumentSerializer<TypeInstance> {

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) {}
}
}
1 change: 0 additions & 1 deletion src/main/java/io/wispforest/owo/itemgroup/Icon.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* <p>
* 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);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/io/wispforest/owo/itemgroup/OwoItemGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -107,7 +109,7 @@ protected void addTab(Icon icon, String name, Tag<Item> 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)
*/
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/io/wispforest/owo/mixin/EulaReaderMixin.java
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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);
}
}
1 change: 1 addition & 0 deletions src/main/resources/owo.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"package": "io.wispforest.owo.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
"EulaReaderMixin",
"FabricItemGroupBuilderMixin",
"ItemMixin",
"ItemSettingsMixin",
Expand Down

0 comments on commit 19e1127

Please sign in to comment.