Skip to content
Merged
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
@@ -0,0 +1,51 @@
package github.nighter.smartspawner.commands.hologram;

import com.mojang.brigadier.context.CommandContext;
import github.nighter.smartspawner.SmartSpawner;
import github.nighter.smartspawner.commands.BaseSubCommand;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jspecify.annotations.NullMarked;

@NullMarked
public class HologramClearSubCommand extends BaseSubCommand {

public HologramClearSubCommand(SmartSpawner plugin) {
super(plugin);
}

@Override
public String getName() {
return "clear";
}

@Override
public String getPermission() {
return "smartspawner.hologram.clear";
}

@Override
public String getDescription() {
return "Clear all text display holograms";
}

@Override
public int execute(CommandContext<CommandSourceStack> context) {
CommandSender sender = context.getSource().getSender();

try {
// Execute the Minecraft command to kill all text_display entities
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "minecraft:kill @e[type=text_display]");

// Send success message to player
plugin.getMessageService().sendMessage(sender, "command_hologram_cleared");

return 1;
} catch (Exception e) {
plugin.getLogger().severe("Error clearing holograms: " + e.getMessage());
plugin.getMessageService().sendMessage(sender, "command_hologram_clear_error");
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package github.nighter.smartspawner.commands.hologram;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import github.nighter.smartspawner.SmartSpawner;
import github.nighter.smartspawner.commands.BaseSubCommand;
import github.nighter.smartspawner.spawner.properties.SpawnerManager;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.jspecify.annotations.NullMarked;

@NullMarked
public class HologramSubCommand extends BaseSubCommand {
private final SpawnerManager spawnerManager;
private final HologramClearSubCommand clearSubCommand;

public HologramSubCommand(SmartSpawner plugin) {
super(plugin);
this.spawnerManager = plugin.getSpawnerManager();
this.clearSubCommand = new HologramClearSubCommand(plugin);
}

@Override
Expand All @@ -33,6 +37,20 @@ public String getDescription() {
return "Toggle hologram display for spawners";
}

@Override
public LiteralArgumentBuilder<CommandSourceStack> build() {
LiteralArgumentBuilder<CommandSourceStack> builder = Commands.literal(getName());
builder.requires(source -> hasPermission(source.getSender()));

// Add the toggle functionality as the default execution
builder.executes(this::execute);

// Add the clear subcommand
builder.then(clearSubCommand.build());

return builder;
}

@Override
public int execute(CommandContext<CommandSourceStack> context) {
CommandSender sender = context.getSource().getSender();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,6 @@ public void updateText() {
}

public void updateData(int stackSize, EntityType entityType, int currentExp, int maxExp, int currentItems, int maxSlots) {
// First, ensure we have a valid hologram
TextDisplay display = textDisplay.get();
if (display == null || !display.isValid()) {
// If hologram doesn't exist or is invalid, recreate it
createHologram();
}

// Update data values
this.stackSize = stackSize;
this.entityType = entityType;
Expand All @@ -150,17 +143,39 @@ public void updateData(int stackSize, EntityType entityType, int currentExp, int
this.currentItems = currentItems;
this.maxSlots = maxSlots;

// Update the text display
updateText();
// First, ensure we have a valid hologram
TextDisplay display = textDisplay.get();
if (display == null) {
// If hologram doesn't exist, recreate it
createHologram();
} else {
// Check validity on the entity thread to avoid race conditions
Scheduler.runEntityTask(display, () -> {
if (!display.isValid()) {
// If invalid, recreate the hologram
textDisplay.set(null);
createHologram();
} else {
// Update the text display
updateText();
}
});
}
}

public void remove() {
TextDisplay display = textDisplay.get();
if (display != null && display.isValid()) {
// Run on the entity's thread
Scheduler.runEntityTask(display, display::remove);
if (display != null) {
// Run on the entity's thread to ensure safe removal
Scheduler.runEntityTask(display, () -> {
if (display.isValid()) {
display.remove();
}
});
textDisplay.set(null);
}
// Also clean up any stuck holograms
cleanupExistingHologram();
}

public void cleanupExistingHologram() {
Expand All @@ -169,13 +184,16 @@ public void cleanupExistingHologram() {
// First, check if our tracked hologram is still valid
TextDisplay display = textDisplay.get();
if (display != null) {
if (display.isValid()) {
// If it's valid but we're cleaning up, remove it
display.remove();
}
// Always remove the tracked display, even if it appears invalid
Scheduler.runEntityTask(display, () -> {
if (display.isValid()) {
display.remove();
}
});
textDisplay.set(null);
}

// Use async task to avoid blocking
Scheduler.runLocationTask(spawnerLocation, () -> {
// Define a tighter search radius just to catch any potentially duplicated holograms
// with the same identifier (which shouldn't happen but being safe)
Expand All @@ -186,7 +204,9 @@ public void cleanupExistingHologram() {
.stream()
.filter(entity -> entity instanceof TextDisplay && entity.getCustomName() != null)
.filter(entity -> entity.getCustomName().equals(uniqueIdentifier))
.forEach(Entity::remove);
.forEach(entity -> {
Scheduler.runEntityTask(entity, entity::remove);
});
});
}
}
8 changes: 8 additions & 0 deletions core/src/main/resources/language/DonutSMP/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ command_hologram_disabled:
message: "&#f8f8ffʜᴏʟᴏɢʀᴀᴍꜱ &#e05252ᴅɪꜱᴀʙʟᴇᴅ &#f8f8ffꜰᴏʀ ᴀʟʟ ꜱᴘᴀᴡɴᴇʀꜱ"
sound: block.note_block.bass

command_hologram_cleared:
message: "&#37eb9aꜱᴜᴄᴄᴇꜱꜱꜰᴜʟʟʏ &#f8f8ffᴄʟᴇᴀʀᴇᴅ ᴀʟʟ ʜᴏʟᴏɢʀᴀᴍꜱ"
sound: entity.experience_orb.pickup

command_hologram_clear_error:
message: "&#ff5252ꜰᴀɪʟᴇᴅ ᴛᴏ ᴄʟᴇᴀʀ ʜᴏʟᴏɢʀᴀᴍꜱ"
sound: block.note_block.pling

# Prices command messages
prices_not_available:
message: "&#ff5252ꜱᴇʟʟ ɪɴᴛᴇɢʀᴀᴛɪᴏɴ ɪꜱ ɴᴏᴛ ᴀᴠᴀɪʟᴀʙʟᴇ. ᴘʟᴇᴀꜱᴇ ᴄᴏɴꜰɪɢᴜʀᴇ ᴇᴄᴏɴᴏᴍʏ ꜱᴇᴛᴛɪɴɢꜱ."
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/resources/language/de_DE/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ command_hologram_disabled:
message: "&#f8f8ffʜᴏʟᴏɢʀᴀᴍᴍᴇ &#e05252ᴅᴇᴀᴋᴛɪᴠɪᴇʀᴛ &#f8f8ffꜰᴜ̈ʀ ᴀʟʟᴇ ꜱᴘᴀᴡɴᴇʀ"
sound: block.note_block.bass

command_hologram_cleared:
message: "&#37eb9aᴇʀꜰᴏʟɢʀᴇɪᴄʜ &#f8f8ffᴀʟʟᴇ ʜᴏʟᴏɢʀᴀᴍᴍᴇ ɢᴇʟᴏ̈ꜱᴄʜᴛ"
sound: entity.experience_orb.pickup

command_hologram_clear_error:
message: "&#ff5252ꜰᴇʜʟᴇʀ ʙᴇɪᴍ ʟᴏ̈ꜱᴄʜᴇɴ ᴅᴇʀ ʜᴏʟᴏɢʀᴀᴍᴍᴇ"
sound: block.note_block.pling

# Prices command messagess
prices_not_available:
message: "&#ff5252ᴠᴇʀᴋᴀᴜꜰ ɪɴᴛᴇɢʀᴀᴛɪᴏɴ ɪꜱᴛ ɴɪᴄʜᴛ ᴠᴇʀꜰᴜ̈ɢʙᴀʀ. ʙɪᴛᴛᴇ ᴇᴄᴏɴᴏᴍʏ ᴇɪɴꜱᴛᴇʟʟᴜɴɢᴇɴ ᴋᴏɴꜰɪɢᴜʀɪᴇʀᴇɴ."
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/resources/language/en_US/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ command_hologram_disabled:
message: "&#f8f8ffʜᴏʟᴏɢʀᴀᴍꜱ &#e05252ᴅɪꜱᴀʙʟᴇᴅ &#f8f8ffꜰᴏʀ ᴀʟʟ ꜱᴘᴀᴡɴᴇʀꜱ"
sound: block.note_block.bass

command_hologram_cleared:
message: "&#37eb9aꜱᴜᴄᴄᴇꜱꜱꜰᴜʟʟʏ &#f8f8ffᴄʟᴇᴀʀᴇᴅ ᴀʟʟ ʜᴏʟᴏɢʀᴀᴍꜱ"
sound: entity.experience_orb.pickup

command_hologram_clear_error:
message: "&#ff5252ꜰᴀɪʟᴇᴅ ᴛᴏ ᴄʟᴇᴀʀ ʜᴏʟᴏɢʀᴀᴍꜱ"
sound: block.note_block.pling

# Prices command messagess
prices_not_available:
message: "&#ff5252ꜱᴇʟʟ ɪɴᴛᴇɢʀᴀᴛɪᴏɴ ɪꜱ ɴᴏᴛ ᴀᴠᴀɪʟᴀʙʟᴇ. ᴘʟᴇᴀꜱᴇ ᴄᴏɴꜰɪɢᴜʀᴇ ᴇᴄᴏɴᴏᴍʏ ꜱᴇᴛᴛɪɴɢꜱ."
Expand Down