<commit_before>SimpleImpl
==========

SimpleImpl is the name given to the packet system that revolves around the `SimpleNetworkWrapper` class. Using this system is by far the easiest way to send custom data between clients and the server.

Getting Started
---------------

First you need to create your `SimpleNetworkWrapper` object. We recommend that you do this in a separate class, possibly something like `ModidPacketHandler`. Create your `SimpleNetworkWrapper` as a static field in this class, like so:

`public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel("mymodid");`

Where `"mymodid"` is a short identifier for your packet channel, typically just your mod ID, unless that is unusually long.

Making Packets
--------------

### IMessage

A packet is defined by using the `IMessage` interface. This interface defines 2 methods, `toBytes` and `fromBytes`. These methods, respectively, write and read the data in your packet to and from a `ByteBuf` object, which is an object used to hold a stream (array) of bytes which are sent through the network.

For an example, let's define a small packet that is designed to send a single int over the network:

```
public class Packet implements IMessage {
  // A default constructor is always required
  public Packet(){}

  private int toSend;
  public Packet(int toSend) {
    this.toSend = toSend;
  }

  @Override public void toBytes(ByteBuf buf) {
    // Writes the int into the buf
    buf.writeInt(toSend);
  }

  @Override public void fromBytes(ByteBuf buf) {
    // Reads the int back from the buf. Note that if you have multiple values, you must read in the same order you wrote.
    toSend = buf.readInt();
  }
}
```

### IMessageHandler

Now, how can we use this packet? Well, first we must have a class that can *handle* this packet. This is created with the `IMessageHandler` interface. Say we wanted to use this integer we sent to give the player that many diamonds on the server. Let's make this handler:

```
// The params of the IMessageHandler are <REQ, REPLY>, meaning that the first is the packet you are receiving, and the second is the packet you are returning. The returned packet can be used as a "response" from a sent packet.
public class PacketHandler implements IMessageHandler<Packet, IMessage> {
  // Do note that the default constructor is required, but implicitly defined in this case

  @Override public IMessage onMessage(Packet message, MessageContext ctx) {
    // This is the player the packet was sent to the server from
    EntityPlayerMP serverPlayer = ctx.getServerHandler().playerEntity;
    // The value that was sent
    int amount = message.toSend;
    serverPlayer.inventory.addItemStackToInventory(new ItemStack(Items.diamond, amount));
    // No response packet
    return null;
  }
}
```
It is recommended (but not required) that for organization's sake, this class is an inner class to your Packet class. If this is done, note that the class must also be declared `static`.

Registering Packets
-------------------

So now we have a packet, and a handler for this packet. But the `SimpleNetworkWrapper` needs one more thing to function! For it to use a packet, the packet must be registered with an *discriminator*, which is just an integer used to map packet types between server and client. To do this, call `INSTANCE.registerMessage(PacketHandler.class, Packet.class, 0, Side.Server);` where `INSTANCE` is the `SimpleNetworkWrapper` we defined earlier.<commit_msg>Add note about packets on different threads in 1.8<commit_after>SimpleImpl
==========

SimpleImpl is the name given to the packet system that revolves around the `SimpleNetworkWrapper` class. Using this system is by far the easiest way to send custom data between clients and the server.

Getting Started
---------------

First you need to create your `SimpleNetworkWrapper` object. We recommend that you do this in a separate class, possibly something like `ModidPacketHandler`. Create your `SimpleNetworkWrapper` as a static field in this class, like so:

`public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel("mymodid");`

Where `"mymodid"` is a short identifier for your packet channel, typically just your mod ID, unless that is unusually long.

Making Packets
--------------

### IMessage

A packet is defined by using the `IMessage` interface. This interface defines 2 methods, `toBytes` and `fromBytes`. These methods, respectively, write and read the data in your packet to and from a `ByteBuf` object, which is an object used to hold a stream (array) of bytes which are sent through the network.

For an example, let's define a small packet that is designed to send a single int over the network:

```
public class Packet implements IMessage {
  // A default constructor is always required
  public Packet(){}

  private int toSend;
  public Packet(int toSend) {
    this.toSend = toSend;
  }

  @Override public void toBytes(ByteBuf buf) {
    // Writes the int into the buf
    buf.writeInt(toSend);
  }

  @Override public void fromBytes(ByteBuf buf) {
    // Reads the int back from the buf. Note that if you have multiple values, you must read in the same order you wrote.
    toSend = buf.readInt();
  }
}
```

### IMessageHandler

Now, how can we use this packet? Well, first we must have a class that can *handle* this packet. This is created with the `IMessageHandler` interface. Say we wanted to use this integer we sent to give the player that many diamonds on the server. Let's make this handler:

```
// The params of the IMessageHandler are <REQ, REPLY>, meaning that the first is the packet you are receiving, and the second is the packet you are returning. The returned packet can be used as a "response" from a sent packet.
public class PacketHandler implements IMessageHandler<Packet, IMessage> {
  // Do note that the default constructor is required, but implicitly defined in this case

  @Override public IMessage onMessage(Packet message, MessageContext ctx) {
    // This is the player the packet was sent to the server from
    EntityPlayerMP serverPlayer = ctx.getServerHandler().playerEntity;
    // The value that was sent
    int amount = message.toSend;
    serverPlayer.inventory.addItemStackToInventory(new ItemStack(Items.diamond, amount));
    // No response packet
    return null;
  }
}
```
It is recommended (but not required) that for organization's sake, this class is an inner class to your Packet class. If this is done, note that the class must also be declared `static`.

Note also that as of Minecraft 1.8 packets are by default handled on the network thread. That means that your `IMessageHandler` can _not_ interact with most game objects directly. The example above for example would not be correct.
Minecraft provides a convenient way to make your code execute on the main thread instead, like so:

```
mainThread.addScheduledTask(new Runnable() {
    @Override
    public void run() {
        // do stuff
    }
  });
```

The `mainThread` variable is of type `IThreadListener`. On the server use the world (`(WorldServer) serverPlayer.worldObj`), on the client use the `Minecraft` instance (`Minecraft.getMinecraft()`).

Registering Packets
-------------------

So now we have a packet, and a handler for this packet. But the `SimpleNetworkWrapper` needs one more thing to function! For it to use a packet, the packet must be registered with an *discriminator*, which is just an integer used to map packet types between server and client. To do this, call `INSTANCE.registerMessage(PacketHandler.class, Packet.class, 0, Side.Server);` where `INSTANCE` is the `SimpleNetworkWrapper` we defined earlier.
{'fraction_non_alphanumeric': 0.07745199792423456, 'fraction_numerical': 0.0010378827192527244, 'mean_word_length': 3.9767591994835376, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '85048', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1920, 'n_words': 1055}