Jump to content

Recommended Posts

Posted

Damn I was stressing over this part of my mod.  Turns out it was actually the easiest part of the whole thing xD  Even figured out the two different pages of settings trick.  Thanks guys :>

Posted

Well, almost anyway xD  I did this:

package com.fuzzybat23.csbr.config;

import com.fuzzybat23.csbr.CSBR;
import net.minecraftforge.common.config.Config;
import net.minecraftforge.common.config.ConfigManager;
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.Mod;


/**
 * Created by V0idWa1k3r on 31-May-17.
 */
@Config(modid = CSBR.MODID)
public class ModConfig
{
    @Config.Comment("Configure AetherWorks's worldgen here")
    public static final Generation worldGen = new Generation();

    public static class Generation
    {
        @Config.Comment("Aether Ore generation settings")
        public final GenSettings oreAether = new GenSettings(1, 80, 128, 4,-1, 1);
    }

    public static class GenSettings
    {
        @Config.Comment("The amount of times the ore will try to generate in each chunk. Set to less than 1 to turn this into a chance to generate type of value")
        public float triesPerChunk;

        @Config.Comment("Minimum Y coordinate for this ore")
        public int minHeight;

        @Config.Comment("Maximum Y coordinate for this ore")
        public int maxHeight;

        @Config.Comment("The maximum size of the vein")
        public int veinSize;

        @Config.Comment("The list of dimension IDs this ore is NOT allowed to generate in")
        public int[] blacklistDimensions;

        GenSettings(float triesPerChunk, int minHeight, int maxHeight, int veinSize, int... blacklistDimensions)
        {
            this.triesPerChunk = triesPerChunk;
            this.minHeight = minHeight;
            this.maxHeight = maxHeight;
            this.veinSize = veinSize;
            this.blacklistDimensions = blacklistDimensions;
        }
    }

    @Config.Comment("Configure AetherWorks's worldgen here  -- 2")
    public static final Generation2 worldGen2 = new Generation2();

    public static class Generation2
    {
        @Config.Comment("Aether Ore generation settings -- 2")
        public final GenSettings2 oreAether2 = new GenSettings2(1, 80, 128, 4,-1, 1);
    }

    public static class GenSettings2
    {
        @Config.Comment("The amount of times the ore will try to generate in each chunk. Set to less than 1 to turn this into a chance to generate type of value -- 2")
        public float triesPerChunk2;

        @Config.Comment("Minimum Y coordinate for this ore -- 2")
        public int minHeight2;

        @Config.Comment("Maximum Y coordinate for this ore -- 2")
        public int maxHeight2;

        @Config.Comment("The maximum size of the vein -- 2")
        public int veinSize2;

        @Config.Comment("The list of dimension IDs this ore is NOT allowed to generate in -- 2")
        public int[] blacklistDimensions2;

        GenSettings2(float triesPerChunk2, int minHeight2, int maxHeight2, int veinSize2, int... blacklistDimensions2)
        {
            this.triesPerChunk2 = triesPerChunk2;
            this.minHeight2 = minHeight2;
            this.maxHeight2 = maxHeight2;
            this.veinSize2 = veinSize2;
            this.blacklistDimensions2 = blacklistDimensions2;
        }
    }

    @Mod.EventBusSubscriber(modid = CSBR.MODID)
    private static class Handler
    {
        public static void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event)
        {
            if (event.getModID().equals(CSBR.MODID))
            {
                ConfigManager.sync(CSBR.MODID, Config.Type.INSTANCE);
            }
        }
    }
}

Which leads to this in game.  worldgen opens oreAether which opens a first set of settings.  Then worldgen2 opens oreAether2 which opens the second set of settings.  How do I get it so there's the two buttons on the main config screen, click on one and it opens directly to the settings?

capture1.JPG

capture2.JPG

capture5.JPG

Posted

Hm, how do I explain this properly...

So, currently your code structure looks like this:

