Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
config system, public chat
Browse files Browse the repository at this point in the history
  • Loading branch information
0xs2 committed Feb 4, 2024
1 parent 08e5446 commit f658326
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 32 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ this plugin is just dead simple, straight to the point. does staffchat and nothi

<img src="https://i.imgur.com/FoOl2gQ.png" width="500px">

### commands
- `/staffchat`, `/sc` - type in the staff chat
- `/staffchattoggle`, `/sct` - toggle the staff chat on/off
- `/staffchatcheck`, `/scc` - check if you or another user has staff chat toggled on or off
- `/staffchatmenu`, `/scm` - show the plugin information
- `/publicchat`, `/spc`, `/pc` - send a message to the public chat whilst staff chat is toggled

### permissions
- `staffchat.use` - use staffchat command
- `staffchat.public` - send a message to the public chat when active
- `staffchat.check` - Check if staffchat id toggled
- `staffchat.toggle` - toggle staffchat on/off
- `staffchat.menu` - see the staffchat menu
- `staffchat.see` - see staffchat
62 changes: 54 additions & 8 deletions src/main/java/dev/oxs/staffchat/StaffChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.HashMap;
import java.util.UUID;
import java.util.logging.Logger;
Expand All @@ -15,42 +17,86 @@ public class StaffChat extends JavaPlugin implements Listener {

private Logger log;
private String pluginName;
//Basic Plugin Info

private StaffChatLanguage language;

private static StaffChat plugin;

private PluginDescriptionFile pdf;

private HashMap<UUID, Boolean> staffChatToggled = new HashMap<>();

public void StaffChatMessage(String sender, String message) {
for (Player onlinePlayer : getServer().getOnlinePlayers()) {
if (onlinePlayer.hasPermission("staffchat.see") || onlinePlayer.isOp()) {
onlinePlayer.sendMessage(ChatColor.RED + "[StaffChat] " + ChatColor.WHITE + sender + ": " + ChatColor.WHITE + message.replaceAll("(&([4c6f2aeb319d5780]))", "\u00A7$2"));
}
}
}
@Override
public void onEnable() {
plugin = this;
log = this.getServer().getLogger();
pdf = this.getDescription();
pluginName = pdf.getName();

StaffChatSettings.getInstance(plugin);

final StaffChatListener sc = new StaffChatListener(plugin, staffChatToggled);
Bukkit.getPluginManager().registerEvents(sc, plugin);

language = new StaffChatLanguage(new File(this.getDataFolder(), "language.yml"), false);

// commmands
Bukkit.getPluginCommand("StaffChatCommand").setExecutor(new StaffChatCommand(plugin));
Bukkit.getPluginCommand("StaffChatUsage").setExecutor(new StaffChatUsage(plugin));
Bukkit.getPluginCommand("StaffChatCheck").setExecutor(new StaffChatCheck(plugin));
Bukkit.getPluginCommand("StaffChatPublic").setExecutor(new StaffChatPublic(plugin));
Bukkit.getPluginCommand("StaffChatToggle").setExecutor(new StaffChatToggle(plugin, staffChatToggled));

pluginName = pdf.getName();

log.info("[" + pluginName + "] Is Loading, Version: " + pdf.getVersion());
}


@Override
public void onDisable() {
log.info(pluginName + " has been disabled.");
}

public void StaffChatMessage(Player sender, String message) {
for (Player onlinePlayer : getServer().getOnlinePlayers()) {
if (onlinePlayer.hasPermission("staffchat.see") || onlinePlayer.isOp()) {
Boolean useDisplayName = StaffChatSettings.getInstance(plugin).getConfigBoolean("settings.staffchat-use-displayNamesStaffChat");
onlinePlayer.sendMessage(plugin.getPluginPrefix() + " " + ChatColor.WHITE + (useDisplayName ? sender.getDisplayName() : sender.getName()) + ": " + ChatColor.WHITE + printColours(message));
}
}
}

public void PublicChatMessage(Player sender, String message) {
for (Player onlinePlayer : getServer().getOnlinePlayers()) {

String playerPrefix = StaffChatSettings.getInstance(plugin).getConfigString("settings.staffchat-publicChatPrefix");
Boolean useDisplay = StaffChatSettings.getInstance(plugin).getConfigBoolean("settings.staffchat-use-displayNamesPublicChat");

String replacedString = playerPrefix.replace("%player%", (useDisplay ? sender.getDisplayName() : sender.getName()));
onlinePlayer.sendMessage(plugin.printColours(replacedString) + ChatColor.WHITE + plugin.printColours(message));
}
}

public boolean getToggleStatus(UUID player) {
return staffChatToggled.getOrDefault(player, false);
}

public String getPluginPrefix() {
return printColours(StaffChatSettings.getInstance(plugin).getConfigString("settings.staffchat-prefix"));
}

public String printColours(String str) {
return str.replaceAll("(&([4c6f2aeb319d5780]))", "\u00A7$2");
}

public StaffChatLanguage getLanguage() {
return language;
}


public StaffChatSettings getConfig() {
return StaffChatSettings.getInstance(plugin);
}

}
59 changes: 59 additions & 0 deletions src/main/java/dev/oxs/staffchat/StaffChatLanguage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dev.oxs.staffchat;

// thank you johnymuffin

import org.bukkit.util.config.Configuration;

import java.io.File;
import java.util.HashMap;

