Jump to content

Recommended Posts

Posted (edited)

I wish to create a forge mod for 1.15.2 which drains the hunger bar in peaceful. I know you can do /gamerule doMobSpawning false but that doesn't stop spawners. In the minecraft code there is a file called FoodStats.java which contains the game logic for hunger. 

How can I change it so that the hunger goes down in peaceful?

Edited by hungerinpeaceful
Posted
Just now, diesieben07 said:

Please do not post Minecraft code, we can all look at it in our IDE.

Sorry about that.

 

Just now, diesieben07 said:

You can use PlayerTickEvent (make sure to check TickEvent#phase) and change the food level that way.

How would I do that?

Posted
10 minutes ago, hungerinpeaceful said:

that didn't work. which event should i use for the event handler?

PlayerTickEvent. Make sure you are executing the hunger changing code on the server side (read up on Sides in the documentation if you are unsure).

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Posted
9 hours ago, hungerinpeaceful said:

I read the documentation however I do not understand how to make my event work on the server side only. When I use


DistExecuter.runWhenOn

runWhenOn cannot be resolved. How could I make my code into server side only?

Proxies are not necessary in this case.

The event provide you with an instance of World; use World#isRemote (which returns true on client and false on server) to check what side you are on.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Posted

How would I make my code 

@SubscribeEvent
    public void hunger(TickEvent.PlayerTickEvent event) {
        PlayerEntity player = null;
        Difficulty difficulty = player.world.getDifficulty();
        this.prevFoodLevel = this.foodLevel;
        if (this.foodExhaustionLevel > 4.0F) {
            this.foodExhaustionLevel -= 4.0F;
            if (this.foodSaturationLevel > 0.0F) {
                this.foodSaturationLevel = Math.max(this.foodSaturationLevel - 1.0F, 0.0F);
            } else if (difficulty != Difficulty.PEACEFUL) {
                this.foodLevel = (int) Math.max(this.foodLevel - 1.0f, 0.0f);
            }
        }
    }

use world#isRemote?

 

Posted (edited)
3 hours ago, hungerinpeaceful said:

How would I make my code 


@SubscribeEvent
    public void hunger(TickEvent.PlayerTickEvent event) {
        PlayerEntity player = null;
        Difficulty difficulty = player.world.getDifficulty();
        this.prevFoodLevel = this.foodLevel;
        if (this.foodExhaustionLevel > 4.0F) {
            this.foodExhaustionLevel -= 4.0F;
            if (this.foodSaturationLevel > 0.0F) {
                this.foodSaturationLevel = Math.max(this.foodSaturationLevel - 1.0F, 0.0F);
            } else if (difficulty != Difficulty.PEACEFUL) {
                this.foodLevel = (int) Math.max(this.foodLevel - 1.0f, 0.0f);
            }
        }
    }

use world#isRemote?

 

Use an if statement, 

if(event.player.world.isRemote()) {
  //Insert code here
}

if you don't know how to change the player's hunger level try something like:

event.player.getFoodStats().addStats(0, 0.0f);

 

Edited by Novârch

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Posted
Just now, hungerinpeaceful said:

I am getting this error. Cannot resolve symbol event and the word event turns red. How do I fix this?

Post your code.

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Posted
Just now, Novârch said:

Post your code.

if(event.player.world.isRemote()) {
            public void hunger(TickEvent.PlayerTickEvent event) {
                PlayerEntity player = null;
                Difficulty difficulty = player.world.getDifficulty();
                this.prevFoodLevel = this.foodLevel;
                if (this.foodExhaustionLevel > 4.0F) {
                    this.foodExhaustionLevel -= 4.0F;
                    if (this.foodSaturationLevel > 0.0F) {
                        this.foodSaturationLevel = Math.max(this.foodSaturationLevel - 1.0F, 0.0F);
                    } else if (difficulty != Difficulty.PEACEFUL) {
                        this.foodLevel = (int) Math.max(this.foodLevel - 1.0f, 0.0f);
                    }
                }
            }
        }

 

Posted (edited)
5 minutes ago, hungerinpeaceful said:

if(event.player.world.isRemote()) {
            public void hunger(TickEvent.PlayerTickEvent event) {
                PlayerEntity player = null;
                Difficulty difficulty = player.world.getDifficulty();
                this.prevFoodLevel = this.foodLevel;
                if (this.foodExhaustionLevel > 4.0F) {
                    this.foodExhaustionLevel -= 4.0F;
                    if (this.foodSaturationLevel > 0.0F) {
                        this.foodSaturationLevel = Math.max(this.foodSaturationLevel - 1.0F, 0.0F);
                    } else if (difficulty != Difficulty.PEACEFUL) {
                        this.foodLevel = (int) Math.max(this.foodLevel - 1.0f, 0.0f);
                    }
                }
            }
        }

 

Do you know anything about Java? The code should look like this:

            public void hunger(TickEvent.PlayerTickEvent event) {
              if(event.player.world.isRemote())
              {
                PlayerEntity player = event.player;
                Difficulty difficulty = player.world.getDifficulty();
                this.prevFoodLevel = this.foodLevel;
                if (this.foodExhaustionLevel > 4.0F) {
                    this.foodExhaustionLevel -= 4.0F;
                    if (this.foodSaturationLevel > 0.0F) {
                        this.foodSaturationLevel = Math.max(this.foodSaturationLevel - 1.0F, 0.0F);
                    } else if (difficulty != Difficulty.PEACEFUL) {
                        this.foodLevel = (int) Math.max(this.foodLevel - 1.0f, 0.0f);
                    }
                }
              }
            }

Anyway, learn Java basics before modding.

Quote

You need to learn basic Java programming before writing mods.

 

Edited by Novârch
Dieseben made a nice comment.

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

  • 7 months later...
Posted

Someone please tell me the OP has been spending the last eight months learning Java? 

 

Also, side note; if you would like to add another type of survival data, like "Insanity", which drains the longer you spend in low light levels, you could use compatibilities to create the data and assign it to the players. 

  • 3 years later...
Posted

Wait guys, I still don't don't understand... I shall send my code

-- Define the function to decrease hunger
local function decreaseHunger(player)
    local currentHunger = player.getHunger()
    
    -- Check if hunger is not already zero
    if currentHunger > 0 then
        player.setHunger(currentHunger - 1)
    end
end

-- Register event to decrease hunger over time
script.registerEvent(EntityPlayer, function(player)
    while true do
        -- Decrease hunger every few seconds
        decreaseHunger(player)
        script.sleep(1000) -- Sleep for 1 second
    end
end)
 

 

Any help appriecated.

It's been a while since I learnt Javascript or whatever Minecraft is made with... ... ...

...

Nicknotname Hungerinpeaceful xx

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.