forked from SteamWar/SteamWar
Added cleanup of cursor
This commit is contained in:
@@ -112,6 +112,11 @@ public class RCursor {
|
|||||||
onClickHandler.handle(cursorLocation, isHittingEntity, action);
|
onClickHandler.handle(cursorLocation, isHittingEntity, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
cursorServer.close();
|
||||||
|
RCursorManager.getInstance().unregisterCursor(this);
|
||||||
|
}
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum CursorMode {
|
public enum CursorMode {
|
||||||
FREE((player, rayTraceResult) -> {
|
FREE((player, rayTraceResult) -> {
|
||||||
|
|||||||
+20
-4
@@ -8,6 +8,7 @@ import lombok.Getter;
|
|||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerInteractEvent;
|
import org.bukkit.event.player.PlayerInteractEvent;
|
||||||
|
|
||||||
import java.lang.ref.SoftReference;
|
import java.lang.ref.SoftReference;
|
||||||
@@ -18,7 +19,7 @@ import java.util.Set;
|
|||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
@Linked
|
@Linked
|
||||||
public class RCursorManager {
|
public class RCursorManager implements Listener {
|
||||||
@Getter
|
@Getter
|
||||||
private static RCursorManager instance;
|
private static RCursorManager instance;
|
||||||
|
|
||||||
@@ -50,10 +51,23 @@ public class RCursorManager {
|
|||||||
TinyProtocol.instance.addFilter(positionLookPacketClass, function);
|
TinyProtocol.instance.addFilter(positionLookPacketClass, function);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerCursor(RCursor cursor) {
|
void registerCursor(RCursor cursor) {
|
||||||
|
SoftReference<RCursor> currentPlayerCursorRef = activeCursors.get(cursor.getOwner());
|
||||||
|
if(currentPlayerCursorRef != null) {
|
||||||
|
RCursor currentCursor = currentPlayerCursorRef.get();
|
||||||
|
if(currentCursor != null) {
|
||||||
|
currentCursor.close();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
activeCursors.put(cursor.getOwner(), new SoftReference<>(cursor));
|
activeCursors.put(cursor.getOwner(), new SoftReference<>(cursor));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void unregisterCursor(RCursor cursor) {
|
||||||
|
activeCursors.remove(cursor.getOwner());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void updateCursor(Player player) {
|
private void updateCursor(Player player) {
|
||||||
if (!activeCursors.containsKey(player)) {
|
if (!activeCursors.containsKey(player)) {
|
||||||
return;
|
return;
|
||||||
@@ -85,9 +99,11 @@ public class RCursorManager {
|
|||||||
@EventHandler
|
@EventHandler
|
||||||
private void handlePlayerInteract(PlayerInteractEvent event) {
|
private void handlePlayerInteract(PlayerInteractEvent event) {
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
RCursor cursorOfPlayer = activeCursors.get(player).get();
|
|
||||||
|
|
||||||
if(cursorOfPlayer == null) return;
|
SoftReference<RCursor> cursorOfPlayerRef = activeCursors.get(player);
|
||||||
|
if(cursorOfPlayerRef == null) return;
|
||||||
|
|
||||||
|
RCursor cursorOfPlayer = cursorOfPlayerRef.get();
|
||||||
|
|
||||||
cursorOfPlayer.handleClick(event.getAction());
|
cursorOfPlayer.handleClick(event.getAction());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user