Skip to content

Commit

Permalink
work on home creation
Browse files Browse the repository at this point in the history
SetHomeCommand
  • Loading branch information
UsainSrht committed Dec 26, 2023
1 parent d384b81 commit e55c83a
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,11 @@
<artifactId>commodore</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>net.luckperms</groupId>
<artifactId>api</artifactId>
<version>5.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
45 changes: 45 additions & 0 deletions src/main/java/me/usainsrht/uhomes/HomeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import de.tr7zw.changeme.nbtapi.NBTFile;
import de.tr7zw.changeme.nbtapi.NBTList;
import de.tr7zw.changeme.nbtapi.NBTListCompound;
import me.usainsrht.uhomes.config.MainConfig;
import me.usainsrht.uhomes.util.NBTUtil;
import net.kyori.adventure.text.minimessage.tag.resolver.Formatter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.PermissionAttachmentInfo;

import javax.annotation.Nullable;
import java.io.File;
Expand Down Expand Up @@ -113,6 +117,47 @@ public NBTFile getNBTFile(UUID uuid) {
return nbtFile;
}

public int getHomeLimit(UUID uuid) {
return getHomeLimit(Bukkit.getEntity(uuid));
}

public int getHomeLimit(Permissible permissible) {
if (MainConfig.isSumHomeLimits()) {
int total = 0;
for (PermissionAttachmentInfo permInfo : permissible.getEffectivePermissions()) {
String perm = permInfo.getPermission();
if (!perm.startsWith(MainConfig.getHomeLimitPermission())) continue;
String substr = perm.substring(MainConfig.getHomeLimitPermission().length());
try {
int number = Integer.parseInt(substr);
total += number;
} catch (NumberFormatException ignore) {}
}
return total;
} else {
int highest = 0;
for (PermissionAttachmentInfo permInfo : permissible.getEffectivePermissions()) {
String perm = permInfo.getPermission();
if (!perm.startsWith(MainConfig.getHomeLimitPermission())) continue;
String substr = perm.substring(MainConfig.getHomeLimitPermission().length());
try {
int number = Integer.parseInt(substr);
if (number > highest) highest = number;
} catch (NumberFormatException ignore) {}
}
return highest;
}
}

public CompletableFuture<Boolean> canRegisterHome(UUID uuid) {
CompletableFuture<List<Home>> future = getHomes(uuid);
CompletableFuture<Boolean> canRegisterFuture = new CompletableFuture<>();
future.thenAccept(homes -> canRegisterFuture.complete(homes.size() < getHomeLimit(uuid)));
return canRegisterFuture;
}



