Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;

public enum HologramType {
TEXT(Arrays.asList("background", "textshadow", "textalignment", "seethrough", "setline", "removeline", "addline", "insertbefore", "insertafter", "updatetextinterval")),
TEXT(Arrays.asList("background", "textshadow", "textalignment", "seethrough", "setline", "removeline", "addline", "insertbefore", "insertafter", "swaplines", "moveup", "movedown", "updatetextinterval")),
ITEM(List.of("item")),
BLOCK(List.of("block"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,181 +114,204 @@
};
}

@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String label, @NotNull String[] args) throws IllegalArgumentException {
if (args.length == 0) {
return Collections.emptyList();
}

// /holo {tab:action}
if (args.length == 1) {
return Stream.of("help", "list", "teleport", "create", "remove", "edit", "copy", "info", "nearby").filter(input -> input.startsWith(args[0].toLowerCase(Locale.ROOT))).toList();
}

// /holo create {tab:type}
if (args.length == 2 && args[0].equalsIgnoreCase("create")) {
return Arrays.asList("text", "item", "block");
}

// /holo [action] {tab:hologram}
if (args.length == 2) {
final var action = args[0].toLowerCase(Locale.ROOT);

if (!Set.of("teleport", "remove", "edit", "copy", "info").contains(action)) {
return Collections.emptyList();
}

return this.plugin.getHologramsManager().getPersistentHolograms().stream().map(hologram -> hologram.getData().getName()).filter(input -> input.toLowerCase().startsWith(args[1].toLowerCase(Locale.ROOT))).toList();
}

final var hologram = this.plugin.getHologramsManager().getHologram(args[1]).orElse(null);
if (hologram == null) {
return Collections.emptyList();
}

HologramType type = hologram.getData().getType();

// /holo edit [hologram] {tab:option}
if (args.length == 3) {
if (!args[0].equalsIgnoreCase("edit")) {
return Collections.emptyList();
}

final var usingNpcs = PluginUtils.isFancyNpcsEnabled();

List<String> suggestions = new ArrayList<>(Arrays.asList("position", "moveHere", "center", "moveTo", "rotate", "rotatepitch", "billboard", "scale", "translate", "visibilityDistance", "visibility", "shadowRadius", "shadowStrength", "brightness", usingNpcs ? "linkWithNpc" : "", usingNpcs ? "unlinkWithNpc" : ""));
suggestions.addAll(type.getCommands());

return suggestions.stream().filter(input -> input.toLowerCase().startsWith(args[2].toLowerCase(Locale.ROOT))).toList();
}

if (!args[0].equalsIgnoreCase("edit")) {
return Collections.emptyList();
}

// /holo edit [hologram] [option] {tab:contextual}
if (args.length == 4) {
final var suggestions = switch (args[2].toLowerCase(Locale.ROOT)) {
case "billboard" -> {
final var values = new ArrayList<>(List.of(Display.Billboard.values()));

if (hologram.getData() instanceof DisplayHologramData displayData) {
values.remove(displayData.getBillboard());
}

yield values.stream().map(Enum::name);
}
case "background" -> {
TextHologramData textData = (TextHologramData) hologram.getData();
final var colors = new ArrayList<>(NamedTextColor.NAMES.keys());

colors.add("reset");
colors.add("default");
colors.add("transparent");

final var current = textData.getBackground();

if (current == null) {
colors.remove("reset");
colors.remove("default");
} else if (current == Hologram.TRANSPARENT) {
colors.remove("transparent");
} else {
NamedTextColor named = current.getAlpha() == 255 ? NamedTextColor.namedColor(current.asRGB()) : null;
colors.add(named != null ? named.toString() : '#' + Integer.toHexString(current.asARGB()));
}

yield colors.stream();
}
case "textshadow" -> {
TextHologramData textData = (TextHologramData) hologram.getData();
yield Stream.of(!textData.hasTextShadow()).map(Object::toString);
}
case "brightness" -> Stream.of("block", "sky");
case "textalignment" -> Arrays.stream(TextDisplay.TextAlignment.values()).map(Enum::name);
case "setline", "removeline" -> {
case "setline", "removeline", "swaplines" -> {
TextHologramData textData = (TextHologramData) hologram.getData();
yield IntStream.range(1, textData.getText().size() + 1).mapToObj(Integer::toString);
}
case "moveup" -> {
TextHologramData textData = (TextHologramData) hologram.getData();
yield IntStream.range(2, textData.getText().size() + 1).mapToObj(Integer::toString);
}
case "movedown" -> {
TextHologramData textData = (TextHologramData) hologram.getData();
yield IntStream.range(1, textData.getText().size()).mapToObj(Integer::toString);
}
case "linkwithnpc" -> {
if (!PluginUtils.isFancyNpcsEnabled()) {
yield Stream.<String>empty();
}

yield FancyNpcsPlugin.get().getNpcManager().getAllNpcs().stream().map(npc -> npc.getData().getName());
}
case "block" -> Arrays.stream(Material.values()).filter(Material::isBlock).map(Enum::name);
case "seethrough" -> Stream.of("true", "false");
case "visibility" -> new VisibilityCMD().tabcompletion(sender, hologram, args).stream();

default -> null;
};

if (suggestions != null) {
return suggestions.filter(input -> input.toLowerCase().startsWith(args[3].toLowerCase(Locale.ROOT))).toList();
}
}

if(args.length == 5) {
final var suggestions = switch (args[2].toLowerCase(Locale.ROOT)) {
case "swaplines" -> {
TextHologramData textData = (TextHologramData) hologram.getData();
yield IntStream.range(1, textData.getText().size() + 1).mapToObj(Integer::toString);
}

default -> null;
};

if (suggestions != null) {
return suggestions.filter(input -> input.toLowerCase().startsWith(args[3].toLowerCase(Locale.ROOT))).toList();
}
}

// /holo edit [hologram] setline [number] {tab:line_text}
if (args[2].equalsIgnoreCase("setline")) {
TextHologramData textData = (TextHologramData) hologram.getData();

final var index = Ints.tryParse(args[3]);
if (index == null || index < 1 || index > textData.getText().size()) {
return Collections.emptyList();
}

return List.of(textData.getText().get(index - 1));
}

// /holo edit [hologram] moveto {tab:x} {tab:y} {tab:z}
if (args[2].equalsIgnoreCase("moveto")) {
if (!(sender instanceof Player player)) {
return Collections.emptyList();
}

final var suggestions = new ArrayList<String>();
suggestions.add("~");
suggestions.add("~~");

if (args.length == 7) {
suggestions.add(String.valueOf(player.getLocation().getYaw()));
}

if (args.length == 8) {
suggestions.add(String.valueOf(player.getLocation().getPitch()));
}

final var target = player.getTargetBlockExact(10);
if (target != null) {
final var coordinate = switch (args.length) {
case 4 -> target.getX();
case 5 -> target.getY();
case 6 -> target.getZ();
default -> null;
};

suggestions.add(String.valueOf(coordinate));
}

return suggestions;
}

if(args[2].equalsIgnoreCase("brightness")) {
if(args.length == 4) {
return List.of("block", "sky");
}

if(args.length > 5) {
return Collections.emptyList();
}

return List.of("0", "5", "10", "15");
}

return Collections.emptyList();
}

Check notice on line 314 in plugins/fancyholograms-v2/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java

View check run for this annotation

codefactor.io / CodeFactor

plugins/fancyholograms-v2/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java#L117-L314

Complex Method
private boolean edit(@NotNull final CommandSender player, @NotNull final Hologram hologram, @NotNull final String[] args) {
final var action = args[2].toLowerCase();

Expand Down Expand Up @@ -336,6 +359,9 @@
case "removeline" -> new RemoveLineCMD().run(player, hologram, args);
case "insertbefore" -> new InsertBeforeCMD().run(player, hologram, args);
case "insertafter" -> new InsertAfterCMD().run(player, hologram, args);
case "swaplines" -> new SwapLinesCMD().run(player, hologram, args);
case "moveup" -> new MoveUpCMD().run(player, hologram, args);
case "movedown" -> new MoveDownCMD().run(player, hologram, args);
case "textshadow" -> new TextShadowCMD().run(player, hologram, args);
case "textalignment" -> new TextAlignmentCMD().run(player, hologram, args);
case "seethrough" -> new SeeThroughCMD().run(player, hologram, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package de.oliver.fancyholograms.commands.hologram;

import com.google.common.primitives.Ints;
import de.oliver.fancyholograms.FancyHolograms;
import de.oliver.fancyholograms.api.data.TextHologramData;
import de.oliver.fancyholograms.api.events.HologramUpdateEvent;
import de.oliver.fancyholograms.api.hologram.Hologram;
import de.oliver.fancyholograms.commands.HologramCMD;
import de.oliver.fancyholograms.commands.Subcommand;
import de.oliver.fancylib.MessageHelper;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MoveDownCMD implements Subcommand {

@Override
public List<String> tabcompletion(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) {
return null;
}

@Override
public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) {
if (!player.hasPermission("fancyholograms.hologram.edit.line.movedown")) {
MessageHelper.error(player, "You don't have the required permission to move lines down");
return false;
}

if (!(hologram.getData() instanceof TextHologramData textData)) {
MessageHelper.error(player, "This command can only be used on text holograms");
return false;
}

if (args.length < 4) {
MessageHelper.error(player, "Wrong usage: /hologram help");
return false;
}

final var parsedIndex = Ints.tryParse(args[3]);
final var lines = new ArrayList<>(textData.getText());

if (parsedIndex == null || parsedIndex < 0 || parsedIndex > lines.size()) {
MessageHelper.error(player, "Invalid line index");
return false;
}

final var index = parsedIndex - 1;

if(index < 0 || index + 1 > lines.size() - 1) {
MessageHelper.error(player, "Invalid line index");
return false;
}

String temp = lines.get(index);
lines.set(index, lines.get(index + 1));
lines.set(index + 1, temp);

final var copied = textData.copy(textData.getName());
copied.setText(lines);

if (!HologramCMD.callModificationEvent(hologram, player, copied, HologramUpdateEvent.HologramModification.TEXT)) {
return false;
}

textData.setText(copied.getText());

if (FancyHolograms.get().getHologramConfiguration().isSaveOnChangedEnabled()) {
FancyHolograms.get().getHologramStorage().save(hologram);
}

MessageHelper.success(player, "Moved line " + parsedIndex + " down");
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package de.oliver.fancyholograms.commands.hologram;

import com.google.common.primitives.Ints;
import de.oliver.fancyholograms.FancyHolograms;
import de.oliver.fancyholograms.api.data.TextHologramData;
import de.oliver.fancyholograms.api.events.HologramUpdateEvent;
import de.oliver.fancyholograms.api.hologram.Hologram;
import de.oliver.fancyholograms.commands.HologramCMD;
import de.oliver.fancyholograms.commands.Subcommand;
import de.oliver.fancylib.MessageHelper;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MoveUpCMD implements Subcommand {

@Override
public List<String> tabcompletion(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) {
return null;
}

@Override
public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) {
if (!player.hasPermission("fancyholograms.hologram.edit.line.moveup")) {
MessageHelper.error(player, "You don't have the required permission to move lines up");
return false;
}

if (!(hologram.getData() instanceof TextHologramData textData)) {
MessageHelper.error(player, "This command can only be used on text holograms");
return false;
}

if (args.length < 4) {
MessageHelper.error(player, "Wrong usage: /hologram help");
return false;
}

final var parsedIndex = Ints.tryParse(args[3]);

if (parsedIndex == null || parsedIndex <= 0) {
MessageHelper.error(player, "Invalid line index");
return false;
}

final var index = parsedIndex - 1;
final var lines = new ArrayList<>(textData.getText());

if (index >= lines.size() || index - 1 < 0) {
MessageHelper.error(player, "Invalid line index");
return false;
}

String temp = lines.get(index);
lines.set(index, lines.get(index - 1));
lines.set(index - 1, temp);

final var copied = textData.copy(textData.getName());
copied.setText(lines);

if (!HologramCMD.callModificationEvent(hologram, player, copied, HologramUpdateEvent.HologramModification.TEXT)) {
return false;
}

textData.setText(copied.getText());

if (FancyHolograms.get().getHologramConfiguration().isSaveOnChangedEnabled()) {
FancyHolograms.get().getHologramStorage().save(hologram);
}

MessageHelper.success(player, "Moved line " + parsedIndex + " up");
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package de.oliver.fancyholograms.commands.hologram;

import com.google.common.primitives.Ints;
import de.oliver.fancyholograms.FancyHolograms;
import de.oliver.fancyholograms.api.data.TextHologramData;
import de.oliver.fancyholograms.api.events.HologramUpdateEvent;
import de.oliver.fancyholograms.api.hologram.Hologram;
import de.oliver.fancyholograms.commands.HologramCMD;
import de.oliver.fancyholograms.commands.Subcommand;
import de.oliver.fancylib.MessageHelper;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class SwapLinesCMD implements Subcommand {

@Override
public List<String> tabcompletion(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) {
return null;
}

@Override
public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) {
if (!(player.hasPermission("fancyholograms.hologram.edit.line.swap"))) {
MessageHelper.error(player, "You don't have the required permission to swap lines on this hologram");
return false;
}

if (!(hologram.getData() instanceof TextHologramData textData)) {
MessageHelper.error(player, "This command can only be used on text holograms");
return false;
}

if (args.length < 5) {
MessageHelper.error(player, "Wrong usage: /hologram help");
return false;
}

final var parsedIndex1 = Ints.tryParse(args[3]);
final var parsedIndex2 = Ints.tryParse(args[4]);

if (parsedIndex1 == null || parsedIndex2 == null) {
MessageHelper.error(player, "Could not parse one or both line numbers");
return false;
}

final var lines = new ArrayList<>(textData.getText());

if (parsedIndex1 < 0 || parsedIndex1 > lines.size() || parsedIndex2 < 0 || parsedIndex2 > lines.size()) {
MessageHelper.error(player, "Invalid line index");
return false;
}

final var index1 = parsedIndex1 - 1;
final var index2 = parsedIndex2 - 1;

if(index1 == index2) {
MessageHelper.error(player, "Cannot swap a line with itself");
return false;
}

if(index1 < 0 || index2 < 0) {
MessageHelper.error(player, "Invalid line index");
return false;
}

String tempLine = lines.get(index1);
lines.set(index1, lines.get(index2));
lines.set(index2, tempLine);

final var copied = textData.copy(textData.getName());
copied.setText(lines);

if (!HologramCMD.callModificationEvent(hologram, player, copied, HologramUpdateEvent.HologramModification.TEXT)) {
return false;
}

textData.setText(copied.getText());

if (FancyHolograms.get().getHologramConfiguration().isSaveOnChangedEnabled()) {
FancyHolograms.get().getHologramStorage().save(hologram);
}

MessageHelper.success(player, "Swapped lines " + parsedIndex1 + " and " + parsedIndex2);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public enum Constants {
<%primary_color%>- /hologram edit <hologram> removeLine <dark_gray>- <white>Removes a line at the bottom
<%primary_color%>- /hologram edit <hologram> insertBefore <line number> <text ...> <dark_gray>- <white>Inserts a line before another
<%primary_color%>- /hologram edit <hologram> insertAfter <line number> <text ...> <dark_gray>- <white>Inserts a line after another
<%primary_color%>- /hologram edit <hologram> swapLines <line number> <line number> <dark_gray>- <white>Swap line positions
<%primary_color%>- /hologram edit <hologram> moveUp <line number> <line number> <dark_gray>- <white>Move a line up
<%primary_color%>- /hologram edit <hologram> moveDown <line number> <line number> <dark_gray>- <white>Move a line down
<%primary_color%>- /hologram edit <hologram> setLine <line number> <text ...> <dark_gray>- <white>Edits the line
<%primary_color%>- /hologram edit <hologram> position <dark_gray>- <white>Teleports the hologram to you
<%primary_color%>- /hologram edit <hologram> moveTo <x> <y> <z> [yaw] [pitch] <dark_gray>- <white>Teleports the hologram to the coordinates
Expand Down