Skip to content

Commit 231177d

Browse files
authored
Merge pull request #6 from GoldenShad0w/v1.0.8
v1.0.8
2 parents ceea2a1 + 33d007d commit 231177d

File tree

13 files changed

+550
-214
lines changed

13 files changed

+550
-214
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>goldenshadow</groupId>
88
<artifactId>DisplayEntityEditor</artifactId>
9-
<version>1.0.7</version>
9+
<version>1.0.8</version>
1010
<packaging>jar</packaging>
1111

1212
<name>DisplayEntityEditor</name>

src/main/java/goldenshadow/displayentityeditor/DisplayEntityEditor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package goldenshadow.displayentityeditor;
22

33
import goldenshadow.displayentityeditor.commands.Command;
4+
import goldenshadow.displayentityeditor.commands.TabComplete;
45
import goldenshadow.displayentityeditor.events.Interact;
56
import goldenshadow.displayentityeditor.events.InventoryClick;
67
import goldenshadow.displayentityeditor.events.InventoryClose;
@@ -28,6 +29,7 @@ public final class DisplayEntityEditor extends JavaPlugin {
2829
public static InventoryFactory inventoryFactory;
2930
public static HashMap<UUID, Display> currentEditMap = new HashMap<>();
3031
public static NamespacedKey toolPrecisionKey;
32+
public static boolean alternateTextInput = false;
3133

3234
/**
3335
* Used for when the plugin starts up
@@ -38,10 +40,12 @@ public void onEnable() {
3840

3941
getConfig().options().copyDefaults(true);
4042
saveConfig();
43+
alternateTextInput = getConfig().getBoolean("alternate-text-input");
4144

4245
conversationFactory = new ConversationFactory(plugin);
4346
inventoryFactory = new InventoryFactory(new GUIItems(), new InventoryItems());
4447
Objects.requireNonNull(getCommand("displayentityeditor")).setExecutor(new Command());
48+
Objects.requireNonNull(getCommand("displayentityeditor")).setTabCompleter(new TabComplete());
4549
Bukkit.getPluginManager().registerEvents(new Interact(), plugin);
4650
Bukkit.getPluginManager().registerEvents(new InventoryClick(), plugin);
4751
Bukkit.getPluginManager().registerEvents(new InventoryClose(), plugin);

src/main/java/goldenshadow/displayentityeditor/Utilities.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package goldenshadow.displayentityeditor;
22

3+
import net.md_5.bungee.api.chat.BaseComponent;
4+
import net.md_5.bungee.api.chat.ClickEvent;
5+
import net.md_5.bungee.api.chat.ComponentBuilder;
6+
import net.md_5.bungee.api.chat.TextComponent;
37
import org.bukkit.ChatColor;
48
import org.bukkit.Color;
9+
import org.bukkit.Location;
510
import org.bukkit.NamespacedKey;
611
import org.bukkit.entity.Display;
12+
import org.bukkit.entity.Entity;
713
import org.bukkit.inventory.ItemFlag;
814
import org.bukkit.inventory.ItemStack;
915
import org.bukkit.inventory.meta.ItemMeta;
1016
import org.bukkit.persistence.PersistentDataType;
1117

18+
import javax.annotation.Nullable;
1219
import java.util.List;
1320

1421
public class Utilities {
@@ -106,5 +113,47 @@ public static String getErrorMessageFormat(String message) {
106113
return ChatColor.DARK_RED + "[DEE] " + ChatColor.RED + message;
107114
}
108115

116+
/**
117+
* Used to get the nearest display entity
118+
* @param location The location from where the nearest display entity should be gotten
119+
* @param lockSearchToggle If this method should look for locked or unlocked entities. If true, it will only look for unlocked entities, and if false it will only look for locked ones
120+
* @return The nearest display entity or null if none were found
121+
*/
122+
@Nullable
123+
public static Display getNearestDisplayEntity(Location location, boolean lockSearchToggle) {
124+
Display entity = null;
125+
double distance = 5;
126+
assert location.getWorld() != null;
127+
for (Entity e : location.getWorld().getNearbyEntities(location, 5,5,5)) {
128+
if (e instanceof Display d) {
129+
if (lockSearchToggle) {
130+
if (!d.getScoreboardTags().contains("dee:locked")) {
131+
double dis = d.getLocation().distance(location);
132+
if (dis < distance) {
133+
entity = d;
134+
distance = dis;
135+
}
136+
}
137+
} else {
138+
if (d.getScoreboardTags().contains("dee:locked")) {
139+
double dis = d.getLocation().distance(location);
140+
if (dis < distance) {
141+
entity = d;
142+
distance = dis;
143+
}
144+
}
145+
}
146+
}
147+
}
148+
return entity;
149+
}
150+
151+
public static BaseComponent[] getCommandMessage(String commandMessage, String hint) {
152+
153+
TextComponent click = new TextComponent(net.md_5.bungee.api.ChatColor.DARK_AQUA + "[DEE] " + net.md_5.bungee.api.ChatColor.AQUA + "Run command " + net.md_5.bungee.api.ChatColor.GRAY + "/deeditor " + commandMessage + net.md_5.bungee.api.ChatColor.AQUA + " to edit the entity or click this message. " + hint);
154+
click.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/deeditor edit " + commandMessage));
155+
156+
return new ComponentBuilder(click).create();
157+
}
109158

110159
}

