Jump to content

[1.19] can i create a new GameRule value type?


Kaboom Roads

Recommended Posts

i want to make a gamerule with a float value. is this possible and how can i do it.

so far i have tried copying an existing type and changing it:

 public static class FloatValue extends GameRules.Value<FloatValue> {
        private float value;

        private static GameRules.Type<FloatValue> create(float p_46295_, BiConsumer<MinecraftServer, FloatValue> p_46296_) {
            return new GameRules.Type<>(FloatArgumentType::floatArg, (p_46293_) -> new FloatValue(p_46293_, p_46295_), p_46296_, GameRules.GameRuleTypeVisitor::visit);
        }

        public static GameRules.Type<FloatValue> create(float p_46313_) {
            return create(p_46313_, (p_46309_, p_46310_) -> {
            });
        }

        public FloatValue(GameRules.Type<FloatValue> p_46286_, float p_46287_) {
            super(p_46286_);
            this.value = p_46287_;
        }

        protected void updateFromArgument(@NotNull CommandContext<CommandSourceStack> p_46304_, @NotNull String p_46305_) {
            this.value = FloatArgumentType.getFloat(p_46304_, p_46305_);
        }

        public float get() {
            return this.value;
        }

        public void set(float p_151490_, @Nullable MinecraftServer p_151491_) {
            this.value = p_151490_;
            this.onChanged(p_151491_);
        }

        @NotNull
        public String serialize() {
            return Float.toString(this.value);
        }

        protected void deserialize(@NotNull String p_46307_) {
            this.value = safeParse(p_46307_);
        }

        public boolean tryDeserialize(String p_46315_) {
            try {
                this.value = Float.parseFloat(p_46315_);
                return true;
            } catch (NumberFormatException numberformatexception) {
                return false;
            }
        }

        private static float safeParse(String p_46318_) {
            if (!p_46318_.isEmpty()) {
                try {
                    return Float.parseFloat(p_46318_);
                } catch (NumberFormatException numberformatexception) {
                    LogUtils.getLogger().warn("Failed to parse float {}", p_46318_);
                }
            }
            return 0;
        }

        public int getCommandResult() {
            return (int) this.value;
        }

        @NotNull
        protected FloatValue getSelf() {
            return this;
        }

        @NotNull
        protected FloatValue copy() {
            return new FloatValue(this.type, this.value);
        }

        public void setFrom(FloatValue p_46298_, @Nullable MinecraftServer p_46299_) {
            this.value = p_46298_.value;
            this.onChanged(p_46299_);
        }
    }

there is a problem with the private static GameRules.Type<FloatValue> create(float p_46295_, BiConsumer<MinecraftServer, FloatValue> p_46296_) method.

the last parameter shows me this error when i hover over it:

'net.minecraft.world.level.GameRules.VisitorCaller' is not public in 'net.minecraft.world.level.GameRules'. Cannot be accessed from outside package

when i try compiling it tells me this:

error: <T>Type(Supplier<ArgumentType<?>>,Function<Type<T>,T>,BiConsumer<MinecraftServer,T>,VisitorCaller<T>) is not public in Type; cannot be accessed from outside package
            return new GameRules.Type<>(FloatArgumentType::floatArg, (p_46293_) -> new FloatValue(p_46293_, p_46295_), p_46296_, GameRules.GameRuleTypeVisitor::visit);
                   ^
  where T is a type-variable:
    T extends Value<T> declared in class Type

i tried using access transformers for this, one for the visitor caller as it tells me its private, and one for the Type constructor that it also tells me is private.

public net.minecraft.world.level.GameRules$VisitorCaller
public net.minecraft.world.level.GameRules$Type <init>

doesnt seem to do anything.

nothing shows up in the ide with the second error, its only when i try to compile.

Link to comment
Share on other sites

You need to rebuild the project using gradle so ForgeGradle has a chance to apply the access transformers to the minecraft code.

Trying running the clean then the build tasks

You might also have to run any "refresh gradle project" your ide has. e.g. for eclipse this can be accessed by right clicking the build.gradle

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

after refreshing gradle project a lot of things now line up with the compilation errors.

now it only says the Type constructor is private.

Type(java.util.function.Supplier<com.mojang.brigadier.arguments.ArgumentType<?>>, java.util.function.Function<net.minecraft.world.level.GameRules.Type<T>,T>, java.util.function.BiConsumer<net.minecraft.server.MinecraftServer,T>, net.minecraft.world.level.GameRules.VisitorCaller<T>)' is not public in 'net.minecraft.world.level.GameRules.Type'. Cannot be accessed from outside package

this is the access transformer i use for it: 

public net.minecraft.world.level.GameRules$Type <init>

 

Link to comment
Share on other sites

You are not using the default constructor. new Type()

The constructor you are using has parameters so you need to make that one public.

I don't know if you can use wildcards for constructors to make them all public?

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

if i ctrl click on it, it takes me to the correct constructor. i cant find anything about constructor access transformers. adding params is weird because it wants a return type as if it was a method, even though it is a constructor. i cant find anything about constructors in the AT documentation. when i have public net.minecraft.world.level.GameRules$Type <init> it tells me Access Transformer entry is never used.

 

Link to comment
Share on other sites

I have to admit I've never used an access transformer for a constructor

So, I went to the source code and found this example from the AT testsuite. Basically, Constructors have the Void (V) return type which is not very intuitive. 🙂 

https://github.com/MinecraftForge/AccessTransformers/blob/16c1bdbaf5abdde424a9cc31675beed5b9167e36/src/test/resources/forge_at.cfg#L5

But those examples look very old. Pre 1.17

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

it now works very well, i cant really find any problems with it at the moment.

these are the access transformers i used:

# Access visit in GameRuleTypeVisitor
public net.minecraft.world.level.GameRules$GameRuleTypeVisitor m_6889_(Lnet/minecraft/world/level/GameRules$Key;Lnet/minecraft/world/level/GameRules$Type;)V # visit

# Access VisitorCaller in GameRules
public net.minecraft.world.level.GameRules$VisitorCaller

# Access Type constructor in GameRules$Type
public net.minecraft.world.level.GameRules$Type <init>(Ljava/util/function/Supplier;Ljava/util/function/Function;Ljava/util/function/BiConsumer;Lnet/minecraft/world/level/GameRules$VisitorCaller;)V

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.