Skip to content

Commit

Permalink
feat: add option to hide players on join by default
Browse files Browse the repository at this point in the history
Signed-off-by: Cristóbal Veas <[email protected]>
  • Loading branch information
zetastormy committed Dec 21, 2024
1 parent a771148 commit a798241
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ public void onEnable() {
}

if (hiderSection.getBoolean("enabled")) {
ItemStack item = ItemStackBuilder.getItemStack(hiderSection.getConfigurationSection("not_hidden")).build();
boolean playersHidden = config.getBoolean("join_settings.players_hidden", false);
ItemStack item;

if (playersHidden) {
item = ItemStackBuilder.getItemStack(hiderSection.getConfigurationSection("hidden")).build();
} else {
item = ItemStackBuilder.getItemStack(hiderSection.getConfigurationSection("not_hidden")).build();
}

PlayerHider playerHider = new PlayerHider(this, item, hiderSection.getInt("slot"), "PLAYER_HIDER");

playerHider.setAllowMovement(hiderSection.getBoolean("disable_inventory_movement"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,31 @@
import java.util.UUID;

public class PlayerHider extends HotbarItem {
private final int cooldown;
private final ItemStack hiddenItem;
private final List<UUID> hidden;
private final ItemStack hiddenItem;
private final ItemStack notHiddenItem;
private final boolean playersHidden;
private final int cooldown;

public PlayerHider(HotbarManager hotbarManager, ItemStack item, int slot, String keyValue) {
super(hotbarManager, item, slot, keyValue);
hidden = new ArrayList<>();

FileConfiguration config = getHotbarManager().getConfig(ConfigType.SETTINGS);

ItemStack hiddenItem = ItemStackBuilder.getItemStack(config.getConfigurationSection("player_hider.hidden")).build();
ItemMeta hiddenItemMeta = hiddenItem.getItemMeta();
PersistentDataContainer hiddenItemContainer = hiddenItemMeta.getPersistentDataContainer();
ItemStack notHiddenItem = ItemStackBuilder.getItemStack(config.getConfigurationSection("player_hider.not_hidden")).build();

hiddenItemContainer.set(NamespacedKey.minecraft("hotbar-item"), PersistentDataType.STRING, keyValue);
hiddenItem.setItemMeta(hiddenItemMeta);
ItemMeta playerHiderMeta = hiddenItem.getItemMeta();
PersistentDataContainer playerHiderContainer = playerHiderMeta.getPersistentDataContainer();
playerHiderContainer.set(NamespacedKey.minecraft("hotbar-item"), PersistentDataType.STRING, keyValue);

hiddenItem.setItemMeta(playerHiderMeta);
notHiddenItem.setItemMeta(playerHiderMeta);

this.hiddenItem = hiddenItem;
this.notHiddenItem = notHiddenItem;
playersHidden = config.getBoolean("join_settings.players_hidden");
cooldown = config.getInt("player_hider.cooldown");
}

Expand Down Expand Up @@ -91,7 +99,7 @@ protected void onInteract(Player player) {
hidden.remove(player.getUniqueId());
Message.PLAYER_HIDER_SHOWN.sendFrom(player);

player.getInventory().setItem(getSlot(), getItem());
player.getInventory().setItem(getSlot(), notHiddenItem);
}
}

Expand Down Expand Up @@ -121,6 +129,14 @@ public void onPlayerJoin(PlayerJoinEvent event) {

player.hidePlayer(playerToHide);
});

if (playersHidden) {
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
playerToHide.hidePlayer(player);
}

hidden.add(playerToHide.getUniqueId());
}
}

@SuppressWarnings("deprecation")
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ join_events:
- "[EFFECT] SPEED;1"

join_settings:
# Decide whether players will be hidden on join or not
players_hidden: false
# Should we teleport the player to the spawn point (if set) on join?
spawn_join: true
# Should we heal the player?
Expand Down

0 comments on commit a798241

Please sign in to comment.