walterhiggins / ScriptCraft

Write Minecraft Plugins in JavaScript.
MIT License
1.84k stars 380 forks source link

Cannot send action bar messages to players! (without vanilla commands) #420

Closed ghost closed 4 years ago

ghost commented 4 years ago

There is no built-in way to send action bar messages to players without running the "/title" command from the console. We need access to this class "net.md_5.bungee.api" which has the "ChatMessageType" and "chat.BaseComponent" subclasses responsible for sending the actionbar text.

nanocodex commented 4 years ago

I don’t have a deep understanding of how Scriptcraft works yet, but if your server is run on CanaryMod, Bukkit, Spigot, or Paper, you may want to check their API for something similar that you could use.

Bukkit API : https://hub.spigotmc.org/javadocs/bukkit/overview-summary.html Spigot API : https://hub.spigotmc.org/javadocs/spigot/overview-summary.html Paper API : https://papermc.io/javadocs/paper/1.13/overview-summary.html (I think that this one may be outdated) CanaryMod API : https://ci.visualillusionsent.net/job/Canarylib/javadoc/

I hope this helps and if you do happen to find the answer, please let me know as well by replying here. Thanks.

jwulf commented 4 years ago

https://github.com/Magikcraft/magikcraft-core/blob/master/actionbar/index.ts

JavaScript version: https://github.com/Magikcraft/magikcraft-core/blob/master/actionbar/index.js

ghost commented 4 years ago

Alright, with help from jwulf, I was able to create a working action bar script, however it does require you at least have spigot installed, not just bukkit.

var jx = {
   color: function (text) {
      return text.toString().split('&').join('\xA7').split('\xA7\xA7').join('&');
   },
   player: function (name) {
      return server.getPlayer(name.toString());
   },
   actionBar: function (target, message) {
      try {
         var bkClass = Java.type('net.md_5.bungee.api.ChatMessageType').ACTION_BAR;
         var bkClass2 = Java.type('net.md_5.bungee.api.chat.TextComponent');
         jx.player(target).sendMessage(bkClass, new bkClass2(jx.color(message.toString())));
         return true;
      } catch (error) {
         return false;
      }
   }
};

jx.actionBar('Notch', '&aHello there!');
// returns false on bukkit servers
// returns true on spigot/paper servers