public class StaffChatLanguage extends Configuration {
private HashMap<String, String> map;

public StaffChatLanguage(File file, boolean dev) {
super(file);
map = new HashMap<String, String>();
loadDefaults();
if(!dev) {
loadFile();
}
}

private void loadDefaults() {
map.put("no_permission", "&4Sorry, you don't have permission for this command.");
map.put("no_permission_other", "&4%player% &cdoes not have permission for this command.");
map.put("staffchat_usage", "&cUsage: /sc <message>");
map.put("publicchat_usage", "&cUsage: /pc <message>");
map.put("staffchat_toggle", "&7You have turned staff chat %status%&7.");
map.put("staffchat_togglejoin", "%prefix% &7Hello &f%player%&7, currently have staff chat %status%&7.");
map.put("staffchat_check", "&7You currently have staff chat %status%&7.");
map.put("staffchat_check_other", "&f%player% &7currently has staff chat %status%&7.");
map.put("staffchat_check_online", "&4%player% &cis not online.");
map.put("staffchat_menu", "&7--------- %prefix% &7---------" +
"\n&7Version: &f%version%" +
"\n&7Description: &f%description%" +
"\n&7Author(s): &f%author%");
}

private void loadFile() {
this.load();
for (String key : map.keySet()) {
if (this.getString(key) == null) {
this.setProperty(key, map.get(key));
} else {
map.put(key, this.getString(key));
}
}
this.save();
}

public String getMessage(String msg) {
String loc = map.get(msg);
if (loc != null) {
return loc.replace("&", "\u00a7");
}
return msg;
}


}
29 changes: 24 additions & 5 deletions src/main/java/dev/oxs/staffchat/StaffChatListener.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package dev.oxs.staffchat;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;

import java.util.HashMap;
import java.util.UUID;

Expand All @@ -26,17 +29,33 @@ public void onPlayerChat(PlayerChatEvent event) {
String message = event.getMessage();

if(player.hasPermission("staffchat.toggle") && player.hasPermission("staffchat.see") || player.isOp()) {
if (getToggleStatus(UUID.fromString(uuid))) {
if (plugin.getToggleStatus(UUID.fromString(uuid))) {
event.setCancelled(true);
plugin.StaffChatMessage(player.getName(), message);
plugin.StaffChatMessage(player, message);
}
}
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Boolean toggleJoinMsg = StaffChatSettings.getInstance(plugin).getConfigBoolean("settings.staffchat-toggle-alert");

if(toggleJoinMsg) {
Player player = event.getPlayer();
String uuid = String.valueOf(player.getUniqueId());

if (player.hasPermission("staffchat.toggle") && player.hasPermission("staffchat.see") || player.isOp()) {

private boolean getToggleStatus(UUID player) {
return staffChatToggled.getOrDefault(player, false);
}
Boolean s = plugin.getToggleStatus(UUID.fromString(uuid));

if (s) {
String message = plugin.getLanguage().getMessage("staffchat_togglejoin");
message = message.replace("%status%", (s ? ChatColor.GREEN + "on" : ChatColor.RED +"off"));
message = message.replace("%prefix%", plugin.getPluginPrefix());
message = message.replace("%player%", player.getName());
player.sendMessage(message);
}
}
}
}
}
110 changes: 110 additions & 0 deletions src/main/java/dev/oxs/staffchat/StaffChatSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package dev.oxs.staffchat;

// thank you johnymuffin

import org.bukkit.util.config.Configuration;

import java.io.File;

public class StaffChatSettings extends Configuration {
private static StaffChatSettings singleton;

private StaffChatSettings(StaffChat plugin) {
super(new File(plugin.getDataFolder(), "settings.yml"));
this.reload();
}

private void write() {
//Main
generateConfigOption("config-version", 1);
//Setting
generateConfigOption("settings.staffchat-prefix", "&c[StaffChat]&f");
generateConfigOption("settings.staffchat-toggle-alert", true);
generateConfigOption("settings.staffchat-use-displayNamesStaffChat", false);
generateConfigOption("settings.staffchat-use-displayNamesPublicChat", true);
generateConfigOption("settings.staffchat-publicChatPrefix", "&f<%player%&f> ");
}

public void generateConfigOption(String key, Object defaultValue) {
if (this.getProperty(key) == null) {
this.setProperty(key, defaultValue);
}
final Object value = this.getProperty(key);
this.removeProperty(key);
this.setProperty(key, value);
}

//Getters Start
public Object getConfigOption(String key) {
return this.getProperty(key);
}

public String getConfigString(String key) {
return String.valueOf(getConfigOption(key));
}

public Integer getConfigInteger(String key) {
return Integer.valueOf(getConfigString(key));
}

public Long getConfigLong(String key) {
return Long.valueOf(getConfigString(key));
}

public Double getConfigDouble(String key) {
return Double.valueOf(getConfigString(key));
}

public Boolean getConfigBoolean(String key) {
return Boolean.valueOf(getConfigString(key));
}


//Getters End
public Long getConfigLongOption(String key) {
if (this.getConfigOption(key) == null) {
return null;
}
return Long.valueOf(String.valueOf(this.getProperty(key)));
}


private boolean convertToNewAddress(String newKey, String oldKey) {
if (this.getString(newKey) != null) {
return false;
}
if (this.getString(oldKey) == null) {
return false;
}
System.out.println("Converting Config: " + oldKey + " to " + newKey);
Object value = this.getProperty(oldKey);
this.setProperty(newKey, value);
this.removeProperty(oldKey);
return true;

}


private void reload() {
this.load();
this.write();
this.save();
}

@Deprecated
public static StaffChatSettings getInstance() {
if (StaffChatSettings.singleton == null) {
throw new RuntimeException("A instance of StaffChat hasn't been passed into StaffChatConfig yet.");
}
return StaffChatSettings.singleton;
}

@Deprecated
public static StaffChatSettings getInstance(StaffChat plugin) {
if (StaffChatSettings.singleton == null) {
StaffChatSettings.singleton = new StaffChatSettings(plugin);
}
return StaffChatSettings.singleton;
}

}
Loading

0 comments on commit f658326

Please sign in to comment.