Skip to content

Commit

Permalink
v2.0 upload
Browse files Browse the repository at this point in the history
  • Loading branch information
cgolden15 committed Apr 11, 2024
1 parent 10d9917 commit 471f021
Show file tree
Hide file tree
Showing 36 changed files with 328 additions and 206 deletions.
Binary file added .gradle/8.5/checksums/checksums.lock
Binary file not shown.
Binary file added .gradle/8.5/checksums/md5-checksums.bin
Binary file not shown.
Binary file added .gradle/8.5/checksums/sha1-checksums.bin
Binary file not shown.
Binary file not shown.
Empty file.
Binary file added .gradle/8.5/executionHistory/executionHistory.bin
Binary file not shown.
Binary file added .gradle/8.5/executionHistory/executionHistory.lock
Binary file not shown.
Binary file added .gradle/8.5/fileChanges/last-build.bin
Binary file not shown.
Binary file added .gradle/8.5/fileHashes/fileHashes.bin
Binary file not shown.
Binary file added .gradle/8.5/fileHashes/fileHashes.lock
Binary file not shown.
Binary file added .gradle/8.5/fileHashes/resourceHashesCache.bin
Binary file not shown.
Empty file added .gradle/8.5/gc.properties
Empty file.
Binary file added .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
2 changes: 2 additions & 0 deletions .gradle/buildOutputCleanup/cache.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Wed Apr 10 23:40:31 CDT 2024
gradle.version=8.5
Binary file added .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file added .gradle/file-system.probe
Binary file not shown.
Empty file added .gradle/vcs-1/gc.properties
Empty file.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/modules/BetterGroups.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = 'dev.goldenn'
version = '1.1'
version = '2.0'

