Skip to content

Commit

Permalink
work on home naming
Browse files Browse the repository at this point in the history
  • Loading branch information
UsainSrht committed Dec 29, 2023
1 parent 26f3549 commit 4a00e86
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 32 deletions.
20 changes: 16 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,20 @@
<pattern>de.tr7zw.changeme.nbtapi</pattern>
<shadedPattern>me.usainsrht.nbtapi</shadedPattern>
</relocation>
<relocation>
<pattern>net.wesjd.anvilgui</pattern>
<shadedPattern>me.usainsrht.anvilgui</shadedPattern> <!-- Replace [YOUR_PLUGIN_PACKAGE] with your namespace -->
</relocation>
</relocations>
<filters>
<filter>
<artifact>*:*</artifact>
<excludeDefaults>false</excludeDefaults>
<includes>
<include>net/wesjd/anvilgui/**</include>
</includes>
</filter>
</filters>
</configuration>
<executions>
<execution>
Expand Down Expand Up @@ -112,10 +125,9 @@
<version>2.2</version>
</dependency>
<dependency>
<groupId>net.luckperms</groupId>
<artifactId>api</artifactId>
<version>5.4</version>
<scope>provided</scope>
<groupId>net.wesjd</groupId>
<artifactId>anvilgui</artifactId>
<version>1.9.2-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
8 changes: 6 additions & 2 deletions src/main/java/me/usainsrht/uhomes/HomeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import de.tr7zw.changeme.nbtapi.NBTList;
import de.tr7zw.changeme.nbtapi.NBTListCompound;
import me.usainsrht.uhomes.config.MainConfig;
import me.usainsrht.uhomes.util.MessageUtil;
import me.usainsrht.uhomes.util.NBTUtil;
import me.usainsrht.uhomes.util.SoundUtil;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -178,11 +181,12 @@ public CompletableFuture<Boolean> canRegisterHome(UUID uuid) {
}

public void teleport(Entity entity, Home home) {
home.setLastTeleport(System.currentTimeMillis());
entity.teleport(home.getLocation());
MessageUtil.send(entity, MainConfig.getMessage("teleport"), Placeholder.unparsed("home_name", home.getName() == null ? "" : home.getName()));
SoundUtil.play(entity, MainConfig.getSound("teleport"));
}



public UHomes getPlugin() {
return plugin;
}
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/me/usainsrht/uhomes/IntArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.usainsrht.uhomes;

public class IntArray {

private int min;
private int max;

public IntArray(String string) {
if (string.contains("-")) {
String[] strings = string.split("-");
this.min = Integer.parseInt(strings[0]);
this.max = Integer.parseInt(strings[1]);
} else {
this.min = Integer.parseInt(string);
this.max = Integer.parseInt(string);
}
}

public IntArray(int min, int max) {
this.min = min;
this.max = max;
}

public boolean isInBetween(int i) {
return i >= min && i <= max;
}

public int getMax() {
return max;
}

public int getMin() {
return min;
}

public int next() {
java.util.Random random = new java.util.Random();
return random.nextInt(getMax() + 1 - getMin()) + getMin();
}

}
49 changes: 32 additions & 17 deletions src/main/java/me/usainsrht/uhomes/command/SetHomeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mojang.brigadier.tree.LiteralCommandNode;
import me.usainsrht.uhomes.Home;
import me.usainsrht.uhomes.HomeManager;
import me.usainsrht.uhomes.Metrics;
import me.usainsrht.uhomes.UHomes;
import me.usainsrht.uhomes.config.MainConfig;
import me.usainsrht.uhomes.util.MessageUtil;
Expand All @@ -15,10 +16,13 @@
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 net.wesjd.anvilgui.AnvilGUI;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -58,14 +62,14 @@ public boolean execute(CommandSender sender, String command, String[] args) {
return false;
}
Player player = (Player) sender;
HomeManager homeManager = UHomes.getInstance().getHomeManager();
UUID uuid = player.getUniqueId();
String name = null;
Location location = player.getLocation().clone();
if (args.length > 0) {
name = String.join(" ", args);
if (name.length() > MainConfig.getHomeNameCharLimit()) {
String name = null;
name = String.join(" ", args).trim();
if (!MainConfig.getHomeNameCharLimit().isInBetween(name.length())) {
MessageUtil.send(sender, MainConfig.getMessage("home_name_limit"),
Formatter.number("amount", MainConfig.getHomeNameCharLimit()),
Formatter.number("min", MainConfig.getHomeNameCharLimit().getMin()),
Formatter.number("max", MainConfig.getHomeNameCharLimit().getMax()),
Formatter.number("home_name_char_size", name.length()),
Placeholder.unparsed("home_name", name));
SoundUtil.play(sender, MainConfig.getSound("home_name_limit"));
Expand All @@ -78,29 +82,40 @@ public boolean execute(CommandSender sender, String command, String[] args) {
SoundUtil.play(sender, MainConfig.getSound("home_name_not_valid"));
return false;
}
registerHome(player, location, name);
} else {
new AnvilGUI.Builder()
.plugin(UHomes.getInstance())
.
.open(player);
}
String finalName = name;

return true;
}

public static void registerHome(Player player, Location location, @Nullable String name) {
UUID uuid = player.getUniqueId();
HomeManager homeManager = UHomes.getInstance().getHomeManager();
CompletableFuture<List<Home>> homesFuture = homeManager.getHomes(uuid);
homesFuture.thenAccept(homes -> {
int homeLimit = homeManager.getHomeLimit(uuid);
if (homes.size() >= homeLimit) {
MessageUtil.send(sender, MainConfig.getMessage("home_limit"), Formatter.number("home_limit", homeLimit));
SoundUtil.play(sender, MainConfig.getSound("home_limit"));
MessageUtil.send(player, MainConfig.getMessage("home_limit"), Formatter.number("home_limit", homeLimit));
SoundUtil.play(player, MainConfig.getSound("home_limit"));
return;
}

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"));
if (homes.stream().anyMatch(home -> home.getName() != null && home.getName().equalsIgnoreCase(name))) {
MessageUtil.send(player, MainConfig.getMessage("home_name_already_in_use"), Placeholder.unparsed("home_name", name));
SoundUtil.play(player, MainConfig.getSound("home_name_already_in_use"));
return;
}
Home home = new Home(uuid, player.getLocation().clone());
if (finalName != null) home.setName(finalName);
Home home = new Home(uuid, location);
if (name != null) home.setName(name);
homeManager.addHome(uuid, home);
MessageUtil.send(sender, MainConfig.getMessage("sethome"), Placeholder.unparsed("home_name", finalName == null ? "" : finalName));
SoundUtil.play(sender, MainConfig.getSound("sethome"));
MessageUtil.send(player, MainConfig.getMessage("sethome"), Placeholder.unparsed("home_name", name == null ? "" : name));
SoundUtil.play(player, MainConfig.getSound("sethome"));
});
return true;
}

}
18 changes: 15 additions & 3 deletions src/main/java/me/usainsrht/uhomes/config/MainConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.usainsrht.uhomes.config;

import me.usainsrht.uhomes.IntArray;
import me.usainsrht.uhomes.UHomes;
import me.usainsrht.uhomes.command.YamlCommand;
import me.usainsrht.uhomes.util.ItemUtil;
Expand All @@ -20,7 +21,7 @@ public class MainConfig {

private static String homeLimitPermission;
private static boolean sumHomeLimits;
private static int homeNameCharLimit;
private static IntArray homeNameCharLimit;
private static String homeNameValidChars;
private static boolean askForNameBeforeSave;
private static String tpBetweenWorldsPerm;
Expand All @@ -33,6 +34,10 @@ public class MainConfig {
private static ConfigurationSection noHomeItem;
private static ConfigurationSection setHomeItem;

private static String setHomeGuiTitle;
private static String setHomeGuiText;
private static HashMap<Integer, ConfigurationSection> setHomeGuiSlots;

private static HashMap<String, String> worldNames;

private static YamlCommand homeCommand;
Expand Down Expand Up @@ -61,7 +66,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");
homeNameCharLimit = new IntArray(config.getString("home_name_character_limit"));
homeNameValidChars = config.getString("home_name_valid_characters");
askForNameBeforeSave = config.getBoolean("ask_for_name_before_save");
tpBetweenWorldsPerm = config.getString("teleport_between_worlds_permission");
Expand All @@ -75,6 +80,13 @@ public static void create(ConfigurationSection config) {
noHomeItem = config.getConfigurationSection("gui.no_home");
setHomeItem = config.getConfigurationSection("gui.sethome");

setHomeGuiTitle = config.getString("anvil_gui.title");
setHomeGuiText = config.getString("anvil_gui.text");
setHomeGuiSlots = new HashMap<>();
config.getConfigurationSection("anvil_gui.slots").getKeys(false).forEach(keyString -> {
setHomeGuiSlots.put(Integer.parseInt(keyString), config.getConfigurationSection("anvil_gui.slots."+keyString));
});

worldNames = new HashMap<>();
config.getConfigurationSection("world_names").getKeys(false).forEach(key -> {
worldNames.put(key, config.getString("world_names."+key));
Expand Down Expand Up @@ -135,7 +147,7 @@ public static boolean isSumHomeLimits() {
return sumHomeLimits;
}

public static int getHomeNameCharLimit() {
public static IntArray getHomeNameCharLimit() {
return homeNameCharLimit;
}

Expand Down
26 changes: 20 additions & 6 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ messages:
- "<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."
- "<red>Your home name length should be between <#F78383><italic><min></italic> <red>and <#F78383><italic><max></italic><red>."
- "<#F78383><italic><home_name></italic> <red>is <#F78383><italic><home_name_char_size></italic> <red>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>."
sethome: "<gray>Home <#F78383><italic><home_name></italic> <gray>successfully saved."
no_home_with_that_name: "<red>You don't have a home named <#F78383><italic><home_name></italic><red>."
teleport: "<gray>You have teleported to <#F78383><italic><home_name></italic><gray>."

sounds:
reload: entity.villager.yes
Expand All @@ -24,6 +25,7 @@ sounds:
home_name_already_in_use: entity.villager.no
sethome: block.respawn_anchor.set_spawn
no_home_with_that_name: entity.villager.no
teleport: entity.enderman.teleport

# permission to calculate home limit of a player
# for example if player has a "home.limit.5" permission he/she
Expand All @@ -36,7 +38,8 @@ home_limit_permission: "home.limit."
# false: use highest home limit permission
sum_limit_permissions: true

home_name_character_limit: 32
# min-max
home_name_character_limit: 1-32

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

Expand All @@ -50,6 +53,7 @@ teleport_between_worlds_permission: "uhomes.tp_between_worlds"

# the name that will be shown when a home saved without a name
# ex: unnamed_home_1, unnamed_home_2, ...
# if ask_for_name_before_save is true, this setting won't be used.
unnamed_home: "unnamed_home_<index>"

gui:
Expand Down Expand Up @@ -91,7 +95,7 @@ gui:
name: "<red>You have no home!"
lore:
- ""
- " &eClick to sethome! "
- " <yellow>Click to sethome! "
- ""
sethome:
material: CARTOGRAPHY_TABLE
Expand All @@ -101,10 +105,20 @@ gui:
- " <yellow>Click to create new home! "
- ""

anvil_gui:
sethome:
title: "<red>↓ Home name ↓"
text: " "
slots:
0:
material: WHITE_BED
2:
material: LIME_BED

world_names:
world: "<green>World"
world: "<green>Overworld"
world_nether: "<red>Nether"
world_the_end: "<yellow>End"
world_the_end: "<yellow>The End"

commands:
home:
Expand Down

0 comments on commit 4a00e86

Please sign in to comment.