Skip to content

Commit

Permalink
Claim may now be named!
Browse files Browse the repository at this point in the history
This feature is not supported/persistent with use of MySQL.

I do not intend to support the current implementation of MySQL with new functionality, but will also not break compatability with upstream at this point.
  • Loading branch information
nouish committed Aug 4, 2024
1 parent fd6dd05 commit 0d83f59
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 31 deletions.
13 changes: 13 additions & 0 deletions src/main/java/me/ryanhamshire/GriefPrevention/Claim.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;
Expand All @@ -66,6 +67,8 @@ public class Claim
//id number. unique to this claim, never changes.
Long id = null;

private String name;

//ownerID. for admin claims, this is NULL
//use getOwnerName() to get a friendly name (will be "an administrator" for admin claims)
public UUID ownerID;
Expand Down Expand Up @@ -132,6 +135,16 @@ public boolean canSiege(Player defender)
return true;
}

public void setName(String name)
{
this.name = name;
}

public Optional<String> getName()
{
return name != null ? Optional.of(name) : Optional.empty();
}

//removes any lava above sea level in a claim
//exclusionClaim is another claim indicating an sub-area to be excluded from this operation
//it may be null
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/me/ryanhamshire/GriefPrevention/DataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,8 @@ protected void loadMessages()
this.addDefault(defaults, Messages.NoEnoughBlocksForChestClaim, "Because you don't have any claim blocks available, no automatic land claim was created for you. You can use /claimslist to monitor your available claim block total.", null);
this.addDefault(defaults, Messages.MustHoldModificationToolForThat, "You must be holding a golden shovel to do that.", null);
this.addDefault(defaults, Messages.StandInClaimToResize, "Stand inside the land claim you want to resize.", null);
this.addDefault(defaults, Messages.RenameSuccess, "Claim name changed to: {0}.", "0: The new claim name.");
this.addDefault(defaults, Messages.BadNameInput, "Sorry, but that is not a valid name. A valid name is 3-20 characters long, and must only contain letters, numbers and spaces.", null);
this.addDefault(defaults, Messages.ClaimsExtendToSky, "Land claims always extend to max build height.", null);
this.addDefault(defaults, Messages.ClaimsAutoExtendDownward, "Land claims auto-extend deeper into the ground when you place blocks under them.", null);
this.addDefault(defaults, Messages.MinimumRadius, "Minimum radius is {0}.", "0: minimum radius");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -482,6 +483,8 @@ Claim loadClaim(String input, ArrayList<Long> out_parentID, long lastModifiedDat
}
}

String name = yaml.getString("Name");

List<String> builders = yaml.getStringList("Builders");

List<String> containers = yaml.getStringList("Containers");
Expand All @@ -498,6 +501,7 @@ Claim loadClaim(String input, ArrayList<Long> out_parentID, long lastModifiedDat
claim = new Claim(lesserBoundaryCorner, greaterBoundaryCorner, ownerID, builders, containers, accessors, managers, inheritNothing, claimID);
claim.modifiedDate = new Date(lastModifiedDate);
claim.id = claimID;
claim.setName(name);

return claim;
}
Expand All @@ -515,6 +519,8 @@ String getYamlForClaim(Claim claim)
if (claim.ownerID != null) ownerID = claim.ownerID.toString();
yaml.set("Owner", ownerID);

yaml.set("Name", claim.getName().orElse(null));

ArrayList<String> builders = new ArrayList<>();
ArrayList<String> containers = new ArrayList<>();
ArrayList<String> accessors = new ArrayList<>();
Expand Down
87 changes: 66 additions & 21 deletions src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,6 @@

