Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hello, this is my first try at making a mod on Minecraft, and I'm facing a problem. I'm using forge's 1.16.1 example mod that i modified to suit my needs. My mod is very simple, I just want to execute some code when the player takes damage. For that, I'm using the LivingDamageEvent event, and it works exactly as i want in singleplayer. However, when I join a multiplayer server, it seems like my mod is turned off automatically for some reason. The FMLServerStartingEvent doesn't trigger because I don't see anything in the launcher_log.txt after I close minecraft, and same for LivingDamageEvent. Is there a reason for that ?

 

Here's the code i'm using, it's just a modified version of forge's example mod :
 

package com.example.examplemod;

import net.minecraft.block.Blocks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

// The value here should match an entry in the META-INF/mods.toml file
@Mod("examplemod")
public class examplemod
{
    // Directly reference a log4j logger.
    private static final Logger LOGGER = LogManager.getLogger();

    public examplemod() {
        // Register the setup method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
        // Register the doClientStuff method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);

        // Register ourselves for server and other game events we are interested in
        MinecraftForge.EVENT_BUS.register(this);
    }

    private void setup(final FMLCommonSetupEvent event)
    {
        // some preinit code
        LOGGER.info("HELLO FROM PREINIT");
        LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
    }

    @SuppressWarnings("resource")
	private void doClientStuff(final FMLClientSetupEvent event) {
        // do something that can only be done on the client
        LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().gameSettings);
    }
    // You can use SubscribeEvent and let the Event Bus discover methods to call
    @SubscribeEvent
    public void onServerStarting(FMLServerStartingEvent event) {
        // do something when the server starts
        LOGGER.info("HELLO from server starting");
    }
    
    public class MyForgeEventHandler {
        @SubscribeEvent
        public void pickupItem(EntityItemPickupEvent event) {
            System.out.println("Item picked up!");
        }
    }
    
    @SubscribeEvent
    public void onPlayerHurt(LivingDamageEvent event) throws IOException {
        System.out.println("Player attacked ! damage : " + event.getAmount());
        
        #Execute some code here ...
    }
    
}

 

15 minutes ago, clubmax27 said:

throws IOException

That method never cause this exception. There is no point in adding it here.

Even if some of your code might cause this, you should handle it inside the method properly (you are making a mod, not an API).

 

LivingDamageEvent is a Forge event. You are putting it in the Mod event subscriber.

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.

 

 

On 7/4/2020 at 10:01 AM, clubmax27 said:

However, when I join a multiplayer server, it seems like my mod is turned off automatically for some reason. The FMLServerStartingEvent doesn't trigger because I don't see anything in the launcher_log.txt after I close minecraft, and same for LivingDamageEvent. Is there a reason for that ?

When your client joins to a multiplayer server, the FMLServerStartingEvent doesn't run on the client because it's not running a server.

For some background info, see here http://greyminecraftcoder.blogspot.com/2020/02/the-client-server-division-1144.html

 

Just to make sure - do you realise that the multiplayer server will need to be running your mod as well as the client that you're using to connect to it?  You can create client-only mods but they are very limited in what they can do.

 

-TGG

 

 

 

18 hours ago, TheGreyGhost said:

When your client joins to a multiplayer server, the FMLServerStartingEvent doesn't run on the client because it's not running a server.

For some background info, see here http://greyminecraftcoder.blogspot.com/2020/02/the-client-server-division-1144.html

 

Just to make sure - do you realise that the multiplayer server will need to be running your mod as well as the client that you're using to connect to it?  You can create client-only mods but they are very limited in what they can do.

 

-TGG

 

 

 

Yes I am doing a client-only mod, it only checks if the player takes damage, and what amout of damage
Thanks to DavidM, I managed to get it working, but now I have a second, more complicated problem.

I'm now using LivingAttackEvent event to detect when i take damage, I check if the entity taking damage is my own character, and then I log the amout of damage taken.
This method works great with all "singleplayer" methods of taking damage, like fall damage, mobs, fire ... But when an other player hits me, the amout of damage that LivingAttackEvent logs is 0.0 damage, even with a diamond sword. Is that a bug from forge or am I doing this wrong ?

Here's the code i'm using :

@SubscribeEvent
	public void onEntityAttacked(LivingAttackEvent event)
    {
		if (event != null && event.getEntity() != null) 
		{
			
			Entity victimentity = event.getEntity();
			Entity sourceentity = event.getSource().getTrueSource();
			
			String playerName = "";       
			playerName = (String)(victimentity.getDisplayName().getString());
	        
	        
			if (victimentity.getDisplayName().getString().equals("Superburger90"))
			{
				System.out.println("Player attacked ! damage : " + event.getAmount());
			}
		}
	}

 

Hi

 

15 hours ago, clubmax27 said:

But when an other player hits me, the amout of damage that LivingAttackEvent logs is 0.0 damage, even with a diamond sword. Is that a bug from forge or am I doing this wrong ?

I don't know the answer, but you can find out fairly easily yourself by putting a breakpoint into your LivingAttackEvent, getting another  player to damage you, and then tracing back up the call stack to see why the damage is zero.

 

-TGG

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.