repositories {
mavenCentral()
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added build/libs/BetterGroups-2.0.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion build/resources/main/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: BetterGroups
version: '1.1'
version: '2.0'
main: dev.goldenn.bettergroups.BetterGroups
api-version: '1.16'
prefix: BetterGroups
Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
220 changes: 16 additions & 204 deletions src/main/java/dev/goldenn/bettergroups/BetterGroups.java
Original file line number Diff line number Diff line change
@@ -1,227 +1,39 @@
package dev.goldenn.bettergroups;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.*;

public class BetterGroups extends JavaPlugin {

public class BetterGroups extends JavaPlugin implements CommandExecutor, Listener {


private Map<UUID, List<String>> playerGroups = new HashMap<>();
private Map<String, String> groups = new HashMap<>();
private Map<String, Integer> groupHierarchy = new HashMap<>();
private Map<String, Set<UUID>> groupUsers = new HashMap<>();
private GroupManager groupManager;
private CommandManager commandManager;
private String chatFormat;


@Override
public void onEnable() {
// Load groups and group hierarchy from config
FileConfiguration config = getConfig();
ConfigurationSection groupsConfig = config.getConfigurationSection("groups");
if (groupsConfig != null) {
for (String groupName : groupsConfig.getKeys(false)) {
String prefix = groupsConfig.getString(groupName + ".prefix");
int hierarchy = groupsConfig.getInt(groupName + ".hierarchy");
groups.put(groupName, prefix);
// Initialize GroupManager
this.groupManager = new GroupManager(this);

// Load users as UUIDs from config
Set<String> userStrings = new HashSet<>(groupsConfig.getStringList(groupName + ".users"));
Set<UUID> users = userStrings.stream()
.map(UUID::fromString)
.collect(Collectors.toSet());
groupUsers.put(groupName, users);

groupHierarchy.put(groupName, hierarchy);
}
}
// Initialize CommandManager with GroupManager instance
this.commandManager = new CommandManager(this, groupManager);

// Load chat format from config
FileConfiguration config = getConfig();
chatFormat = config.getString("chatFormat", "{TAG}&r {PLAYER}:&r {MESSAGE}");

// Register commands and events
this.getCommand("addtogroup").setExecutor(this);
this.getCommand("removefromgroup").setExecutor(this);
this.getCommand("tagreload").setExecutor(this);
this.getCommand("creategroup").setExecutor(this);
Bukkit.getPluginManager().registerEvents(this, this);
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("creategroup")) {
if (args.length >= 2) {
String groupName = args[0];
String prefix = args[1];

if (!groups.containsKey(groupName)) {
int hierarchy = args.length > 2 ? Integer.parseInt(args[2]) : 0;

groups.put(groupName, prefix);
groupHierarchy.put(groupName, hierarchy);

// Save to config
FileConfiguration config = getConfig();
config.set("groups." + groupName + ".prefix", prefix);
config.set("groups." + groupName + ".hierarchy", hierarchy);
saveConfig();

sender.sendMessage(ChatColor.GREEN + "Group created successfully!");
} else {
sender.sendMessage(ChatColor.RED + "Group already exists!");
}
return true;
}
} else if (cmd.getName().equalsIgnoreCase("addtogroup")) {
if (args.length >= 2) {
String groupName = args[0];
String playerName = args[1];

if (groups.containsKey(groupName)) {
Player player = Bukkit.getPlayer(playerName);
if (player != null) {
UUID playerUUID = player.getUniqueId();
Set<UUID> users = groupUsers.getOrDefault(groupName, new HashSet<>());
if (!users.contains(playerUUID)) {
users.add(playerUUID);
groupUsers.put(groupName, users);

// Save to config
FileConfiguration config = getConfig();
config.set("groups." + groupName + ".users", new ArrayList<>(users.stream().map(UUID::toString).collect(Collectors.toList())));
saveConfig();

sender.sendMessage(ChatColor.GREEN + "User added to group successfully!");
} else {
sender.sendMessage(ChatColor.RED + "User is already in the group!");
}
} else {
sender.sendMessage(ChatColor.RED + "Player not found!");
}
} else {
sender.sendMessage(ChatColor.RED + "Group does not exist!");
}
return true;
}
} else if (cmd.getName().equalsIgnoreCase("removefromgroup")) {
if (args.length == 2) {
String playerName = args[0];
String groupName = args[1];

if (groups.containsKey(groupName)) {
Player player = Bukkit.getPlayer(playerName);
if (player != null) {
UUID playerUUID = player.getUniqueId();
Set<UUID> users = groupUsers.getOrDefault(groupName, new HashSet<>());
if (users.contains(playerUUID)) {
users.remove(playerUUID);
groupUsers.put(groupName, users);

// Save to config
FileConfiguration config = getConfig();
config.set("groups." + groupName + ".users", new ArrayList<>(users.stream().map(UUID::toString).collect(Collectors.toList())));
saveConfig();

sender.sendMessage(ChatColor.GREEN + "User removed from group successfully!");
} else {
sender.sendMessage(ChatColor.RED + "User is not in the group!");
}
} else {
sender.sendMessage(ChatColor.RED + "Player not found!");
}
} else {
sender.sendMessage(ChatColor.RED + "Group does not exist!");
}
return true;
}
} else if (cmd.getName().equalsIgnoreCase("tagreload")) {
reloadConfig();

// Reload groups, group hierarchy, and chat format from config
groups.clear();
groupHierarchy.clear();

FileConfiguration config = getConfig();
ConfigurationSection groupsConfig = config.getConfigurationSection("groups");
if (groupsConfig != null) {
for (String groupName : groupsConfig.getKeys(false)) {
String prefix = groupsConfig.getString(groupName + ".prefix");
int hierarchy = groupsConfig.getInt(groupName + ".hierarchy");
groups.put(groupName, prefix);
groupHierarchy.put(groupName, hierarchy);
}
}

// Reload chat format
chatFormat = config.getString("chatFormat", "{TAG}&r {PLAYER}:&r {MESSAGE}");

sender.sendMessage(ChatColor.GREEN + "Config reloaded!");
return true;
}
return false;
// Register events
getServer().getPluginManager().registerEvents(groupManager, this);
}

private void addGroup(UUID playerId, String groupName) {
List<String> groups = playerGroups.computeIfAbsent(playerId, k -> new ArrayList<>());
groups.add(groupName);

// Sort groups by hierarchy
groups.sort(Comparator.comparingInt(group -> groupHierarchy.getOrDefault(group, 0)));

// Remove duplicate groups
Set<String> uniqueGroups = new LinkedHashSet<>(groups);
groups.clear();
groups.addAll(uniqueGroups);
public GroupManager getGroupManager() {
return groupManager;
}

private void removeGroup(UUID playerId, String groupName) {
List<String> groups = playerGroups.get(playerId);
if (groups != null) {
groups.remove(groupName);
}
public CommandManager getCommandManager() {
return commandManager;
}

@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event) {
Player player = event.getPlayer();
String playerName = player.getName();
String message = event.getMessage();

String playerGroup = null;
int highestHierarchy = Integer.MIN_VALUE;

for (Map.Entry<String, Set<UUID>> entry : groupUsers.entrySet()) {
if (entry.getValue().contains(player.getUniqueId())) {
int hierarchy = groupHierarchy.get(entry.getKey());
if (hierarchy > highestHierarchy) {
highestHierarchy = hierarchy;
playerGroup = entry.getKey();
}
}
}

if (playerGroup != null) {
String prefix = groups.get(playerGroup);
String formattedMessage = chatFormat
.replace("{TAG}", prefix)
.replace("{PLAYER}", playerName)
.replace("{MESSAGE}", message);

event.setFormat(ChatColor.translateAlternateColorCodes('&', formattedMessage));
}
public String getChatFormat() {
return chatFormat;
}

}
Loading

0 comments on commit 471f021

Please sign in to comment.