Config

| worldGen [oreAether]

| worldGen2 [oreAether2]

 

And this is exactly what you see in game. So basically for each object that holds at least one non-primitive non-serializable object forge creates a, let's call it a subcategory. Everything in that object that can't directly be serialized to a property will also be assigned a "sub-category" and so on and so forth.

 

Let's say I have a Config class. In that config class I have the following structure

Config

| client [rendering[primitives], sounds[primitives], options[primitives]]

That structure will lead to a GUI with a client button. Pressing it will reveal 3 buttons: rendering, sounds and options and pressing each one will lead to the corresponding settings.

Let's take a more direct world-gen example, with some code. Let's assume that the Generation class stays unchanged and I create a WorldGen class that simply contains a bunch of different Generation objects. Let's say that my structure is the following:

Config

| worldGen[bauxite, copper, tin, silver, nickel, ruby, sapphire, tungsten, pitchblende, beryl]

In game I then would see a worldgen button. Pressing it would reveal all those other buttons and pressing each one would lead me to a specific ore setting.

 

I hope that this is at least semi-understandable ;)

Posted

Ok.. so I did this with the two GenSettings.  That put the initial worldGen button leading to two oreAether buttons. 

    public static class Generation
    {
        @Config.Comment("Aether Ore generation settings")
        public final GenSettings oreAether = new GenSettings(1, 80, 128, 4,-1, 1);
        @Config.Comment("Aether Ore generation settings -- 2")
        public final GenSettings2 oreAether2 = new GenSettings2(1, 80, 128, 4,-1, 1);
    }

Basically I'm trying to get it to the point where there's x number of buttons on the initial config screen.  Click the button and it goes straight to the data entry, not to more buttons leading to the data entry xD

Posted

Never mind, I figured it out xD

 

@Config(modid=YourClass.MODID)
public class ModConfigClass
{
	@Config.Comment("Button 1")
	public static final Client client = new Client();
	
	public static class Client
	{
		@Config.Comment("Input field 1")
		public int var1 = 1;

		@Config.Comment("Input field 2")
		public static boolean foobar = false;
	}

	@Config.Comment("Button 2")
	public static final Client2 client2 = new Client2();
	
	public static class Client2
	{
		@Config.Comment("Input field 1")
		public int var1 = 1;

		@Config.Comment("Input field 2")
		public static boolean foobar = false;
	}

    @Mod.EventBusSubscriber(modid = YourClass.MODID)
    private static class Handler
    {
        public static void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event)
        {
            if (event.getModID().equals(YourClass.MODID))
            {
                ConfigManager.sync(YourClass.MODID, Config.Type.INSTANCE);
            }
        }
    }
}

 

Posted

Now that I have a working config file and config screen, how exactly do you access the values set in the cfg file? xD  Like.. say I'm in a method in my main class, do I call

ModConfigClass.Client.var1
or
ModConfigClass.Client2.foobar

 

Posted

Only the top-level @Config class can have static fields, the other classes need to have non-static fields.

 

You need to access the values from the static fields of the top-level class and the non-static fields of the other classes.

 

In your example, this would be ModConfigClass.client.var1 and ModConfigClass.client2.foobar (after you make the ModConfigClass.Client2.foobar field non-static).

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted (edited)

Yeah, I realized that about the booleans and fixed it.  So now when Minecraft starts up, it creates the config file.  I can go into the config screen from the mod options and there are the two buttons, Client and Client2.  They have the data entry fields that can be changed.  But I noticed a new problem.  Every time the game runs, it creates a brand new config file.  Also, and I checked this by opening the cfg file before and after changing the fields from the config screen in game, this isn't saving anything new to the config file.  If I click on foobar to change it from false to true and click done, it should save csbr.cfg with foobar now obeing true, and it doesn't.

 

Everything I have for my ModConfig.java is here.

package com.fuzzybat23.csbr.config;

