Skip to content

Commit

Permalink
[1.5] - Change the way block and item values are changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Luncaaa committed Jul 12, 2024
1 parent 60f17b4 commit a94d369
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Display;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

import java.awt.*;
import java.math.BigDecimal;
Expand All @@ -28,16 +27,20 @@

public class EditorGUI extends InventoryMethods {
private final BaseDisplay display;
private final Map<Player, EditText> editMap = new HashMap<>();
private final EditorItems items;
private final Map<Player, EditAction> editMap = new HashMap<>();

public EditorGUI(AdvancedDisplays plugin, BaseDisplay display) {
super(plugin, Bukkit.createInventory(null, 27, Utils.getColoredText(("&6Editing " + display.getType().name() + " display: &e" + display.getName()))));
this.display = display;
this.items = new EditorItems(display);
}

@Override
public void onClick(InventoryClickEvent event) {
if (display.getType() == DisplayType.TEXT || event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY) {
event.setCancelled(true);
//* This would be useful if the player could use their inventory to drag items into the "current value" slot.
/*if (display.getType() == DisplayType.TEXT || event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY) {
event.setCancelled(true);
} else {
Expand All @@ -53,7 +56,7 @@ public void onClick(InventoryClickEvent event) {
event.setCancelled(true);
}
}
}
}*/

if (event.getClickedInventory() == this.getInventory()) {
super.onClick(event);
Expand All @@ -62,8 +65,6 @@ public void onClick(InventoryClickEvent event) {

@Override
public void decorate() {
EditorItems items = new EditorItems(this.display);

// ---[ BRIGHTNESS ]----
addButton(0, new Button.InventoryButton(items.BLOCK_LIGHT) {
@Override
Expand Down Expand Up @@ -194,9 +195,21 @@ public void onClick(InventoryClickEvent event) {
addButton(13, new Button.InventoryButton(items.CURRENT_VALUE) {
@Override
public void onClick(InventoryClickEvent event) {
ItemStack cursorItem = Objects.requireNonNull(event.getCursor()).clone();
event.getWhoClicked().closeInventory();
plugin.getInventoryManager().getEditingPlayer((Player) event.getWhoClicked()).setChatEditing(true);

if (display.getType() != DisplayType.TEXT) {
editMap.put((Player) event.getWhoClicked(), EditAction.CHANGE_MATERIAL);
if (display.getType() == DisplayType.BLOCK) {
event.getWhoClicked().sendMessage(plugin.getMessagesManager().getColoredMessage("&6Enter the name of a valid block.", true));
event.getWhoClicked().sendMessage(plugin.getMessagesManager().getColoredMessage("&6You can find a list of them here: &ehttps://hub.spigotmc.org/javadocs/spigot/org/bukkit/block/BlockType.html", true));
} else {
event.getWhoClicked().sendMessage(plugin.getMessagesManager().getColoredMessage("&6Enter the name of a valid material.", true));
event.getWhoClicked().sendMessage(plugin.getMessagesManager().getColoredMessage("&6You can find a list of them here: &ehttps://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html", true));
}
//* This would be useful if the player could use their inventory to drag items into the "current value" slot.
/*ItemStack cursorItem = Objects.requireNonNull(event.getCursor()).clone();
if (cursorItem.getType() == Material.AIR) return;
switch (display.getType()) {
Expand All @@ -218,16 +231,14 @@ public void run() {
}
}.runTask(plugin);
if (display.getType() == DisplayType.BLOCK) setBlockData(items);
if (display.getType() == DisplayType.BLOCK) setBlockData(items);*/
} else {
event.getWhoClicked().closeInventory();
plugin.getInventoryManager().getEditingPlayer((Player) event.getWhoClicked()).setChatEditing(true);
if (event.isLeftClick()) {
editMap.put((Player) event.getWhoClicked(), EditText.REMOVE);
editMap.put((Player) event.getWhoClicked(), EditAction.REMOVE_TEXT);
event.getWhoClicked().sendMessage(plugin.getMessagesManager().getColoredMessage("&6Enter the name of the animation to remove. Valid animations: &e" + String.join("&6, &e", ((TextDisplay) display).getText().keySet()), true));
event.getWhoClicked().sendMessage(plugin.getMessagesManager().getColoredMessage("&6Type \"&ecancel&6\" to cancel the operation.", true));
} else {
editMap.put((Player) event.getWhoClicked(), EditText.ADD);
editMap.put((Player) event.getWhoClicked(), EditAction.ADD_TEXT);
event.getWhoClicked().sendMessage(plugin.getMessagesManager().getColoredMessage("&6Enter the name of the animation to add along with its value. The name must not include spaces. You may use legacy color codes, minimessage format, placeholders and '\\n' to add a new line.", true));
event.getWhoClicked().sendMessage(plugin.getMessagesManager().getColoredMessage("&6Example: \"&emyAnimation3 <red>Hello %player%\\n<yellow>How are you?&6\"", true));
event.getWhoClicked().sendMessage(plugin.getMessagesManager().getColoredMessage("&6Type \"&ecancel&6\" to cancel the operation.", true));
Expand All @@ -247,7 +258,7 @@ public void onClick(InventoryClickEvent event) {

// ----[ DISPLAY-SPECIFIC ]-----
switch (display.getType()) {
case BLOCK -> setBlockData(items);
case BLOCK -> setBlockData();

case ITEM -> addButton(8, new Button.InventoryButton(items.ITEM_TRANSFORMATION) {
@Override
Expand Down Expand Up @@ -353,7 +364,7 @@ public void onClick(InventoryClickEvent event) {
super.decorate();
}

private void setBlockData(EditorItems items) {
private void setBlockData() {
BlockDisplay blockDisplay = (BlockDisplay) display;
String data = blockDisplay.getBlock().getAsString();
if (data.contains("[")) {
Expand Down Expand Up @@ -384,44 +395,82 @@ public void onClick(InventoryClickEvent event) {}

@Override
public void handleChatEdit(Player player, String input) {
if (!(display instanceof TextDisplay textDisplay)) return;

if (!input.equalsIgnoreCase("cancel")) {
if (editMap.get(player) == EditText.REMOVE) {
if (textDisplay.removeText(input)) {
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&aThe animation &e" + input + " &a has been removed. If it didn't exist, nothing will be changed.", true));
} else {
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&cThe animation &b" + input + " &cdoes not exist!", true));
return;
switch (editMap.get(player)) {
case REMOVE_TEXT -> {
if (((TextDisplay) display).removeText(input)) {
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&aThe animation &e" + input + " &a has been removed. If it didn't exist, nothing will be changed.", true));
} else {
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&cThe animation &b" + input + " &cdoes not exist!", true));
return;
}

InventoryUtils.changeCurrentValue(getInventory().getItem(13), ((TextDisplay) display).getText().size() + " text animation(s)");
}

} else {
int firstSpace = input.indexOf(" ");
case ADD_TEXT -> {
int firstSpace = input.indexOf(" ");

if (firstSpace == -1){
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&cThe text you have entered is invalid. Remember that the format is &b<animation name (no spaces)> <animation text>", true));
return;
}

String identifier = input.substring(0, firstSpace);
List<String> value = Arrays.stream(input.substring(firstSpace + 1).split(Pattern.quote("\\n"))).toList();
if (((TextDisplay) display).addText(identifier, value)) {
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&aThe animation &e" + identifier + " &a has been created and added after the last animation.", true));
} else {
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&cAn animation with the name &b" + identifier + " &calready exists!", true));
return;
}

if (firstSpace == -1){
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&cThe text you have entered is invalid. Remember that the format is &b<animation name (no spaces)> <animation text>", true));
return;
InventoryUtils.changeCurrentValue(getInventory().getItem(13), ((TextDisplay) display).getText().size() + " text animation(s)");
}

String identifier = input.substring(0, firstSpace);
List<String> value = Arrays.stream(input.substring(firstSpace + 1).split(Pattern.quote("\\n"))).toList();
if (textDisplay.addText(identifier, value)) {
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&aThe animation &e" + identifier + " &a has been created and added after the last animation.", true));
} else {
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&cAn animation with the name &b" + identifier + " &calready exists!", true));
return;
case CHANGE_MATERIAL -> {
Material material = Material.getMaterial(input.toUpperCase());

if (material == null) {
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&b" + input + " &cis not a valid material!", true));
return;
}

if (material == Material.AIR) {
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&cThe material cannot be air!", true));
return;
}

if (display.getType() == DisplayType.ITEM) {
((ItemDisplay) display).setMaterial(material);

} else {
try {
BlockData blockData = material.createBlockData();
((BlockDisplay) display).setBlock(blockData);
setBlockData();

} catch (IllegalArgumentException | NullPointerException ignored) {
player.sendMessage(plugin.getMessagesManager().getColoredMessage("&b" + material.name() + " &cis not a valid block!", true));
return;
}
}

Objects.requireNonNull(getInventory().getItem(13)).setType(material);
Objects.requireNonNull(getInventory().getItem(13)).setAmount(1);
InventoryUtils.changeCurrentValue(getInventory().getItem(13), material.name());
}
}
}

InventoryUtils.changeCurrentValue(getInventory().getItem(13), ((TextDisplay) display).getText().size() + " text animation(s)");
getInventory().setItem(13, getInventory().getItem(13));
editMap.remove(player);
plugin.getInventoryManager().handleOpen(player, this, display);
}

private enum EditText {
ADD,
REMOVE
private enum EditAction {
ADD_TEXT,
REMOVE_TEXT,
CHANGE_MATERIAL
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public EditorItems(BaseDisplay display) {

switch (display.getType()) {
case TEXT -> CURRENT_VALUE = GlobalItems.create(Material.OAK_SIGN, "Display text", List.of("Changes the text that is being displayed", "", "&7Use &cLEFT_CLICK &7to remove an animation", "&7Use &cRIGHT_CLICK &7to add an animation"), ((TextDisplay) display).getText().size() + " text animation(s)", false, false, 0.0, 0.0);
case ITEM -> CURRENT_VALUE = GlobalItems.create(((ItemDisplay) display).getMaterial(), "Display item", List.of("Changes the item that is being displayed", "You must have an item in your cursor."), ((ItemDisplay) display).getMaterial(), false, false, 0.0, 0.0);
case BLOCK -> CURRENT_VALUE = GlobalItems.create(((BlockDisplay) display).getBlock().getMaterial(), "Display block", List.of("Changes the block that is being displayed", "You must have a valid block in your cursor."), ((BlockDisplay) display).getBlock().getMaterial(), false, false, 0.0, 0.0);
case ITEM -> CURRENT_VALUE = GlobalItems.create(((ItemDisplay) display).getMaterial(), "Display item", List.of("Changes the item that is being displayed"/*, "You must have an item in your cursor."*/), ((ItemDisplay) display).getMaterial(), false, false, 0.0, 0.0);
case BLOCK -> CURRENT_VALUE = GlobalItems.create(((BlockDisplay) display).getBlock().getMaterial(), "Display block", List.of("Changes the block that is being displayed"/*, "You must have a valid block in your cursor."*/), ((BlockDisplay) display).getBlock().getMaterial(), false, false, 0.0, 0.0);
default -> CURRENT_VALUE = new ItemStack(Material.BARRIER);
}
REMOVE = GlobalItems.create(Material.BARRIER, "&cRemove", "Permanently removes this display", null);
Expand Down

0 comments on commit a94d369

Please sign in to comment.