2020-04-21 05:32:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-04-29 20:01:53 +00:00
|
|
|
package org.geysermc.connector.network.translators.world.block;
|
2020-04-21 05:32:32 +00:00
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2020-05-05 02:32:02 +00:00
|
|
|
import com.nukkitx.nbt.tag.CompoundTag;
|
2020-06-19 01:44:50 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.*;
|
2020-04-21 05:32:32 +00:00
|
|
|
|
2020-05-05 02:32:02 +00:00
|
|
|
import java.util.HashMap;
|
2020-04-21 05:32:32 +00:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for block entities if the Java block state contains Bedrock block information.
|
|
|
|
*/
|
|
|
|
public class BlockStateValues {
|
|
|
|
|
2020-06-19 01:44:50 +00:00
|
|
|
private static final Int2IntMap BANNER_COLORS = new Int2IntOpenHashMap();
|
|
|
|
private static final Int2ByteMap BED_COLORS = new Int2ByteOpenHashMap();
|
2020-05-10 19:26:00 +00:00
|
|
|
private static final Int2ObjectMap<DoubleChestValue> DOUBLE_CHEST_VALUES = new Int2ObjectOpenHashMap<>();
|
2020-05-05 02:32:02 +00:00
|
|
|
private static final Int2ObjectMap<String> FLOWER_POT_VALUES = new Int2ObjectOpenHashMap<>();
|
|
|
|
private static final Map<String, CompoundTag> FLOWER_POT_BLOCKS = new HashMap<>();
|
2020-06-19 01:44:50 +00:00
|
|
|
private static final Int2IntMap NOTEBLOCK_PITCHES = new Int2IntOpenHashMap();
|
2020-05-05 02:32:02 +00:00
|
|
|
private static final Int2BooleanMap IS_STICKY_PISTON = new Int2BooleanOpenHashMap();
|
|
|
|
private static final Int2BooleanMap PISTON_VALUES = new Int2BooleanOpenHashMap();
|
2020-06-19 01:44:50 +00:00
|
|
|
private static final Int2ByteMap SKULL_VARIANTS = new Int2ByteOpenHashMap();
|
|
|
|
private static final Int2ByteMap SKULL_ROTATIONS = new Int2ByteOpenHashMap();
|
|
|
|
private static final Int2ByteMap SHULKERBOX_DIRECTIONS = new Int2ByteOpenHashMap();
|
2020-04-21 05:32:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the block state contains Bedrock block information
|
2020-04-22 06:03:46 +00:00
|
|
|
* @param entry The String to JsonNode map used in BlockTranslator
|
2020-04-21 05:32:32 +00:00
|
|
|
* @param javaBlockState the Java Block State of the block
|
|
|
|
*/
|
2020-06-19 01:44:50 +00:00
|
|
|
public static void storeBlockStateValues(Map.Entry<String, JsonNode> entry, int javaBlockState) {
|
2020-04-21 05:32:32 +00:00
|
|
|
JsonNode bannerColor = entry.getValue().get("banner_color");
|
|
|
|
if (bannerColor != null) {
|
2020-05-01 05:51:23 +00:00
|
|
|
BANNER_COLORS.put(javaBlockState, (byte) bannerColor.intValue());
|
2020-04-21 05:32:32 +00:00
|
|
|
return; // There will never be a banner color and a skull variant
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode bedColor = entry.getValue().get("bed_color");
|
|
|
|
if (bedColor != null) {
|
2020-05-01 05:51:23 +00:00
|
|
|
BED_COLORS.put(javaBlockState, (byte) bedColor.intValue());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-10 19:26:00 +00:00
|
|
|
if (entry.getValue().get("double_chest_position") != null) {
|
|
|
|
boolean isX = (entry.getValue().get("x") != null);
|
|
|
|
boolean isDirectionPositive = ((entry.getValue().get("x") != null && entry.getValue().get("x").asBoolean()) ||
|
|
|
|
(entry.getValue().get("z") != null && entry.getValue().get("z").asBoolean()));
|
|
|
|
boolean isLeft = (entry.getValue().get("double_chest_position").asText().contains("left"));
|
2020-06-19 01:44:50 +00:00
|
|
|
DOUBLE_CHEST_VALUES.put(javaBlockState, new DoubleChestValue(isX, isDirectionPositive, isLeft));
|
2020-05-10 19:26:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-05 04:03:51 +00:00
|
|
|
if (entry.getKey().contains("potted_") || entry.getKey().contains("flower_pot")) {
|
2020-06-19 01:44:50 +00:00
|
|
|
FLOWER_POT_VALUES.put(javaBlockState, entry.getKey().replace("potted_", ""));
|
2020-05-05 02:32:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-01 05:51:23 +00:00
|
|
|
JsonNode notePitch = entry.getValue().get("note_pitch");
|
|
|
|
if (notePitch != null) {
|
|
|
|
NOTEBLOCK_PITCHES.put(javaBlockState, entry.getValue().get("note_pitch").intValue());
|
2020-04-21 05:32:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-05 02:32:02 +00:00
|
|
|
if (entry.getKey().contains("piston")) {
|
|
|
|
// True if extended, false if not
|
2020-06-19 01:44:50 +00:00
|
|
|
PISTON_VALUES.put(javaBlockState, entry.getKey().contains("extended=true"));
|
|
|
|
IS_STICKY_PISTON.put(javaBlockState, entry.getKey().contains("sticky"));
|
2020-05-05 02:32:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:32:32 +00:00
|
|
|
JsonNode skullVariation = entry.getValue().get("variation");
|
|
|
|
if(skullVariation != null) {
|
2020-05-01 05:51:23 +00:00
|
|
|
SKULL_VARIANTS.put(javaBlockState, (byte) skullVariation.intValue());
|
2020-04-21 05:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode skullRotation = entry.getValue().get("skull_rotation");
|
|
|
|
if (skullRotation != null) {
|
2020-05-01 05:51:23 +00:00
|
|
|
SKULL_ROTATIONS.put(javaBlockState, (byte) skullRotation.intValue());
|
2020-04-21 05:32:32 +00:00
|
|
|
}
|
2020-04-29 16:01:56 +00:00
|
|
|
|
|
|
|
JsonNode shulkerDirection = entry.getValue().get("shulker_direction");
|
|
|
|
if (shulkerDirection != null) {
|
|
|
|
BlockStateValues.SHULKERBOX_DIRECTIONS.put(javaBlockState, (byte) shulkerDirection.intValue());
|
|
|
|
}
|
2020-04-21 05:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Banner colors are part of the namespaced ID in Java Edition, but part of the block entity tag in Bedrock.
|
|
|
|
* This gives an integer color that Bedrock can use.
|
2020-04-29 16:01:56 +00:00
|
|
|
*
|
2020-04-21 05:32:32 +00:00
|
|
|
* @param state BlockState of the block
|
2020-04-29 16:01:56 +00:00
|
|
|
* @return Banner color integer or -1 if no color
|
2020-04-21 05:32:32 +00:00
|
|
|
*/
|
2020-06-19 01:44:50 +00:00
|
|
|
public static int getBannerColor(int state) {
|
2020-04-21 05:32:32 +00:00
|
|
|
if (BANNER_COLORS.containsKey(state)) {
|
2020-06-19 01:44:50 +00:00
|
|
|
return BANNER_COLORS.get(state);
|
2020-04-21 05:32:32 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bed colors are part of the namespaced ID in Java Edition, but part of the block entity tag in Bedrock.
|
|
|
|
* This gives a byte color that Bedrock can use - Bedrock needs a byte in the final tag.
|
2020-04-29 16:01:56 +00:00
|
|
|
*
|
2020-04-21 05:32:32 +00:00
|
|
|
* @param state BlockState of the block
|
2020-04-29 16:01:56 +00:00
|
|
|
* @return Bed color byte or -1 if no color
|
2020-04-21 05:32:32 +00:00
|
|
|
*/
|
2020-06-19 01:44:50 +00:00
|
|
|
public static byte getBedColor(int state) {
|
2020-04-21 05:32:32 +00:00
|
|
|
if (BED_COLORS.containsKey(state)) {
|
2020-06-19 01:44:50 +00:00
|
|
|
return BED_COLORS.get(state);
|
2020-04-21 05:32:32 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-10 19:26:00 +00:00
|
|
|
/**
|
|
|
|
* All double chest values are part of the block state in Java and part of the block entity tag in Bedrock.
|
|
|
|
* This gives the DoubleChestValue that can be calculated into the final tag.
|
|
|
|
* @return The map of all DoubleChestValues.
|
|
|
|
*/
|
|
|
|
public static Int2ObjectMap<DoubleChestValue> getDoubleChestValues() {
|
|
|
|
return DOUBLE_CHEST_VALUES;
|
|
|
|
}
|
|
|
|
|
2020-05-05 02:32:02 +00:00
|
|
|
/**
|
|
|
|
* Get the Int2ObjectMap of flower pot block states to containing plant
|
|
|
|
* @return Int2ObjectMap of flower pot values
|
|
|
|
*/
|
|
|
|
public static Int2ObjectMap<String> getFlowerPotValues() {
|
|
|
|
return FLOWER_POT_VALUES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the map of contained flower pot plants to Bedrock CompoundTag
|
|
|
|
* @return Map of flower pot blocks.
|
|
|
|
*/
|
|
|
|
public static Map<String, CompoundTag> getFlowerPotBlocks() {
|
|
|
|
return FLOWER_POT_BLOCKS;
|
|
|
|
}
|
|
|
|
|
2020-05-01 05:51:23 +00:00
|
|
|
/**
|
|
|
|
* The note that noteblocks output when hit is part of the block state in Java but sent as a BlockEventPacket in Bedrock.
|
|
|
|
* This gives an integer pitch that Bedrock can use.
|
|
|
|
* @param state BlockState of the block
|
|
|
|
* @return note block note integer or -1 if not present
|
|
|
|
*/
|
2020-06-19 01:44:50 +00:00
|
|
|
public static int getNoteblockPitch(int state) {
|
2020-05-01 05:51:23 +00:00
|
|
|
if (NOTEBLOCK_PITCHES.containsKey(state)) {
|
2020-06-19 01:44:50 +00:00
|
|
|
return NOTEBLOCK_PITCHES.get(state);
|
2020-05-01 05:51:23 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-05 02:32:02 +00:00
|
|
|
/**
|
|
|
|
* Get the Int2BooleanMap showing if a piston block state is extended or not.
|
|
|
|
* @return the Int2BooleanMap of piston extensions.
|
|
|
|
*/
|
|
|
|
public static Int2BooleanMap getPistonValues() {
|
|
|
|
return PISTON_VALUES;
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:44:50 +00:00
|
|
|
public static boolean isStickyPiston(int blockState) {
|
|
|
|
return IS_STICKY_PISTON.get(blockState);
|
2020-05-05 02:32:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:32:32 +00:00
|
|
|
/**
|
|
|
|
* Skull variations are part of the namespaced ID in Java Edition, but part of the block entity tag in Bedrock.
|
|
|
|
* This gives a byte variant ID that Bedrock can use.
|
2020-04-29 16:01:56 +00:00
|
|
|
*
|
2020-04-21 05:32:32 +00:00
|
|
|
* @param state BlockState of the block
|
2020-04-29 16:01:56 +00:00
|
|
|
* @return Skull variant byte or -1 if no variant
|
2020-04-21 05:32:32 +00:00
|
|
|
*/
|
2020-06-19 01:44:50 +00:00
|
|
|
public static byte getSkullVariant(int state) {
|
2020-04-21 05:32:32 +00:00
|
|
|
if (SKULL_VARIANTS.containsKey(state)) {
|
2020-06-19 01:44:50 +00:00
|
|
|
return SKULL_VARIANTS.get(state);
|
2020-04-21 05:32:32 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-29 16:01:56 +00:00
|
|
|
* Skull rotations are part of the namespaced ID in Java Edition, but part of the block entity tag in Bedrock.
|
|
|
|
* This gives a byte rotation that Bedrock can use.
|
2020-04-21 05:32:32 +00:00
|
|
|
*
|
|
|
|
* @param state BlockState of the block
|
2020-04-29 16:01:56 +00:00
|
|
|
* @return Skull rotation value or -1 if no value
|
2020-04-21 05:32:32 +00:00
|
|
|
*/
|
2020-06-19 01:44:50 +00:00
|
|
|
public static byte getSkullRotation(int state) {
|
2020-04-21 05:32:32 +00:00
|
|
|
if (SKULL_ROTATIONS.containsKey(state)) {
|
2020-06-19 01:44:50 +00:00
|
|
|
return SKULL_ROTATIONS.get(state);
|
2020-04-21 05:32:32 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:01:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shulker box directions are part of the namespaced ID in Java Edition, but part of the block entity tag in Bedrock.
|
|
|
|
* This gives a byte direction that Bedrock can use.
|
|
|
|
*
|
|
|
|
* @param state BlockState of the block
|
|
|
|
* @return Shulker direction value or -1 if no value
|
|
|
|
*/
|
2020-06-19 01:44:50 +00:00
|
|
|
public static byte getShulkerBoxDirection(int state) {
|
2020-04-29 16:01:56 +00:00
|
|
|
if (SHULKERBOX_DIRECTIONS.containsKey(state)) {
|
2020-06-19 01:44:50 +00:00
|
|
|
return SHULKERBOX_DIRECTIONS.get(state);
|
2020-04-29 16:01:56 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2020-04-21 05:32:32 +00:00
|
|
|
}
|