forked from runescapejon/Minecraft-Backpack-Mod
-
Notifications
You must be signed in to change notification settings - Fork 12
Fix Backpack slot usage tooltip allways displaying 0/0 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
384b76d
Fix Backpack slot usage tooltip allways displaying 0/0
Worive 85ff094
Merge branch 'master' into fix-usage-tooltip
Dream-Master ca30f3c
Merge branch 'master' into fix-usage-tooltip
Worive dc40b41
Merge branch 'master' into fix-usage-tooltip
Worive d6f0222
Remove usage of isValid
Worive 588573b
Fix cacheLock and improper atomic operations
Worive d00e66a
Update docs
Worive 4af6067
Merge branch 'master' into fix-usage-tooltip
Dream-Master 190dffc
Fix server using clientOnly code
Worive 204a671
Merge branch 'master' into fix-usage-tooltip
Worive File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
190 changes: 190 additions & 0 deletions
190
src/main/java/de/eydamos/backpack/misc/BackpackUsageCache.java
This file contains hidden or 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,190 @@ | ||
| package de.eydamos.backpack.misc; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.locks.ReadWriteLock; | ||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
|
||
| import cpw.mods.fml.relauncher.Side; | ||
| import cpw.mods.fml.relauncher.SideOnly; | ||
| import de.eydamos.backpack.Backpack; | ||
| import de.eydamos.backpack.network.message.MessageBackpackInfoRequest; | ||
|
|
||
| @SideOnly(Side.CLIENT) | ||
| public class BackpackUsageCache { | ||
|
|
||
| private static final Map<String, CacheEntry> cache = new ConcurrentHashMap<>(); | ||
| private static final Map<String, Long> requestTimestamps = new ConcurrentHashMap<>(); | ||
|
|
||
| // Configuration constants | ||
| private static final long CACHE_DURATION_MS = 60000; // 1 minute | ||
| private static final long REQUEST_THROTTLE_MS = 1000; // 1 second throttle between requests | ||
| private static final int MAX_CACHE_SIZE = 100; // Prevent memory leaks | ||
|
|
||
| // Lock is only needed for the compound "put-and-evict" operation | ||
| private static final ReadWriteLock cacheLock = new ReentrantReadWriteLock(); | ||
|
|
||
| private static class CacheEntry { | ||
|
|
||
| public final BackpackSlotUsageInfo info; | ||
| public final long timestamp; | ||
|
|
||
| public CacheEntry(BackpackSlotUsageInfo info, long timestamp) { | ||
| this.info = info; | ||
| this.timestamp = timestamp; | ||
| } | ||
|
|
||
| public boolean isExpired() { | ||
| return (System.currentTimeMillis() - timestamp) > CACHE_DURATION_MS; | ||
| } | ||
| } | ||
|
|
||
| public static class BackpackSlotUsageInfo { | ||
|
|
||
| public final int usedSlots; | ||
| public final int totalSlots; | ||
|
|
||
| public BackpackSlotUsageInfo(int used, int total) { | ||
| if (used < 0 || total < 0 || used > total) { | ||
| throw new IllegalArgumentException("Invalid backpack slot values: used=" + used + ", total=" + total); | ||
| } | ||
| this.usedSlots = used; | ||
| this.totalSlots = total; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) return true; | ||
| if (obj == null || getClass() != obj.getClass()) return false; | ||
| BackpackSlotUsageInfo that = (BackpackSlotUsageInfo) obj; | ||
| return usedSlots == that.usedSlots && totalSlots == that.totalSlots; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return 31 * usedSlots + totalSlots; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "BackpackInfo{used=" + usedSlots + ", total=" + totalSlots + "}"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Updates backpack information in the cache | ||
| * | ||
| * @param uuid The backpack UUID | ||
| * @param used Number of used slots | ||
| * @param total Total number of slots | ||
| */ | ||
| public static void updateBackpackInfo(String uuid, int used, int total) { | ||
| if (uuid == null || uuid.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("UUID cannot be null or empty"); | ||
| } | ||
|
|
||
| BackpackSlotUsageInfo info = new BackpackSlotUsageInfo(used, total); | ||
| long currentTime = System.currentTimeMillis(); | ||
| CacheEntry entry = new CacheEntry(info, currentTime); | ||
|
|
||
| // A write lock is required here to make the "put-and-evict" operation atomic. | ||
| cacheLock.writeLock().lock(); | ||
| try { | ||
| cache.put(uuid, entry); | ||
|
|
||
| if (cache.size() > MAX_CACHE_SIZE) { | ||
| evictOldestEntries(); | ||
| } | ||
| } finally { | ||
| cacheLock.writeLock().unlock(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves backpack information from cache. If an entry is found to be expired, it is removed | ||
| * | ||
| * @param uuid The backpack UUID | ||
| * @return BackpackSlotUsageInfo if valid and not expired, null otherwise | ||
| */ | ||
| public static BackpackSlotUsageInfo getBackpackInfo(String uuid) { | ||
| if (uuid == null || uuid.trim().isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| CacheEntry entry = cache.get(uuid); | ||
|
|
||
| if (entry == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (entry.isExpired()) { | ||
| invalidate(uuid); | ||
| return null; | ||
| } | ||
|
|
||
| return entry.info; | ||
| } | ||
|
|
||
| /** | ||
| * Requests backpack information from the server if not present in the cache, respecting a throttle to prevent | ||
| * spamming requests. | ||
| * | ||
| * @param uuid The player UUID | ||
| */ | ||
| public static void requestBackpackInfo(String uuid) { | ||
| if (uuid == null || uuid.trim().isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| if (getBackpackInfo(uuid) != null) { | ||
| return; | ||
| } | ||
|
|
||
| long currentTime = System.currentTimeMillis(); | ||
|
|
||
| requestTimestamps.compute(uuid, (key, lastRequestTime) -> { | ||
| // Throttle check | ||
| if (lastRequestTime != null && (currentTime - lastRequestTime) < REQUEST_THROTTLE_MS) { | ||
| return lastRequestTime; | ||
| } | ||
|
|
||
| Backpack.packetHandler.networkWrapper.sendToServer(new MessageBackpackInfoRequest(key)); | ||
| return currentTime; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Invalidates a specific UUID from the cache | ||
| * | ||
| * @param uuid The player UUID to invalidate | ||
| */ | ||
| public static void invalidate(String uuid) { | ||
| if (uuid == null || uuid.trim().isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| cache.remove(uuid); | ||
| requestTimestamps.remove(uuid); | ||
| } | ||
|
|
||
| /** | ||
| * Evicts the oldest entries when cache size exceeds the limit. WARNING: This method must be called from within a | ||
| * write-locked context. | ||
| */ | ||
| private static void evictOldestEntries() { | ||
| // This check is a safeguard, but the caller should ensure the lock is held. | ||
| if (cache.size() <= MAX_CACHE_SIZE) { | ||
| return; | ||
| } | ||
|
|
||
| // Find the oldest entries to evict (evict 10% of cache) | ||
| int entriesToEvict = Math.max(1, cache.size() / 10); | ||
|
|
||
| cache.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.comparingLong(e -> e.timestamp))) | ||
| .limit(entriesToEvict).map(Map.Entry::getKey).forEach(key -> { | ||
| cache.remove(key); | ||
| requestTimestamps.remove(key); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or 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
48 changes: 48 additions & 0 deletions
48
src/main/java/de/eydamos/backpack/network/message/MessageBackpackInfo.java
This file contains hidden or 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,48 @@ | ||
| package de.eydamos.backpack.network.message; | ||
|
|
||
| import cpw.mods.fml.common.network.simpleimpl.IMessage; | ||
| import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; | ||
| import cpw.mods.fml.common.network.simpleimpl.MessageContext; | ||
| import de.eydamos.backpack.misc.BackpackUsageCache; | ||
| import io.netty.buffer.ByteBuf; | ||
|
|
||
| /** | ||
| * Used to pass to the client the slot usage information for a specific backpack. | ||
| */ | ||
| public class MessageBackpackInfo implements IMessage, IMessageHandler<MessageBackpackInfo, IMessage> { | ||
|
|
||
| private String backpackUUID; | ||
| private int usedSlots; | ||
| private int totalSlots; | ||
|
|
||
| public MessageBackpackInfo() {} | ||
|
|
||
| public MessageBackpackInfo(String uuid, int used, int total) { | ||
| this.backpackUUID = uuid; | ||
| this.usedSlots = used; | ||
| this.totalSlots = total; | ||
| } | ||
|
|
||
| @Override | ||
| public void fromBytes(ByteBuf buf) { | ||
| int length = buf.readInt(); | ||
| this.backpackUUID = new String(buf.readBytes(length).array()); | ||
| this.usedSlots = buf.readInt(); | ||
| this.totalSlots = buf.readInt(); | ||
| } | ||
|
|
||
| @Override | ||
| public void toBytes(ByteBuf buf) { | ||
| buf.writeInt(backpackUUID.length()); | ||
| buf.writeBytes(backpackUUID.getBytes()); | ||
| buf.writeInt(usedSlots); | ||
| buf.writeInt(totalSlots); | ||
| } | ||
|
|
||
| @Override | ||
| public IMessage onMessage(MessageBackpackInfo message, MessageContext ctx) { | ||
| BackpackUsageCache.updateBackpackInfo(message.backpackUUID, message.usedSlots, message.totalSlots); | ||
| return null; | ||
| } | ||
|
|
||
| } |
49 changes: 49 additions & 0 deletions
49
src/main/java/de/eydamos/backpack/network/message/MessageBackpackInfoRequest.java
This file contains hidden or 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,49 @@ | ||
| package de.eydamos.backpack.network.message; | ||
|
|
||
| import net.minecraft.nbt.NBTTagCompound; | ||
|
|
||
| import cpw.mods.fml.common.network.simpleimpl.IMessage; | ||
| import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; | ||
| import cpw.mods.fml.common.network.simpleimpl.MessageContext; | ||
| import de.eydamos.backpack.Backpack; | ||
| import de.eydamos.backpack.misc.Constants; | ||
| import de.eydamos.backpack.saves.BackpackSave; | ||
| import io.netty.buffer.ByteBuf; | ||
|
|
||
| /** | ||
| * Request from the client to ask the server to send him the slot usage information about a backpack. | ||
| */ | ||
| public class MessageBackpackInfoRequest implements IMessage, IMessageHandler<MessageBackpackInfoRequest, IMessage> { | ||
|
|
||
| private String backpackUUID; | ||
|
|
||
| public MessageBackpackInfoRequest() {} | ||
|
|
||
| public MessageBackpackInfoRequest(String uuid) { | ||
| this.backpackUUID = uuid; | ||
| } | ||
|
|
||
| @Override | ||
| public void fromBytes(ByteBuf buf) { | ||
| int length = buf.readInt(); | ||
| this.backpackUUID = new String(buf.readBytes(length).array()); | ||
| } | ||
|
|
||
| @Override | ||
| public void toBytes(ByteBuf buf) { | ||
| buf.writeInt(backpackUUID.length()); | ||
| buf.writeBytes(backpackUUID.getBytes()); | ||
| } | ||
|
|
||
| @Override | ||
| public IMessage onMessage(MessageBackpackInfoRequest message, MessageContext ctx) { | ||
| NBTTagCompound backpack = Backpack.saveFileHandler.loadBackpack(message.backpackUUID); | ||
|
|
||
| BackpackSave backpackSave = new BackpackSave(backpack); | ||
|
|
||
| int used = backpackSave.getInventory(Constants.NBT.INVENTORY_BACKPACK).tagCount(); | ||
| int total = backpackSave.getSize(); | ||
|
|
||
| return new MessageBackpackInfo(message.backpackUUID, used, total); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.