3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-11-19 22:40:18 +01:00

Translate block item

Dieser Commit ist enthalten in:
Joshua Castle 2023-01-04 22:44:28 -08:00
Ursprung 6409d7982d
Commit 96967dafa2
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: F674F38216C35D5D
5 geänderte Dateien mit 38 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -4,5 +4,5 @@ import java.util.Map;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
public record CustomBlockMapping(@NonNull CustomBlockData data, @NonNull Map<String, CustomBlockState> states) { public record CustomBlockMapping(@NonNull CustomBlockData data, @NonNull Map<String, CustomBlockState> states, @NonNull String javaIdentifier, @NonNull boolean overrideItem) {
} }

Datei anzeigen

@ -106,6 +106,11 @@ public class BlockRegistries {
*/ */
public static final MappedRegistry<Integer, CustomBlockState, Int2ObjectMap<CustomBlockState>> CUSTOM_BLOCK_STATE_OVERRIDES = MappedRegistry.create(RegistryLoaders.empty(Int2ObjectOpenHashMap::new)); public static final MappedRegistry<Integer, CustomBlockState, Int2ObjectMap<CustomBlockState>> CUSTOM_BLOCK_STATE_OVERRIDES = MappedRegistry.create(RegistryLoaders.empty(Int2ObjectOpenHashMap::new));
/**
* A registry which stores clean Java Ids and the custom block it should be replaced with in the context of items.
*/
public static final SimpleMappedRegistry<String, CustomBlockData> CUSTOM_BLOCK_ITEM_OVERRIDES = SimpleMappedRegistry.create(RegistryLoaders.empty(Object2ObjectOpenHashMap::new));
/** /**
* A registry which stores skin texture hashes to custom skull blocks. * A registry which stores skin texture hashes to custom skull blocks.
*/ */

Datei anzeigen

@ -222,7 +222,7 @@ public class MappingsReader_v1 extends MappingsReader {
Map<String, CustomBlockState> states = createCustomBlockStatesMap(identifier, stateKeys, defaultStates, onlyOverrideStates, customBlockData, Map<String, CustomBlockState> states = createCustomBlockStatesMap(identifier, stateKeys, defaultStates, onlyOverrideStates, customBlockData,
blockPropertyTypeMaps.stateKeyStrings(), blockPropertyTypeMaps.stateKeyInts(), blockPropertyTypeMaps.stateKeyBools()); blockPropertyTypeMaps.stateKeyStrings(), blockPropertyTypeMaps.stateKeyInts(), blockPropertyTypeMaps.stateKeyBools());
return new CustomBlockMapping(customBlockData, states); return new CustomBlockMapping(customBlockData, states, identifier, !onlyOverrideStates);
} }
private List<CustomBlockPermutation> createCustomBlockPermutations(JsonNode node, String identifier, String name) { private List<CustomBlockPermutation> createCustomBlockPermutations(JsonNode node, String identifier, String name) {

Datei anzeigen

@ -1,6 +1,7 @@
package org.geysermc.geyser.registry.populator; package org.geysermc.geyser.registry.populator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -39,6 +40,7 @@ public class CustomBlockRegistryPopulator {
Set<String> customBlockNames = new ObjectOpenHashSet<>(); Set<String> customBlockNames = new ObjectOpenHashSet<>();
Set<CustomBlockData> customBlocks = new ObjectOpenHashSet<>(); Set<CustomBlockData> customBlocks = new ObjectOpenHashSet<>();
Int2ObjectMap<CustomBlockState> blockStateOverrides = new Int2ObjectOpenHashMap<>(); Int2ObjectMap<CustomBlockState> blockStateOverrides = new Int2ObjectOpenHashMap<>();
Map<String, CustomBlockData> customBlockItemOverrides = new HashMap<>();
GeyserImpl.getInstance().getEventBus().fire(new GeyserDefineCustomBlocksEvent() { GeyserImpl.getInstance().getEventBus().fire(new GeyserDefineCustomBlocksEvent() {
@Override @Override
public void registerCustomBlock(@NonNull CustomBlockData customBlockData) { public void registerCustomBlock(@NonNull CustomBlockData customBlockData) {
@ -79,6 +81,9 @@ public class CustomBlockRegistryPopulator {
MappingsConfigReader mappingsConfigReader = new MappingsConfigReader(); MappingsConfigReader mappingsConfigReader = new MappingsConfigReader();
mappingsConfigReader.loadBlockMappingsFromJson((key, block) -> { mappingsConfigReader.loadBlockMappingsFromJson((key, block) -> {
customBlocks.add(block.data()); customBlocks.add(block.data());
if (block.overrideItem()) {
customBlockItemOverrides.put(block.javaIdentifier(), block.data());
}
block.states().forEach((javaIdentifier, customBlockState) -> { block.states().forEach((javaIdentifier, customBlockState) -> {
int id = BlockRegistries.JAVA_IDENTIFIERS.getOrDefault(javaIdentifier, -1); int id = BlockRegistries.JAVA_IDENTIFIERS.getOrDefault(javaIdentifier, -1);
blockStateOverrides.put(id, customBlockState); blockStateOverrides.put(id, customBlockState);
@ -90,6 +95,9 @@ public class CustomBlockRegistryPopulator {
BlockRegistries.CUSTOM_BLOCK_STATE_OVERRIDES.set(blockStateOverrides); BlockRegistries.CUSTOM_BLOCK_STATE_OVERRIDES.set(blockStateOverrides);
GeyserImpl.getInstance().getLogger().debug("Registered " + blockStateOverrides.size() + " custom block overrides."); GeyserImpl.getInstance().getLogger().debug("Registered " + blockStateOverrides.size() + " custom block overrides.");
BlockRegistries.CUSTOM_BLOCK_ITEM_OVERRIDES.set(customBlockItemOverrides);
GeyserImpl.getInstance().getLogger().debug("Registered " + customBlockItemOverrides.size() + " custom block item overrides.");
} }
static void generateCustomBlockStates(CustomBlockData customBlock, List<NbtMap> blockStates, List<CustomBlockState> customExtBlockStates, int stateVersion) { static void generateCustomBlockStates(CustomBlockData customBlock, List<NbtMap> blockStates, List<CustomBlockState> customExtBlockStates, int stateVersion) {

Datei anzeigen

@ -188,9 +188,15 @@ public abstract class ItemTranslator {
ItemTranslator itemStackTranslator = ITEM_STACK_TRANSLATORS.getOrDefault(bedrockItem.getJavaId(), DEFAULT_TRANSLATOR); ItemTranslator itemStackTranslator = ITEM_STACK_TRANSLATORS.getOrDefault(bedrockItem.getJavaId(), DEFAULT_TRANSLATOR);
ItemData.Builder builder = itemStackTranslator.translateToBedrock(itemStack, bedrockItem, session.getItemMappings()); ItemData.Builder builder = itemStackTranslator.translateToBedrock(itemStack, bedrockItem, session.getItemMappings());
if (bedrockItem.isBlock()) { if (bedrockItem.isBlock()) {
CustomBlockData customBlockData = BlockRegistries.CUSTOM_BLOCK_ITEM_OVERRIDES.getOrDefault(bedrockItem.getJavaIdentifier(), null);
if (customBlockData != null) {
translateCustomBlock(customBlockData, session, builder);
} else {
builder.blockRuntimeId(bedrockItem.getBedrockBlockId()); builder.blockRuntimeId(bedrockItem.getBedrockBlockId());
} }
}
if (bedrockItem == session.getItemMappings().getStoredItems().playerHead()) { if (bedrockItem == session.getItemMappings().getStoredItems().playerHead()) {
translatePlayerHead(session, nbt, builder); translatePlayerHead(session, nbt, builder);
@ -302,6 +308,12 @@ public abstract class ItemTranslator {
.getItemMapping(javaId, itemStack.getNbt(), session.getItemMappings()); .getItemMapping(javaId, itemStack.getNbt(), session.getItemMappings());
int itemId = mapping.getBedrockId(); int itemId = mapping.getBedrockId();
CustomBlockData customBlockData = BlockRegistries.CUSTOM_BLOCK_ITEM_OVERRIDES.getOrDefault(mapping.getJavaIdentifier(), null);
if (customBlockData != null) {
itemId = session.getItemMappings().getCustomBlockItemIds().getInt(customBlockData);
}
if (mapping == session.getItemMappings().getStoredItems().playerHead()) { if (mapping == session.getItemMappings().getStoredItems().playerHead()) {
CustomSkull customSkull = getCustomSkull(session, itemStack.getNbt()); CustomSkull customSkull = getCustomSkull(session, itemStack.getNbt());
if (customSkull != null) { if (customSkull != null) {
@ -547,6 +559,16 @@ public abstract class ItemTranslator {
} }
} }
/**
* Translates a custom block override
*/
private static void translateCustomBlock(CustomBlockData customBlockData, GeyserSession session, ItemData.Builder builder) {
int itemId = session.getItemMappings().getCustomBlockItemIds().getInt(customBlockData);
int blockRuntimeId = session.getBlockMappings().getCustomBlockStateIds().getInt(customBlockData.defaultBlockState());
builder.id(itemId);
builder.blockRuntimeId(blockRuntimeId);
}
private static CustomSkull getCustomSkull(GeyserSession session, CompoundTag nbt) { private static CustomSkull getCustomSkull(GeyserSession session, CompoundTag nbt) {
if (nbt != null && nbt.contains("SkullOwner")) { if (nbt != null && nbt.contains("SkullOwner")) {
if (!(nbt.get("SkullOwner") instanceof CompoundTag skullOwner)) { if (!(nbt.get("SkullOwner") instanceof CompoundTag skullOwner)) {
@ -577,6 +599,4 @@ public abstract class ItemTranslator {
} }
} }
// TODO: Add translator for generic custom blocks
} }