public UHomes getPlugin() {
return plugin;
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/me/usainsrht/uhomes/UHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import me.lucko.commodore.CommodoreProvider;
import me.usainsrht.uhomes.command.CommandHandler;
import me.usainsrht.uhomes.command.HomeCommand;
import me.usainsrht.uhomes.command.SetHomeCommand;
import me.usainsrht.uhomes.config.MainConfig;
import net.luckperms.api.LuckPerms;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
Expand All @@ -15,6 +17,7 @@ public final class UHomes extends JavaPlugin {
private static final int pluginID = 20539;
private Metrics metrics;
private HomeManager homeManager;
private LuckPerms luckPerms;
private Commodore commodore;
public File HOMES_FOLDER;

Expand Down Expand Up @@ -54,7 +57,10 @@ public void registerCommands() {
HomeCommand homeCommand = new HomeCommand(MainConfig.getHomeCommand());
CommandHandler.register("uhomes", homeCommand);
commodore.register(homeCommand, homeCommand.getCommodoreCommand());
getLogger().info("home command registered " + homeCommand + " " + homeCommand.isRegistered());

SetHomeCommand setHomeCommand = new SetHomeCommand(MainConfig.getSetHomeCommand());
CommandHandler.register("uhomes", setHomeCommand);
commodore.register(setHomeCommand, setHomeCommand.getCommodoreCommand());
}

public Metrics getMetrics() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/usainsrht/uhomes/command/HomeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public HomeCommand(YamlCommand cmd) {
public LiteralCommandNode<?> getCommodoreCommand() {
return LiteralArgumentBuilder.literal(super.getName())
.then(RequiredArgumentBuilder.argument("home-name", StringArgumentType.greedyString()))
.then(RequiredArgumentBuilder.argument("player", StringArgumentType.word()))
.then(RequiredArgumentBuilder.argument("player", StringArgumentType.word())
.then(RequiredArgumentBuilder.argument("home-name", StringArgumentType.greedyString())))
.build();
}

Expand Down
96 changes: 95 additions & 1 deletion src/main/java/me/usainsrht/uhomes/command/SetHomeCommand.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,98 @@
package me.usainsrht.uhomes.command;

public class SetHomeCommand {
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import me.usainsrht.uhomes.Home;
import me.usainsrht.uhomes.HomeManager;
import me.usainsrht.uhomes.UHomes;
import me.usainsrht.uhomes.config.MainConfig;
import me.usainsrht.uhomes.util.MessageUtil;
import me.usainsrht.uhomes.util.SoundUtil;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Formatter;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class SetHomeCommand extends Command {

private String permission;
private String permissionMessage;
private Collection<Sound> permissionSounds;

public SetHomeCommand(YamlCommand cmd) {
super(cmd.getName(), cmd.getDescription(), cmd.getUsage(), cmd.getAliases());
this.permission = cmd.getPermission();
this.permissionMessage = cmd.getPermissionMessage();
this.permissionSounds = cmd.getPermissionSounds();
}

public LiteralCommandNode<?> getCommodoreCommand() {
return LiteralArgumentBuilder.literal(super.getName())
.then(RequiredArgumentBuilder.argument("home-name", StringArgumentType.greedyString()))
.build();
}

@Override
public boolean execute(CommandSender sender, String command, String[] args) {
if (!sender.hasPermission(permission)) {
Component permMsg = MiniMessage.miniMessage().deserialize(permissionMessage,
Placeholder.unparsed("permission", permission));
sender.sendMessage(permMsg);
SoundUtil.play(sender, permissionSounds);
return false;
}
if (!(sender instanceof Player)) {
MessageUtil.send(sender, MainConfig.getMessage("player_only_command"));
return false;
}
Player player = (Player) sender;
HomeManager homeManager = UHomes.getInstance().getHomeManager();
UUID uuid = player.getUniqueId();
String name = null;
if (args.length > 0) {
name = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
if (name.length() > MainConfig.getHomeNameCharLimit()) {
MessageUtil.send(sender, MainConfig.getMessage("home_name_limit"),
Formatter.number("amount", MainConfig.getHomeNameCharLimit()),
Formatter.number("home_name_char_size", name.length()),
Placeholder.unparsed("home_name", name));
SoundUtil.play(sender, MainConfig.getSound("home_name_limit"));
return false;
}
if (!name.matches(MainConfig.getHomeNameValidChars())) {
MessageUtil.send(sender, MainConfig.getMessage("home_name_not_valid"),
Placeholder.unparsed("home_name", name),
Placeholder.unparsed("invalid_characters", String.join(" ", name.split(MainConfig.getHomeNameValidChars()))));
SoundUtil.play(sender, MainConfig.getSound("home_name_not_valid"));
return false;
}
}
String finalName = name;
CompletableFuture<List<Home>> homesFuture = homeManager.getHomes(uuid);
homesFuture.thenAccept(homes -> {
if (homes.stream().anyMatch(home -> home.getName() != null && home.getName().equalsIgnoreCase(finalName))) {
MessageUtil.send(sender, MainConfig.getMessage("home_name_already_in_use"), Placeholder.unparsed("home_name", finalName));
SoundUtil.play(sender, MainConfig.getSound("home_name_already_in_use"));
return false;
}

Home home = new Home(uuid, player.getLocation().clone());
if (finalName != null) home.setName(finalName);
homeManager.addHome(uuid, home);
});
return true;
}

}
6 changes: 6 additions & 0 deletions src/main/java/me/usainsrht/uhomes/config/MainConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class MainConfig {
private static String homeLimitPermission;
private static boolean sumHomeLimits;
private static int homeNameCharLimit;
private static String homeNameValidChars;
private static boolean askForNameBeforeSave;

private static String homesGuiTitle;
Expand Down Expand Up @@ -59,6 +60,7 @@ public static void create(ConfigurationSection config) {
homeLimitPermission = config.getString("home_limit_permission");
sumHomeLimits = config.getBoolean("sum_limit_permissions");
homeNameCharLimit = config.getInt("home_name_character_limit");
homeNameValidChars = config.getString("home_name_valid_characters");
askForNameBeforeSave = config.getBoolean("ask_for_name_before_save");

homesGuiTitle = config.getString("gui.title");
Expand Down Expand Up @@ -125,6 +127,10 @@ public static int getHomeNameCharLimit() {
return homeNameCharLimit;
}

public static String getHomeNameValidChars() {
return homeNameValidChars;
}

public static ConfigurationSection getDefaultHomeItem() {
return defaultHomeItem;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/me/usainsrht/uhomes/gui/HomesGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public static void open(Player player, List<Home> homes, int size) {
inventory.setItem(13, ItemUtil.getItemFromYaml(MainConfig.getNoHomeItem()));
}

inventory.setItem(size-5, ItemUtil.getItemFromYaml(MainConfig.getSetHomeItem()));

int i = 0;
for (Home home : homes) {
Location location = home.getLocation();
Expand Down
23 changes: 22 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ prefix: "<yellow>µHomes <dark_gray>»"

messages:
reload: "<green>Config reloaded!"
player_only_command: "<red>This command can only be used by players!"
home_limit:
- "<red>You have reached your home limit <bold><home_limit></bold>."
- "<red>You can delete or relocate one of your homes."
home_name_limit:
- "<red>Your home name can only be up to <amount> characters."
- "<#F78383><italic><home_name> is <home_name_char_size> characters."
home_name_not_valid:
- "<red>Invalid characters found in <#F78383><italic><home_name></italic><red>."
- "<#F78383><italic><invalid_characters>"
home_name_already_in_use: "<red>You already have a home named <#F78383><italic><home_name></italic><red>."

sounds:
reload: entity.villager.yes
home_limit: entity.villager.no
home_name_limit: entity.villager.no
home_name_not_valid: entity.villager.no
home_name_already_in_use: entity.villager.no

# permission to calculate home limit of a player
# for example if player has a "home.limit.5" permission he/she
Expand All @@ -19,6 +34,8 @@ sum_limit_permissions: true

home_name_character_limit: 32

home_name_valid_characters: "/^[\w\-\s]+$/"

# true: opens an input gui to player asking for name of the home
# false: saves the home without a name (can be changed later)
ask_for_name_before_save: false
Expand Down Expand Up @@ -66,7 +83,11 @@ gui:
- ""
sethome:
material: CARTOGRAPHY_TABLE
name: ""
name: "<yellow><homesize><bold>/</bold><maxhomesize>"
lore:
- ""
- " <yellow>Click to create new home! "
- ""

world_names:
world: "<green>World"
Expand Down

0 comments on commit e55c83a

Please sign in to comment.