Skip to content

Commit

Permalink
Merge pull request #44 from UselessBullets/bugFix
Browse files Browse the repository at this point in the history
Bug fix
  • Loading branch information
UselessBullets authored Jan 12, 2024
2 parents 0b2ce51 + 95ea999 commit 6dafe59
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ bta_version=7.1-pre1a
loader_version=0.14.19-babric.3-bta

# Mod
mod_version=3.1.1
mod_version=3.1.2
mod_group=turniplabs
mod_name=halplibe

28 changes: 28 additions & 0 deletions src/main/java/turniplabs/halplibe/HalpLibe.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;
import net.minecraft.core.Global;
import net.minecraft.core.block.Block;
import net.minecraft.core.item.Item;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import turniplabs.halplibe.helper.AchievementHelper;
Expand All @@ -15,6 +17,8 @@
import turniplabs.halplibe.util.toml.Toml;
import turniplabs.halplibe.util.version.PacketModList;

import java.util.HashMap;

public class HalpLibe implements ModInitializer, PreLaunchEntrypoint{
public static final String MOD_ID = "halplibe";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
Expand Down Expand Up @@ -46,6 +50,30 @@ public class HalpLibe implements ModInitializer, PreLaunchEntrypoint{
public static String addModId(String modId, String name) {
return modId + "." + name;
}
public static HashMap<String, Integer> itemKeyToIdMap = new HashMap<>();
public static int getTrueItemOrBlockId(String key){
// This all exists since the item key to id maps are somewhat unreliable due to blocks having their keys remapped after creation
if (itemKeyToIdMap.containsKey(key)) return itemKeyToIdMap.get(key);
if (key.startsWith("item")){
for (Item item : Item.itemsList){
if (item != null && item.getKey() != null && !item.getKey().isEmpty()){
itemKeyToIdMap.put(item.getKey(), item.id);
if (item.getKey().matches(key)) return item.id;
}
}
throw new IllegalArgumentException("Could not find an item that corresponds to the key '" + key + "'");
}
if (key.startsWith("tile")){
for (Block item : Block.blocksList){
if (item != null && item.getKey() != null && !item.getKey().isEmpty()){
itemKeyToIdMap.put(item.getKey(), item.id);
if (item.getKey().matches(key)) return item.id;
}
}
throw new IllegalArgumentException("Could not find a block that corresponds to the key '" + key + "'");
}
throw new IllegalArgumentException("Key '" + key + "' does not start with a valid predicate of 'item' or 'tile'");
}
@Override
public void onInitialize() {
AchievementHelper.addPage(VANILLA_ACHIEVEMENTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import turniplabs.halplibe.HalpLibe;

import java.lang.reflect.Type;

Expand All @@ -22,21 +23,7 @@ protected void deserializeKey(JsonElement json, Type typeOfT, JsonDeserializatio
JsonObject obj = json.getAsJsonObject();
if (obj.has("key")) {
String key = obj.get("key").getAsString();
Integer itemId;
if (key.startsWith("tile")){
itemId = Block.keyToIdMap.get(key);
if (itemId == null) {
throw new IllegalArgumentException("Null return when trying to located block from key '" + obj.get("key") + "'");
}
} else if (key.startsWith("item")) {
itemId = Item.nameToIdMap.get(key);
if (itemId == null) {
throw new IllegalArgumentException("Null return when trying to located item from key '" + obj.get("key") + "'");
}
} else {
throw new IllegalArgumentException("Key '" + key + "' does not start with a valid predicate of 'item' or 'tile'");
}

int itemId = HalpLibe.getTrueItemOrBlockId(key);
ItemStack stack = obj.has("amount") ? new ItemStack(itemId, obj.get("amount").getAsInt(), obj.get("meta").getAsInt()) : new ItemStack(itemId, 1, obj.get("meta").getAsInt());
cir.setReturnValue(stack);
}
Expand Down

0 comments on commit 6dafe59

Please sign in to comment.