2020-08-08 22:41:12 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-08-08 22:41:12 +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.level;
|
2020-08-08 22:41:12 +00:00
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.setting.Difficulty;
|
2021-11-13 03:44:15 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatPacket;
|
2020-12-28 05:29:27 +00:00
|
|
|
import com.nukkitx.nbt.NbtMap;
|
|
|
|
import com.nukkitx.nbt.NbtMapBuilder;
|
2020-08-08 22:41:12 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
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.session.cache.ChunkCache;
|
|
|
|
import org.geysermc.geyser.translator.inventory.LecternInventoryTranslator;
|
|
|
|
import org.geysermc.geyser.level.block.BlockStateValues;
|
2020-08-08 22:41:12 +00:00
|
|
|
|
|
|
|
public class GeyserWorldManager extends WorldManager {
|
|
|
|
|
|
|
|
private static final Object2ObjectMap<String, String> gameruleCache = new Object2ObjectOpenHashMap<>();
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public int getBlockAt(GeyserSession session, int x, int y, int z) {
|
2020-10-13 15:11:52 +00:00
|
|
|
ChunkCache chunkCache = session.getChunkCache();
|
|
|
|
if (chunkCache != null) { // Chunk cache can be null if the session is closed asynchronously
|
|
|
|
return chunkCache.getBlockAt(x, y, z);
|
|
|
|
}
|
2021-07-13 01:19:40 +00:00
|
|
|
return BlockStateValues.JAVA_AIR_ID;
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 06:30:25 +00:00
|
|
|
@Override
|
2021-03-14 16:26:47 +00:00
|
|
|
public boolean hasOwnChunkCache() {
|
2020-10-15 06:30:25 +00:00
|
|
|
// This implementation can only fetch data from the session chunk cache
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-28 05:29:27 +00:00
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public NbtMap getLecternDataAt(GeyserSession session, int x, int y, int z, boolean isChunkLoad) {
|
2020-12-28 05:29:27 +00:00
|
|
|
// Without direct server access, we can't get lectern information on-the-fly.
|
|
|
|
// I should have set this up so it's only called when there is a book in the block state. - Camotoy
|
|
|
|
NbtMapBuilder lecternTag = LecternInventoryTranslator.getBaseLecternTag(x, y, z, 1);
|
|
|
|
lecternTag.putCompound("book", NbtMap.builder()
|
|
|
|
.putByte("Count", (byte) 1)
|
|
|
|
.putShort("Damage", (short) 0)
|
|
|
|
.putString("Name", "minecraft:written_book")
|
|
|
|
.putCompound("tag", NbtMap.builder()
|
|
|
|
.putString("photoname", "")
|
|
|
|
.putString("text", "")
|
|
|
|
.build())
|
|
|
|
.build());
|
2021-02-21 23:17:31 +00:00
|
|
|
lecternTag.putInt("page", -1); // I'm surprisingly glad this exists - it forces Bedrock to stop reading immediately. Usually.
|
2020-12-28 05:29:27 +00:00
|
|
|
return lecternTag.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean shouldExpectLecternHandled() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-08 22:41:12 +00:00
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void setGameRule(GeyserSession session, String name, Object value) {
|
2021-11-13 03:44:15 +00:00
|
|
|
session.sendDownstreamPacket(new ServerboundChatPacket("/gamerule " + name + " " + value));
|
2020-08-08 22:41:12 +00:00
|
|
|
gameruleCache.put(name, String.valueOf(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public Boolean getGameRuleBool(GeyserSession session, GameRule gameRule) {
|
2020-08-08 22:41:12 +00:00
|
|
|
String value = gameruleCache.get(gameRule.getJavaID());
|
|
|
|
if (value != null) {
|
|
|
|
return Boolean.parseBoolean(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return gameRule.getDefaultValue() != null ? (Boolean) gameRule.getDefaultValue() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public int getGameRuleInt(GeyserSession session, GameRule gameRule) {
|
2020-08-08 22:41:12 +00:00
|
|
|
String value = gameruleCache.get(gameRule.getJavaID());
|
|
|
|
if (value != null) {
|
|
|
|
return Integer.parseInt(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return gameRule.getDefaultValue() != null ? (int) gameRule.getDefaultValue() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void setPlayerGameMode(GeyserSession session, GameMode gameMode) {
|
2021-11-13 03:44:15 +00:00
|
|
|
session.sendDownstreamPacket(new ServerboundChatPacket("/gamemode " + gameMode.name().toLowerCase()));
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void setDifficulty(GeyserSession session, Difficulty difficulty) {
|
2021-11-13 03:44:15 +00:00
|
|
|
session.sendDownstreamPacket(new ServerboundChatPacket("/difficulty " + difficulty.name().toLowerCase()));
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public boolean hasPermission(GeyserSession session, String permission) {
|
2020-08-08 22:41:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|