-
Notifications
You must be signed in to change notification settings - Fork 0
fix: move players out of blocks on join and place graves nearest first #53
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
+329
−12
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1c62897
fix: move players out of blocks on join and place graves nearest first
XxFran10xX ca54164
fix: unstick on the tick after join so the teleport sticks
XxFran10xX 6dd2f51
fix: only report a join move after a safe, successful teleport
XxFran10xX dc552c7
fix: re-capture the freeze spot right after the join move
XxFran10xX 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
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
149 changes: 149 additions & 0 deletions
149
src/main/java/net/tfminecraft/rpcharacters/joinsafety/JoinUnstuckListener.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,149 @@ | ||
| package net.tfminecraft.rpcharacters.joinsafety; | ||
|
|
||
| import java.util.EnumSet; | ||
| import java.util.Set; | ||
|
|
||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.GameMode; | ||
| import org.bukkit.Location; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.World; | ||
| import org.bukkit.block.Block; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.Listener; | ||
| import org.bukkit.event.player.PlayerJoinEvent; | ||
| import org.bukkit.util.BoundingBox; | ||
|
|
||
| import net.tfminecraft.rpcharacters.RPCharacters; | ||
|
|
||
| /** | ||
| * Moves players out of blocks when they log in. Someone may have built where | ||
| * they logged out, and they would suffocate while the resource pack loads. | ||
| */ | ||
| public final class JoinUnstuckListener implements Listener { | ||
|
|
||
| private static final String MOVED_MESSAGE = | ||
| "\u00A7eYou logged in inside a block, so you were moved to a safe spot nearby."; | ||
|
|
||
| private static final Set<Material> HAZARDS = EnumSet.of( | ||
| Material.LAVA, Material.FIRE, Material.SOUL_FIRE, Material.MAGMA_BLOCK, | ||
| Material.CAMPFIRE, Material.SOUL_CAMPFIRE, Material.CACTUS, Material.SWEET_BERRY_BUSH, | ||
| Material.POWDER_SNOW, Material.WITHER_ROSE, Material.POINTED_DRIPSTONE); | ||
|
|
||
| @EventHandler | ||
| public void onJoin(PlayerJoinEvent event) { | ||
| Player player = event.getPlayer(); | ||
| // A teleport inside the join event is lost when the client's first position syncs. | ||
| // Scheduled tasks run before players tick, so this still lands before suffocation damage. | ||
| Bukkit.getScheduler().runTask(RPCharacters.plugin, () -> unstick(player)); | ||
| } | ||
|
|
||
| private static void unstick(Player player) { | ||
| if (!player.isOnline()) { | ||
| return; | ||
| } | ||
| GameMode mode = player.getGameMode(); | ||
| if (mode == GameMode.CREATIVE || mode == GameMode.SPECTATOR || player.isInsideVehicle()) { | ||
| return; | ||
| } | ||
| // Vanilla drops a player who still fits crawling into the swimming pose, so leave them be. | ||
| if (!isInWall(player) || !crawlSpaceBlocked(player)) { | ||
| return; | ||
| } | ||
| Location from = player.getLocation(); | ||
| Location target = findSafeSpot(from); | ||
| if (target == null) { | ||
| return; | ||
| } | ||
| if (!player.teleport(target)) { | ||
| return; | ||
| } | ||
| // PlayerManager froze no-character players in the wall on join; re-capture it here. | ||
| RPCharacters.getPlayerManager().releaseFreeze(player); | ||
| RPCharacters.getPlayerManager().reevaluateFreeze(player); | ||
| player.sendMessage(MOVED_MESSAGE); | ||
| RPCharacters.plugin.getLogger().info("Moved " + player.getName() + " out of blocks on join: " | ||
| + describe(from) + " -> " + describe(target)); | ||
| } | ||
|
|
||
| /** Same test vanilla uses for suffocation damage: a thin box at eye level. */ | ||
| static boolean isInWall(Player player) { | ||
| Location eye = player.getEyeLocation(); | ||
| double half = player.getWidth() * 0.4; | ||
| BoundingBox box = new BoundingBox( | ||
| eye.getX() - half, eye.getY() - 1.0E-6, eye.getZ() - half, | ||
| eye.getX() + half, eye.getY() + 1.0E-6, eye.getZ() + half); | ||
| return collides(player.getWorld(), box, true); | ||
| } | ||
|
|
||
| /** True when blocks fill the space a crawling (swimming pose) player would need. */ | ||
| static boolean crawlSpaceBlocked(Player player) { | ||
| Location feet = player.getLocation(); | ||
| double half = player.getWidth() / 2.0 - 1.0E-6; | ||
| BoundingBox box = new BoundingBox( | ||
| feet.getX() - half, feet.getY() + 1.0E-6, feet.getZ() - half, | ||
| feet.getX() + half, feet.getY() + 0.6, feet.getZ() + half); | ||
| return collides(player.getWorld(), box, false); | ||
| } | ||
|
|
||
| private static boolean collides(World world, BoundingBox box, boolean suffocatingOnly) { | ||
| for (int x = floor(box.getMinX()); x <= floor(box.getMaxX()); x++) { | ||
| for (int y = floor(box.getMinY()); y <= floor(box.getMaxY()); y++) { | ||
| for (int z = floor(box.getMinZ()); z <= floor(box.getMaxZ()); z++) { | ||
| Block block = world.getBlockAt(x, y, z); | ||
| if (suffocatingOnly && !block.isSuffocating()) { | ||
| continue; | ||
| } | ||
| if (block.getCollisionShape().overlaps(box.clone().shift(-x, -y, -z))) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static Location findSafeSpot(Location from) { | ||
| World world = from.getWorld(); | ||
| if (world == null) { | ||
| return null; | ||
| } | ||
| int[] spot = SafeSpotSearch.find(from.getBlockX(), from.getBlockY(), from.getBlockZ(), | ||
| (x, y, z) -> canStand(world, x, y, z)); | ||
| if (spot == null) { | ||
| Block top = world.getHighestBlockAt(from.getBlockX(), from.getBlockZ()); | ||
| if (!canStand(world, top.getX(), top.getY() + 1, top.getZ())) { | ||
| return null; | ||
| } | ||
| spot = new int[] { top.getX(), top.getY() + 1, top.getZ() }; | ||
| } | ||
| Location target = new Location(world, spot[0] + 0.5, spot[1], spot[2] + 0.5); | ||
| target.setYaw(from.getYaw()); | ||
| target.setPitch(from.getPitch()); | ||
| return target; | ||
| } | ||
|
|
||
| private static boolean canStand(World world, int x, int y, int z) { | ||
| if (y - 1 < world.getMinHeight() || y + 1 >= world.getMaxHeight()) { | ||
| return false; | ||
| } | ||
| Block floor = world.getBlockAt(x, y - 1, z); | ||
| if (!floor.getType().isSolid() || HAZARDS.contains(floor.getType())) { | ||
| return false; | ||
| } | ||
| return isOpen(world.getBlockAt(x, y, z)) && isOpen(world.getBlockAt(x, y + 1, z)); | ||
| } | ||
|
|
||
| private static boolean isOpen(Block block) { | ||
| return block.isPassable() && !block.isLiquid() && !HAZARDS.contains(block.getType()); | ||
| } | ||
|
|
||
| private static int floor(double value) { | ||
| return (int) Math.floor(value); | ||
| } | ||
|
|
||
| private static String describe(Location loc) { | ||
| return loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ(); | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
src/main/java/net/tfminecraft/rpcharacters/joinsafety/SafeSpotSearch.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,61 @@ | ||
| package net.tfminecraft.rpcharacters.joinsafety; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Finds the nearest block position a player can stand at, checking closer offsets first. | ||
| */ | ||
| public final class SafeSpotSearch { | ||
|
|
||
| public static final int HORIZONTAL_RADIUS = 8; | ||
| public static final int VERTICAL_RADIUS = 8; | ||
|
|
||
| private static final List<int[]> OFFSETS = buildOffsets(HORIZONTAL_RADIUS, VERTICAL_RADIUS); | ||
|
|
||
| @FunctionalInterface | ||
| public interface Probe { | ||
| /** True when feet at (x, y, z) are safe: open feet and head blocks on a solid floor. */ | ||
| boolean canStand(int x, int y, int z); | ||
| } | ||
|
|
||
| private SafeSpotSearch() { | ||
| } | ||
|
|
||
| /** | ||
| * @return {x, y, z} of the nearest standable position, or {@code null} if none is in range | ||
| */ | ||
| public static int[] find(int x, int y, int z, Probe probe) { | ||
| if (probe == null) { | ||
| return null; | ||
| } | ||
| for (int[] offset : OFFSETS) { | ||
| int cx = x + offset[0]; | ||
| int cy = y + offset[1]; | ||
| int cz = z + offset[2]; | ||
| if (probe.canStand(cx, cy, cz)) { | ||
| return new int[] { cx, cy, cz }; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** Offsets ordered by distance; ties prefer less vertical movement, then moving up. */ | ||
| static List<int[]> buildOffsets(int horizontal, int vertical) { | ||
| List<int[]> offsets = new ArrayList<>(); | ||
| for (int dx = -horizontal; dx <= horizontal; dx++) { | ||
| for (int dy = -vertical; dy <= vertical; dy++) { | ||
| for (int dz = -horizontal; dz <= horizontal; dz++) { | ||
| offsets.add(new int[] { dx, dy, dz }); | ||
| } | ||
| } | ||
| } | ||
| offsets.sort(Comparator.<int[]>comparingInt(o -> o[0] * o[0] + o[1] * o[1] + o[2] * o[2]) | ||
| .thenComparingInt(o -> Math.abs(o[1])) | ||
| .thenComparingInt(o -> -o[1]) | ||
| .thenComparingInt(o -> o[0]) | ||
| .thenComparingInt(o -> o[2])); | ||
| return offsets; | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/test/java/net/tfminecraft/rpcharacters/grave/GraveSearchOffsetsTest.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,34 @@ | ||
| package net.tfminecraft.rpcharacters.grave; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class GraveSearchOffsetsTest { | ||
|
|
||
| private static int distanceSq(int[] o) { | ||
| return o[0] * o[0] + o[1] * o[1] + o[2] * o[2]; | ||
| } | ||
|
|
||
| @Test | ||
| void coversRadiusTwoOnSameLevelAndOneAboveWithoutOrigin() { | ||
| List<int[]> offsets = GraveManager.searchOffsets(2); | ||
| assertEquals(5 * 5 * 2 - 1, offsets.size()); | ||
| for (int[] o : offsets) { | ||
| assertTrue(o[0] != 0 || o[1] != 0 || o[2] != 0); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void checksNearestBlocksFirst() { | ||
| List<int[]> offsets = GraveManager.searchOffsets(2); | ||
| assertEquals(1, distanceSq(offsets.get(0))); | ||
| assertEquals(0, offsets.get(0)[1], "same level is tried before the block above"); | ||
| for (int i = 1; i < offsets.size(); i++) { | ||
| assertTrue(distanceSq(offsets.get(i - 1)) <= distanceSq(offsets.get(i))); | ||
| } | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
src/test/java/net/tfminecraft/rpcharacters/joinsafety/SafeSpotSearchTest.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,62 @@ | ||
| package net.tfminecraft.rpcharacters.joinsafety; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class SafeSpotSearchTest { | ||
|
|
||
| private static SafeSpotSearch.Probe spots(int[]... positions) { | ||
| Set<String> open = new HashSet<>(); | ||
| for (int[] p : positions) { | ||
| open.add(p[0] + ":" + p[1] + ":" + p[2]); | ||
| } | ||
| return (x, y, z) -> open.contains(x + ":" + y + ":" + z); | ||
| } | ||
|
|
||
| @Test | ||
| void keepsOriginWhenItIsStandable() { | ||
| int[] found = SafeSpotSearch.find(4079, 196, 1663, spots(new int[] { 4079, 196, 1663 })); | ||
| assertArrayEquals(new int[] { 4079, 196, 1663 }, found); | ||
| } | ||
|
|
||
| @Test | ||
| void picksNearestSpot() { | ||
| int[] found = SafeSpotSearch.find(4079, 196, 1663, spots( | ||
| new int[] { 4077, 196, 1661 }, | ||
| new int[] { 4079, 196, 1662 }, | ||
| new int[] { 4083, 196, 1663 })); | ||
| assertArrayEquals(new int[] { 4079, 196, 1662 }, found); | ||
| } | ||
|
|
||
| @Test | ||
| void prefersUpOverDownAtEqualDistance() { | ||
| int[] found = SafeSpotSearch.find(0, 64, 0, spots(new int[] { 0, 63, 0 }, new int[] { 0, 65, 0 })); | ||
| assertArrayEquals(new int[] { 0, 65, 0 }, found); | ||
| } | ||
|
|
||
| @Test | ||
| void prefersSidewaysOverVerticalAtEqualDistance() { | ||
| int[] found = SafeSpotSearch.find(0, 64, 0, spots(new int[] { 0, 65, 0 }, new int[] { 1, 64, 0 })); | ||
| assertArrayEquals(new int[] { 1, 64, 0 }, found); | ||
| } | ||
|
|
||
| @Test | ||
| void returnsNullWhenNothingInRange() { | ||
| int far = SafeSpotSearch.HORIZONTAL_RADIUS + 1; | ||
| assertNull(SafeSpotSearch.find(0, 64, 0, spots(new int[] { far, 64, 0 }))); | ||
| } | ||
|
|
||
| @Test | ||
| void offsetsCoverTheWholeBoxOnce() { | ||
| List<int[]> offsets = SafeSpotSearch.buildOffsets(2, 1); | ||
| assertEquals(5 * 3 * 5, offsets.size()); | ||
| assertArrayEquals(new int[] { 0, 0, 0 }, offsets.get(0)); | ||
| } | ||
| } |
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.