package me.ryanhamshire.GriefPrevention;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.griefprevention.visualization.BoundaryVisualization;
Expand Down Expand Up @@ -84,6 +63,27 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class GriefPrevention extends JavaPlugin
{
//for convenience, a reference to the instance of this plugin
Expand Down Expand Up @@ -257,6 +257,7 @@ public class GriefPrevention extends JavaPlugin
private String databaseUserName;
private String databasePassword;

private static final Pattern CLAIM_NAME_PATTERN = Pattern.compile("[0-9a-zA-Z-_ ']{3,20}");

//how far away to search from a tree trunk for its branch blocks
public static final int TREE_RADIUS = 5;
Expand Down Expand Up @@ -1527,6 +1528,11 @@ else if (cmd.getName().equalsIgnoreCase("trustlist") && player != null)
.append(String.valueOf(claim.id))
.color(net.md_5.bungee.api.ChatColor.YELLOW)
.append("\n")
.append("Name: ")
.color(net.md_5.bungee.api.ChatColor.GRAY)
.append(claim.getName().orElse("<unnamed>"))
.color(net.md_5.bungee.api.ChatColor.YELLOW)
.append("\n")
.append("Owner: ")
.color(net.md_5.bungee.api.ChatColor.GRAY)
.append(trustEntryToPlayerName(claim.ownerID == null ? "public" : claim.ownerID.toString()))
Expand Down Expand Up @@ -2734,7 +2740,46 @@ else if (cmd.getName().equalsIgnoreCase("gpreload"))

return true;
}
else if (cmd.getName().equalsIgnoreCase("nameclaim") && player != null)
{
final String newName = args.length >= 1 ? String.join(" ", args) : null;

// Validate new name
if (newName != null && !CLAIM_NAME_PATTERN.matcher(newName).matches())
{
GriefPrevention.sendMessage(player, TextMode.Err, Messages.BadNameInput);
return true;
}

//which claim is being abandoned?
Claim claim = this.dataStore.getClaimAt(player.getLocation(), true /*ignore height*/, null);
if (claim == null)
{
GriefPrevention.sendMessage(player, TextMode.Instr, Messages.DeleteClaimMissing);
return true;
}

//verify ownership
if ((claim.isAdminClaim() && !player.hasPermission("griefprevention.adminclaims"))
|| (!claim.isAdminClaim() && !player.getUniqueId().equals(claim.getOwnerID())))
{
PlayerData playerData = dataStore.getPlayerData(player.getUniqueId());
if (!playerData.ignoreClaims)
{
String who = claim.isAdminClaim() ? "the administrators" : claim.getOwnerName();
GriefPrevention.sendMessage(player, TextMode.Err, Messages.OnlyOwnersModifyClaims, who);
return true;
}
}

claim.setName(newName);
sendMessage(player, TextMode.Instr, Messages.RenameSuccess, newName != null ? newName : "<unnamed>");

// IO write on main thread.
// This is not ideal, but it is consistent with prexisting claim-commands.
dataStore.saveClaim(claim);
return true;
}
//givepet
else if (cmd.getName().equalsIgnoreCase("givepet") && player != null)
{
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/me/ryanhamshire/GriefPrevention/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ public enum Messages
IsIgnoringYou,
MustHoldModificationToolForThat,
StandInClaimToResize,
RenameSuccess,
BadNameInput,
ClaimsExtendToSky,
ClaimsAutoExtendDownward,
MinimumRadius,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ void onPlayerMove(PlayerMoveEvent event)

if (claim != null && lastClaim != null)
{
// Skip if both claims are administrative.
if (claim.isAdminClaim() && lastClaim.isAdminClaim())
// Skip if both claims are administrative and have the same name.
if (claim.isAdminClaim() && lastClaim.isAdminClaim() && Objects.equals(claim.getName(), lastClaim.getName()))
{
return;
}

// Skip if both claims share the same owner.
if (Objects.equals(claim.getOwnerID(), lastClaim.getOwnerID()))
// Skip if both claims share the same owner and name
if (Objects.equals(claim.getOwnerID(), lastClaim.getOwnerID()) && Objects.equals(claim.getName(), lastClaim.getName()))
{
return;
}
Expand All @@ -200,14 +200,29 @@ void onPlayerMove(PlayerMoveEvent event)
else
{
String ownerName = claim.ownerID == null
? "an administrator"
? "the administrators"
: GriefPrevention.lookupPlayerName(claim.ownerID);

player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new ComponentBuilder()
.append("You've entered ").color(net.md_5.bungee.api.ChatColor.GREEN)
.append(ownerName).color(net.md_5.bungee.api.ChatColor.AQUA)
.append("'s claim.").color(net.md_5.bungee.api.ChatColor.GREEN)
.create());
String claimName = claim.getName().orElse(null);

if (claimName != null)
{
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new ComponentBuilder()
.append("You've entered \"").color(net.md_5.bungee.api.ChatColor.GREEN)
.append(claimName).color(net.md_5.bungee.api.ChatColor.AQUA)
.append("\" by ").color(net.md_5.bungee.api.ChatColor.GREEN)
.append(ownerName).color(net.md_5.bungee.api.ChatColor.AQUA)
.append(".").color(net.md_5.bungee.api.ChatColor.GREEN)
.create());
}
else
{
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new ComponentBuilder()
.append("You've entered ").color(net.md_5.bungee.api.ChatColor.GREEN)
.append(ownerName).color(net.md_5.bungee.api.ChatColor.AQUA)
.append("'s claim.").color(net.md_5.bungee.api.ChatColor.GREEN)
.create());
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ commands:
description: Gives a player a manual about claiming land.
usage: /<command> <player>
permission: griefprevention.claimbook
nameclaim:
description: Set the name for the current claim.
usage: /<command> <name>
permission: griefprevention.name
permissions:
griefprevention.createclaims:
description: Grants permission to create claims.
Expand All @@ -229,6 +233,7 @@ permissions:
griefprevention.restorenatureaggressive: true
griefprevention.ignoreclaims: true
griefprevention.adminclaims: true
griefprevention.name: true
griefprevention.adjustclaimblocks: true
griefprevention.deleteclaims: true
griefprevention.spam: true
Expand Down Expand Up @@ -291,6 +296,9 @@ permissions:
griefprevention.adjustclaimblocks:
description: Grants permission to add or remove bonus blocks from a player's account.
default: op
griefprevention.name:
description: Grants permission to set a name on claims.
default: true
griefprevention.spam:
description: Grants permission to log in, send messages, and send commands rapidly.
default: op
Expand Down

0 comments on commit 0d83f59

Please sign in to comment.