Some fixes in plugin message channel
This commit is contained in:
parent
a7e61ae28d
commit
6e52a180a0
Binary file not shown.
Binary file not shown.
|
@ -1,179 +1,183 @@
|
||||||
package JH_AuthMeBridge;
|
package JH_AuthMeBridge;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
|
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import net.md_5.bungee.api.CommandSender;
|
import net.md_5.bungee.api.CommandSender;
|
||||||
import net.md_5.bungee.api.chat.TextComponent;
|
import net.md_5.bungee.api.chat.TextComponent;
|
||||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||||
import net.md_5.bungee.api.event.ChatEvent;
|
import net.md_5.bungee.api.event.ChatEvent;
|
||||||
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
|
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
|
||||||
import net.md_5.bungee.api.event.PluginMessageEvent;
|
import net.md_5.bungee.api.event.PluginMessageEvent;
|
||||||
import net.md_5.bungee.api.event.ServerSwitchEvent;
|
import net.md_5.bungee.api.event.ServerDisconnectEvent;
|
||||||
import net.md_5.bungee.api.plugin.Command;
|
import net.md_5.bungee.api.event.ServerSwitchEvent;
|
||||||
import net.md_5.bungee.api.plugin.Listener;
|
import net.md_5.bungee.api.plugin.Command;
|
||||||
import net.md_5.bungee.api.plugin.Plugin;
|
import net.md_5.bungee.api.plugin.Listener;
|
||||||
import net.md_5.bungee.config.Configuration;
|
import net.md_5.bungee.api.plugin.Plugin;
|
||||||
import net.md_5.bungee.config.YamlConfiguration;
|
import net.md_5.bungee.config.Configuration;
|
||||||
import net.md_5.bungee.event.EventHandler;
|
import net.md_5.bungee.config.YamlConfiguration;
|
||||||
import net.md_5.bungee.event.EventPriority;
|
import net.md_5.bungee.event.EventHandler;
|
||||||
|
import net.md_5.bungee.event.EventPriority;
|
||||||
public class bungee extends Plugin implements Listener {
|
|
||||||
|
public class bungee extends Plugin implements Listener {
|
||||||
/*
|
|
||||||
Made by Jheyson Henrique
|
/*
|
||||||
*/
|
Made by Jheyson Henrique
|
||||||
|
*/
|
||||||
@Override
|
|
||||||
public void onEnable() {
|
@Override
|
||||||
getProxy().getPluginManager().registerListener(this, this);
|
public void onEnable() {
|
||||||
try {
|
getProxy().getPluginManager().registerListener(this, this);
|
||||||
loadConfig();
|
try {
|
||||||
} catch (IOException e) {
|
loadConfig();
|
||||||
e.printStackTrace();
|
} catch (IOException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
getProxy().getPluginManager().registerCommand(this, new Command("jh_authmebridge") {
|
}
|
||||||
@Override
|
getProxy().getPluginManager().registerCommand(this, new Command("jh_authmebridge") {
|
||||||
public void execute(CommandSender sender, String[] args) {
|
@Override
|
||||||
if(!sender.hasPermission("jh_authmebridge.reload")){
|
public void execute(CommandSender sender, String[] args) {
|
||||||
sender.sendMessage(new TextComponent("§cYou do not have permission to run this command!"));
|
if(!sender.hasPermission("jh_authmebridge.reload")){
|
||||||
return;
|
sender.sendMessage(new TextComponent("§cYou do not have permission to run this command!"));
|
||||||
}
|
return;
|
||||||
try {
|
}
|
||||||
loadConfig();
|
try {
|
||||||
sender.sendMessage(new TextComponent("§aSuccessfully reloaded configuration"));
|
loadConfig();
|
||||||
} catch (IOException e) {
|
sender.sendMessage(new TextComponent("§aSuccessfully reloaded configuration"));
|
||||||
sender.sendMessage(new TextComponent("§aError loading configuration errors were shown in console"));
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
sender.sendMessage(new TextComponent("§aError loading configuration errors were shown in console"));
|
||||||
}
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
|
||||||
});
|
}
|
||||||
System.out.println("[JH_AuthMeBridge] It started successfully.");
|
});
|
||||||
}
|
System.out.println("[JH_AuthMeBridge] It started successfully.");
|
||||||
|
}
|
||||||
Configuration config;
|
|
||||||
private void loadConfig() throws IOException{
|
Configuration config;
|
||||||
if(!getDataFolder().exists()){
|
private void loadConfig() throws IOException{
|
||||||
getDataFolder().mkdir();
|
if(!getDataFolder().exists()){
|
||||||
}
|
getDataFolder().mkdir();
|
||||||
|
}
|
||||||
File file = new File(getDataFolder(), "config.yml");
|
|
||||||
|
File file = new File(getDataFolder(), "config.yml");
|
||||||
if(!file.exists()){
|
|
||||||
file.createNewFile();
|
if(!file.exists()){
|
||||||
try (InputStream in = getResourceAsStream("config.yml");
|
file.createNewFile();
|
||||||
OutputStream out = new FileOutputStream(file)) {
|
try (InputStream in = getResourceAsStream("config.yml");
|
||||||
ByteStreams.copy(in, out);
|
OutputStream out = new FileOutputStream(file)) {
|
||||||
}
|
ByteStreams.copy(in, out);
|
||||||
}
|
}
|
||||||
config = YamlConfiguration.getProvider(YamlConfiguration.class).load(file);
|
}
|
||||||
comandos.clear();
|
config = YamlConfiguration.getProvider(YamlConfiguration.class).load(file);
|
||||||
lobbyes.clear();
|
comandos.clear();
|
||||||
for(String s : getConfig().getStringList("CommandsAllowed")){
|
lobbyes.clear();
|
||||||
comandos.add(s.toLowerCase());
|
for(String s : getConfig().getStringList("CommandsAllowed")){
|
||||||
}
|
comandos.add(s.toLowerCase());
|
||||||
for(String s : getConfig().getStringList("Lobbyes")){
|
}
|
||||||
lobbyes.add(s.toLowerCase());
|
for(String s : getConfig().getStringList("Lobbyes")){
|
||||||
}
|
lobbyes.add(s.toLowerCase());
|
||||||
}
|
}
|
||||||
private Configuration getConfig()
|
}
|
||||||
{
|
private Configuration getConfig()
|
||||||
return config;
|
{
|
||||||
}
|
return config;
|
||||||
public void saveConfig(String config){
|
}
|
||||||
File file = new File(getDataFolder(), "config.yml");
|
public void saveConfig(String config){
|
||||||
try {
|
File file = new File(getDataFolder(), "config.yml");
|
||||||
YamlConfiguration.getProvider(YamlConfiguration.class).save(getConfig(), file);
|
try {
|
||||||
} catch (IOException e) {
|
YamlConfiguration.getProvider(YamlConfiguration.class).save(getConfig(), file);
|
||||||
e.printStackTrace();
|
} catch (IOException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
List<String> lobbyes = new ArrayList<>();
|
}
|
||||||
HashMap<String, Boolean> logados = new HashMap<String, Boolean>();
|
|
||||||
List<String> comandos = new ArrayList<>();
|
private List<String> lobbyes = new ArrayList<>();
|
||||||
@EventHandler
|
private HashMap<String, Boolean> logados = new HashMap<String, Boolean>();
|
||||||
public void onPluginMessage(PluginMessageEvent e){
|
private List<String> comandos = new ArrayList<>();
|
||||||
if (e.getTag().equalsIgnoreCase("BungeeCord")) {
|
|
||||||
try {
|
@EventHandler
|
||||||
DataInputStream in = new DataInputStream(new ByteArrayInputStream(e.getData()));
|
public void onPluginMessage(PluginMessageEvent event){
|
||||||
String channel = in.readUTF();
|
if (event.getTag().equalsIgnoreCase("BungeeCord")) {
|
||||||
if(channel.equals("JH_AuthMeBridge")){
|
try {
|
||||||
String input = in.readUTF();
|
DataInputStream in = new DataInputStream(new ByteArrayInputStream(event.getData()));
|
||||||
if(isLogged(input))return;
|
String channel = in.readUTF();
|
||||||
logados.put(input, true);
|
if(channel.equals("JH_AuthMeBridge")){
|
||||||
}
|
String input = in.readUTF();
|
||||||
} catch (IOException e1) {
|
if(isLogged(input))return;
|
||||||
e1.printStackTrace();
|
logados.put(input, true);
|
||||||
}
|
}
|
||||||
|
} catch (IOException e1) {
|
||||||
}
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
@EventHandler
|
|
||||||
public void onPlayerLeave(PlayerDisconnectEvent event) {
|
}
|
||||||
if(isLogged(event.getPlayer())){
|
}
|
||||||
logados.remove(event.getPlayer().getName());
|
|
||||||
}
|
@EventHandler
|
||||||
}
|
public void onPlayerLeave(PlayerDisconnectEvent event) {
|
||||||
|
if(isLogged(event.getPlayer())){
|
||||||
@EventHandler(priority=EventPriority.LOWEST)
|
logados.remove(event.getPlayer().getName());
|
||||||
public void onChat(ChatEvent event){
|
}
|
||||||
if (event.isCancelled())return;
|
}
|
||||||
if (!(event.getSender() instanceof ProxiedPlayer))return;
|
|
||||||
ProxiedPlayer player = (ProxiedPlayer)event.getSender();
|
@EventHandler(priority=EventPriority.LOWEST)
|
||||||
if(isLogged(player))return;
|
public void onChat(ChatEvent event){
|
||||||
if(event.isCommand()){
|
if (event.isCancelled())return;
|
||||||
String command = event.getMessage().split(" ")[0].toLowerCase();
|
if (!(event.getSender() instanceof ProxiedPlayer))return;
|
||||||
if(comandos.contains(command)){
|
ProxiedPlayer player = (ProxiedPlayer)event.getSender();
|
||||||
return;
|
if(isLogged(player))return;
|
||||||
}
|
if(event.isCommand()){
|
||||||
}
|
String command = event.getMessage().split(" ")[0].toLowerCase();
|
||||||
if(!isLogged(player)){
|
if(comandos.contains(command)){
|
||||||
event.setCancelled(true);
|
return;
|
||||||
TextComponent message = new TextComponent(ChatColor.translateAlternateColorCodes('&', getConfig().getString("NeedLoginToEnter")));
|
}
|
||||||
player.sendMessage(message);
|
}
|
||||||
}
|
if(!isLogged(player)){
|
||||||
}
|
event.setCancelled(true);
|
||||||
|
TextComponent message = new TextComponent(ChatColor.translateAlternateColorCodes('&', getConfig().getString("NeedLoginToEnter")));
|
||||||
@EventHandler
|
player.sendMessage(message);
|
||||||
public void onServerSwitch(ServerSwitchEvent event) {
|
}
|
||||||
if(isLogged(event.getPlayer())){
|
}
|
||||||
try {
|
|
||||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
@EventHandler
|
||||||
DataOutputStream out = new DataOutputStream(stream);
|
public void onServerSwitch(ServerSwitchEvent event) {
|
||||||
out.writeUTF("JH_AuthMeBridge");
|
if(isLogged(event.getPlayer())){
|
||||||
out.writeUTF(event.getPlayer().getName());
|
try {
|
||||||
event.getPlayer().getServer().sendData("BungeeCord", stream.toByteArray());
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
} catch (IOException e) {
|
DataOutputStream out = new DataOutputStream(stream);
|
||||||
e.printStackTrace();
|
out.writeUTF("JH_AuthMeBridge");
|
||||||
}
|
out.writeUTF(event.getPlayer().getName());
|
||||||
return;
|
event.getPlayer().getServer().sendData("BungeeCord", stream.toByteArray());
|
||||||
}
|
} catch (IOException e) {
|
||||||
if(!lobbyes.contains(event.getPlayer().getServer().getInfo().getName().toLowerCase())) {
|
e.printStackTrace();
|
||||||
TextComponent kickReason = new TextComponent(ChatColor.translateAlternateColorCodes('&', getConfig().getString("KickMessageNeedLogged")));
|
}
|
||||||
kickReason.setColor(ChatColor.RED);
|
return;
|
||||||
event.getPlayer().disconnect(kickReason);
|
}
|
||||||
}
|
if(!lobbyes.contains(event.getPlayer().getServer().getInfo().getName().toLowerCase())) {
|
||||||
}
|
TextComponent kickReason = new TextComponent(ChatColor.translateAlternateColorCodes('&', getConfig().getString("KickMessageNeedLogged")));
|
||||||
|
kickReason.setColor(ChatColor.RED);
|
||||||
public Boolean isLogged(ProxiedPlayer player){
|
event.getPlayer().disconnect(kickReason);
|
||||||
return logados.containsKey(player.getName());
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean isLogged(String player){
|
public boolean isLogged(ProxiedPlayer player){
|
||||||
return logados.containsKey(player);
|
return logados.containsKey(player.getName());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public boolean isLogged(String player){
|
||||||
|
return logados.containsKey(player);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -33,13 +33,14 @@ public class spigot extends JavaPlugin implements Listener, PluginMessageListene
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onLogin(LoginEvent event){
|
public void onLogin(LoginEvent event){
|
||||||
try {
|
try {
|
||||||
ByteArrayOutputStream b = new ByteArrayOutputStream();
|
ByteArrayOutputStream b = new ByteArrayOutputStream();
|
||||||
DataOutputStream out = new DataOutputStream(b);
|
DataOutputStream out = new DataOutputStream(b);
|
||||||
out.writeUTF("JH_AuthMeBridge");
|
out.writeUTF("JH_AuthMeBridge");
|
||||||
out.writeUTF(event.getPlayer().getName());
|
out.writeUTF(event.getPlayer().getName());
|
||||||
Bukkit.getServer().sendPluginMessage(this, "BungeeCord", b.toByteArray());
|
event.getPlayer().sendPluginMessage(this, "BungeeCord", b.toByteArray());
|
||||||
|
//Bukkit.getServer().sendPluginMessage(this, "BungeeCord", b.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -54,12 +55,12 @@ public class spigot extends JavaPlugin implements Listener, PluginMessageListene
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPluginMessageReceived(String channel, Player p, byte[] message) {
|
public void onPluginMessageReceived(String channel, Player p, byte[] message) {
|
||||||
|
if(p == null || !p.isOnline() || channel == null || channel.isEmpty())return;
|
||||||
try {
|
try {
|
||||||
DataInputStream in = new DataInputStream(new ByteArrayInputStream(message));
|
DataInputStream in = new DataInputStream(new ByteArrayInputStream(message));
|
||||||
String subchannel = in.readUTF();
|
String subchannel = in.readUTF();
|
||||||
// debug
|
// debug
|
||||||
//System.out.println((p != null ? p.getName() : " not found ") + " received from channel" + channel + " and subchannel " + subchannel);
|
//System.out.println((p != null ? p.getName() : " not found ") + " received from channel " + channel + " and subchannel " + subchannel);
|
||||||
if(subchannel.equals("JH_AuthMeBridge")){
|
if(subchannel.equals("JH_AuthMeBridge")){
|
||||||
String input = in.readUTF();
|
String input = in.readUTF();
|
||||||
if(isLogged(input))return;
|
if(isLogged(input))return;
|
||||||
|
@ -92,11 +93,11 @@ public class spigot extends JavaPlugin implements Listener, PluginMessageListene
|
||||||
|
|
||||||
List<String> logados = new ArrayList<>();
|
List<String> logados = new ArrayList<>();
|
||||||
|
|
||||||
public Boolean isLogged(Player player){
|
public boolean isLogged(Player player){
|
||||||
return logados.contains(player.getName());
|
return logados.contains(player.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean isLogged(String player){
|
public boolean isLogged(String player){
|
||||||
return logados.contains(player);
|
return logados.contains(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue