forked from lishid/OpenInv
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The addon OITogglePersist restores old behavior.
- Loading branch information
Showing
25 changed files
with
608 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<artifactId>openinvparent</artifactId> | ||
<groupId>com.lishid</groupId> | ||
<relativePath>../../pom.xml</relativePath> | ||
<version>5.1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>openinvtogglepersist</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<artifactId>annotations</artifactId> | ||
<groupId>org.jetbrains</groupId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.spigotmc</groupId> | ||
<artifactId>spigot-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<artifactId>openinvapi</artifactId> | ||
<groupId>com.lishid</groupId> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>OITogglePersist</finalName> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
|
||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-resources-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>copy-final-jar</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>copy-resources</goal> | ||
</goals> | ||
<configuration> | ||
<outputDirectory>${project.parent.build.directory}</outputDirectory> | ||
<resources> | ||
<resource> | ||
<directory>${project.build.directory}</directory> | ||
<includes> | ||
<include>${project.build.finalName}.jar</include> | ||
</includes> | ||
</resource> | ||
</resources> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
|
||
</build> | ||
|
||
</project> |
143 changes: 143 additions & 0 deletions
143
addon/togglepersist/src/main/java/com/github/jikoo/openinv/togglepersist/TogglePersist.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package com.github.jikoo.openinv.togglepersist; | ||
|
||
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.PlayerToggles; | ||
import org.bukkit.configuration.Configuration; | ||
import org.bukkit.configuration.file.YamlConfiguration; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.logging.Level; | ||
|
||
public class TogglePersist extends JavaPlugin implements Listener { | ||
|
||
private final Map<UUID, Set<String>> enabledToggles = new HashMap<>(); | ||
|
||
@Override | ||
public void onEnable() { | ||
getServer().getPluginManager().registerEvents(this, this); | ||
|
||
File file = new File(getDataFolder(), "toggles.yml"); | ||
|
||
// If there's no save file, there's nothing to load. | ||
if (!file.exists()) { | ||
return; | ||
} | ||
|
||
Configuration loaded = YamlConfiguration.loadConfiguration(file); | ||
|
||
// For each toggle, enable loaded players. | ||
for (String toggleName : loaded.getKeys(false)) { | ||
PlayerToggle toggle = PlayerToggles.get(toggleName); | ||
// Ensure toggle exists. | ||
if (toggle == null) { | ||
continue; | ||
} | ||
|
||
for (String idString : loaded.getStringList(toggleName)) { | ||
// Ensure valid UUID. | ||
UUID uuid; | ||
try { | ||
uuid = UUID.fromString(idString); | ||
} catch (IllegalArgumentException e) { | ||
continue; | ||
} | ||
|
||
// Track that toggle is enabled. | ||
set(uuid, toggleName); | ||
} | ||
} | ||
} | ||
|
||
private void set(UUID playerId, String toggleName) { | ||
enabledToggles.compute(playerId, (uuid, toggles) -> { | ||
if (toggles == null) { | ||
toggles = new HashSet<>(); | ||
} | ||
toggles.add(toggleName); | ||
return toggles; | ||
}); | ||
} | ||
|
||
@Override | ||
public void onDisable() { | ||
Map<String, List<String>> converted = getSaveData(); | ||
|
||
YamlConfiguration data = new YamlConfiguration(); | ||
for (Map.Entry<String, List<String>> playerToggle : converted.entrySet()) { | ||
data.set(playerToggle.getKey(), playerToggle.getValue()); | ||
} | ||
|
||
File file = new File(getDataFolder(), "toggles.yml"); | ||
try { | ||
data.save(file); | ||
} catch (IOException e) { | ||
getLogger().log(Level.SEVERE, "Unable to save player toggle states", e); | ||
} | ||
} | ||
|
||
private @NotNull Map<String, List<String>> getSaveData() { | ||
Map<String, List<String>> converted = new HashMap<>(); | ||
|
||
for (Map.Entry<UUID, Set<String>> playerToggles : enabledToggles.entrySet()) { | ||
String idString = playerToggles.getKey().toString(); | ||
for (String toggleName : playerToggles.getValue()) { | ||
// Add player ID to listing for each enabled toggle. | ||
converted.compute(toggleName, (name, ids) -> { | ||
if (ids == null) { | ||
ids = new ArrayList<>(); | ||
} | ||
ids.add(idString); | ||
return ids; | ||
}); | ||
} | ||
} | ||
return converted; | ||
} | ||
|
||
@Keep | ||
@EventHandler | ||
private void onPlayerJoin(@NotNull PlayerJoinEvent event) { | ||
UUID playerId = event.getPlayer().getUniqueId(); | ||
Set<String> toggleNames = enabledToggles.get(playerId); | ||
|
||
if (toggleNames == null) { | ||
return; | ||
} | ||
|
||
for (String toggleName : toggleNames) { | ||
PlayerToggle toggle = PlayerToggles.get(toggleName); | ||
if (toggle != null) { | ||
toggle.set(playerId, true); | ||
} | ||
} | ||
} | ||
|
||
@Keep | ||
@EventHandler | ||
private void onToggleSet(@NotNull PlayerToggledEvent event) { | ||
if (event.isEnabled()) { | ||
set(event.getPlayerId(), event.getToggle().getName()); | ||
} else { | ||
enabledToggles.computeIfPresent(event.getPlayerId(), (uuid, toggles) -> { | ||
toggles.remove(event.getToggle().getName()); | ||
return toggles.isEmpty() ? null : toggles; | ||
}); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name: OITogglePersist | ||
main: com.github.jikoo.openinv.togglepersist.TogglePersist | ||
version: ${project.version} | ||
author: Jikoo | ||
description: An OpenInv addon allowing /anycontainer and /silentcontainer to persist across sessions. | ||
depend: [ OpenInv ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
api/src/main/java/com/lishid/openinv/event/PlayerToggledEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.lishid.openinv.event; | ||
|
||
import com.google.errorprone.annotations.RestrictedApi; | ||
import com.lishid.openinv.util.setting.PlayerToggle; | ||
import org.bukkit.event.Event; | ||
import org.bukkit.event.HandlerList; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* Event fired after OpenInv modifies a toggleable setting for a player. | ||
*/ | ||
public class PlayerToggledEvent extends Event { | ||
|
||
private static final HandlerList HANDLERS = new HandlerList(); | ||
|
||
private final @NotNull PlayerToggle toggle; | ||
private final @NotNull UUID uuid; | ||
private final boolean enabled; | ||
|
||
@RestrictedApi( | ||
explanation = "Constructor is not considered part of the API and may be subject to change.", | ||
link = "", | ||
allowedOnPath = ".*/com/lishid/openinv/event/OpenEvents.java") | ||
@ApiStatus.Internal | ||
PlayerToggledEvent(@NotNull PlayerToggle toggle, @NotNull UUID uuid, boolean enabled) { | ||
this.toggle = toggle; | ||
this.uuid = uuid; | ||
this.enabled = enabled; | ||
} | ||
|
||
/** | ||
* Get the {@link PlayerToggle} affected. | ||
* | ||
* @return the toggle | ||
*/ | ||
public @NotNull PlayerToggle getToggle() { | ||
return toggle; | ||
} | ||
|
||
/** | ||
* Get the {@link UUID} of the player whose setting was changed. | ||
* | ||
* @return the player ID | ||
*/ | ||
public @NotNull UUID getPlayerId() { | ||
return uuid; | ||
} | ||
|
||
/** | ||
* Get whether the toggle is enabled. | ||
* | ||
* @return true if the toggle is enabled | ||
*/ | ||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public HandlerList getHandlers() { | ||
return HANDLERS; | ||
} | ||
|
||
public static HandlerList getHandlerList() { | ||
return HANDLERS; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
api/src/main/java/com/lishid/openinv/util/setting/PlayerToggle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.lishid.openinv.util.setting; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* A per-player setting that may be enabled or disabled. | ||
*/ | ||
public interface PlayerToggle { | ||
|
||
/** | ||
* Get the name of the setting. | ||
* | ||
* @return the setting name | ||
*/ | ||
@NotNull String getName(); | ||
|
||
/** | ||
* Get the state of the toggle for a particular player ID. | ||
* | ||
* @param uuid the player ID | ||
* @return true if the setting is enabled | ||
*/ | ||
boolean is(@NotNull UUID uuid); | ||
|
||
/** | ||
* Set the state of the toggle for a particular player ID. | ||
* | ||
* @param uuid the player ID | ||
* @param enabled whether the setting is enabled | ||
* @return true if the setting changed as a result of being set | ||
*/ | ||
boolean set(@NotNull UUID uuid, boolean enabled); | ||
|
||
} |
Oops, something went wrong.