Skip to content

Commit

Permalink
Fix NPE when no item can be found from a block (GeyserMC#1718)
Browse files Browse the repository at this point in the history
This commit also removes an old map previously used for block entity translators
  • Loading branch information
Camotoy authored Dec 21, 2020
1 parent c921500 commit d69896b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@
import com.google.common.collect.HashBiMap;
import com.nukkitx.nbt.*;
import it.unimi.dsi.fastutil.ints.*;
import it.unimi.dsi.fastutil.objects.*;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.translators.world.block.entity.BlockEntity;
import org.geysermc.connector.utils.FileUtils;
import org.reflections.Reflections;

import java.io.DataInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.Iterator;
import java.util.Map;

public class BlockTranslator {
/**
Expand All @@ -65,8 +68,6 @@ public class BlockTranslator {
// Bedrock carpet ID, used in LlamaEntity.java for decoration
public static final int CARPET = 171;

private static final Int2ObjectMap<String> JAVA_ID_TO_BLOCK_ENTITY_MAP = new Int2ObjectOpenHashMap<>();

public static final Int2DoubleMap JAVA_RUNTIME_ID_TO_HARDNESS = new Int2DoubleOpenHashMap();
public static final Int2BooleanMap JAVA_RUNTIME_ID_TO_CAN_HARVEST_WITH_HAND = new Int2BooleanOpenHashMap();
public static final Int2ObjectMap<String> JAVA_RUNTIME_ID_TO_TOOL_TYPE = new Int2ObjectOpenHashMap<>();
Expand Down Expand Up @@ -175,18 +176,6 @@ public class BlockTranslator {

JAVA_ID_BLOCK_MAP.put(javaId, javaRuntimeId);

// Used for adding all "special" Java block states to block state map
String identifier;
String bedrockIdentifier = entry.getValue().get("bedrock_identifier").asText();
for (Class<?> clazz : ref.getTypesAnnotatedWith(BlockEntity.class)) {
identifier = clazz.getAnnotation(BlockEntity.class).regex();
// Endswith, or else the block bedrock gets picked up for bed
if (bedrockIdentifier.endsWith(identifier) && !identifier.equals("")) {
JAVA_ID_TO_BLOCK_ENTITY_MAP.put(javaRuntimeId, clazz.getAnnotation(BlockEntity.class).name());
break;
}
}

BlockStateValues.storeBlockStateValues(entry, javaRuntimeId);

String cleanJavaIdentifier = entry.getKey().split("\\[")[0];
Expand All @@ -196,6 +185,8 @@ public class BlockTranslator {
JAVA_ID_TO_JAVA_IDENTIFIER_MAP.put(uniqueJavaId, cleanJavaIdentifier);
}

String bedrockIdentifier = entry.getValue().get("bedrock_identifier").asText();

if (!cleanJavaIdentifier.equals(bedrockIdentifier)) {
JAVA_TO_BEDROCK_IDENTIFIERS.put(cleanJavaIdentifier, bedrockIdentifier);
}
Expand Down Expand Up @@ -356,10 +347,6 @@ public static int getJavaBlockState(String javaId) {
return JAVA_ID_BLOCK_MAP.get(javaId);
}

public static String getBlockEntityString(int javaId) {
return JAVA_ID_TO_BLOCK_ENTITY_MAP.get(javaId);
}

public static boolean isWaterlogged(int state) {
return WATERLOGGED.contains(state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,17 @@ public static void findOrCreatePickedBlock(GeyserSession session, String itemNam
}
}

ClientCreativeInventoryActionPacket actionPacket = new ClientCreativeInventoryActionPacket(slot,
new ItemStack(ItemRegistry.getItemEntry(itemName).getJavaId()));
if ((slot - 36) != session.getInventory().getHeldItemSlot()) {
setHotbarItem(session, slot);
ItemEntry entry = ItemRegistry.getItemEntry(itemName);
if (entry != null) {
ClientCreativeInventoryActionPacket actionPacket = new ClientCreativeInventoryActionPacket(slot,
new ItemStack(entry.getJavaId()));
if ((slot - 36) != session.getInventory().getHeldItemSlot()) {
setHotbarItem(session, slot);
}
session.sendDownstreamPacket(actionPacket);
} else {
session.getConnector().getLogger().debug("Cannot find item for block " + itemName);
}
session.sendDownstreamPacket(actionPacket);
}
}

Expand Down

0 comments on commit d69896b

Please sign in to comment.