import com.fuzzybat23.csbr.CSBR;
import net.minecraftforge.common.config.Config;
import net.minecraftforge.common.config.ConfigManager;
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.Mod;

@Config(modid = CSBR.MODID)
public class ModConfig
{
    @Config.Comment("Button 1")
    public static final Client client = new Client();

    public static class Client
    {
        @Config.Comment("Input field 1")
        public int var1 = 1;

        @Config.Comment("Input field 2")
        public boolean foobar = false;
    }

    @Config.Comment("Button 2")
    public static final Client2 client2 = new Client2();

    public static class Client2
    {
        @Config.Comment("Input field 1")
        public int var1 = 1;

        @Config.Comment("Input field 2")
        public boolean foobar = false;
    }

    @Mod.EventBusSubscriber(modid = CSBR.MODID)
    private static class Handler
    {
        public static void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event)
        {
            if (event.getModID().equals(CSBR.MODID))
            {
                ConfigManager.sync(CSBR.MODID, Config.Type.INSTANCE);
            }
        }
    }
}

 

I figure that I need to run a check to see if there is a cfg for my mod first, which I believe would use

 

if(ConfigManager.hasConfigForMod(CSBR.MODID))

 

But I'm not sure exactly where and how to put that in there.  On one side of the if else, it'd load the default values creating the cfg.  on the other side, it'd use ConfigManager.loadData(data) where data is... of the type ASMDataTable, whatever that is?

Edited by fuzzybat23
Posted

Your ModConfig.Handler.onConfigChanged method isn't annotated with @SubscribeEvent, so it's never called and the config file is never saved.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

I don't know how I missed that xD  Late night coding, no doubt.  Thanks!  I don't suppose you know how to do a slider bar with the annotation system, or a Cycle Value String or a slider bar for picking a number, say between 0 amd 255, do you?  In the other system, that uses GuiFactory, it went something like this:

stringsList.add(new DummyConfigElement("cycleString", "this", ConfigGuiType.STRING, "fml.config.sample.cycleString", new String[] {"this", "property", "cycles", "through", "a", "list", "of", "valid", "choices"}));

and
 
numbersList.add((new DummyConfigElement("sliderInteger", 2000, ConfigGuiType.INTEGER, "fml.config.sample.sliderInteger", 100, 10000)).setCustomListEntryClass(NumberSliderEntry.class));

 

Posted

You can get a Cycle Value String control by using an enum field.

 

To get a Slider control, you'll need to get the Property from the Configuration instance Forge created for your @Config class and call Property#setConfigEntryClass with NumberSliderEntry.class.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted
16 minutes ago, fuzzybat23 said:

What would the code look like as far as the number slider goes?  I think instead of a cycle button, I'll just use a number slider for that also, with a set between 0 and 4

 

It looks like you'll need to use reflection to call ConfigManager.getConfiguration with the mod ID and name you specified in your @Config annotation, this will return your mod's Configuration instance.

 

You can then use Configuration#getCategory to get the ConfigCategory (you can specify a full path by separating each category name with periods . ) and ConfigCategory#get(String) to get the Property.

 

You'll want to do this on the physical client (e.g. in your client proxy) in preInit.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted
1 hour ago, fuzzybat23 said:

Because ConfigManager is where the numberLlist and stringList methods are located, right?

 

No, ConfigManager creates and stores the Configuration instance for each @Config class.

 

You get the Property from the Configuration (via the ConfigCategory) and call Property#setConfigEntryClass with NumberSliderEntry.class.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted (edited)

So.. my main java file is pretty empty right now.

package com.fuzzybat23.csbr;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;


@net.minecraftforge.fml.common.Mod(modid = "csbr", version = "1.0", clientSideOnly = true, acceptedMinecraftVersions = "[1.12, 1.13)")
public class CSBR
{
    @net.minecraftforge.fml.common.Mod.Instance("CSBR")
    public static final String MODID = "csbr";

