aboutsummaryrefslogtreecommitdiff
path: root/docs/events.md
blob: 152ae62f2c0afc3a9a2cb329e074fdf1604eb9ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Events in Forge

Forge uses events to allow mods to communicate with Minecraft and each other. Most of the events you will need to use come from Forge, but you can also create your own events if you need more.

## Subscribing to events

If you are interested in an event you need to create an event handler. For this first create a method that has the `@SubscribeEvent` annotation, is `public`, return `void` and takes an event as an argument. The type of the event argument is what decides which events your method receives. You can also only have one argument on an event handler.

```java
public class MyEventHandlerClass {
    int chatCount = 0;
    @SubscribeEvent
    public void onChat(ClientChatReceivedEvent event) {
        chatCount++;
        System.out.println("Chats received total: " + chatCount);
    }
}
```

This on it's own will not do anything yet. You must also register the event handler. To do that you register it on the corresponding event bus. For almost everything you will do, you need the `MinecraftForge.EVENT_BUS` (yes, even your own custom events should use this event bus).


```java
@Mod(modid = "examplemod", useMetadata = true)
public class ExampleMod {
    @Mod.EventHandler
    public void init(FMLInitializationEvent event) {
        MinecraftForge.EVENT_BUS.register(new MyEventHandlerClass());
    }
}
```

## Cancelling Events

Forge Events can be cancelled. What exactly that means depends on the event, but it usually stops the action the event indicates from happening.

```java
@SubscribeEvent
public void onChat(ClientChatReceivedEvent event) {
    // No more talking about cheese
    if (event.message.getFormattedText().contains("cheese"))
        event.setCanceled(true);
}
```

Not all events can be cancelled. Check the event class in the decompilation for the `@Cancellable` annotation.


## Custom Events

!!! note
    This is an advanced topic that most mod developers don't need to worry about.

Forge also allows you to create custom events. Each event needs to have it's own class extending `Event` (transitively or not). (Make sure you extend `net.minecraftforge.fml.common.eventhandler.Event`).

```java
// If you don't want your event to be cancellable, remove this annotation
@Cancelable
public class CheeseEvent extends Event {
    public final int totalCheeseCount;

    public CheeseEvent(int totalCheeseCount) {
        this.totalCheeseCount = totalCheeseCount;
    }
}
```

That's it, you are done. You have a custom event!

I'm kidding of course. The next step is actually using your event. For now, let's put our own custom event inside the forge chat event:

```java
int cheeseCount = 0;

@SubscribeEvent
public void onChat(ClientChatReceivedEvent event) {
    if (event.message.getFormattedText().contains("cheese")) {
        CheeseEvent cheeseEvent = new CheeseEvent(++cheeseCount);
        MinecraftForge.EVENT_BUS.post(cheeseEvent);
    }
}
```

And now we are done. Unless you want your event to be cancellable. We also need to add code to handle cancelled events (if you made your event `@Cancelable`). What that cancelling does is up to you, but in our example let's just cancel the original chat message event (hiding that chat message):

```java
@SubscribeEvent
public void onChat(ClientChatReceivedEvent event) {
    if (event.message.getFormattedText().contains("cheese")) {
        CheeseEvent cheeseEvent = new CheeseEvent(++cheeseCount);
        MinecraftForge.EVENT_BUS.post(cheeseEvent);
        if (cheeseEvent.isCanceled()) {
            event.setCanceled(true);
        }
    }
}
```

You can now subscribe to your custom event like you would to any other event:

```java
@SubscribeEvent
public void onCheese(CheeseEvent event) {
    if (event.totalCheeseCount > 10) {
        // Only 10 cheese messages are allowed per restart
        event.setCanceled(true);
    }
}
```