Jump to content

Run command from Java


Zio

Recommended Posts

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());
}
}
}

Link to comment
Share on other sites

What are you trying to do?

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
        }
    }
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.

×
×
  • Create New...

Important Information

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