    public static CSBR instance;


    @Mod.EventHandler
    @SideOnly(Side.CLIENT)
    public void preInit(FMLPreInitializationEvent event)
    {
		I'd put some of that stuff you mentioned here, correct?
    }
    
} 

 

I just tried putting ConfigManager.getConfiguration in there, but when I started typing, there was no option for getConfiguration, only getModConfigClasses(String str)

Edited by fuzzybat23
Posted

This is frustrating.  I know nothing about reflection or the # symbol.  The annotation system has a @Config.RangeInt(min = x, max = y) built in, I'd think that by using that would automatically insert a number slider.

Posted (edited)
10 hours ago, fuzzybat23 said:

I just tried putting ConfigManager.getConfiguration in there, but when I started typing, there was no option for getConfiguration, only getModConfigClasses(String str)

 

As I said, you need to call it with reflection. This is because it's package-private rather than public.

 

8 hours ago, fuzzybat23 said:

This is frustrating.  I know nothing about reflection or the # symbol. 

 

I use the Class#member notation to refer to the non-static field or method member in Class. I use Class.member to refer to static fields and methods.

 

You can use ReflectionHelper.findMethod to get a Method object referring to a method and then use Method#invoke to call the method. The first argument is the object to call it on (use null for static methods) and the vararg is the arguments to pass to the method.

 

If you were calling this method more than once, you'd want to store the Method object in a private static final field so you only do the expensive lookup once. The same applies to reflecting fields and Field objects.

 

You need to do this in your client proxy (or another client-only class called from it), not just mark the preInit method in your @Mod class as @SideOnly. If your mod is client-only, it's probably safe to do this in your @Mod class.

 

8 hours ago, fuzzybat23 said:

The annotation system has a @Config.RangeInt(min = x, max = y) built in, I'd think that by using that would automatically insert a number slider.

 

You could suggest this in the Suggestions section or on GitHub.

 

Side note:

The @Mod.Instance annotation is meant to be applied to the field that will store the instance of your @Mod class, not the field storing your mod ID. Why are you using fully-qualified names for the @Mod annotation?

Edited by Choonster

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Like I said, I know absolutely nothing about reflection, so saying call it with reflection means pretty much zero to me.  I have absolutely no idea what needs to go in ClientProxy.java's preinit.  Anyway, to put it into perspective, I suppose that I would need to use the expensive lookup more than once.  I would prefer every data entry of my config screen to be a slider, all except for the boolean, and maybe the cycle string, which I have no idea how to create with an enum field either.  As it stands, my config looks like this.

 

package com.fuzzybat23.csbr;

import net.minecraftforge.common.config.Config;
import net.minecraftforge.common.config.Config.*;
import net.minecraftforge.common.config.ConfigManager;
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@Config(modid = CSBR.MODID)
@LangKey("csbr.config.title")
public class ModConfig
{
    @Name("Custom Selection Box Frame")
    @Comment("Color and opacity for custom selection box wire frame.")
    public static final Frame frame = new Frame();

    @Name("Custom Selection Box Cube")
    @Comment("Color and opacity for custom selection box inner cube.")
    public static final Blink blink = new Blink();

    @Name("Animation and frame thickness.")
    @Comment("Break animation style and speed for custom selection box.")
    public static final Break b = new Break();

    public static class Frame
    {
        @Name("1) Red")
        @Comment("Choose a value for red between 0 and 255.")
        @RangeInt(min = 0, max = 255)
        public int Red = 255;

        @Name("2) Green")
        @Comment("Choose a value for green between 0 and 255.")
        @RangeInt(min = 0, max = 255)
        public int Green = 255;

        @Name("3) Blue")
        @Comment("Choose a value for blue between 0 and 255.")
        @RangeInt(min = 0, max = 255)
        public int Blue = 255;

