-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter fabric player move events based on previous location
Affects issues: - Fixed #2324
- Loading branch information
Showing
3 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
.../fabric/src/main/java/net/playeranalytics/plan/gathering/FabricPlayerPositionTracker.java
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,82 @@ | ||
/* | ||
* This file is part of Player Analytics (Plan). | ||
* | ||
* Plan is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License v3 as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Plan is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Plan. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package net.playeranalytics.plan.gathering; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public class FabricPlayerPositionTracker { | ||
private static final ReentrantLock WRITE_LOCK = new ReentrantLock(); | ||
private static final Map<UUID, double[]> POSITIONS = new HashMap<>(); | ||
|
||
private static final double[] EMPTY_POSITION = new double[5]; | ||
|
||
public FabricPlayerPositionTracker() { | ||
// Static method class | ||
} | ||
|
||
public static void removePlayer(UUID playerUUID) { | ||
try { | ||
WRITE_LOCK.lock(); | ||
POSITIONS.remove(playerUUID); | ||
} finally { | ||
WRITE_LOCK.unlock(); | ||
} | ||
} | ||
|
||
public static double[] getPosition(UUID playerUUID) { | ||
return POSITIONS.getOrDefault(playerUUID, EMPTY_POSITION); | ||
} | ||
|
||
public static boolean moved(UUID playerUUID, double x, double y, double z, float yaw, float pitch) { | ||
double[] previous = POSITIONS.get(playerUUID); | ||
if (isDifferent(previous, x, y, z, yaw, pitch)) { | ||
writeNewPosition(playerUUID, x, y, z, yaw, pitch, previous); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static void writeNewPosition(UUID playerUUID, double x, double y, double z, float yaw, float pitch, double[] previous) { | ||
try { | ||
WRITE_LOCK.lock(); | ||
if (previous == null) { | ||
previous = new double[5]; | ||
POSITIONS.put(playerUUID, previous); | ||
} | ||
previous[0] = x; | ||
previous[1] = y; | ||
previous[2] = z; | ||
previous[3] = yaw; | ||
previous[4] = pitch; | ||
|
||
} finally { | ||
WRITE_LOCK.unlock(); | ||
} | ||
} | ||
|
||
private static boolean isDifferent(double[] previous, double x, double y, double z, float yaw, float pitch) { | ||
return previous == null | ||
|| Double.compare(previous[0], x) != 0 | ||
|| Double.compare(previous[1], y) != 0 | ||
|| Double.compare(previous[2], z) != 0 | ||
|| Double.compare(previous[3], yaw) != 0 | ||
|| Double.compare(previous[4], pitch) != 0; | ||
} | ||
} |
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