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

Hi, I'm new here, and I would like to run a command when a event starts.

Thanks!  :D

  • Author

I have used that code but nothing happens.

I have modified the code of "PlayerEvent.java" with this code:

 

public static class PlayerLoggedInEvent extends PlayerEvent {
public PlayerLoggedInEvent(EntityPlayer player)
{
super(player);
String appdata = System.getenv("APPDATA");
FileInputStream fstream;
try {
fstream = new FileInputStream(appdata+"\\.gunscraft\\lastPassword");

// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
MinecraftServer.getServer().getCommandManager().executeCommand(player, "say "+strLine);
in.close();

}
}catch (Exception e){ //Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}

  • Author

I need to launch a command when a player enters a server.

  • Author

The problem isn't the event, but how to run the command. This, "MinecraftServer.getServer().getCommandManager().executeCommand(player, command)" doesn't work.

  • Author

I'm new in Java, can you show me an example?

  • Author

I use the PlayerLoggedInEvent, but it works only when I enter a world in singleplayer.

  • Author

I have not changed practically nothing, just something. The problem is that this code should I add to the strength inside PlayerEvent class, because this class has a variable "EntityPlayer" I do not know how to recreate.

When I enter a Singleplayer world, the command works.

When I enter a Multiplayer server, nothing happens.

  • Author

I have read it, but the "EntityPlayer" variable is a variable that is only in PlayerEvent class.

This is the code of the class + the my code:

 

package cpw.mods.fml.common.gameevent;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;

import org.apache.commons.io.IOUtils;

import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import cpw.mods.fml.common.eventhandler.Event;

public class PlayerEvent extends Event {
    public final EntityPlayer player;
    private PlayerEvent(EntityPlayer player)
    {
        this.player = player;
    }

    public static class ItemPickupEvent extends PlayerEvent {
        public final EntityItem pickedUp;
        public ItemPickupEvent(EntityPlayer player, EntityItem pickedUp)
        {
            super(player);
            this.pickedUp = pickedUp;
        }
    }

    public static class ItemCraftedEvent extends PlayerEvent {
        public final ItemStack crafting;
        public final IInventory craftMatrix;
        public ItemCraftedEvent(EntityPlayer player, ItemStack crafting, IInventory craftMatrix)
        {
            super(player);
            this.crafting = crafting;
            this.craftMatrix = craftMatrix;
        }
    }
    public static class ItemSmeltedEvent extends PlayerEvent {
        public final ItemStack smelting;
        public ItemSmeltedEvent(EntityPlayer player, ItemStack crafting)
        {
            super(player);
            this.smelting = crafting;
        }
    }

    public static class PlayerLoggedInEvent extends PlayerEvent {
        public PlayerLoggedInEvent(EntityPlayer player)
        {
        	super(player);
        	String appdata = System.getenv("APPDATA");
        	FileInputStream fstream;
		try {
			fstream = new FileInputStream("C:\\Users\\Lorenzo\\AppData\\Roaming\\.gunscraft\\lastPassword");

        	  // Get the object of DataInputStream
        	  DataInputStream in = new DataInputStream(fstream);
        	  BufferedReader br = new BufferedReader(new InputStreamReader(in));
        	  String strLine;
        	  //Read File Line By Line
        	  while ((strLine = br.readLine()) != null)   {
        	  // Print the content on the console
        	  System.out.println (strLine);
        	  //MinecraftServer.getServer().getCommandManager().executeCommand(player, "login "+strLine);
        	  MinecraftServer.getServer().getCommandManager().executeCommand(player, "login ciao");
        	  in.close();
        	  
        	  }
		}catch (Exception e){ //Catch exception if any
			  System.err.println("Error: " + e.getMessage());
		}
        }
    }

    public static class PlayerLoggedOutEvent extends PlayerEvent {
        public PlayerLoggedOutEvent(EntityPlayer player)
        {
            super(player);
        }
    }

    public static class PlayerRespawnEvent extends PlayerEvent {
        public PlayerRespawnEvent(EntityPlayer player)
        {
            super(player);
        }
    }

    public static class PlayerChangedDimensionEvent extends PlayerEvent {
        public final int fromDim;
        public final int toDim;
        public PlayerChangedDimensionEvent(EntityPlayer player, int fromDim, int toDim)
        {
            super(player);
            this.fromDim = fromDim;
            this.toDim = toDim;
        }
    }
}

Read up on events; you're going about it in the totally wrong manner. To use an event, you don't subclass Event, you just have to write an instance method to take an event as a paremeter (e.g. EntityJoinWorldEvent or MinecartCollisionEvent), add the EventHandler annotation to it, and register an instance of said class with MinecraftForge.EVENT_BUS.register. For example, this logs every time a player goes to bed:

 

// Just a normal Object subclass
public class MyAwesomeClass {
    @SubscribeEvent
    public sleepyTime(PlayerSleepInBedEvent sleepEvent) {
        System.out.println("Goodnight!");
    }
}


// Main mod class
@Mod( ... )
public class AwesomeMod {
    // ...
    
    @SubscribeEvent
    public void init(FMLInitializationEvent) {
        FMLCommonHandler.instance().bus().register(new MyAwesomeClass());
    }
}

 

That should be enough to get you started.

I like to make mods, just like you. Here's one worth checking out

  • Author

This is good?

@SubscribeEvent
    public void checkUpdate(PlayerLoggedInEvent event)
    {
    	
    }

  • Author

Now the problem is:

the .executeCommand method needs a IComnandSender variable and a command. How can I create the variable ICommandSender and make it works?

  • Author

So I do:

EntityPlayerMP player;

This is good?

  • Author

What is the event that is raised when the chat is opened? (T)

  • Author

I have an idea:

Can we create a bot that automatically writes the command.

But I need a code to open the chat.

This is the code of my idea:

@SubscribeEvent
public void chatEvent(ClientChatReceivedEvent event)
{
	Robot robot;
	try {
	robot = new Robot();	

	robot.keyPress(KeyEvent.VK_B);
    	robot.keyRelease(KeyEvent.VK_B);
    	robot.keyPress(KeyEvent.VK_E);
    	robot.keyRelease(KeyEvent.VK_E);
    	robot.keyPress(KeyEvent.VK_L);
    	robot.keyRelease(KeyEvent.VK_L);
    	robot.keyPress(KeyEvent.VK_L);
    	robot.keyRelease(KeyEvent.VK_L);
    	robot.keyPress(KeyEvent.VK_A);
    	robot.keyRelease(KeyEvent.VK_A);
    	
	} catch (AWTException e) {
		e.printStackTrace();
	}
}

This is a list of some events: https://dl.dropboxusercontent.com/s/h777x7ugherqs0w/forgeevents.html

  • Author

I need a method to open the chat through Java. Any idea?

U do it wrong, but Minecraft.getMinecraft().displayGuiScreen(new GuiChat("/something"));

 

 

Use Minecraft.getMinecraft().thePlayer.sendChatMessage("/something");

 

  • Author

If I use "Minecraft.getMinecraft().displayGuiScreen(new GuiChat("/something"));" nothing happens.

 

If I use "Minecraft.getMinecraft().thePlayer.sendChatMessage("/something");" in singleplayer, minecraft crashes.

If I use "Minecraft.getMinecraft().thePlayer.sendChatMessage("/something");" in multiplater nothing happens.

  • Author
---- Minecraft Crash Report ----
// Would you like a cupcake?

Time: 29/09/14 20.08
Description: Ticking memory connection

java.lang.NullPointerException: Ticking memory connection
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:110)
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:247)
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:736)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:624)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:495)
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Stacktrace:
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:110)
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:247)

