Skip to content

Instantly share code, notes, and snippets.

@NahuLD
Created February 7, 2020 23:07
Show Gist options
  • Save NahuLD/5d10f65fa7faede301a20cdbde31232b to your computer and use it in GitHub Desktop.
Save NahuLD/5d10f65fa7faede301a20cdbde31232b to your computer and use it in GitHub Desktop.
Useful plugin message provider for Bukkit!
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
public class MessageProvider {
@SuppressWarnings("UnstableApiUsage")
private ByteArrayDataOutput output = ByteStreams.newDataOutput();
public MessageProvider() {
this(null, null);
}
public MessageProvider(String subChannel, String argument) {
if (subChannel == null || argument == null)
return;
output.writeUTF(subChannel);
output.writeUTF(argument);
}
public MessageProvider and(String argument) {
output.writeUTF(argument);
return this;
}
public MessageProvider and(short size) {
output.write(size);
return this;
}
public MessageProvider and(boolean argument) {
output.writeBoolean(argument);
return this;
}
public MessageProvider and(double argument) {
output.writeDouble(argument);
return this;
}
public MessageProvider and(float argument) {
output.writeFloat(argument);
return this;
}
public MessageProvider and(long argument) {
output.writeLong(argument);
return this;
}
public MessageProvider with(MessageProvider provider) {
byte[] array = provider.build();
output.writeShort(array.length);
output.write(array);
return this;
}
public void send(Plugin plugin) {
Bukkit.getServer().sendPluginMessage(plugin, "BungeeCord", output.toByteArray());
}
private byte[] build() {
return output.toByteArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment