Skip to content

Commit

Permalink
New:
Browse files Browse the repository at this point in the history
- Added Actionbar mode (per-player)
  - Command: /wit type <actionbar/bossbar>
  - permisson: wit.default (wit.toggledisplay has been replaced with wit.default)
  - New reload command:
    - Reloads main configuration
    - permission: wit.reload

 API Changes:
 - Allow removing Handlers
  • Loading branch information
darksoulq committed Jan 2, 2025
1 parent 1dfd94a commit 6a421d8
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 57 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>me.darksoul</groupId>
<artifactId>whatisthat</artifactId>
<name>whatisthat</name>
<version>1.4.1</version>
<version>1.4.2</version>
<build>
<defaultGoal>clean package</defaultGoal>
<resources>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.darksoul</groupId>
<artifactId>whatisthat</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
<packaging>jar</packaging>

<name>whatisthat</name>
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/me/darksoul/whatIsThat/Handlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class Handlers {
private static YamlConfiguration config = WAILAListener.getConfig();

public static void setup() {
//Litefarm
if (config.getBoolean("litefarm.enabled", true) && LiteFarmCompat.getIsLitefarmInstalled()) {
blockHandlers.add(LiteFarmCompat::handleLitefarmCrop);
}
// Entities Handlers
if (config.getBoolean("valhallammo.enabled", true) && ValhallaMMOCompat.getIsVMMOInstalled()) {
entityHandlers.add(ValhallaMMOCompat::handleVMMOEntity);
Expand All @@ -42,14 +46,17 @@ public static void setup() {
if (config.getBoolean("slimefun.enabled", true) && SlimefunCompat.getIsSlimefunInstalled()) {
blockHandlers.add(SlimefunCompat::handleSlimefunMachines);
}
if (config.getBoolean("litefarm.enabled", true) && LiteFarmCompat.getIsLitefarmInstalled()) {
blockHandlers.add(LiteFarmCompat::handleLitefarmCrop);
}
if (config.getBoolean("nexo.blocks.enabled", true) && NexoCompat.getIsNexoInstalled()) {
blockHandlers.add(NexoCompat::handleNexoBlock);
}
}

public static void removeBlockHandler(BiFunction<Block, Player, Boolean> handler) {
blockHandlers.remove(handler);
}
public static void removeEntityHandler(BiFunction<Entity, Player, Boolean> handler) {
entityHandlers.remove(handler);
}
public static List<BiFunction<Block, Player, Boolean>> getBlockHandlers() {
return blockHandlers;
}
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/me/darksoul/whatIsThat/WAILAListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


public class WAILAListener implements Listener {
private static final YamlConfiguration config = ConfigUtils.loadConfig();
private static YamlConfiguration config = ConfigUtils.loadConfig();
private static final File PREF_FOLDER = new File(WhatIsThat.getInstance().getDataFolder(), "cache/players");
private static final List<Player> players = new ArrayList<>();
private static final int entityDistance = config.getInt("core.entitydistance", 25);
Expand Down Expand Up @@ -68,22 +68,34 @@ private void updateWAILA(Player player) {
}
}
}
WAILAManager.updateBossBar(player, "");
WAILAManager.setBar(player, "bossbar", "");
}

@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
WAILAManager.removeBossBar(event.getPlayer());
WAILAManager.removeBar(event.getPlayer(), "bossbar");
players.remove(event.getPlayer());
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
File playerFile = new File(PREF_FOLDER + "/" + event.getPlayer().getName() + ".yml");
YamlConfiguration config = YamlConfiguration.loadConfiguration(playerFile);
boolean disableBossBar = config.getBoolean("disableBossBar", false);
YamlConfiguration config = new YamlConfiguration();
if (!playerFile.exists()) {
try {
playerFile.createNewFile();
config = YamlConfiguration.loadConfiguration(playerFile);
config.set("disableWAILA", false);
config.set("type", "bossbar");
config.save(playerFile);
} catch (Exception e) {
e.printStackTrace();
}
} else {
config = YamlConfiguration.loadConfiguration(playerFile);
}
boolean disableBossBar = config.getBoolean("disableWAILA", false);
if (!disableBossBar) {
players.add(event.getPlayer());
WAILAManager.createBossBar(event.getPlayer());
}
}
public static void removePlayer(Player player) {
Expand All @@ -95,7 +107,13 @@ public static void addPlayer(Player player) {
public static File getPrefFolder() {
return PREF_FOLDER;
}
public static YamlConfiguration getPlayerConfig(Player player) {
return YamlConfiguration.loadConfiguration(new File(PREF_FOLDER + "/" + player.getName() + ".yml"));
}
public static YamlConfiguration getConfig() {
return config;
}
public static void reloadConfig() {
config = ConfigUtils.loadConfig();
}
}
55 changes: 38 additions & 17 deletions src/main/java/me/darksoul/whatIsThat/WAILAManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.darksoul.whatIsThat;

import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
Expand All @@ -12,34 +13,54 @@
public class WAILAManager {
private static final Map<Player, BossBar> playerBossBars = new HashMap<>();

public static void createBossBar(Player player) {
if (playerBossBars.containsKey(player)) return;

BossBar bossBar = Bukkit.createBossBar("", BarColor.WHITE, BarStyle.SOLID);
bossBar.setVisible(false);
bossBar.addPlayer(player);
playerBossBars.put(player, bossBar);
}

public static void removeBossBar(Player player) {
BossBar bossBar = playerBossBars.remove(player);
if (bossBar != null) {
bossBar.removeAll();
public static void setBar(Player player, String type, String text) {
if ("bossbar".equalsIgnoreCase(type)) {
setBossBar(player, text);
} else if ("actionbar".equalsIgnoreCase(type)) {
setActionBar(player, text);
}
}

public static void updateBossBar(Player player, String text) {
private static void setBossBar(Player player, String text) {
BossBar bossBar = playerBossBars.get(player);
if (bossBar == null) {
return;
bossBar = Bukkit.createBossBar(text, BarColor.WHITE, BarStyle.SOLID);
bossBar.addPlayer(player);
playerBossBars.put(player, bossBar);
}
;

if (text == null || text.isEmpty()) {
bossBar.setVisible(false);
} else {
} else if (!text.equals(bossBar.getTitle())) {
bossBar.setTitle(text);
bossBar.setVisible(true);
}
}

private static void setActionBar(Player player, String text) {
if (text == null || text.isEmpty()) {
player.sendActionBar(Component.text(""));
} else {
player.sendActionBar(Component.text(text));
}
}

public static void removeBar(Player player, String type) {
if ("bossbar".equalsIgnoreCase(type)) {
removeBossBar(player);
} else if ("actionbar".equalsIgnoreCase(type)) {
removeActionBar(player);
}
}

private static void removeBossBar(Player player) {
BossBar bossBar = playerBossBars.remove(player);
if (bossBar != null) {
bossBar.removeAll();
}
}

private static void removeActionBar(Player player) {
player.sendActionBar(Component.text(""));
}
}
27 changes: 25 additions & 2 deletions src/main/java/me/darksoul/whatIsThat/api/WITAPI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.darksoul.whatIsThat.api;

import me.darksoul.whatIsThat.Handlers;
import me.darksoul.whatIsThat.WAILAListener;
import me.darksoul.whatIsThat.WAILAManager;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
Expand Down Expand Up @@ -36,13 +37,35 @@ public static void addBlockHandler(BiFunction<Block, Player, Boolean> function)
Handlers.getBlockHandlers().add(function);
}

/**
* Removes a custom handler for blocks.
* The handler is a {@link BiFunction} that takes a {@link Block} and a {@link Player},
* and returns a {@code Boolean} to indicate whether the handling was successful.
*
* @param function the handler function to remove from processing blocks.
*/
public static void removeBlockHandler(BiFunction<Block, Player, Boolean> function) {
Handlers.removeBlockHandler(function);
}

/**
* Removes a custom handler for entities.
* The handler is a {@link BiFunction} that takes an {@link Entity} and a {@link Player},
* and returns a {@code Boolean} to indicate whether the handling was successful.
*
* @param function the handler function to remove from processing entities.
*/
public static void removeEntityHandler(BiFunction<Entity, Player, Boolean> function) {
Handlers.removeEntityHandler(function);
}

/**
* Updates the boss bar text displayed to a specific player.
*
* @param text the new text to display on the boss bar.
* @param player the {@link Player} for whom the boss bar text is updated.
*/
public static void updateBossbar(String text, Player player) {
WAILAManager.updateBossBar(player, text);
public static void updateBar(String text, Player player) {
WAILAManager.setBar(player, WAILAListener.getPlayerConfig(player).getString("type"), text);
}
}
54 changes: 46 additions & 8 deletions src/main/java/me/darksoul/whatIsThat/commands/WITCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class WITCommand implements CommandExecutor, TabCompleter {

private static final String COMMAND_HELP = "Usage: /wit [disable|enable]";
private static final String COMMAND_HELP = "Usage: /wit [type <bossbar|actionbar>|disable|enable]";

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Expand All @@ -37,6 +38,24 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
disableBossBar(player);
} else if ("enable".equalsIgnoreCase(args[0])) {
enableBossBar(player);
} else if ("type".equalsIgnoreCase(args[0])) {
if (args.length < 2 || Stream.of("bossbar", "actionbar").noneMatch(args[1]::equalsIgnoreCase)) {
sender.sendMessage(COMMAND_HELP);
return false;
}
if (args[1].equalsIgnoreCase("actionbar")) {
WAILAManager.removeBar(player, "bossbar");
} else {
WAILAManager.removeBar(player, "actionbar");
}
setType(player, args[1]);
} else if ("reload".equalsIgnoreCase(args[0])) {
if (player.hasPermission("wit.reload")) {
WAILAListener.reloadConfig();
sender.sendMessage("§2WIT config reloaded.");
} else {
sender.sendMessage("§cYou do not have permission to run this command.");
}
} else {
sender.sendMessage(COMMAND_HELP);
return false;
Expand All @@ -52,7 +71,7 @@ private void disableBossBar(Player player) {
playerFile.createNewFile();
}
YamlConfiguration config = YamlConfiguration.loadConfiguration(playerFile);
config.set("disableBossBar", true);
config.set("disableWAILA", true);
config.save(playerFile);
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -61,8 +80,23 @@ private void disableBossBar(Player player) {
}

WAILAListener.removePlayer(player);
WAILAManager.removeBossBar(player);
player.sendMessage("Boss bar disabled.");
WAILAManager.removeBar(player, "bossbar");
player.sendMessage("WAILA bar disabled.");
}

private void setType(Player player, String type) {
File playerFile = new File(WAILAListener.getPrefFolder() + "/" + player.getName() + ".yml");
try {
if (!playerFile.exists()) {
playerFile.createNewFile();
}
YamlConfiguration config = YamlConfiguration.loadConfiguration(playerFile);
config.set("type", type);
config.save(playerFile);
} catch (IOException e) {
e.printStackTrace();
player.sendMessage("An error occurred while saving your settings.");
}
}

private void enableBossBar(Player player) {
Expand All @@ -72,7 +106,7 @@ private void enableBossBar(Player player) {
playerFile.createNewFile();
}
YamlConfiguration config = YamlConfiguration.loadConfiguration(playerFile);
config.set("disableBossBar", false);
config.set("disableWAILA", false);
config.save(playerFile);
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -81,16 +115,20 @@ private void enableBossBar(Player player) {
}

WAILAListener.addPlayer(player);
WAILAManager.createBossBar(player);
player.sendMessage("Boss bar enabled.");
WAILAManager.setBar(player, "bossbar", "");
player.sendMessage("WAILA bar enabled.");
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
List<String> suggestions = new ArrayList<>();

if (args.length == 1) {
StringUtil.copyPartialMatches(args[0], List.of("disable", "enable"), suggestions);
StringUtil.copyPartialMatches(args[0], List.of("disable", "enable", "type"), suggestions);
} else if (args.length == 2) {
if (args[0].equalsIgnoreCase("type")) {
StringUtil.copyPartialMatches(args[1], List.of("bossbar", "actionbar"), suggestions);
}
}

return suggestions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.aurelium.auramobs.api.AuraMobsAPI;
import me.darksoul.whatIsThat.Information;
import me.darksoul.whatIsThat.WAILAListener;
import me.darksoul.whatIsThat.WAILAManager;
import me.darksoul.whatIsThat.WhatIsThat;
import me.darksoul.whatIsThat.misc.ConfigUtils;
Expand Down Expand Up @@ -42,7 +43,8 @@ public static boolean handleAuraMobs(Entity entity, Player player) {
info.append(ASEntityPInfo).append(" §f| ");
}
info.append(entityName).append(ASEntitySInfo);
WAILAManager.updateBossBar(player, info.toString());
WAILAManager.setBar(player, WAILAListener.getPlayerConfig(player).getString("type"),
info.toString());
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public static boolean handleEMEntity(Entity entity, Player player) {
if (!EMEntitySInfo.isEmpty()) {
info.append(" §f| ").append(EMEntitySInfo);
}
WAILAManager.updateBossBar(player, info.toString());
WAILAManager.setBar(player, WAILAListener.getPlayerConfig(player).getString("type"),
info.toString());
return true;
}
return false;
Expand Down
Loading

0 comments on commit 6a421d8

Please sign in to comment.