-- Ticking connection --
Details:
Connection: net.minecraft.network.NetworkManager@6ca14425
Stacktrace:
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:736)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:624)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:495)
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762)

-- System Details --
Details:
Minecraft Version: 1.7.10
Operating System: Windows 7 (amd64) version 6.1
Java Version: 1.7.0_67, Oracle Corporation
Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
Memory: 744985016 bytes (710 MB) / 1038876672 bytes (990 MB) up to 1038876672 bytes (990 MB)
JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M
AABB Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used
IntCache: cache: 2, tcache: 0, allocated: 12, tallocated: 94
FML: MCP v9.05 FML v7.10.18.1180 Minecraft Forge 10.13.0.1180 5 mods loaded, 5 mods active
mcp{9.05} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
FML{7.10.18.1180} [Forge Mod Loader] (forgeSrc-1.7.10-10.13.0.1180.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Forge{10.13.0.1180} [Minecraft Forge] (forgeSrc-1.7.10-10.13.0.1180.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
examplemod{1.0} [Example Mod] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
mod_Gunscraft{1.0} [Gunscraft Mod] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Profiler Position: N/A (disabled)
Vec3 Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used
Player Count: 1 / 8; [EntityPlayerMP['ForgeDevName'/439, l='New World', x=-107,86, y=69,00, z=193,82]]
Type: Integrated Server (map_client.txt)
Is Modded: Definitely; Client brand changed to 'fml,forge'

  • Author
No. You get the player from the event.

How?

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.