5zig / The-5zig-API

The official client API of The 5zig Mod.
Apache License 2.0
11 stars 2 forks source link

How to update module item every time an action bar message is received? #3

Closed ghost closed 7 years ago

ghost commented 7 years ago

From the example plugin provided and the source code I can't figure out how to do this.

5zig commented 7 years ago

First, you need to register a server instance, then you need to register a server listener, provide a data-handler class and override the onActionBar method. You can then update the variables you want by referencing the gameMode parameter. Now register a module item, provide your created date class to the super constructor and implement the get value method.

ghost commented 7 years ago

Do I need to make a ServerListener or a ServerGameListener class at

then you need to register a server listener

And what methods would I need to specify?

ghost commented 7 years ago

You can then update the variables you want by referencing the gameMode parameter.

Sorry to be a pain, but do you have an example of how I can do this?

What if I want to make the ModuleItem simply render the most recent action bar message?

5zig commented 7 years ago

I've added an example plugin to examples/basic2 which should help you.

ghost commented 7 years ago

Wow, thank you so much for putting all the work into creating that helpful example for me!

I am having a problem though, I can't see the module in the HUD, even though I can locate it in Options>5zig>Display

5zig commented 7 years ago

Are you sure that you are playing on the server you specified? Also, you need to add the created item in the Customize Display GUI of the mod.

ghost commented 7 years ago

are you playing on the server you specified?

What do you mean? How can I just make it run on every server?

5zig commented 7 years ago

Call return true here.

ghost commented 7 years ago

I already am. In fact, my file is identical to that one:

package io.github.theonlygusti.fivezigaddons;

import eu.the5zig.mod.server.ServerInstance;

public class ServerInstanceHandler extends ServerInstance {
  @Override
  public void registerListeners() {
    // Register a listener class
    getGameListener().registerListener(new MyActionBarListener());
  }

  @Override
  public String getName() {
    return "localhost";
  }

  @Override
  public String getConfigName() {
    return "localhost";
  }

  @Override
  public boolean handleServer(String host, int port) {
    return true;
  }
}
ghost commented 7 years ago

Then I have my MyActionBarListener:

package io.github.theonlygusti.fivezigaddons;

import eu.the5zig.mod.server.AbstractGameListener;
import eu.the5zig.util.minecraft.ChatColor;

public class MyActionBarListener extends AbstractGameListener<MyServer.MainServer> {
  @Override
  public Class<MyServer.MainServer> getGameMode() {
    return MyServer.MainServer.class;
  }

  @Override
  public boolean matchLobby(String lobby) {
    return true;
  }

  @Override
  public boolean onActionBar(MyServer.MainServer gameMode, String message) {
    gameMode.lastActionBarMessage = ChatColor.stripColor(message);
    return false;
  }
}
ghost commented 7 years ago

and my MyServer:

package io.github.theonlygusti.fivezigaddons;

import eu.the5zig.mod.server.GameMode;

public class MyServer {

  public static class MainServer extends GameMode {

    public String lastActionBarMessage;

    @Override
    public String getName() {
      return "Server";
    }

  }
}
ghost commented 7 years ago

MyModuleItem:

package io.github.theonlygusti.fivezigaddons;

import eu.the5zig.mod.modules.GameModeItem;

public class MyModuleItem extends GameModeItem<MyServer.MainServer> {
  public MyModuleItem() {
    super(MyServer.MainServer.class);
  }

  @Override
  protected Object getValue(boolean dummy) {
    if (dummy) {
      return "Example Action Bar Message";
    } else {
      return getGameMode().lastActionBarMessage;
    }
  }

  @Override
  public String getName() {
    return "AddOns";
  }
}

Main

package io.github.theonlygusti.fivezigaddons;

import eu.the5zig.mod.The5zigAPI;
import eu.the5zig.mod.event.EventHandler;
import eu.the5zig.mod.event.LoadEvent;
import eu.the5zig.mod.modules.Category;
import eu.the5zig.mod.plugin.Plugin;

@Plugin(name = "AddOns", version = "1.0")
public class Main {
  @EventHandler(priority = EventHandler.Priority.LOW)
  public void onLoad(LoadEvent event) {
    The5zigAPI.getAPI().registerServerInstance(this, ServerInstanceHandler.class);
    The5zigAPI.getAPI().registerModuleItem(this, "AddOns", MyModuleItem.class, Category.OTHER);
  }
}
ghost commented 7 years ago

I guess you've been pretty busy for the last four days, I definitely have! Too many deadlines...

Anyway, I was wondering if you could give me a guess as to when you'll get around to taking a look at this?

All your help so far has been absolutely brilliant so don't feel that you have to do anything soon (I know I just dumped a huge load of code here, me myself having no idea what to do xD) I'd just like to know when you'd next be able to help me with this :)

5zig commented 7 years ago

I'm sorry for the late response, but I've now finally figured out how to solve your problem.

