fix bug
This commit is contained in:
parent
294a5ad7c0
commit
310938774d
5 changed files with 272 additions and 45 deletions
|
@ -5,9 +5,10 @@
|
|||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/APIConfig.java" beforeDir="false" afterPath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/APIConfig.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/utils/ConcurrentWeakIdentityHashMap.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/InternalRegistryListener.java" beforeDir="false" afterPath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/InternalRegistryListener.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java" beforeDir="false" afterPath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
|
@ -88,39 +89,39 @@
|
|||
<option name="showExcludedFiles" value="false" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent"><![CDATA[{
|
||||
"keyToString": {
|
||||
"Downloaded.Files.Path.Enabled": "false",
|
||||
"Gradle.Build EntityLib.executor": "Run",
|
||||
"Gradle.EntityLib [dependencies].executor": "Run",
|
||||
"Gradle.EntityLib [publish].executor": "Run",
|
||||
"Gradle.EntityLib:code-gen [:code-gen:Main.main()].executor": "Run",
|
||||
"Gradle.EntityLib:test-plugin [publish].executor": "Run",
|
||||
"Gradle.EntityLib:test-plugin [runServer].executor": "Run",
|
||||
"Gradle.EntityLib:test-plugin [shadowJar].executor": "Run",
|
||||
"Repository.Attach.Annotations": "false",
|
||||
"Repository.Attach.JavaDocs": "false",
|
||||
"Repository.Attach.Sources": "false",
|
||||
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"WebServerToolWindowFactoryState": "false",
|
||||
"git-widget-placeholder": "master",
|
||||
"ignore.virus.scanning.warn.message": "true",
|
||||
"jdk.selected.JAVA_MODULE": "corretto-17",
|
||||
"kotlin-language-version-configured": "true",
|
||||
"last_opened_file_path": "D:/Github/EntityLib/api/src/main/java/me/tofaa/entitylib/extras",
|
||||
"node.js.detected.package.eslint": "true",
|
||||
"node.js.detected.package.tslint": "true",
|
||||
"node.js.selected.package.eslint": "(autodetect)",
|
||||
"node.js.selected.package.tslint": "(autodetect)",
|
||||
"nodejs_package_manager_path": "npm",
|
||||
"project.structure.last.edited": "Project",
|
||||
"project.structure.proportion": "0.15",
|
||||
"project.structure.side.proportion": "0.2",
|
||||
"settings.editor.selected.configurable": "preferences.pluginManager",
|
||||
"vue.rearranger.settings.migration": "true"
|
||||
<component name="PropertiesComponent">{
|
||||
"keyToString": {
|
||||
"Downloaded.Files.Path.Enabled": "false",
|
||||
"Gradle.Build EntityLib.executor": "Run",
|
||||
"Gradle.EntityLib [dependencies].executor": "Run",
|
||||
"Gradle.EntityLib [publish].executor": "Run",
|
||||
"Gradle.EntityLib:code-gen [:code-gen:Main.main()].executor": "Run",
|
||||
"Gradle.EntityLib:test-plugin [publish].executor": "Run",
|
||||
"Gradle.EntityLib:test-plugin [runServer].executor": "Run",
|
||||
"Gradle.EntityLib:test-plugin [shadowJar].executor": "Run",
|
||||
"Repository.Attach.Annotations": "false",
|
||||
"Repository.Attach.JavaDocs": "false",
|
||||
"Repository.Attach.Sources": "false",
|
||||
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"WebServerToolWindowFactoryState": "false",
|
||||
"git-widget-placeholder": "master",
|
||||
"ignore.virus.scanning.warn.message": "true",
|
||||
"jdk.selected.JAVA_MODULE": "corretto-17",
|
||||
"kotlin-language-version-configured": "true",
|
||||
"last_opened_file_path": "D:/Github/EntityLib/api/src/main/java/me/tofaa/entitylib/extras",
|
||||
"node.js.detected.package.eslint": "true",
|
||||
"node.js.detected.package.tslint": "true",
|
||||
"node.js.selected.package.eslint": "(autodetect)",
|
||||
"node.js.selected.package.tslint": "(autodetect)",
|
||||
"nodejs_package_manager_path": "npm",
|
||||
"project.structure.last.edited": "Project",
|
||||
"project.structure.proportion": "0.15",
|
||||
"project.structure.side.proportion": "0.2",
|
||||
"settings.editor.selected.configurable": "preferences.pluginManager",
|
||||
"vue.rearranger.settings.migration": "true"
|
||||
}
|
||||
}]]></component>
|
||||
}</component>
|
||||
<component name="RecentsManager">
|
||||
<key name="CopyFile.RECENT_KEYS">
|
||||
<recent name="D:\Github\EntityLib\api\src\main\java\me\tofaa\entitylib\extras" />
|
||||
|
@ -304,7 +305,7 @@
|
|||
<workItem from="1708265094998" duration="204000" />
|
||||
<workItem from="1708265345702" duration="358000" />
|
||||
<workItem from="1708343107313" duration="8276000" />
|
||||
<workItem from="1708352150286" duration="1849000" />
|
||||
<workItem from="1708352150286" duration="3592000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
|
|
|
@ -35,7 +35,7 @@ public class Metadata {
|
|||
this.metadataMap.put(index, entry);
|
||||
|
||||
final WrapperEntity entity = EntityLib.getApi().getEntity(entityId);
|
||||
if (entity == null || entity.isSpawned()) return; // Not EntityLib entity then, the user must send the packet manually. OR not spawned.
|
||||
if (entity == null || !entity.isSpawned()) return; // Not EntityLib entity then, the user must send the packet manually. OR not spawned.
|
||||
if (!this.notifyAboutChanges) {
|
||||
synchronized (this.notNotifiedChanges) {
|
||||
this.notNotifiedChanges.put(index, entry);
|
||||
|
|
|
@ -0,0 +1,232 @@
|
|||
package me.tofaa.entitylib.utils;
|
||||
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.util.AbstractMap.SimpleEntry;
|
||||
import java.util.AbstractSet;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/**
|
||||
* @author Alex Snaps
|
||||
*/
|
||||
public class ConcurrentWeakIdentityHashMap<K, V> implements ConcurrentMap<K, V> {
|
||||
|
||||
private final ConcurrentMap<WeakReference<K>, V> map = new ConcurrentHashMap<WeakReference<K>, V>();
|
||||
private final ReferenceQueue<K> queue = new ReferenceQueue<K>();
|
||||
|
||||
@Override
|
||||
public V putIfAbsent(final K key, final V value) {
|
||||
purgeKeys();
|
||||
return map.putIfAbsent(newKey(key), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(final Object key, final Object value) {
|
||||
purgeKeys();
|
||||
return map.remove(new WeakReference<Object>( key, null), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean replace(final K key, final V oldValue, final V newValue) {
|
||||
purgeKeys();
|
||||
return map.replace(newKey(key), oldValue, newValue);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V replace(final K key, final V value) {
|
||||
purgeKeys();
|
||||
return map.replace(newKey(key), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
purgeKeys();
|
||||
return map.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
purgeKeys();
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(final Object key) {
|
||||
purgeKeys();
|
||||
return map.containsKey(new WeakReference<Object>(key, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(final Object value) {
|
||||
purgeKeys();
|
||||
return map.containsValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(final Object key) {
|
||||
purgeKeys();
|
||||
return map.get(new WeakReference<Object>(key, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(final K key, final V value) {
|
||||
purgeKeys();
|
||||
return map.put(newKey(key), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(final Object key) {
|
||||
purgeKeys();
|
||||
return map.remove(new WeakReference<Object>(key, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(final Map<? extends K, ? extends V> m) {
|
||||
purgeKeys();
|
||||
for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
|
||||
map.put(newKey(entry.getKey()), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
purgeKeys();
|
||||
map.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<K> keySet() {
|
||||
return new AbstractSet<K>() {
|
||||
@Override
|
||||
public Iterator<K> iterator() {
|
||||
purgeKeys();
|
||||
return new WeakSafeIterator<K, WeakReference<K>>(map.keySet().iterator()) {
|
||||
@Override
|
||||
protected K extract(WeakReference<K> u) {
|
||||
return u.get();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return ConcurrentWeakIdentityHashMap.this.containsKey(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> values() {
|
||||
purgeKeys();
|
||||
return map.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<K, V>> entrySet() {
|
||||
return new AbstractSet<Entry<K, V>>() {
|
||||
@Override
|
||||
public Iterator<Entry<K, V>> iterator() {
|
||||
purgeKeys();
|
||||
return new WeakSafeIterator<Entry<K, V>, Entry<WeakReference<K>, V>>(map.entrySet().iterator()) {
|
||||
@Override
|
||||
protected Entry<K, V> extract(Entry<WeakReference<K>, V> u) {
|
||||
K key = u.getKey().get();
|
||||
if (key == null) {
|
||||
return null;
|
||||
} else {
|
||||
return new SimpleEntry<K, V>(key, u.getValue());
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void purgeKeys() {
|
||||
Reference<? extends K> reference;
|
||||
while ((reference = queue.poll()) != null) {
|
||||
map.remove(reference);
|
||||
}
|
||||
}
|
||||
|
||||
private WeakReference<K> newKey(final K key) {
|
||||
return new WeakReference<K>(key, queue);
|
||||
}
|
||||
|
||||
private static class WeakReference<T> extends java.lang.ref.WeakReference<T> {
|
||||
|
||||
private final int hashCode;
|
||||
|
||||
private WeakReference(final T referent, final ReferenceQueue<? super T> q) {
|
||||
super(referent, q);
|
||||
hashCode = referent.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
return obj != null && obj.getClass() == this.getClass() && (this == obj || this.get() == ((WeakReference)obj).get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
private static abstract class WeakSafeIterator<T, U> implements Iterator<T> {
|
||||
|
||||
private final Iterator<U> weakIterator;
|
||||
protected T strongNext;
|
||||
|
||||
public WeakSafeIterator(Iterator<U> weakIterator) {
|
||||
this.weakIterator = weakIterator;
|
||||
advance();
|
||||
}
|
||||
|
||||
private void advance() {
|
||||
while (weakIterator.hasNext()) {
|
||||
U nextU = weakIterator.next();
|
||||
if ((strongNext = extract(nextU)) != null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
strongNext = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return strongNext != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final T next() {
|
||||
T next = strongNext;
|
||||
advance();
|
||||
return next;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
protected abstract T extract(U u);
|
||||
}
|
||||
}
|
|
@ -97,7 +97,7 @@ final class InternalRegistryListener extends PacketListenerAbstract implements L
|
|||
public void onEntitySpawn(EntitySpawnEvent event) {
|
||||
Entity e = event.getEntity();
|
||||
platform.getPlatformEntities().put(e.getEntityId(), e);
|
||||
System.out.println("Entity spawned: " + e.getEntityId() + " " + e.getType() + " " + e.getLocation());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -3,26 +3,20 @@ package me.tofaa.entitylib.spigot;
|
|||
import me.tofaa.entitylib.APIConfig;
|
||||
import me.tofaa.entitylib.TrackedEntity;
|
||||
import me.tofaa.entitylib.common.AbstractPlatform;
|
||||
import org.bukkit.Bukkit;
|
||||
import me.tofaa.entitylib.utils.ConcurrentWeakIdentityHashMap;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntitySpawnEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SpigotEntityLibPlatform extends AbstractPlatform<JavaPlugin> {
|
||||
|
||||
private SpigotEntityLibAPI api;
|
||||
private Map<Integer, Entity> platformEntities = Collections.synchronizedMap(new WeakHashMap<>()); // Silly
|
||||
private Map<Integer, Entity> platformEntities = new ConcurrentWeakIdentityHashMap<>();
|
||||
|
||||
public SpigotEntityLibPlatform(@NotNull JavaPlugin plugin) {
|
||||
super(plugin);
|
||||
|
|
Loading…
Reference in a new issue