Changed cache to a WeakMap and fixed bug where all packets were marked as ignored
package dev.fiki.forgehax.asm.events.packet;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
import dev.fiki.forgehax.api.event.Event;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.minecraft.network.IPacket;
import net.minecraft.network.NetworkManager;
-import java.util.concurrent.TimeUnit;
+import java.util.WeakHashMap;
@Getter
@AllArgsConstructor
public class PacketEvent extends Event {
- private static final LoadingCache<IPacket<?>, Boolean> CACHE = CacheBuilder.newBuilder()
- .expireAfterWrite(15L, TimeUnit.SECONDS)
- .build(new CacheLoader<IPacket<?>, Boolean>() {
- @Override
- public Boolean load(IPacket<?> key) throws Exception {
- return false;
- }
- });
+ private static final WeakHashMap<IPacket<?>, Boolean> CACHE = new WeakHashMap<>();
public static void ignore(IPacket<?> packet) {
- CACHE.put(packet, true);
+ CACHE.put(packet, Boolean.TRUE);
}
public static void remove(IPacket<?> packet) {
- CACHE.invalidate(packet);
+ CACHE.remove(packet);
}
public static boolean isIgnored(IPacket<?> packet) {
- return CACHE.getUnchecked(packet) != null;
+ return CACHE.getOrDefault(packet, Boolean.FALSE);
}
private final NetworkManager manager;
