From deee58c40ec6190b88f878f80b7794c30b411707 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 2 Oct 2020 22:52:58 +0200 Subject: [PATCH] Add event api documentation --- ReadMe.md | 2 +- doc/Modding.md | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index fac95b9fe..07f8bab17 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -39,7 +39,7 @@ Sadly, we cannot support all versions. We will add support for all major version - April fools: I will not add any support for them, there are only a few things known. They might work, untested ## Modding -Minosoft is modding friendly. I am working hard on an reasonable API. See !17 for more details. See [Modding.md](https://gitlab.bixilon.de/bixilon/minosoft/-/blob/modding/doc/Modding.md) for more details. The code is pretty dynamic (all blocks, items, entities, etc are stored in json files). It is pretty easy to load custom entities, etc. +Minosoft is modding friendly. I am working hard on an reasonable API. See !17 for more details. See [Modding.md](doc/Modding.md) for more details. The code is pretty dynamic (all blocks, items, entities, etc are stored in json files). It is pretty easy to load custom entities, etc. We will add a real modding API once Rendering is complete. Spoiler: Forge mods are NOT compatible. Feel free to write a compatibility layer (This is a really hard thing, we do things completely different). ## Contribution diff --git a/doc/Modding.md b/doc/Modding.md index 206e66d2d..bfec2dda4 100644 --- a/doc/Modding.md +++ b/doc/Modding.md @@ -61,7 +61,7 @@ Your main class must extend the following class: `de.bixilon.minosoft.MinosoftMo ### Phases There are different phases (states) for the loading. There are the following phases: 1. `BOOTING` Happens after loading all configuration files and while displaying the server list. - 2. `INITIALIZING` All mods are loaded into the ram and everything before registering anything happens here (like OpenGL stuff, etc). + 2. `INITIALIZING` All mods are loaded into the ram and everything before registering anything happens here (like OpenGL stuff, etc). You should also register Events here. 3. `LOADING` You have custom items, entities, blocks, etc? Load it here. 4. `STARTING` All items, etc are loaded. If you want to do anything else, do it here. 5. `STARTED` The loading is complete @@ -122,3 +122,32 @@ Your `mod.json` can look like this ## Events There are global events (which works on all connections) and connections events (server specific). +To register a global event you need to use (in the `INITIALIZING` phase) `getEventManager().registerGlobalListener(new XYEventListener());`. +If you want to register an event depending on an IP (like server specific support, you can use the following): +`getEventManager().registerConnectionListener(new XYEventListener(), new ServerAddress("127.0.0.1", 25565));` + +Your XYListener class needs to implement `de.bixilon.minosoft.modding.event.EventListener`; +```java +import de.bixilon.minosoft.modding.event.EventListener; +import de.bixilon.minosoft.modding.event.events.ChatMessageReceivingEvent; + +public class ChatEvent extends EventListener { + @Override + public void onChatMessageReceiving(ChatMessageReceivingEvent event) { + if (event.getMessage().getRawMessage().contains("Bixilon")) { + MinosoftExampleMod.getInstance().getLogger().game("Bixilon is awful, suppressing this bad chat message!"); + event.setCancelled(true); + } + } + + @Override + public void onChatMessageSending(ChatMessageSendingEvent event) { + if(event.getMessage().contains("jeb_")){ + event.setCancelled(true); + event.getConnection().getSender().sendChatMessage("_jeb is awesome!"); + } + } +} +``` +The following code would suppress messages containing the word "Bixilon" and if you write "jeb_" into the chat, the message's text will be "jeb_ is awesome". +To see a list of all events look into `de.bixilon.minosoft.modding.event.events`. There is also a javadoc. \ No newline at end of file