Skip to content
This repository was archived by the owner on Feb 17, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void registerCommands(){
registerCommand(new RemoveBlacklist());
registerCommand(new ShowBlacklist());
registerCommand(new NameLayerGroupGui());
registerCommand(new RenameGroup());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import vg.civcraft.mc.namelayer.GroupManager;
import vg.civcraft.mc.namelayer.NameAPI;
Expand All @@ -34,27 +35,9 @@ public void execute(Player sender, String groupName, @Optional String userPasswo
}

//enforce regulations on the name
if (name.length() > 32) {
p.sendMessage(ChatColor.RED + "The group name is not allowed to contain more than 32 characters");
return;
}
Charset latin1 = StandardCharsets.ISO_8859_1;
boolean invalidChars = false;
if (!latin1.newEncoder().canEncode(name)) {
invalidChars = true;
}
//cant allow them to hurt mercury :(
if (name.contains("|")) {
invalidChars = true;
}
boolean validName = validateGroupName(p, name);

for(char c:name.toCharArray()) {
if (Character.isISOControl(c)) {
invalidChars = true;
}
}

if(invalidChars) {
if(!validName) {
p.sendMessage(ChatColor.RED + "You used characters, which are not allowed");
return;
}
Expand Down Expand Up @@ -93,4 +76,33 @@ public void run() {
}
p.sendMessage(ChatColor.GREEN + "Group creation request is in process.");
}

/**
* Returns if the group name was validated successfully
* @param sender Player or console
* @param groupName Group name to validate
* @return True if the group name is fine, false if it violated any of our constraints
*/
public static boolean validateGroupName(CommandSender sender, String groupName) {
if (groupName.length() > 32) {
sender.sendMessage(ChatColor.RED + "The group name is not allowed to contain more than 32 characters");
return false;
}
Charset latin1 = StandardCharsets.ISO_8859_1;
boolean invalidCharacters = false;
if (!latin1.newEncoder().canEncode(groupName)) {
invalidCharacters = true;
}
//cant allow them to hurt mercury :(
if (groupName.contains("|")) {
invalidCharacters = true;
}

for(char c:groupName.toCharArray()) {
if (Character.isISOControl(c)) {
invalidCharacters = true;
}
}
return !invalidCharacters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package vg.civcraft.mc.namelayer.command.commands;

import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Syntax;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import vg.civcraft.mc.namelayer.GroupManager;
import vg.civcraft.mc.namelayer.NameAPI;
import vg.civcraft.mc.namelayer.command.BaseCommandMiddle;
import vg.civcraft.mc.namelayer.group.Group;
import vg.civcraft.mc.namelayer.permission.PermissionType;

public class RenameGroup extends BaseCommandMiddle {

@CommandAlias("nlrg|renamegroup")
@Syntax("<current_group> <new_name>")
@Description("Renames a group")
@CommandCompletion("@NL_Groups @nothing")
public void execute(CommandSender sender, String currentGroup, String newGroupName) {
if (currentGroup == null || currentGroup.isEmpty()) {
sender.sendMessage(Component.text(currentGroup + " is not a valid group", NamedTextColor.RED));
return;
}
if (newGroupName == null || newGroupName.isEmpty()) {
sender.sendMessage(Component.text(currentGroup + " is not a valid name", NamedTextColor.RED));
return;
}
boolean validGroupName = CreateGroup.validateGroupName(sender, newGroupName);
if (!validGroupName) {
sender.sendMessage(ChatColor.RED + "You used characters, which are not allowed");
return;
}
Group targetGroup = GroupManager.getGroup(currentGroup);
if (targetGroup == null) {
sender.sendMessage(Component.text(currentGroup + " is not a valid group", NamedTextColor.RED));
return;
}
if (!(sender instanceof Player player)) {
//We are console here
targetGroup.setName(newGroupName, true);
sender.sendMessage(Component.text("You have changed the group " + currentGroup + " to " + newGroupName, NamedTextColor.GREEN));
return;
}
boolean hasPerm = NameAPI.getGroupManager().hasAccess(targetGroup, player.getUniqueId(), PermissionType.getPermission("OWNER"));
if (!hasPerm) {
player.sendMessage(Component.text("You do not have the adequate permission to change this group's name!", NamedTextColor.RED));
return;
}
//We do have permission here
GroupManager.invalidateCache(targetGroup.getName());
targetGroup.setName(newGroupName);
//We use getGroup here to force the group to be reloaded and readded to the cache. We removed it above here using invalidateCache.
GroupManager.getGroup(targetGroup.getName());
player.sendMessage(Component.text("You have changed the group's name from " + currentGroup + " to " + newGroupName, NamedTextColor.GREEN));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ public class GroupManagerDao {

private static final String updateOwner = "update faction set founder = ? "
+ "where group_name = ?";


private static final String updateGroupName = "update faction, faction_id set faction.group_name = ?, faction_id.group_name = ? " +
"where faction.group_name = ? AND faction_id.group_name = ?;";

private static final String updateDisciplined = "update faction set discipline_flags = ? "
+ "where group_name = ?";

Expand Down Expand Up @@ -1317,6 +1320,23 @@ public Map <UUID, String> getAllDefaultGroups() {
}
return groups;
}

public void setNewGroupName(String oldGroupName, String newGroupName) {
try (Connection connection = db.getConnection();
PreparedStatement updateName = connection.prepareStatement(GroupManagerDao.updateGroupName);){
updateName.setString(1, newGroupName);
updateName.setString(2, newGroupName);
updateName.setString(3, oldGroupName);
updateName.setString(4, oldGroupName);
updateName.executeUpdate();
} catch (SQLException e) {
logger.log(Level.WARNING, "Problem setting new name of group " + oldGroupName + " to " + newGroupName, e);
}
}

public void setNewGroupNameAsync(String oldGroupName, String newGroupName) {
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> setNewGroupName(oldGroupName, newGroupName));
}

/**
* Use this method to override the current founder of a group.
Expand Down
13 changes: 12 additions & 1 deletion paper/src/main/java/vg/civcraft/mc/namelayer/group/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,18 @@ public void setOwner(UUID uuid, boolean savetodb) {
db.setFounder(uuid, this);
}
}


public void setName(String name) {
setName(name, true);
}

public void setName(String name, boolean saveToDB) {
if (saveToDB) {
db.setNewGroupName(this.name, name);
}
this.name = name;
}

public void setDisciplined(boolean value){
setDisciplined(value, true);
}
Expand Down