This repository was archived by the owner on Aug 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathChannelsPlugin.java
More file actions
152 lines (136 loc) · 5.48 KB
/
ChannelsPlugin.java
File metadata and controls
152 lines (136 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.github.rmsy.channels;
import com.github.rmsy.channels.command.GlobalChannelCommands;
import com.github.rmsy.channels.impl.SimpleChannel;
import com.github.rmsy.channels.impl.SimplePlayerManager;
import com.github.rmsy.channels.listener.ChatListener;
import com.github.rmsy.channels.listener.PlayerListener;
import com.google.common.base.Preconditions;
import com.sk89q.bukkit.util.BukkitCommandsManager;
import com.sk89q.bukkit.util.CommandsManagerRegistration;
import com.sk89q.minecraft.util.commands.*;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.Configuration;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.java.JavaPlugin;
import javax.annotation.Nullable;
public class ChannelsPlugin extends JavaPlugin {
public static final String GLOBAL_CHANNEL_PARENT_NODE = "channels.global";
public static final String GLOBAL_CHANNEL_SEND_NODE = ChannelsPlugin.GLOBAL_CHANNEL_PARENT_NODE + ".send";
public static final String GLOBAL_CHANNEL_RECEIVE_NODE = ChannelsPlugin.GLOBAL_CHANNEL_PARENT_NODE + ".receive";
/** The plugin instance. */
private static ChannelsPlugin plugin;
/** The global channel. */
private Channel globalChannel;
/** The default channel. */
private Channel defaultChannel;
/** The commands manager. */
private CommandsManager commands;
/** The commands' registration. */
private CommandsManagerRegistration commandsRegistration;
/** The player manager. */
private PlayerManager playerManager;
/**
* Gets the plugin instance
*
* @return The plugin instance.
*/
public static ChannelsPlugin get() {
return ChannelsPlugin.plugin;
}
/**
* Gets the universal player manager.
*
* @return The universal player manager.
*/
public PlayerManager getPlayerManager() {
return this.playerManager;
}
@SuppressWarnings("unchecked")
@Override
public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String commandLabel, String[] args) {
try {
this.commands.execute(cmd.getName(), args, sender, sender);
} catch (CommandPermissionsException e) {
sender.sendMessage(ChatColor.RED + "You don't have permission.");
} catch (MissingNestedCommandException e) {
sender.sendMessage(ChatColor.RED + e.getUsage());
} catch (CommandUsageException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
sender.sendMessage(ChatColor.RED + "Usage: " + e.getUsage());
} catch (WrappedCommandException e) {
sender.sendMessage(ChatColor.RED + "An unknown error has occurred. Please notify an administrator.");
e.printStackTrace();
} catch (CommandException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
}
return true;
}
@Override
public void onDisable() {
HandlerList.unregisterAll(this);
this.commandsRegistration = null;
this.commands = null;
this.playerManager = null;
this.defaultChannel = null;
this.globalChannel = null;
}
@Override
public void onEnable() {
ChannelsPlugin.plugin = this;
Configuration config = this.getConfig();
config.options().copyDefaults(true);
this.saveConfig();
this.globalChannel = new SimpleChannel(new Permission(ChannelsPlugin.GLOBAL_CHANNEL_PARENT_NODE, PermissionDefault.TRUE)) {
@Override
public BaseComponent getFormat(BaseComponent message, @Nullable Player sender, CommandSender receiver, boolean broadcast) {
return broadcast ? new TextComponent(new TextComponent("[Broadcast] "), message) : new TextComponent(new TextComponent("<"), new TextComponent(sender != null ? sender.getDisplayName(receiver) : "Console"), new TextComponent(">: "), message);
}
};
this.defaultChannel = this.globalChannel;
this.playerManager = new SimplePlayerManager();
Bukkit.getPluginManager().registerEvents(new ChatListener(this), this);
Bukkit.getPluginManager().registerEvents(new PlayerListener(this), this);
this.commands = new BukkitCommandsManager();
this.commandsRegistration = new CommandsManagerRegistration(this, this.commands);
this.commandsRegistration.register(GlobalChannelCommands.class);
}
/**
* Gets the global channel.
*
* @return The global channel.
*/
public Channel getGlobalChannel() {
return this.globalChannel;
}
/**
* Sets the global channel.
*
* @param channel The new channel.
*/
public void setGlobalChannel(Channel channel) {
this.globalChannel = Preconditions.checkNotNull(channel, "Channel");
}
/**
* Gets the channel that newly-connected players will be added to.
*
* @return The channel.
*/
public Channel getDefaultChannel() {
return this.defaultChannel;
}
/**
* Sets the channel that newly-connected players will be added to.
*
* @param channel The channel.
*/
public void setDefaultChannel(Channel channel) {
this.defaultChannel = Preconditions.checkNotNull(channel, "Channel");
}
}