        @Name("4) Alpha Channel")
        @Comment("Choose a value for the opacity between 0 and 255.")
        @RangeInt(min = 0, max = 255)
        public int Alpha = 255;

        @Name("5) Wire Thickness")
        @Comment("Choose a value for the wire frame thickness between 1 and 7.")
        @RangeInt(min = 1, max = 7)
        public int Width = 2;
    }

    public static class Blink
    {
        @Name("1) Red")
        @Comment("Choose a value for red between 0 and 255.")
        @RangeInt(min = 0, max = 255)
        public int Red = 255;

        @Name("2) Green")
        @Comment("Choose a value for green between 0 and 255.")
        @RangeInt(min = 0, max = 255)
        public int Green = 255;

        @Name("3) Blue")
        @Comment("Choose a value for blue between 0 and 255.")
        @RangeInt(min = 0, max = 255)
        public int Blue = 255;

        @Name("4) Alpha Channel")
        @Comment("Choose a value for the opacity between 0 and 255.")
        @RangeInt(min = 0, max = 255)
        public int Alpha = 255;

        @Name("5) Blink Speed")
        @Comment("Choose how fast the custom selection box blinks.")
        @RangeInt(min = 0, max = 100)
        public int Speed = 0;
    }

    public static class Break
    {
        @Name("1) Break Animation")
        @Comment({"Choose a value for the animation to display when breaking blocks.", "0) NONE, 1) SHRINK, 2) DOWN. 3) ALPHA"})
        @RangeInt(min = 0, max = 3)
        public int Animation = 0;

        @Name("2) Depth Buffer")
        @Comment("Enable or disable the depth buffer for the custom selection box wire frame.")
        public boolean dBuffer = false;
    }

    @Mod.EventBusSubscriber(modid = CSBR.MODID)
    private static class Handler
    {
        @SubscribeEvent
        public static void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event)
        {
            if (event.getModID().equals(CSBR.MODID))
            {
                ConfigManager.sync(CSBR.MODID, Config.Type.INSTANCE);
            }
        }
    }
}

 

which generates a nice pretty cfg as such

 

# Configuration file

general {

    ##########################################################################################################
    # custom selection box frame
    #--------------------------------------------------------------------------------------------------------#
    # Color and opacity for custom selection box wire frame.
    ##########################################################################################################

    "custom selection box frame" {
        # Choose a value for red between 0 and 255.
        # Min: 0
        # Max: 255
        I:"1) Red"=255

        # Choose a value for green between 0 and 255.
        # Min: 0
        # Max: 255
        I:"2) Green"=255

        # Choose a value for blue between 0 and 255.
        # Min: 0
        # Max: 255
        I:"3) Blue"=255

        # Choose a value for the opacity between 0 and 255.
        # Min: 0
        # Max: 255
        I:"4) Alpha Channel"=255

        # Choose a value for the wire frame thickness between 1 and 7.
        # Min: 1
        # Max: 7
        I:"5) Wire Thickness"=2
    }

    ##########################################################################################################
    # custom selection box cube
    #--------------------------------------------------------------------------------------------------------#
    # Color and opacity for custom selection box inner cube.
    ##########################################################################################################

    "custom selection box cube" {
        # Choose a value for red between 0 and 255.
        # Min: 0
        # Max: 255
        I:"1) Red"=255

        # Choose a value for green between 0 and 255.
        # Min: 0
        # Max: 255
        I:"2) Green"=255

        # Choose a value for blue between 0 and 255.
        # Min: 0
        # Max: 255
        I:"3) Blue"=255

        # Choose a value for the opacity between 0 and 255.
        # Min: 0
        # Max: 255
        I:"4) Alpha Channel"=255

        # Choose how fast the custom selection box blinks.
        # Min: 0
        # Max: 100
        I:"5) Blink Speed"=0
    }

    ##########################################################################################################
    # animation and frame thickness
    #--------------------------------------------------------------------------------------------------------#
    # Break animation style and speed for custom selection box.
    ##########################################################################################################

    "animation and frame thickness" {
        # Choose a value for the animation to display when breaking blocks.
        # 0) NONE, 1) SHRINK, 2) DOWN. 3) ALPHA
        # Min: 0
        # Max: 3
        I:"1) Break Animation"=0

        # Enable or disable the depth buffer for the custom selection box wire frame.
        B:"2) Depth Buffer"=false
    }

}

 

Posted
26 minutes ago, fuzzybat23 said:

Like I said, I know absolutely nothing about reflection, so saying call it with reflection means pretty much zero to me.  I have absolutely no idea what needs to go in ClientProxy.java's preinit.

 

Have you tried to follow the instructions in my previous post?

 

27 minutes ago, fuzzybat23 said:

and maybe the cycle string, which I have no idea how to create with an enum field either.

 

Create an enum with the values NONE, SHRINK, DOWN and ALPHA and use this as the field's type.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

I Tried, but since, as I've said, I know nothing about reflection, I'm stumbling in the dark. What I need is actual code examples I can learn from. Just simply saying use reflection confuses the hell out of me xD As far as the enumerate field goes, it'd look something like this?

 

Public static class Break
{
     @Name("Break animation") 
     @Comment("0) none, 1)shrink 2)down 3)alpha")
     public enum animation 
     {     NONE, SHRINK, DOWN, ALPHA    } 
} 

 

Posted
2 minutes ago, fuzzybat23 said:

I Tried, but since, as I've said, I know nothing about reflection, I'm stumbling in the dark. What I need is actual code examples I can learn from. Just simply saying use reflection confuses the hell out of me

 

Was there a particular part you didn't understand? I tried to explain it step-by-step.

 

5 minutes ago, fuzzybat23 said:

As far as the enumerate field goes, it'd look something like this?

 

The enum looks correct, but you need to create a field that uses it and annotate that field with @Config.Name/@Config.Comment rather than annotating the enum. The field is what Forge uses as the basis of the config property.

 

You should also use PascalCase for enum, class and interface names, not camelCase.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Yeah, normally I use PascalCase.  I was writing that email from my phone at work, since our internet blocks out this forum because someone just had to put the keyword "game" in its meta data.  Anyway, I did get the cycle string working.

 

    public static class Break
    {
        public enum Animation
        {   NONE, SHRINK, DOWN, ALPHA   }

        @Name("1) Break Animation")
        @Comment({"Choose a value for the animation to display when breaking blocks.", "0) NONE, 1) SHRINK, 2) DOWN. 3) ALPHA"})
        public Animation animation = Animation.NONE;

        @Name("2) Depth Buffer")
        @Comment("Enable or disable the depth buffer for the custom selection box wire frame.")
        public boolean dBuffer = false;
    }

 

Now as to the number slider..  I think I need to start fresh from scratch on that.

On 7/13/2017 at 7:45 AM, Choonster said:

You can get a Cycle Value String control by using an enum field.

 

To get a Slider control, you'll need to get the Property from the Configuration instance Forge created for your @Config class and call Property#setConfigEntryClass with NumberSliderEntry.class.

That's the first thing you said in regard to the number slider control.  So first, how exactly do I get the property from the Configuration instance forge created when I first called @Config?  You mentioned that would need to go in the preInit in my ClientProxy.java, or could I insert this into my ModConfig.java?

    @Mod.EventHandler
    @SideOnly(Side.CLIENT)
    public void preInit(FMLPreInitializationEvent event)
    {

    }

 

Posted

Actually, the cycle selection control only half works.  It does cycle through NONE, SHRINK, DOWN and ALPHA, but it doesn't save the value last selected when clicking the Done button.

Posted
51 minutes ago, fuzzybat23 said:

So first, how exactly do I get the property from the Configuration instance forge created when I first called @Config?

 

  • Use reflection to call ConfigManager.getConfiguration with the mod ID and name specified in the @Config annotation and get the Configuration instance:
    11 hours ago, Choonster said:

    You can use ReflectionHelper.findMethod to get a Method object referring to a method and then use Method#invoke to call the method. The first argument is the object to call it on (use null for static methods) and the vararg is the arguments to pass to the method.

     

  • Call Configuration#getCategory with the full path of the property's category (separating each category name with periods) to get the ConfigCategory.

  • Call ConfigCategory#get(String) with the property's name to get the Property.

 

58 minutes ago, fuzzybat23 said:

You mentioned that would need to go in the preInit in my ClientProxy.java, or could I insert this into my ModConfig.java?

 

@Mod.EventHandler methods are only called if they're in your @Mod class, annotating methods in other classes won't do anything.

 

GUIs only exist on the physical client, so you can't reference GuiConfigEntries.NumberSliderEntry in a method that would be called on the physical server. If your entire mod is client-only, you don't really need to worry about this since none of your methods will be called on the physical server.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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




  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • It is 1.12.2 - I have no idea if there is a 1.12 pack
    • Okay, but does the modpack works with 1.12 or just with 1.12.2, because I need the Forge client specifically for Minecraft 1.12, not 1.12.2
    • Version 1.19 - Forge 41.0.63 I want to create a wolf entity that I can ride, so far it seems to be working, but the problem is that when I get on the wolf, I can’t control it. I then discovered that the issue is that the server doesn’t detect that I’m riding the wolf, so I’m struggling with synchronization. However, it seems to not be working properly. As I understand it, the server receives the packet but doesn’t register it correctly. I’m a bit new to Java, and I’ll try to provide all the relevant code and prints *The comments and prints are translated by chatgpt since they were originally in Spanish* Thank you very much in advance No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. MountableWolfEntity package com.vals.valscraft.entity; import com.vals.valscraft.network.MountSyncPacket; import com.vals.valscraft.network.NetworkHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.animal.Wolf; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.network.PacketDistributor; public class MountableWolfEntity extends Wolf { private boolean hasSaddle; private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(MountableWolfEntity.class, EntityDataSerializers.BYTE); public MountableWolfEntity(EntityType<? extends Wolf> type, Level level) { super(type, level); this.hasSaddle = false; } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_FLAGS, (byte)0); } public static AttributeSupplier.Builder createAttributes() { return Wolf.createAttributes() .add(Attributes.MAX_HEALTH, 20.0) .add(Attributes.MOVEMENT_SPEED, 0.3); } @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { ItemStack itemstack = player.getItemInHand(hand); if (itemstack.getItem() == Items.SADDLE && !this.hasSaddle()) { if (!player.isCreative()) { itemstack.shrink(1); } this.setSaddle(true); return InteractionResult.SUCCESS; } else if (!level.isClientSide && this.hasSaddle()) { player.startRiding(this); MountSyncPacket packet = new MountSyncPacket(true); // 'true' means the player is mounted NetworkHandler.CHANNEL.sendToServer(packet); // Ensure the server handles the packet return InteractionResult.SUCCESS; } return InteractionResult.PASS; } @Override public void travel(Vec3 travelVector) { if (this.isVehicle() && this.getControllingPassenger() instanceof Player) { System.out.println("The wolf has a passenger."); System.out.println("The passenger is a player."); Player player = (Player) this.getControllingPassenger(); // Ensure the player is the controller this.setYRot(player.getYRot()); this.yRotO = this.getYRot(); this.setXRot(player.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = this.getYRot(); this.yHeadRot = this.yBodyRot; float forward = player.zza; float strafe = player.xxa; if (forward <= 0.0F) { forward *= 0.25F; } this.flyingSpeed = this.getSpeed() * 0.1F; this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.5F); this.setDeltaMovement(new Vec3(strafe, travelVector.y, forward).scale(this.getSpeed())); this.calculateEntityAnimation(this, false); } else { // The wolf does not have a passenger or the passenger is not a player System.out.println("No player is mounted, or the passenger is not a player."); super.travel(travelVector); } } public boolean hasSaddle() { return this.hasSaddle; } public void setSaddle(boolean hasSaddle) { this.hasSaddle = hasSaddle; } @Override protected void dropEquipment() { super.dropEquipment(); if (this.hasSaddle()) { this.spawnAtLocation(Items.SADDLE); this.setSaddle(false); } } @SubscribeEvent public static void onServerTick(TickEvent.ServerTickEvent event) { if (event.phase == TickEvent.Phase.START) { MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(); if (server != null) { for (ServerPlayer player : server.getPlayerList().getPlayers()) { if (player.isPassenger() && player.getVehicle() instanceof MountableWolfEntity) { MountableWolfEntity wolf = (MountableWolfEntity) player.getVehicle(); System.out.println("Tick: " + player.getName().getString() + " is correctly mounted on " + wolf); } } } } } private boolean lastMountedState = false; @Override public void tick() { super.tick(); if (!this.level.isClientSide) { // Only on the server boolean isMounted = this.isVehicle() && this.getControllingPassenger() instanceof Player; // Only print if the state changed if (isMounted != lastMountedState) { if (isMounted) { Player player = (Player) this.getControllingPassenger(); // Verify the passenger is a player System.out.println("Server: Player " + player.getName().getString() + " is now mounted."); } else { System.out.println("Server: The wolf no longer has a passenger."); } lastMountedState = isMounted; } } } @Override public void addPassenger(Entity passenger) { super.addPassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(true)); } } } @Override public void removePassenger(Entity passenger) { super.removePassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is no longer mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(false)); } } } @Override public boolean isControlledByLocalInstance() { Entity entity = this.getControllingPassenger(); return entity instanceof Player; } @Override public void positionRider(Entity passenger) { if (this.hasPassenger(passenger)) { double xOffset = Math.cos(Math.toRadians(this.getYRot() + 90)) * 0.4; double zOffset = Math.sin(Math.toRadians(this.getYRot() + 90)) * 0.4; passenger.setPos(this.getX() + xOffset, this.getY() + this.getPassengersRidingOffset() + passenger.getMyRidingOffset(), this.getZ() + zOffset); } } } MountSyncPacket package com.vals.valscraft.network; import com.vals.valscraft.entity.MountableWolfEntity; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class MountSyncPacket { private final boolean isMounted; public MountSyncPacket(boolean isMounted) { this.isMounted = isMounted; } public void encode(FriendlyByteBuf buffer) { buffer.writeBoolean(isMounted); } public static MountSyncPacket decode(FriendlyByteBuf buffer) { return new MountSyncPacket(buffer.readBoolean()); } public void handle(NetworkEvent.Context context) { context.enqueueWork(() -> { ServerPlayer player = context.getSender(); // Get the player from the context if (player != null) { // Verifies if the player has dismounted if (!isMounted) { Entity vehicle = player.getVehicle(); if (vehicle instanceof MountableWolfEntity wolf) { // Logic to remove the player as a passenger wolf.removePassenger(player); System.out.println("Server: Player " + player.getName().getString() + " is no longer mounted."); } } } }); context.setPacketHandled(true); // Marks the packet as handled } } networkHandler package com.vals.valscraft.network; import com.vals.valscraft.valscraft; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class NetworkHandler { private static final String PROTOCOL_VERSION = "1"; public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel( new ResourceLocation(valscraft.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); public static void init() { int packetId = 0; // Register the mount synchronization packet CHANNEL.registerMessage( packetId++, MountSyncPacket.class, MountSyncPacket::encode, MountSyncPacket::decode, (msg, context) -> msg.handle(context.get()) // Get the context with context.get() ); } }  
  • Topics

×
×
  • Create New...

Important Information

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