Skip to content

Commit

Permalink
Version 1.2
Browse files Browse the repository at this point in the history
[Bugfix]
- removing a status now functions
- the console can now change status messages from players

changes:
- revamped the whole StatusCommand.java for a better understanding of the code and efficiency
added:
- maxlenght commands to set a maxiumum character lenght for the Status (colorcodes are excluded)
- added permissions for said command (only admin)
- added a config.yml for saving purposes of the maxium set lenght (default is 15)
- added StatusTabCompleter.java for Tab completion in the chat windows when using /status commands

Tested-by:
Me on a localserver

PS: hopefullly this works
PPS: working on multi version support

Took 3 hours 20 minutes
  • Loading branch information
TubYoub committed Nov 26, 2023
1 parent bcad4e5 commit 3c17c38
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 130 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.tubyoub</groupId>
<artifactId>StatusPlugin</artifactId>
<version>1.1</version>
<version>1.2</version>
<packaging>jar</packaging>

<name>Tub's Status Plugin</name>
Expand Down Expand Up @@ -39,7 +39,7 @@
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<outputFile>C:\Users\%USERPROFILE%\OneDrive\Desktop\Server\plugins\TubsStatusPlugin-1.1.jar</outputFile>
<outputFile>C:\Users\jornw\OneDrive\Desktop\Server\plugins\TubsStatusPlugin-1.1.jar</outputFile>
</configuration>
</execution>
</executions>
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/de/tubyoub/statusplugin/PlayerJoinListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ public PlayerJoinListener(StatusManager statusManager) {
this.statusManager = statusManager;
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
String status = (String) statusManager.getStatus(player);
if (status != null) {
player.setDisplayName("[" + status + ChatColor.RESET + "] " + ChatColor.WHITE + player.getName());
player.setPlayerListName("[" + status + ChatColor.RESET + "] " + ChatColor.WHITE + player.getName());
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
statusManager.updateDisplayName(player);
}

}
65 changes: 53 additions & 12 deletions src/main/java/de/tubyoub/statusplugin/StatusManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,71 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


import static org.bukkit.Bukkit.getLogger;

