Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Shanwer committed Nov 12, 2024
2 parents 8cdb3f2 + e645935 commit b0daba6
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/main/java/org/geysermc/geyser/item/Items.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.geysermc.geyser.item.type.FishingRodItem;
import org.geysermc.geyser.item.type.GoatHornItem;
import org.geysermc.geyser.item.type.Item;
import org.geysermc.geyser.item.type.LightItem;
import org.geysermc.geyser.item.type.MaceItem;
import org.geysermc.geyser.item.type.MapItem;
import org.geysermc.geyser.item.type.PlayerHeadItem;
Expand Down Expand Up @@ -526,7 +527,7 @@ public final class Items {
public static final Item RED_TERRACOTTA = register(new BlockItem(builder(), Blocks.RED_TERRACOTTA));
public static final Item BLACK_TERRACOTTA = register(new BlockItem(builder(), Blocks.BLACK_TERRACOTTA));
public static final Item BARRIER = register(new BlockItem(builder(), Blocks.BARRIER));
public static final Item LIGHT = register(new BlockItem(builder(), Blocks.LIGHT));
public static final Item LIGHT = register(new LightItem(builder(), Blocks.LIGHT));
public static final Item HAY_BLOCK = register(new BlockItem(builder(), Blocks.HAY_BLOCK));
public static final Item WHITE_CARPET = register(new BlockItem(builder(), Blocks.WHITE_CARPET));
public static final Item ORANGE_CARPET = register(new BlockItem(builder(), Blocks.ORANGE_CARPET));
Expand Down
79 changes: 79 additions & 0 deletions core/src/main/java/org/geysermc/geyser/item/type/LightItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2024 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
*/

package org.geysermc.geyser.item.type;

import org.cloudburstmc.protocol.bedrock.data.definitions.ItemDefinition;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.geysermc.geyser.level.block.property.Properties;
import org.geysermc.geyser.level.block.type.Block;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.BlockStateProperties;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;

public class LightItem extends BlockItem {

public LightItem(Builder builder, Block block, Block... otherBlocks) {
super(builder, block, otherBlocks);
}

@Override
public ItemData.Builder translateToBedrock(GeyserSession session, int count, DataComponents components, ItemMapping mapping, ItemMappings mappings) {
ItemMapping lightLevelMapping = getLightLevelMapping(components, mappings);
if (lightLevelMapping != null) {
return super.translateToBedrock(session, count, components, lightLevelMapping, mappings);
}
return super.translateToBedrock(session, count, components, mapping, mappings);
}

@Override
public ItemMapping toBedrockDefinition(DataComponents components, ItemMappings mappings) {
ItemMapping lightLevelMapping = getLightLevelMapping(components, mappings);
if (lightLevelMapping != null) {
return lightLevelMapping;
}
return super.toBedrockDefinition(components, mappings);
}


private static ItemMapping getLightLevelMapping(DataComponents components, ItemMappings mappings) {
String lightLevel = "15";
if (components != null) {
BlockStateProperties blockStateProperties = components.get(DataComponentType.BLOCK_STATE);

if (blockStateProperties != null) {
lightLevel = blockStateProperties.getProperties().get(Properties.LEVEL.name());
}
}
ItemDefinition definition = mappings.getDefinition("minecraft:light_block_" + lightLevel);
if (definition != null) {
return mappings.getLightBlocks().get(definition.getRuntimeId());
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.geysermc.geyser.item.components.Rarity;
import org.geysermc.geyser.item.type.BlockItem;
import org.geysermc.geyser.item.type.Item;
import org.geysermc.geyser.level.block.property.Properties;
import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.registry.Registries;
import org.geysermc.geyser.registry.type.BlockMappings;
Expand Down Expand Up @@ -508,6 +509,26 @@ public static void populate() {
javaItemToMapping.put(javaItem, mapping);
}

// Add the light block level since it doesn't exist on java but we need it for item conversion
Int2ObjectMap<ItemMapping> lightBlocks = new Int2ObjectOpenHashMap<>();

for (int i = 0; i <= Properties.LEVEL.high(); i++) {
ItemDefinition lightBlock = definitions.get("minecraft:light_block_" + i);
if (lightBlock == null) {
break;
}

ItemMapping lightBlockEntry = ItemMapping.builder()
.javaItem(Items.LIGHT)
.bedrockIdentifier("minecraft:light_block_" + i)
.bedrockDefinition(lightBlock)
.bedrockData(0)
.bedrockBlockDefinition(null)
.customItemOptions(Collections.emptyList())
.build();
lightBlocks.put(lightBlock.getRuntimeId(), lightBlockEntry);
}

ItemDefinition lodestoneCompass = definitions.get("minecraft:lodestone_compass");
if (lodestoneCompass == null) {
throw new RuntimeException("Lodestone compass not found in item palette!");
Expand Down Expand Up @@ -641,6 +662,7 @@ public static void populate() {
.javaOnlyItems(javaOnlyItems)
.buckets(buckets)
.componentItemData(componentItemData)
.lightBlocks(lightBlocks)
.lodestoneCompass(lodestoneEntry)
.customIdMappings(customIdMappings)
.customBlockItemDefinitions(customBlockItemDefinitions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class ItemMappings implements DefinitionRegistry<ItemDefinition> {
* A unique exception as this is an item in Bedrock, but not in Java.
*/
ItemMapping lodestoneCompass;
Int2ObjectMap<ItemMapping> lightBlocks;

ItemData[] creativeItems;
Int2ObjectMap<ItemDefinition> itemDefinitions;
Expand Down Expand Up @@ -136,6 +137,11 @@ public ItemMapping getMapping(ItemData data) {
return lodestoneCompass;
}

ItemMapping lightBlock = lightBlocks.get(definition.getRuntimeId());
if (lightBlock != null) {
return lightBlock;
}

boolean isBlock = data.getBlockDefinition() != null;
boolean hasDamage = data.getDamage() != 0;

Expand Down

0 comments on commit b0daba6

Please sign in to comment.