The issue is the following: the MyActionBarListener doesn't get activated because you have to call switchLobby at least once. To do this, you need to create another Listener class, register it and return null in getGameMode(). Now override the onServerJoin method and call getGameListener().switchLobby("Lobby"); The issue was also present in the example plugin, so I've just updated it.

Let me know if that resolved your issue. Sorry again for the late reply, I didn't have a lot time during the past week.

5zig commented 7 years ago

I realize that the Api may seem a little bit too complicated, especially if you just want to support a single server, but it is really powerful when you want to support large server networks with many different child servers and lobbies (like for example the hypixel.net server). I might add a simplified concept for vanilla servers sometime in the future though.

ghost commented 7 years ago

That hasn't resolved the issue, it's still not displaying.

I added this file:

package io.github.theonlygusti.fivezigaddons;

import eu.the5zig.mod.server.AbstractGameListener;

public class MyLobbyListener extends AbstractGameListener<MyServer.MainServer> {
  @Override
  public Class<MyServer.MainServer> getGameMode() {
    return null;
  }

  @Override
  public boolean matchLobby(String lobby) {
    return false;
  }

  @Override
  public void onServerJoin() {
    getGameListener().switchLobby("Lobby");
  }
}
5zig commented 7 years ago

Have you registered it, too?

ghost commented 7 years ago

support large server networks with many different child servers and lobbies

Reading this I tried to use it on mineplex, and it still wasn't working. Are there actually ways for a module item to display only on one type of lobby? E.g. SG-1?

ghost commented 7 years ago

Oh I don't think I registered it. Where does that happen? (And by the way thanks for your awesomely fast response XD)

5zig commented 7 years ago

Same as with the MyActionBarListener class.

ghost commented 7 years ago

It still doesn't work, and I've registered it now.

Here's my MyServerInstance:

package io.github.theonlygusti.fivezigaddons;

import eu.the5zig.mod.server.ServerInstance;

public class MyServerInstance extends ServerInstance {
  @Override
  public void registerListeners() {
    // Register a listener class
    getGameListener().registerListener(new MyActionBarListener());
    getGameListener().registerListener(new MyLobbyListener());
  }

  @Override
  public String getName() {
    return "localhost";
  }

  @Override
  public String getConfigName() {
    return "localhost";
  }

  @Override
  public boolean handleServer(String host, int port) {
    return true;
  }
}
5zig commented 7 years ago

Did you try connecting to a server that is currently not supported by the 5zig mod?

ghost commented 7 years ago

I don't know. I just tried it on mineplex. Everything else works though, e.g. the text replacements in chat.

5zig commented 7 years ago

Well, do you need your plugin to work on mineplex? If not, try it out on another server.

ghost commented 7 years ago

I'd like it to work everywhere, and I just checked; it doesn't work anywhere.

Checked on mineplex, havoc, badlion, my localhost, a friends server. This covered BungeeCord, Spigot, Vanilla. Idk what Badlion is.

5zig commented 7 years ago

Could you please compare your plugin with the following source folder: src.zip? It worked fine for me. Note that the module item will only be displayed if there actually was a recent action bar message and if the 5zig mod does not support the current server by itself.

ghost commented 7 years ago

What do I need to do if it does support the server? And how can I get it to show all the time even if there has been no "recent' action bar message?

ghost commented 7 years ago

And your source code didn't work for me, I saw your message

Note that the module item will only be displayed if there actually was a recent action bar message

and so tried running /title @p actionbar {"text":"boi"} a bunch of times. Still doesn't display the module item in 5zig.

Maven is creating this jarfile:

fivezigaddons-0.0.1.jar.zip

5zig commented 7 years ago

Your plugin worked totally fine for me. Just make sure you are actually connected to a real server when testing. The item will be always displayed if you don't return null in the getValue method.

But actually, I think there is a way easier way for you to display an action bar message, even without all the limitations I talked about earlier, if I reconsider your special case. (I really don't know why I didn't though about that earlier, to be honest, sorry). The way it would work is that you would register an event listener that gets called on every action message, no matter if you are connected to a server or not. You would need to reset the message when leaving the world by yourself, but that's it. Heres the source folder of my test-project: src.zip The only problem I noticed is that the action bar event does not get called when executed by the /title command, that will be fixed in an upcoming mod update. When using the raw chat command (what most server do use), the plugin does work though. Hope that helps.

ghost commented 7 years ago

Nice, that new plugin works just as expected.

Is there a way to show/hide (toggle) module items on keypresses?

5zig commented 7 years ago

Sure, just register a keybinding like in the basic1 example plugin.

ghost commented 7 years ago

Awesome, everything is working perfectly now! Thank you so much for all your help, you've been incredible!

Damn I love your client so much, it's so nice as well for it to have a brilliant developer willing to spend time with noobs like me ;) Thanks so much

5zig commented 7 years ago

No problem, glad I could help. ;)