public class StatusManager {
private final File statusFile;
private final Map<UUID, String> statusMap = new HashMap<>();

private static final int DEFAULT_MAX_LENGTH = 15;
private int maxStatusLength = DEFAULT_MAX_LENGTH;
private final StatusPlugin plugin;

public StatusManager(StatusPlugin plugin) {
this.plugin = plugin;
this.maxStatusLength = plugin.getConfig().getInt("maxStatusLength", DEFAULT_MAX_LENGTH);
this.statusFile = new File(plugin.getDataFolder(), "statuses.yml");
loadStatuses();
}

public void setStatus(Player player, String status, CommandSender sender) {
status = translateColorsAndFormatting(status, sender);
statusMap.put(player.getUniqueId(), status);
updateDisplayName(player);
saveStatuses();
public boolean setStatus(Player player, String status, CommandSender sender) {
String translatedStatus = translateColorsAndFormatting(status, sender);
if (calculateEffectiveLength(translatedStatus) > maxStatusLength) {
sender.sendMessage(ChatColor.RED + "Status is too long. Max length is " + maxStatusLength + " characters.");
return false;
}
statusMap.put(player.getUniqueId(), translatedStatus);
updateDisplayName(player);
saveStatuses();
return true;
}


public String getStatus(Player player) {
return statusMap.get(player.getUniqueId());
}

public void updateDisplayName(Player player) {
String status = statusMap.get(player.getUniqueId());
String status = getStatus(player);
if (status != null) {
player.setDisplayName("[" + status + ChatColor.RESET + "] " + ChatColor.WHITE + player.getName());
player.setPlayerListName("[" + status + ChatColor.RESET + "] " + ChatColor.WHITE + player.getName());
String displayName = "[" + status + ChatColor.RESET + "] " + ChatColor.WHITE + player.getName();
ChatColor.translateAlternateColorCodes('&', displayName);
player.setDisplayName(displayName);
player.setPlayerListName(displayName);
} else {
player.setDisplayName(player.getName());
player.setPlayerListName(player.getName());
}
}
public int getMaxStatusLength() {
return maxStatusLength;
}

public void setMaxStatusLength(int maxLength) {
this.maxStatusLength = maxLength;
plugin.getConfig().set("maxStatusLength", maxLength);
plugin.saveConfig();
}

public void resetMaxStatusLength() {
this.maxStatusLength = DEFAULT_MAX_LENGTH;
plugin.getConfig().set("maxStatusLength", DEFAULT_MAX_LENGTH);
plugin.saveConfig();
}

private void loadStatuses() {
if (!statusFile.exists()) return;
Expand All @@ -55,7 +92,7 @@ public void saveStatuses() {
try {
yaml.save(statusFile);
} catch (IOException e) {
e.printStackTrace();
getLogger().severe("Could not save statuses: " + e.getMessage());
}
}

Expand All @@ -72,9 +109,13 @@ public void removeStatus(Player player) {
player.setDisplayName(player.getName());
player.setPlayerListName(player.getName());
saveStatuses();
}


}
public int calculateEffectiveLength(String text) {
Pattern pattern = Pattern.compile("&[0-9a-fk-or]");
Matcher matcher = pattern.matcher(text);
String withoutColorCodes = matcher.replaceAll("");
return withoutColorCodes.length();
}
public void reloadStatuses() {
statusMap.clear();
loadStatuses();
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/de/tubyoub/statusplugin/StatusPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@ public class StatusPlugin extends JavaPlugin {
@Override
public void onEnable() {
this.statusManager = new StatusManager(this);
this.saveDefaultConfig();
this.statusManager = new StatusManager(this);
StatusCommand statusCommand = new StatusCommand(statusManager);
getCommand("status").setExecutor(new StatusCommand(this.statusManager));
getServer().getPluginManager().registerEvents(new PlayerJoinListener(this.statusManager), this);
getCommand("status").setExecutor(statusCommand);
getCommand("status").setTabCompleter(new StatusTabCompleter());
}

public void sendPluginMessages(CommandSender sender, String type) {
if ("title".equals(type)) {
sender.sendMessage(ChatColor.GOLD + "◢◤" + ChatColor.YELLOW + "Tu" + ChatColor.DARK_GREEN + "b's" + ChatColor.DARK_AQUA + " Status"
+ ChatColor.GOLD + " Plugin" + ChatColor.YELLOW + "◥◣");
sender.sendMessage(ChatColor.GOLD + "◢◤" + ChatColor.YELLOW + "Tu" + ChatColor.DARK_GREEN + "b's" + ChatColor.DARK_AQUA + " Status" + ChatColor.GOLD + " Plugin" + ChatColor.YELLOW + "◥◣");
} else if ("line".equals(type)) {
sender.sendMessage(ChatColor.GOLD + "-" + ChatColor.YELLOW + "-" + ChatColor.GREEN + "-" + ChatColor.DARK_GREEN + "-" + ChatColor.BLUE + "-" + ChatColor.DARK_AQUA + "-"
+ ChatColor.GOLD + "-" + ChatColor.YELLOW + "-" + ChatColor.GREEN + "-" + ChatColor.DARK_GREEN + "-" + ChatColor.BLUE + "-" + ChatColor.DARK_AQUA + "-"
+ ChatColor.GOLD + "-" + ChatColor.YELLOW + "-" + ChatColor.GREEN + "-" + ChatColor.DARK_GREEN + "-" + ChatColor.BLUE + "-" + ChatColor.DARK_AQUA + "-"
+ ChatColor.GOLD + "-");
}
}

@Override
public void onDisable() {
statusManager.saveStatuses();
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/de/tubyoub/statusplugin/StatusTabCompleter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.tubyoub.statusplugin;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;

public class StatusTabCompleter implements TabCompleter {
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
List<String> suggestions = new ArrayList<>();

if (args.length == 1) {
suggestions.add("help");
suggestions.add("remove");
suggestions.add("setmaxlength");
suggestions.add("resetmaxlength");
suggestions.add("info");
suggestions.add("reload");
} else if (args.length == 2) {
if (args[0].equalsIgnoreCase("remove")) {
suggestions.addAll(Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()));
} else if (args[0].equalsIgnoreCase("setmaxlength")) {
suggestions.add("10");
suggestions.add("20");
suggestions.add("30");
} else if (args[0].equalsIgnoreCase("help")) {
suggestions.add("colorcodes");
}
}
return suggestions;
}
}
Loading

0 comments on commit 3c17c38

Please sign in to comment.