fix: use IDs for follow and allow multiple words as name for pin

This commit is contained in:
Janik Heß
2025-03-08 11:47:31 +01:00
parent f76d3c3b01
commit d6ad531769
2 changed files with 152 additions and 69 deletions

View File

@@ -1,9 +1,13 @@
package de.hessj.environmentex;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
@@ -13,6 +17,7 @@ import org.bukkit.boss.BossBar;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
@@ -48,13 +53,15 @@ public class EnvironmentExCommands implements CommandExecutor, Listener {
Player p = (Player) sender;
p.playerListName(App.main.helper.R3SMessage(Type.ERROR, "[AFK] " + p.getName()));
}
if (label.equalsIgnoreCase("pin")) {
Player p = (Player) sender;
UUID playerId = p.getUniqueId();
playerPins.putIfAbsent(playerId, new HashMap<>());
if (args.length < 1) {
p.sendMessage(helper.R3SMessage(Type.ERROR,
"Benutzung: /pin <set | list | remove | follow | unfollow> [name]"));
"Benutzung: /pin <set | list | remove | follow | unfollow> [name | id]"));
return true;
}
@@ -67,18 +74,39 @@ public class EnvironmentExCommands implements CommandExecutor, Listener {
p.sendMessage(helper.R3SMessage(Type.ERROR, "Benutzung: /pin set <name>"));
return true;
}
String pinName = args[1];
Location playerLocation = p.getLocation();
pins.put(pinName, playerLocation);
// Save to YAML file
PinFeature.pinConfig.set(playerId + "." + pinName + ".loc",
playerLocation.getWorld().getName() + ", " + playerLocation.getBlockX() + ", "
+ playerLocation.getBlockY() + ", " + playerLocation.getBlockZ());
// **Nimmt den gesamten Text nach "/pin set" als Namen**
String pinName = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
Location playerLocation = p.getLocation();
// **Kleinste verfügbare ID suchen**
int nextId = 1;
ConfigurationSection playerSection = PinFeature.pinConfig
.getConfigurationSection(playerId.toString());
if (playerSection != null) {
Set<Integer> existingIds = playerSection.getKeys(false).stream()
.map(Integer::parseInt)
.collect(Collectors.toSet());
while (existingIds.contains(nextId)) {
nextId++;
}
}
pins.put(pinName, playerLocation);
String locationString = playerLocation.getWorld().getName() + ", " +
playerLocation.getBlockX() + ", " +
playerLocation.getBlockY() + ", " +
playerLocation.getBlockZ();
PinFeature.pinConfig.set(playerId + "." + nextId + ".name", pinName);
PinFeature.pinConfig.set(playerId + "." + nextId + ".loc", locationString);
savePins();
p.sendMessage(helper.R3SMessage(Type.SUCCESS, "Pin '" + pinName + "' wurde hinzugefügt."));
p.sendMessage(helper.R3SMessage(Type.SUCCESS,
"Pin '" + pinName + "' wurde mit der ID " + nextId + " erstellt."));
break;
case "list":
loadPinsFromConfig(playerId);
@@ -87,83 +115,102 @@ public class EnvironmentExCommands implements CommandExecutor, Listener {
return true;
}
p.sendMessage(helper.R3SMessage(Type.OK, "Deine Pins:"));
for (String name : pins.keySet()) {
Location location = pins.get(name);
String w = "Overworld: ";
if (location.getWorld().getName().endsWith("_nether")) {
w = "Nether: ";
} else if (location.getWorld().getName().endsWith("_end")) {
w = "End: ";
ConfigurationSection ps = PinFeature.pinConfig.getConfigurationSection(playerId.toString());
if (ps != null) {
List<Integer> sortedIds = ps.getKeys(false).stream()
.map(Integer::parseInt)
.sorted()
.collect(Collectors.toList());
for (int id : sortedIds) {
String name = PinFeature.pinConfig.getString(playerId + "." + id + ".name");
String locString = PinFeature.pinConfig.getString(playerId + "." + id + ".loc");
if (name == null || locString == null)
continue;
String[] locParts = locString.split(", ");
int x = Integer.parseInt(locParts[1]);
int y = Integer.parseInt(locParts[2]);
int z = Integer.parseInt(locParts[3]);
String worldLabel = "Overworld";
if (locParts[0].endsWith("_nether")) {
worldLabel = "Nether";
} else if (locParts[0].endsWith("_end")) {
worldLabel = "End";
}
p.sendMessage(" [" + id + "] " + worldLabel + ": " + name + " (" + x + ", " + y + ", "
+ z + ")");
}
p.sendMessage(
"- " + w + name + " (" + location.getBlockX() + ", " + location.getBlockY() + ", "
+ location.getBlockZ() + ")");
}
break;
case "remove":
if (args.length < 2) {
p.sendMessage(helper.R3SMessage(Type.ERROR, "Benutzung: /pin remove <name>"));
p.sendMessage(helper.R3SMessage(Type.ERROR, "Benutzung: /pin remove <id>"));
return true;
}
pinName = args[1];
if (pins.remove(pinName) != null) {
PinFeature.pinConfig.set(playerId + "." + pinName, null);
String pinId = args[1];
if (PinFeature.pinConfig.contains(playerId + "." + pinId)) {
String pinName1 = PinFeature.pinConfig.getString(playerId + "." + pinId + ".name");
PinFeature.pinConfig.set(playerId + "." + pinId, null);
savePins();
p.sendMessage(helper.R3SMessage(Type.SUCCESS, "Pin '" + pinName + "' wurde entfernt."));
p.sendMessage(
helper.R3SMessage(Type.SUCCESS, "Dein Pin '" + pinName1 + "' wurde entfernt."));
} else {
p.sendMessage(helper.R3SMessage(Type.ERROR, "Kein Pin mit diesem Namen gefunden."));
p.sendMessage(helper.R3SMessage(Type.ERROR, "Kein Pin mit dieser ID gefunden."));
}
break;
case "follow":
if (args.length < 2) {
p.sendMessage(helper.R3SMessage(Type.ERROR, "Benutzung: /pin follow <name>"));
p.sendMessage(helper.R3SMessage(Type.ERROR, "Benutzung: /pin follow <id>"));
return true;
}
pinName = args[1];
if (PinFeature.pinConfig.contains(playerId + "." + pinName)) {
try {
int pinId1 = Integer.parseInt(args[1]);
String pinName1 = PinFeature.pinConfig.getString(playerId + "." + pinId1 + ".name");
String locationString1 = PinFeature.pinConfig.getString(playerId + "." + pinId1 + ".loc");
String locationString = PinFeature.pinConfig.getString(playerId + "." + pinName + ".loc");
if (locationString != null) {
String[] locParts = locationString.split(", ");
String worldName = locParts[0];
int x = Integer.parseInt(locParts[1]);
int y = Integer.parseInt(locParts[2]);
int z = Integer.parseInt(locParts[3]);
Location pinLocation = new Location(Bukkit.getWorld(worldName), x, y, z);
String w = "Overworld: ";
if (pinLocation.getWorld().getName().endsWith("_nether")) {
w = "Nether: ";
} else if (pinLocation.getWorld().getName().endsWith("_end")) {
w = "End: ";
}
BossBar bossBar = Bukkit.createBossBar(w + x + " " + y + " " + z, BarColor.WHITE,
BarStyle.SOLID);
bossBar.setProgress(0.0);
if (!bossBars.containsKey(p.getUniqueId())) {
bossBar.addPlayer(p);
bossBars.put(p.getUniqueId(), bossBar);
} else {
bossBars.get(p.getUniqueId()).removeAll();
bossBars.remove(p.getUniqueId());
bossBar.addPlayer(p);
bossBars.put(p.getUniqueId(), bossBar);
}
p.sendMessage(
helper.R3SMessage(Type.SUCCESS, "Du folgst nun dem Pin '" + pinName + "'."));
if (pinName1 == null || locationString1 == null) {
p.sendMessage(helper.R3SMessage(Type.ERROR, "Kein Pin mit dieser ID gefunden."));
return true;
}
} else {
p.sendMessage(helper.R3SMessage(Type.ERROR, "Kein Pin mit diesem Namen gefunden."));
}
// Standort aus der YML auslesen
String[] locParts = locationString1.split(", ");
String worldName = locParts[0];
int x = Integer.parseInt(locParts[1]);
int y = Integer.parseInt(locParts[2]);
int z = Integer.parseInt(locParts[3]);
String worldLabel = "Overworld";
if (worldName.endsWith("_nether"))
worldLabel = "Nether";
if (worldName.endsWith("_end"))
worldLabel = "End";
// BossBar erstellen/aktualisieren
BossBar bossBar = Bukkit.createBossBar(worldLabel + ": " + x + ", " + y + ", " + z,
BarColor.WHITE, BarStyle.SOLID);
bossBar.setProgress(0.0);
if (bossBars.containsKey(p.getUniqueId())) {
bossBars.get(p.getUniqueId()).removeAll();
bossBars.remove(p.getUniqueId());
}
bossBar.addPlayer(p);
bossBars.put(p.getUniqueId(), bossBar);
p.sendMessage(helper.R3SMessage(Type.SUCCESS, "Du folgst nun dem Pin '" + pinName1 + "'"));
} catch (NumberFormatException e) {
p.sendMessage(helper.R3SMessage(Type.ERROR, "Kein Pin mit dieser ID gefunden."));
}
break;
case "unfollow":
@@ -176,9 +223,8 @@ public class EnvironmentExCommands implements CommandExecutor, Listener {
default:
p.sendMessage(helper.R3SMessage(Type.ERROR,
"Benutzung: /pin <set | list | remove | follow | unfollow> [name]"));
"Benutzung: /pin <set | list | remove | follow | unfollow> [name | id]"));
}
}
} else {

View File

@@ -1,22 +1,59 @@
package de.hessj.environmentex;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
public class PinFeature implements TabCompleter {
//TODO: add to webpage
public static File pinYml = new File(App.main.getDataFolder() + "/pins.yml");
public static FileConfiguration pinConfig = YamlConfiguration.loadConfiguration(pinYml);
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
return List.of(); // Return an empty list to disable autocompletion
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)) {
return Collections.emptyList();
}
Player player = (Player) sender;
UUID playerId = player.getUniqueId();
if (args.length == 1) {
return Arrays.asList("set", "list", "remove", "follow", "unfollow");
}
if (args.length == 2) {
String subCommand = args[0].toLowerCase();
if (subCommand.equals("set")) {
return Collections.emptyList(); // Spieler gibt selbst einen Namen ein
}
if (subCommand.equals("list") || subCommand.equals("unfollow")) {
return Collections.emptyList(); // Keine Argumente notwendig
}
if (subCommand.equals("remove") || subCommand.equals("follow")) {
ConfigurationSection playerSection = PinFeature.pinConfig.getConfigurationSection(playerId.toString());
if (playerSection != null) {
return playerSection.getKeys(false).stream()
.sorted(Comparator.comparingInt(Integer::parseInt))
.collect(Collectors.toList());
}
}
}
return Collections.emptyList();
}
//For more see EnvironmentExCommands.java