2020-11-20 19:56:39 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-11-20 19:56:39 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.translator.collision;
|
2020-11-20 19:56:39 +00:00
|
|
|
|
2021-07-31 02:35:13 +00:00
|
|
|
import lombok.EqualsAndHashCode;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.level.physics.BoundingBox;
|
2020-11-20 19:56:39 +00:00
|
|
|
|
2021-07-31 02:35:13 +00:00
|
|
|
@EqualsAndHashCode(callSuper = true)
|
|
|
|
@CollisionRemapper(regex = "^snow$", passDefaultBoxes = true, usesParams = true)
|
2020-11-20 19:56:39 +00:00
|
|
|
public class SnowCollision extends BlockCollision {
|
|
|
|
private final int layers;
|
|
|
|
|
2021-07-31 02:35:13 +00:00
|
|
|
public SnowCollision(String params, BoundingBox[] defaultBoxes) {
|
|
|
|
super(defaultBoxes);
|
|
|
|
int layerCharIndex = params.indexOf("=") + 1;
|
|
|
|
layers = Integer.parseInt(params.substring(layerCharIndex, layerCharIndex + 1));
|
2020-11-20 19:56:39 +00:00
|
|
|
|
|
|
|
pushUpTolerance = 0.125;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needs to run before the main correction code or it can move the player into blocks
|
|
|
|
// This is counteracted by the main collision code pushing them out
|
|
|
|
@Override
|
2021-09-26 14:16:22 +00:00
|
|
|
public void beforeCorrectPosition(int x, int y, int z, BoundingBox playerCollision) {
|
2020-11-20 19:56:39 +00:00
|
|
|
// In Bedrock, snow layers round down to half blocks but you can't sink into them at all
|
|
|
|
// This means the collision each half block reaches above where it should be on Java so the player has to be
|
|
|
|
// pushed down
|
|
|
|
if (layers == 4 || layers == 8) {
|
|
|
|
double playerMinY = playerCollision.getMiddleY() - (playerCollision.getSizeY() / 2);
|
2021-09-26 14:16:22 +00:00
|
|
|
double boxMaxY = (boundingBoxes[0].getMiddleY() + y) + (boundingBoxes[0].getSizeY() / 2);
|
2020-11-20 19:56:39 +00:00
|
|
|
// If the player is in the buggy area, push them down
|
|
|
|
if (playerMinY > boxMaxY &&
|
|
|
|
playerMinY <= (boxMaxY + 0.125)) {
|
|
|
|
playerCollision.translate(0, boxMaxY - playerMinY, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public boolean correctPosition(GeyserSession session, int x, int y, int z, BoundingBox playerCollision) {
|
2021-07-31 02:35:13 +00:00
|
|
|
if (layers == 1) {
|
|
|
|
// 1 layer of snow does not have collision
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-20 19:56:39 +00:00
|
|
|
// Hack to prevent false positives
|
|
|
|
playerCollision.setSizeX(playerCollision.getSizeX() - 0.0001);
|
|
|
|
playerCollision.setSizeY(playerCollision.getSizeY() - 0.0001);
|
|
|
|
playerCollision.setSizeZ(playerCollision.getSizeZ() - 0.0001);
|
|
|
|
|
2021-09-26 14:16:22 +00:00
|
|
|
if (this.checkIntersection(x, y, z, playerCollision)) {
|
2020-11-20 19:56:39 +00:00
|
|
|
double playerMinY = playerCollision.getMiddleY() - (playerCollision.getSizeY() / 2);
|
2021-09-26 14:16:22 +00:00
|
|
|
double boxMaxY = (boundingBoxes[0].getMiddleY() + y) + (boundingBoxes[0].getSizeY() / 2);
|
2020-11-20 19:56:39 +00:00
|
|
|
// If the player actually can't step onto it (they can step onto it from other snow layers)
|
|
|
|
if ((boxMaxY - playerMinY) > 0.5) {
|
|
|
|
// Cancel the movement
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
playerCollision.setSizeX(playerCollision.getSizeX() + 0.0001);
|
|
|
|
playerCollision.setSizeY(playerCollision.getSizeY() + 0.0001);
|
|
|
|
playerCollision.setSizeZ(playerCollision.getSizeZ() + 0.0001);
|
2021-09-26 14:16:22 +00:00
|
|
|
return super.correctPosition(session, x, y, z, playerCollision);
|
2020-11-20 19:56:39 +00:00
|
|
|
}
|
|
|
|
}
|