treasure chests
Some checks failed
Build / build (push) Failing after 3s

This commit is contained in:
unurled 2024-04-09 22:00:16 +02:00
parent acc7f076e0
commit c3c82b4478
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
7 changed files with 285 additions and 96 deletions

View file

@ -1,5 +1,6 @@
package me.unurled.sacredrealms.sr.commands.admin;
import static me.unurled.sacredrealms.sr.utils.Component.NOT_PLAYER;
import static me.unurled.sacredrealms.sr.utils.Component.textComp;
import java.util.List;
@ -7,6 +8,7 @@ import java.util.Set;
import me.unurled.sacredrealms.sr.components.treasure.Treasure;
import me.unurled.sacredrealms.sr.components.treasure.TreasureManager;
import me.unurled.sacredrealms.sr.gui.treasure.TreasureGUI;
import me.unurled.sacredrealms.sr.managers.Manager;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -22,6 +24,97 @@ public class TreasureCommand implements TabExecutor {
public static final Set<Material> TRANSPARENT =
Set.of(Material.AIR, Material.CAVE_AIR, Material.VOID_AIR, Material.WATER, Material.LAVA);
public static final String SR_TREASURE_PERMISSION = "sr.treasure.";
public static final String CREATE = "create";
public static final String MODIFY = "modify";
public static final String LIST = "list";
public static final String DELETE = "delete";
private static void delete(@NotNull CommandSender sender, TreasureManager instance) {
Block block;
if (!(sender instanceof Player p)) {
sender.sendMessage(NOT_PLAYER);
return;
}
block = p.getTargetBlock(TRANSPARENT, 3);
if (block.getBlockData().getMaterial().equals(Material.CHEST)) {
// find the treasure chest
Treasure treasure = instance.findByLocation(block.getLocation());
if (treasure == null) {
sender.sendMessage("No treasure chest found at this location.");
return;
}
// remove the treasure chest
instance.removeTreasure(treasure);
}
}
private static void list(@NotNull CommandSender sender, @NotNull TreasureManager instance) {
sender.sendMessage("Treasures:");
for (Treasure treasure : instance.getTreasures()) {
String s =
String.format(
"<click:run_command:tp %s %s %s>%s</click>x: %s, y: %s, z: %s",
treasure.getLocation().getBlockX(),
treasure.getLocation().getBlockY(),
treasure.getLocation().getBlockZ(),
treasure.getLocation().toString(),
treasure.getLocation().getBlockX(),
treasure.getLocation().getBlockY(),
treasure.getLocation().getBlockZ());
Component text = textComp(s);
sender.sendMessage(text);
}
}
private static void modify(@NotNull CommandSender sender, TreasureManager instance) {
Block block;
if (!(sender instanceof Player p)) {
sender.sendMessage(NOT_PLAYER);
return;
}
block = p.getTargetBlock(TRANSPARENT, 3);
if (block.getBlockData().getMaterial().equals(Material.CHEST)) {
// find the treasure chest
Treasure treasure = instance.findByLocation(block.getLocation());
if (treasure == null) {
sender.sendMessage("No treasure chest found at this location.");
return;
}
// open the GUI
Window window =
Window.single()
.setViewer(p)
.setTitle("Attributes")
.setGui(TreasureGUI.createGui(treasure))
.build();
window.open();
}
}
private static void create(
@NotNull CommandSender sender, @NotNull String @NotNull [] args, TreasureManager instance) {
Block block;
if (!(sender instanceof Player p)) {
sender.sendMessage(NOT_PLAYER);
return;
}
block = p.getTargetBlock(TRANSPARENT, 3);
if (block.getBlockData().getMaterial().equals(Material.CHEST)) {
// create a new treasure chest
Treasure treasure;
if (args.length == 2) {
if (!args[1].startsWith(SR_TREASURE_PERMISSION)) {
p.sendMessage(textComp("<red>Invalid permission node. It must start with sr.treasure."));
return;
}
treasure = new Treasure(instance.getNextId(), block.getLocation(), args[1]);
} else {
treasure = new Treasure(instance.getNextId(), block.getLocation());
}
instance.addTreasure(treasure);
}
}
/**
* Executes the given command, returning its success. <br>
@ -39,97 +132,19 @@ public class TreasureCommand implements TabExecutor {
@NotNull CommandSender sender,
@NotNull Command command,
@NotNull String label,
@NotNull String[] args) {
@NotNull String @NotNull [] args) {
// create, modify, list, get, delete
if (args.length == 0) {
sender.sendMessage("Usage: /treasure <create|modify|list|delete>");
return true;
}
Block block;
final TreasureManager instance = Manager.getInstance(TreasureManager.class);
switch (args[0]) {
case "create" -> {
if (!(sender instanceof Player p)) {
sender.sendMessage("You must be a player to use this command.");
return true;
}
block = p.getTargetBlock(TRANSPARENT, 3);
if (block.getBlockData().getMaterial().equals(Material.CHEST)) {
// create a new treasure chest
Treasure treasure;
if (args.length == 2) {
treasure = new Treasure(block.getLocation(), args[1]);
} else {
treasure = new Treasure(block.getLocation());
}
TreasureManager.getInstance(TreasureManager.class).addTreasure(treasure);
}
}
case "modify" -> {
if (!(sender instanceof Player p)) {
sender.sendMessage("You must be a player to use this command.");
return true;
}
block = p.getTargetBlock(TRANSPARENT, 3);
if (block.getBlockData().getMaterial().equals(Material.CHEST)) {
// find the treasure chest
Treasure treasure =
TreasureManager.getInstance(TreasureManager.class)
.findByLocation(block.getLocation());
if (treasure == null) {
sender.sendMessage("No treasure chest found at this location.");
return true;
}
// open the GUI
Window window =
Window.single()
.setViewer(p)
.setTitle("Attributes")
.setGui(TreasureGUI.createGui(treasure))
.build();
window.open();
}
}
case "list" -> {
sender.sendMessage("Treasures:");
for (Treasure treasure :
TreasureManager.getInstance(TreasureManager.class).getTreasures()) {
String s =
String.format(
"<click:run_command:tp %s %s %s>%s</click>x: %s, y: %s, z: %s",
treasure.getLocation().getBlockX(),
treasure.getLocation().getBlockY(),
treasure.getLocation().getBlockZ(),
treasure.getLocation().toString(),
treasure.getLocation().getBlockX(),
treasure.getLocation().getBlockY(),
treasure.getLocation().getBlockZ());
Component text = textComp(s);
sender.sendMessage(text);
}
}
case "delete" -> {
if (!(sender instanceof Player p)) {
sender.sendMessage("You must be a player to use this command.");
return true;
}
block = p.getTargetBlock(TRANSPARENT, 3);
if (block.getBlockData().getMaterial().equals(Material.CHEST)) {
// find the treasure chest
Treasure treasure =
TreasureManager.getInstance(TreasureManager.class)
.findByLocation(block.getLocation());
if (treasure == null) {
sender.sendMessage("No treasure chest found at this location.");
return true;
}
// remove the treasure chest
TreasureManager.getInstance(TreasureManager.class).removeTreasure(treasure);
}
}
default -> {
sender.sendMessage("Usage: /treasure <create|modify|list|delete>");
return true;
}
case CREATE -> create(sender, args, instance);
case MODIFY -> modify(sender, instance);
case LIST -> list(sender, instance);
case DELETE -> delete(sender, instance);
default -> sender.sendMessage("Usage: /treasure <create|modify|list|delete>");
}
return true;
}
@ -151,7 +166,17 @@ public class TreasureCommand implements TabExecutor {
@NotNull CommandSender sender,
@NotNull Command command,
@NotNull String label,
@NotNull String[] args) {
@NotNull String @NotNull [] args) {
if (args.length == 0) {
return List.of(CREATE, MODIFY, LIST, DELETE);
}
if (args.length == 2 && (args[0].equals(CREATE))) {
if (args[1].startsWith(SR_TREASURE_PERMISSION)) {
return List.of();
}
return List.of(SR_TREASURE_PERMISSION);
}
return List.of();
}
}