Skip to content

Commit

Permalink
Rename Toggles for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Jikoo committed Jul 28, 2024
1 parent 31539df commit 5d5fb92
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.errorprone.annotations.Keep;
import com.lishid.openinv.event.PlayerToggledEvent;
import com.lishid.openinv.util.setting.PlayerToggle;
import com.lishid.openinv.util.setting.Toggles;
import com.lishid.openinv.util.setting.PlayerToggles;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.EventHandler;
Expand Down Expand Up @@ -42,7 +42,7 @@ public void onEnable() {

// For each toggle, enable loaded players.
for (String toggleName : loaded.getKeys(false)) {
PlayerToggle toggle = Toggles.get(toggleName);
PlayerToggle toggle = PlayerToggles.get(toggleName);
// Ensure toggle exists.
if (toggle == null) {
continue;
Expand Down Expand Up @@ -120,7 +120,7 @@ private void onPlayerJoin(@NotNull PlayerJoinEvent event) {
}

for (String toggleName : toggleNames) {
PlayerToggle toggle = Toggles.get(toggleName);
PlayerToggle toggle = PlayerToggles.get(toggleName);
if (toggle != null) {
toggle.set(playerId, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public final class Toggles {
public final class PlayerToggles {

private static final Map<String, PlayerToggle> TOGGLES = new HashMap<>();
private static final PlayerToggle ANY = add(new MemoryToggle("anycontainer"));
Expand All @@ -27,12 +30,16 @@ public final class Toggles {
return TOGGLES.get(toggleName);
}

public static @UnmodifiableView @NotNull Collection<PlayerToggle> get() {
return Collections.unmodifiableCollection(TOGGLES.values());
}

private static @NotNull PlayerToggle add(@NotNull PlayerToggle toggle) {
TOGGLES.put(toggle.getName(), toggle);
return toggle;
}

private Toggles() {
private PlayerToggles() {
throw new IllegalStateException("Cannot create instance of utility class.");
}

Expand Down
11 changes: 6 additions & 5 deletions plugin/src/main/java/com/lishid/openinv/OpenInv.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
import com.lishid.openinv.util.InventoryManager;
import com.lishid.openinv.util.lang.LangMigrator;
import com.lishid.openinv.util.PlayerLoader;
import com.lishid.openinv.util.setting.Toggles;
import com.lishid.openinv.util.setting.PlayerToggle;
import com.lishid.openinv.util.setting.PlayerToggles;
import com.lishid.openinv.util.lang.LanguageManager;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
Expand Down Expand Up @@ -204,22 +205,22 @@ public boolean noArgsOpensSelf() {

@Override
public boolean getAnyContainerStatus(@NotNull final OfflinePlayer offline) {
return Toggles.any().is(offline.getUniqueId());
return PlayerToggles.any().is(offline.getUniqueId());
}

@Override
public void setAnyContainerStatus(@NotNull final OfflinePlayer offline, final boolean status) {
Toggles.any().set(offline.getUniqueId(), status);
PlayerToggles.any().set(offline.getUniqueId(), status);
}

@Override
public boolean getSilentContainerStatus(@NotNull final OfflinePlayer offline) {
return Toggles.silent().is(offline.getUniqueId());
return PlayerToggles.silent().is(offline.getUniqueId());
}

@Override
public void setSilentContainerStatus(@NotNull final OfflinePlayer offline, final boolean status) {
Toggles.silent().set(offline.getUniqueId(), status);
PlayerToggles.silent().set(offline.getUniqueId(), status);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.lishid.openinv.event.OpenEvents;
import com.lishid.openinv.util.setting.PlayerToggle;
import com.lishid.openinv.util.TabCompleter;
import com.lishid.openinv.util.setting.Toggles;
import com.lishid.openinv.util.setting.PlayerToggles;
import com.lishid.openinv.util.lang.LanguageManager;
import com.lishid.openinv.util.lang.Replacement;
import org.bukkit.command.Command;
Expand Down Expand Up @@ -49,7 +49,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return true;
}

PlayerToggle toggle = Toggles.get(command.getName());
PlayerToggle toggle = PlayerToggles.get(command.getName());

// Shouldn't be possible.
if (toggle == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.lishid.openinv.internal.ViewOnly;
import com.lishid.openinv.util.InternalAccessor;
import com.lishid.openinv.util.Permissions;
import com.lishid.openinv.util.setting.Toggles;
import com.lishid.openinv.util.setting.PlayerToggles;
import com.lishid.openinv.util.lang.LanguageManager;
import org.bukkit.GameMode;
import org.bukkit.entity.HumanEntity;
Expand Down Expand Up @@ -72,14 +72,14 @@ private void onPlayerInteract(@NotNull PlayerInteractEvent event) {

Player player = event.getPlayer();
UUID playerId = player.getUniqueId();
boolean any = Permissions.CONTAINER_ANY.hasPermission(player) && Toggles.any().is(playerId);
boolean any = Permissions.CONTAINER_ANY.hasPermission(player) && PlayerToggles.any().is(playerId);
boolean needsAny = accessor.getAnySilentContainer().isAnyContainerNeeded(event.getClickedBlock());

if (!any && needsAny) {
return;
}

boolean silent = Permissions.CONTAINER_SILENT.hasPermission(player) && Toggles.silent().is(playerId);
boolean silent = Permissions.CONTAINER_SILENT.hasPermission(player) && PlayerToggles.silent().is(playerId);

// If anycontainer or silentcontainer is active
if (any || silent) {
Expand All @@ -104,7 +104,7 @@ private void onInventoryClose(@NotNull final InventoryCloseEvent event) {
}

InventoryHolder holder = event.getInventory().getHolder();
if (Toggles.silent().is(player.getUniqueId())
if (PlayerToggles.silent().is(player.getUniqueId())
&& holder != null
&& this.accessor.getAnySilentContainer().isAnySilentContainer(holder)) {
this.accessor.getAnySilentContainer().deactivateContainer(player);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.lishid.openinv.listener;

import com.lishid.openinv.util.setting.Toggles;
import com.lishid.openinv.util.setting.PlayerToggles;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
Expand All @@ -13,8 +13,8 @@ public class ToggleListener implements Listener {
@EventHandler
private void onPlayerQuit(@NotNull PlayerQuitEvent event) {
UUID playerId = event.getPlayer().getUniqueId();
Toggles.any().set(playerId, false);
Toggles.silent().set(playerId, false);
PlayerToggles.any().set(playerId, false);
PlayerToggles.silent().set(playerId, false);
}

}

0 comments on commit 5d5fb92

Please sign in to comment.