-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added saving of the warp sign cache to database.
Relates to #68
- Loading branch information
1 parent
ef523d4
commit 79b8c0c
Showing
8 changed files
with
179 additions
and
54 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,79 @@ | ||
package world.bentobox.warps; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.Material; | ||
import org.bukkit.World; | ||
|
||
import world.bentobox.bentobox.database.Database; | ||
import world.bentobox.warps.objects.SignCache; | ||
|
||
public class SignCacheManager { | ||
private Map<World, Map<UUID, SignCacheItem>> cachedSigns = new HashMap<>(); | ||
private Warp addon; | ||
// Database handler for level data | ||
private Database<SignCache> handler; | ||
|
||
public SignCacheManager(Warp addon) { | ||
this.addon = addon; | ||
handler = new Database<>(addon, SignCache.class); | ||
// Load the sign caches | ||
loadCache(); | ||
} | ||
|
||
private void loadCache() { | ||
handler.loadObjects().forEach(w -> { | ||
World world = Bukkit.getWorld(w.getUniqueId()); | ||
if (world != null) { | ||
cachedSigns.put(world, w.getSigns()); | ||
} | ||
}); | ||
} | ||
|
||
void saveCache() { | ||
cachedSigns.forEach((w, m) -> handler.saveObject(new SignCache(w, m))); | ||
} | ||
|
||
Material getSignIcon(World world, UUID warpOwner) { | ||
// Add the worlds if we haven't seen this before | ||
cachedSigns.putIfAbsent(world, new HashMap<>()); | ||
if (cachedSigns.get(world).containsKey(warpOwner)) { | ||
return cachedSigns.get(world).get(warpOwner).getType(); | ||
} | ||
// Not in cache | ||
SignCacheItem sc = addon.getWarpSignsManager().getSignInfo(world, warpOwner); | ||
cachedSigns.get(world).put(warpOwner, sc); | ||
return sc.getType(); | ||
} | ||
|
||
/** | ||
* Gets sign text and cache it | ||
* @param playerUUID | ||
* @return sign text in a list | ||
*/ | ||
List<String> getSign(World world, UUID playerUUID) { | ||
// Add the worlds if we haven't seen this before | ||
cachedSigns.putIfAbsent(world, new HashMap<>()); | ||
if (cachedSigns.get(world).containsKey(playerUUID)) { | ||
return cachedSigns.get(world).get(playerUUID).getSignText(); | ||
} | ||
SignCacheItem result = addon.getWarpSignsManager().getSignInfo(world, playerUUID); | ||
cachedSigns.get(world).put(playerUUID, result); | ||
return result.getSignText(); | ||
} | ||
|
||
/** | ||
* Removes sign text from the cache | ||
* @param world - world | ||
* @param key - uuid of owner | ||
*/ | ||
void removeWarp(World world, UUID key) { | ||
cachedSigns.putIfAbsent(world, new HashMap<>()); | ||
cachedSigns.get(world).remove(key); | ||
} | ||
|
||
} |
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
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,54 @@ | ||
package world.bentobox.warps.objects; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import org.bukkit.World; | ||
|
||
import com.google.gson.annotations.Expose; | ||
|
||
import world.bentobox.bentobox.database.objects.DataObject; | ||
import world.bentobox.warps.SignCacheItem; | ||
|
||
public class SignCache implements DataObject { | ||
|
||
@Expose | ||
private String uniqueId = ""; | ||
@Expose | ||
private Map<UUID, SignCacheItem> signs = new HashMap<>(); | ||
|
||
public SignCache() { | ||
// Required by YAML database | ||
} | ||
|
||
public SignCache(World w, Map<UUID, SignCacheItem> m) { | ||
this.uniqueId = w.getName(); | ||
this.signs = m; | ||
} | ||
|
||
@Override | ||
public String getUniqueId() { | ||
return uniqueId; | ||
} | ||
|
||
@Override | ||
public void setUniqueId(String uniqueId) { | ||
this.uniqueId = uniqueId; | ||
} | ||
|
||
/** | ||
* @return the signs | ||
*/ | ||
public Map<UUID, SignCacheItem> getSigns() { | ||
return signs; | ||
} | ||
|
||
/** | ||
* @param signs the signs to set | ||
*/ | ||
public void setSigns(Map<UUID, SignCacheItem> signs) { | ||
this.signs = signs; | ||
} | ||
|
||
} |
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