2020-01-09 03:05:42 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-01-09 03:05:42 +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.util;
|
2019-12-27 11:29:46 +00:00
|
|
|
|
2020-05-24 04:45:39 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.Effect;
|
2020-08-10 02:43:57 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
2021-08-16 15:53:56 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3f;
|
2020-08-10 02:43:57 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.ChangeDimensionPacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.MobEffectPacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.StopSoundPacket;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.entity.type.Entity;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2019-12-27 12:03:12 +00:00
|
|
|
|
2021-08-16 15:53:56 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
2019-12-27 11:29:46 +00:00
|
|
|
public class DimensionUtils {
|
2020-05-23 21:02:51 +00:00
|
|
|
|
|
|
|
// Changes if the above-bedrock Nether building workaround is applied
|
|
|
|
private static int BEDROCK_NETHER_ID = 1;
|
|
|
|
|
2020-11-12 00:28:45 +00:00
|
|
|
/**
|
|
|
|
* String reference to vanilla Java overworld dimension identifier
|
|
|
|
*/
|
2020-06-25 00:27:10 +00:00
|
|
|
public static final String OVERWORLD = "minecraft:overworld";
|
2020-11-12 00:28:45 +00:00
|
|
|
/**
|
|
|
|
* String reference to vanilla Java nether dimension identifier
|
|
|
|
*/
|
2020-06-25 00:27:10 +00:00
|
|
|
public static final String NETHER = "minecraft:the_nether";
|
2020-11-12 00:28:45 +00:00
|
|
|
/**
|
|
|
|
* String reference to vanilla Java end dimension identifier
|
|
|
|
*/
|
2020-06-25 00:27:10 +00:00
|
|
|
public static final String THE_END = "minecraft:the_end";
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void switchDimension(GeyserSession session, String javaDimension) {
|
2019-12-27 11:29:46 +00:00
|
|
|
int bedrockDimension = javaToBedrock(javaDimension);
|
2021-12-08 18:23:05 +00:00
|
|
|
int previousDimension = javaToBedrock(session.getDimension());
|
|
|
|
|
2019-12-27 11:29:46 +00:00
|
|
|
Entity player = session.getPlayerEntity();
|
2020-11-02 21:04:08 +00:00
|
|
|
|
2021-08-30 19:12:17 +00:00
|
|
|
session.getChunkCache().clear();
|
2019-12-28 13:35:21 +00:00
|
|
|
session.getEntityCache().removeAllEntities();
|
2020-05-02 20:44:05 +00:00
|
|
|
session.getItemFrameCache().clear();
|
2021-09-04 18:55:22 +00:00
|
|
|
if (session.getLecternCache() != null) {
|
|
|
|
session.getLecternCache().clear();
|
|
|
|
}
|
2021-08-31 23:49:55 +00:00
|
|
|
session.getLodestoneCache().clear();
|
2021-09-10 01:20:25 +00:00
|
|
|
session.getPistonCache().clear();
|
2020-12-04 21:55:24 +00:00
|
|
|
session.getSkullCache().clear();
|
2020-02-06 04:21:09 +00:00
|
|
|
|
2021-08-16 15:53:56 +00:00
|
|
|
Vector3f pos = Vector3f.from(0, Short.MAX_VALUE, 0);
|
2019-12-27 11:29:46 +00:00
|
|
|
|
|
|
|
ChangeDimensionPacket changeDimensionPacket = new ChangeDimensionPacket();
|
|
|
|
changeDimensionPacket.setDimension(bedrockDimension);
|
|
|
|
changeDimensionPacket.setRespawn(true);
|
2021-08-16 15:53:56 +00:00
|
|
|
changeDimensionPacket.setPosition(pos);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(changeDimensionPacket);
|
2020-11-12 00:28:45 +00:00
|
|
|
session.setDimension(javaDimension);
|
2021-08-16 15:53:56 +00:00
|
|
|
player.setPosition(pos);
|
2020-02-06 04:21:09 +00:00
|
|
|
session.setSpawned(false);
|
2020-03-06 01:26:36 +00:00
|
|
|
session.setLastChunkPosition(null);
|
2019-12-27 11:29:46 +00:00
|
|
|
|
2021-08-16 15:53:56 +00:00
|
|
|
Set<Effect> entityEffects = session.getEffectCache().getEntityEffects();
|
|
|
|
for (Effect effect : entityEffects) {
|
2020-05-24 04:45:39 +00:00
|
|
|
MobEffectPacket mobEffectPacket = new MobEffectPacket();
|
|
|
|
mobEffectPacket.setEvent(MobEffectPacket.Event.REMOVE);
|
2021-08-16 15:53:56 +00:00
|
|
|
mobEffectPacket.setRuntimeEntityId(player.getGeyserId());
|
2020-05-24 04:45:39 +00:00
|
|
|
mobEffectPacket.setEffectId(EntityUtils.toBedrockEffectId(effect));
|
|
|
|
session.sendUpstreamPacket(mobEffectPacket);
|
|
|
|
}
|
|
|
|
// Effects are re-sent from server
|
2021-08-16 15:53:56 +00:00
|
|
|
entityEffects.clear();
|
2020-05-24 04:45:39 +00:00
|
|
|
|
2019-12-27 11:29:46 +00:00
|
|
|
//let java server handle portal travel sound
|
|
|
|
StopSoundPacket stopSoundPacket = new StopSoundPacket();
|
|
|
|
stopSoundPacket.setStoppingAllSound(true);
|
|
|
|
stopSoundPacket.setSoundName("");
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(stopSoundPacket);
|
2020-08-24 13:31:21 +00:00
|
|
|
|
2021-11-14 05:36:43 +00:00
|
|
|
// TODO - fix this hack of a fix by sending the final dimension switching logic after sections have been sent.
|
|
|
|
// The client wants sections sent to it before it can successfully respawn.
|
2020-08-24 13:31:21 +00:00
|
|
|
ChunkUtils.sendEmptyChunks(session, player.getPosition().toInt(), 3, true);
|
2021-12-08 18:23:05 +00:00
|
|
|
|
|
|
|
// If the bedrock nether height workaround is enabled, meaning the client is told it's in the end dimension,
|
|
|
|
// we check if the player is entering the nether and apply the nether fog to fake the fact that the client
|
|
|
|
// thinks they are in the end dimension.
|
|
|
|
if (BEDROCK_NETHER_ID == 2) {
|
|
|
|
if (bedrockDimension == BEDROCK_NETHER_ID) {
|
|
|
|
session.sendFog("minecraft:fog_hell");
|
|
|
|
} else if (previousDimension == BEDROCK_NETHER_ID) {
|
|
|
|
session.removeFog("minecraft:fog_hell");
|
|
|
|
}
|
|
|
|
}
|
2019-12-27 11:29:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
|
|
|
* Map the Java edition dimension IDs to Bedrock edition
|
|
|
|
*
|
|
|
|
* @param javaDimension Dimension ID to convert
|
|
|
|
* @return Converted Bedrock edition dimension ID
|
|
|
|
*/
|
2020-06-21 02:24:45 +00:00
|
|
|
public static int javaToBedrock(String javaDimension) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (javaDimension) {
|
|
|
|
case NETHER -> BEDROCK_NETHER_ID;
|
|
|
|
case THE_END -> 2;
|
|
|
|
default -> 0;
|
|
|
|
};
|
2020-06-21 02:24:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 02:43:57 +00:00
|
|
|
/**
|
2021-11-13 03:44:15 +00:00
|
|
|
* Determines the new dimension based on the {@link CompoundTag} sent by either the {@link com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket}
|
|
|
|
* or {@link com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundRespawnPacket}.
|
2020-11-12 00:28:45 +00:00
|
|
|
*
|
2020-08-10 02:43:57 +00:00
|
|
|
* @param dimensionTag the packet's dimension tag.
|
|
|
|
* @return the dimension identifier.
|
|
|
|
*/
|
|
|
|
public static String getNewDimension(CompoundTag dimensionTag) {
|
|
|
|
if (dimensionTag == null || dimensionTag.isEmpty()) {
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl.getInstance().getLogger().debug("Dimension tag was null or empty.");
|
2020-11-12 00:28:45 +00:00
|
|
|
return OVERWORLD;
|
2020-08-10 02:43:57 +00:00
|
|
|
}
|
|
|
|
if (dimensionTag.getValue().get("effects") != null) {
|
|
|
|
return ((StringTag) dimensionTag.getValue().get("effects")).getValue();
|
|
|
|
}
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl.getInstance().getLogger().debug("Effects portion of the tag was null or empty.");
|
2020-11-12 00:28:45 +00:00
|
|
|
return OVERWORLD;
|
2020-08-10 02:43:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 00:28:45 +00:00
|
|
|
/**
|
|
|
|
* The Nether dimension in Bedrock does not permit building above Y128 - the Bedrock above the dimension.
|
|
|
|
* This workaround sets the Nether as the End dimension to ignore this limit.
|
|
|
|
*
|
|
|
|
* @param isAboveNetherBedrockBuilding true if we should apply The End workaround
|
|
|
|
*/
|
|
|
|
public static void changeBedrockNetherId(boolean isAboveNetherBedrockBuilding) {
|
2020-05-23 21:02:51 +00:00
|
|
|
// Change dimension ID to the End to allow for building above Bedrock
|
2020-11-12 00:28:45 +00:00
|
|
|
BEDROCK_NETHER_ID = isAboveNetherBedrockBuilding ? 2 : 1;
|
2020-05-23 21:02:51 +00:00
|
|
|
}
|
2021-01-04 00:06:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the fake, temporary dimension we send clients to so we aren't switching to the same dimension without an additional
|
|
|
|
* dimension switch.
|
|
|
|
*
|
|
|
|
* @param currentDimension the current dimension of the player
|
|
|
|
* @param newDimension the new dimension that the player will be transferred to
|
|
|
|
* @return the fake dimension to transfer to
|
|
|
|
*/
|
|
|
|
public static String getTemporaryDimension(String currentDimension, String newDimension) {
|
|
|
|
if (BEDROCK_NETHER_ID == 2) {
|
|
|
|
// Prevents rare instances of Bedrock locking up
|
|
|
|
return javaToBedrock(newDimension) == 2 ? OVERWORLD : NETHER;
|
|
|
|
}
|
|
|
|
return currentDimension.equals(OVERWORLD) ? NETHER : OVERWORLD;
|
|
|
|
}
|
2021-12-08 18:23:05 +00:00
|
|
|
|
|
|
|
public static boolean isCustomBedrockNetherId() {
|
|
|
|
return BEDROCK_NETHER_ID == 2;
|
|
|
|
}
|
2019-12-27 11:29:46 +00:00
|
|
|
}
|