src/main/java/goldenshadow/displayentityeditor/commands/Command.java

Lines changed: 163 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
import goldenshadow.displayentityeditor.DisplayEntityEditor;
44
import goldenshadow.displayentityeditor.Utilities;
5+
import goldenshadow.displayentityeditor.conversation.InputData;
6+
import goldenshadow.displayentityeditor.conversation.InputManager;
7+
import goldenshadow.displayentityeditor.enums.InputType;
58
import org.bukkit.ChatColor;
69
import org.bukkit.command.CommandExecutor;
710
import org.bukkit.command.CommandSender;
11+
import org.bukkit.entity.BlockDisplay;
12+
import org.bukkit.entity.Display;
813
import org.bukkit.entity.Player;
14+
import org.bukkit.entity.TextDisplay;
915
import org.bukkit.inventory.ItemStack;
1016
import org.bukkit.persistence.PersistentDataType;
1117
import org.jetbrains.annotations.NotNull;
@@ -30,22 +36,157 @@ public class Command implements CommandExecutor {
3036
@Override
3137
public boolean onCommand(@NotNull CommandSender sender,@NotNull org.bukkit.command.Command command,@NotNull String label, String[] args) {
3238
if (sender instanceof Player p) {
33-
if (savedInventories.containsKey(p.getUniqueId())) {
34-
returnInventory(p);
35-
p.sendMessage(Utilities.getInfoMessageFormat("Your inventory has been returned to you!"));
36-
savedInventories.remove(p.getUniqueId());
39+
if (args.length == 0) {
40+
if (savedInventories.containsKey(p.getUniqueId())) {
41+
returnInventory(p);
42+
p.sendMessage(Utilities.getInfoMessageFormat("Your inventory has been returned to you!"));
43+
savedInventories.remove(p.getUniqueId());
44+
return true;
45+
}
46+
saveInventory(p);
47+
ItemStack[] array = DisplayEntityEditor.inventoryFactory.getInventoryArray();
48+
for (int i = 0; i < array.length; i++) {
49+
p.getInventory().setItem(i, array[i]);
50+
}
51+
if (!p.getPersistentDataContainer().has(DisplayEntityEditor.toolPrecisionKey, PersistentDataType.DOUBLE)) {
52+
p.getPersistentDataContainer().set(DisplayEntityEditor.toolPrecisionKey, PersistentDataType.DOUBLE, 1d);
53+
}
54+
p.sendMessage(Utilities.getInfoMessageFormat("Given display entity tools. Left click to cycle through the tools."));
55+
p.sendMessage(ChatColor.DARK_AQUA + "[DEE] " + ChatColor.BLUE + "Run this command again to have your inventory returned!");
3756
return true;
3857
}
39-
saveInventory(p);
40-
ItemStack[] array = DisplayEntityEditor.inventoryFactory.getInventoryArray();
41-
for (int i = 0; i < array.length; i++) {
42-
p.getInventory().setItem(i, array[i]);
58+
if (args.length == 1) {
59+
if (args[0].equalsIgnoreCase("reload")) {
60+
DisplayEntityEditor.getPlugin().reloadConfig();
61+
DisplayEntityEditor.alternateTextInput = DisplayEntityEditor.getPlugin().getConfig().getBoolean("alternate-text-input");
62+
p.sendMessage(Utilities.getInfoMessageFormat("Config reloaded!"));
63+
return true;
64+
}
4365
}
44-
if (!p.getPersistentDataContainer().has(DisplayEntityEditor.toolPrecisionKey, PersistentDataType.DOUBLE)) {
45-
p.getPersistentDataContainer().set(DisplayEntityEditor.toolPrecisionKey, PersistentDataType.DOUBLE, 1d);
66+
67+
if (args.length > 2) {
68+
if (DisplayEntityEditor.alternateTextInput) {
69+
if (args[0].equalsIgnoreCase("edit")) {
70+
String input = collectArgsToString(args);
71+
Display display = Utilities.getNearestDisplayEntity(p.getLocation(), true);
72+
if (display == null) {
73+
p.sendMessage(Utilities.getErrorMessageFormat("There is no unlocked display entity within 5 blocks!"));
74+
return true;
75+
}
76+
if (args[1].equalsIgnoreCase("name")) {
77+
InputManager.successfulTextInput(new InputData(display, InputType.NAME, null), input, p);
78+
return true;
79+
}
80+
if (args[1].equalsIgnoreCase("background_color")) {
81+
if (display instanceof TextDisplay) {
82+
InputManager.successfulTextInput(new InputData(display, InputType.BACKGROUND_COLOR, null), input, p);
83+
return true;
84+
}
85+
p.sendMessage(Utilities.getErrorMessageFormat("There is no unlocked text display entity within 5 blocks!"));
86+
return true;
87+
}
88+
if (args[1].equalsIgnoreCase("text")) {
89+
if (display instanceof TextDisplay) {
90+
InputManager.successfulTextInput(new InputData(display, InputType.TEXT, null), input, p);
91+
return true;
92+
}
93+
p.sendMessage(Utilities.getErrorMessageFormat("There is no unlocked text display entity within 5 blocks!"));
94+
return true;
95+
}
96+
if (args[1].equalsIgnoreCase("text_append")) {
97+
if (display instanceof TextDisplay) {
98+
InputManager.successfulTextInput(new InputData(display, InputType.TEXT_APPEND, null), input, p);
99+
return true;
100+
}
101+
p.sendMessage(Utilities.getErrorMessageFormat("There is no unlocked text display entity within 5 blocks!"));
102+
return true;
103+
}
104+
if (args[1].equalsIgnoreCase("glow_color")) {
105+
if (!(display instanceof TextDisplay)) {
106+
InputManager.successfulTextInput(new InputData(display, InputType.GLOW_COLOR, null), input, p);
107+
return true;
108+
}
109+
p.sendMessage(Utilities.getErrorMessageFormat("There is no unlocked block/item display entity within 5 blocks!"));
110+
return true;
111+
}
112+
if (args[1].equalsIgnoreCase("block_state")) {
113+
if (display instanceof BlockDisplay) {
114+
InputManager.successfulTextInput(new InputData(display, InputType.BLOCK_STATE, ((BlockDisplay) display).getBlock().getMaterial()), input, p);
115+
return true;
116+
}
117+
p.sendMessage(Utilities.getErrorMessageFormat("There is no unlocked block display entity within 5 blocks!"));
118+
return true;
119+
}
120+
if (args[1].equalsIgnoreCase("view_range")) {
121+
if (InputManager.isFloat(input)) {
122+
InputManager.successfulFloatInput(new InputData(display, InputType.VIEW_RANGE, null), Float.parseFloat(input), p);
123+
return true;
124+
}
125+
}
126+
if (args[1].equalsIgnoreCase("display_height")) {
127+
if (InputManager.isFloat(input)) {
128+
InputManager.successfulFloatInput(new InputData(display, InputType.DISPLAY_HEIGHT, null), Float.parseFloat(input), p);
129+
return true;
130+
}
131+
}
132+
if (args[1].equalsIgnoreCase("display_width")) {
133+
if (InputManager.isFloat(input)) {
134+
InputManager.successfulFloatInput(new InputData(display, InputType.DISPLAY_WIDTH, null), Float.parseFloat(input), p);
135+
return true;
136+
}
137+
}
138+
if (args[1].equalsIgnoreCase("shadow_radius")) {
139+
if (InputManager.isFloat(input)) {
140+
InputManager.successfulFloatInput(new InputData(display, InputType.SHADOW_RADIUS, null), Float.parseFloat(input), p);
141+
return true;
142+
}
143+
}
144+
if (args[1].equalsIgnoreCase("shadow_strength")) {
145+
if (InputManager.isFloat(input)) {
146+
float f = Float.parseFloat(input);
147+
if (0 <= f && f <= 1) {
148+
InputManager.successfulFloatInput(new InputData(display, InputType.SHADOW_STRENGTH, null), Float.parseFloat(input), p);
149+
return true;
150+
}
151+
p.sendMessage(Utilities.getErrorMessageFormat("Value should be between 0 and 1!"));
152+
return true;
153+
}
154+
}
155+
if (args[1].equalsIgnoreCase("text_opacity")) {
156+
if (display instanceof TextDisplay) {
157+
if (InputManager.isByte(input)) {
158+
InputManager.successfulByteInput(new InputData(display, InputType.TEXT_OPACITY, null), Integer.parseInt(input), p);
159+
return true;
160+
}
161+
}
162+
p.sendMessage(Utilities.getErrorMessageFormat("There is no unlocked text display entity within 5 blocks!"));
163+
return true;
164+
}
165+
if (args[1].equalsIgnoreCase("background_opacity")) {
166+
if (display instanceof TextDisplay) {
167+
if (InputManager.isByte(input)) {
168+
InputManager.successfulByteInput(new InputData(display, InputType.BACKGROUND_OPACITY, null), Integer.parseInt(input), p);
169+
return true;
170+
}
171+
}
172+
p.sendMessage(Utilities.getErrorMessageFormat("There is no unlocked text display entity within 5 blocks!"));
173+
return true;
174+
}
175+
if (args[1].equalsIgnoreCase("line_width")) {
176+
if (display instanceof TextDisplay) {
177+
if (InputManager.isInteger(input)) {
178+
InputManager.successfulIntegerInput(new InputData(display, InputType.LINE_WIDTH, null), Integer.parseInt(input), p);
179+
return true;
180+
}
181+
}
182+
p.sendMessage(Utilities.getErrorMessageFormat("There is no unlocked text display entity within 5 blocks!"));
183+
return true;
184+
}
185+
}
186+
}
187+
return true;
46188
}
47-
p.sendMessage(Utilities.getInfoMessageFormat("Given display entity tools. Left click to cycle through the tools."));
48-
p.sendMessage(ChatColor.DARK_AQUA + "[DEE] " + ChatColor.BLUE + "Run this command again to have your inventory returned!");
189+
p.sendMessage(Utilities.getErrorMessageFormat("Invalid arguments!"));
49190
return true;
50191
}
51192
sender.sendMessage("This command must be run by a player!");
@@ -73,4 +214,14 @@ private static void returnInventory(Player player) {
73214
player.getInventory().setItem(i, saved[i]);
74215
}
75216
}
217+
218+
private static String collectArgsToString(String[] args) {
219+
StringBuilder s = new StringBuilder();
220+
for (int i = 2; i < args.length; i++) {
221+
if (i != 2) s.append(" ");
222+
s.append(args[i]);
223+
}
224+
return s.toString();
225+
}
226+
76227
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package goldenshadow.displayentityeditor.commands;
2+
3+
import goldenshadow.displayentityeditor.DisplayEntityEditor;
4+
import org.bukkit.command.Command;
5+
import org.bukkit.command.CommandSender;
6+
import org.bukkit.command.TabCompleter;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
public class TabComplete implements TabCompleter {
14+
15+
16+
17+
List<String> arguments = new ArrayList<>();
18+
19+
@Override
20+
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
21+
22+
List<String> result = new ArrayList<>();
23+
if (args.length == 1) {
24+
if (DisplayEntityEditor.alternateTextInput) {
25+
arguments = new ArrayList<>(List.of("reload", "edit"));
26+
} else {
27+
arguments = new ArrayList<>(List.of("reload"));
28+
}
29+
30+
for (String a : arguments) {
31+
if (a.toLowerCase().startsWith(args[0].toLowerCase()))
32+
result.add(a);
33+
}
34+
return result;
35+
}
36+
if (args.length == 2) {
37+
if (DisplayEntityEditor.alternateTextInput) {
38+
if (args[0].equalsIgnoreCase("edit")) {
39+
arguments = new ArrayList<>(Arrays.asList("name", "view_range", "display_height", "display_width", "shadow_radius", "shadow_strength", "text_opacity", "line_width", "background_opacity", "background_color", "text", "text_append", "glow_color", "block_state"));
40+
}
41+
}
42+
else arguments.clear();
43+
for (String a : arguments) {
44+
if (a.toLowerCase().startsWith(args[1].toLowerCase()))
45+
result.add(a);
46+
}
47+
return result;
48+
}
49+
50+
51+
if (args.length > 2) {
52+
arguments.clear();
53+
return arguments;
54+
}
55+
56+
57+
return null;
58+
}
59+
}

0 commit comments

Comments
 (0)