Skip to content

Commit

Permalink
Update item comparing mechanism to fix #4, bump mod version to 1.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
madjoel committed May 22, 2023
1 parent 50bf3a2 commit 1ef38b6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ loader_version = 0.14.17
fabric_version = 0.75.1+1.19.3

# Mod Properties
mod_version = 1.1.4
mod_version = 1.1.5
maven_group = one.laqua
archives_base_name = waig

Expand Down
21 changes: 13 additions & 8 deletions src/main/java/one/laqua/waig/client/CompassHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import one.laqua.waig.client.config.WaigConfig;
import one.laqua.waig.mixin.BossBarHudAccessor;
import one.laqua.waig.mixin.CombinedInventoryAccessor;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -20,9 +21,7 @@ public class CompassHud {
private static final String compass_text_triple = compass_text_simple + compass_text_simple + compass_text_simple;
private static final int oneSideLength = 20;

private static final Set<ItemStack> compass_stacks = WaigConfig.getCompassItems().stream()
.map(Item::getDefaultStack)
.collect(Collectors.toSet());
private static final Set<Integer> compass_stacks = WaigConfig.getCompassItems();

private static boolean visible = true;

Expand All @@ -44,16 +43,22 @@ public static void onHudRender(MatrixStack matrices, float v) {
// nothing to check in this case, just continue
}
case INVENTORY -> {
boolean containsCompass = compass_stacks.stream()
.anyMatch(itemStack -> p.getInventory().contains(itemStack));
Set<Integer> compassIds = new HashSet<>(compass_stacks);
Set<Integer> inventory = ((CombinedInventoryAccessor) p.getInventory()).getCombinedInventory()
.stream().flatMap(defaultedList -> defaultedList.stream()
.map(e -> Item.getRawId(e.getItem())))
.collect(Collectors.toSet());

compassIds.retainAll(inventory);
boolean containsCompass = !compassIds.isEmpty();

if (!containsCompass) {
return;
}
}
case HAND -> {
boolean holdsCompass = WaigConfig.getCompassItems().contains(p.getOffHandStack().getItem())
|| WaigConfig.getCompassItems().contains(p.getMainHandStack().getItem());
boolean holdsCompass = WaigConfig.getCompassItems().contains(Item.getRawId(p.getOffHandStack().getItem()))
|| WaigConfig.getCompassItems().contains(Item.getRawId(p.getMainHandStack().getItem()));

if (!holdsCompass) {
return;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/one/laqua/waig/client/config/WaigConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public class WaigConfig {
private static final String DEFAULT_COMPASS_ID = "minecraft:compass";

private static HudShowMode hudShowMode = HudShowMode.ALWAYS;
private static Set<Item> compassItems = Set.of(Items.COMPASS);
private static Set<Integer> compassItems = Set.of(Item.getRawId(Items.COMPASS));

public static HudShowMode getHudShowMode() {
return hudShowMode;
}

public static Set<Item> getCompassItems() {
public static Set<Integer> getCompassItems() {
return compassItems;
}

Expand Down Expand Up @@ -74,7 +74,7 @@ public static boolean readConfigFile() {

if (key.equals(KEY_COMPASS_ITEMS)) {
String[] potentialItems = pieces[1].strip().toLowerCase().split(",");
Set<Item> configItems = Arrays.stream(potentialItems)
Set<Integer> configItems = Arrays.stream(potentialItems)
.filter(potentialItemId -> potentialItemId.contains(":"))
.map(potentialItemId -> {
String[] idPieces = potentialItemId.split(":");
Expand All @@ -87,15 +87,15 @@ public static boolean readConfigFile() {
WaigClient.log(Level.ERROR, "The config value '" + potentialItemId + "' " +
"contains illegal characters and cannot be parsed into an item. Please " +
"check the config file for errors. Ignoring this value.");
return Items.AIR;
return Item.getRawId(Items.AIR);
}

Identifier itemIdentifier = new Identifier(idPieces[0].strip(), idPieces[1].strip());

// this will need updating on Minecraft versions >=1.19.3, see https://fabricmc.net/wiki/tutorial:registry
return Registries.ITEM.get(itemIdentifier);
return Item.getRawId(Registries.ITEM.get(itemIdentifier));
})
.filter(item -> !item.equals(Items.AIR))
.filter(item -> !item.equals(Item.getRawId(Items.AIR)))
.collect(Collectors.toSet());
if (!configItems.isEmpty()) {
WaigConfig.compassItems = configItems;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/one/laqua/waig/mixin/CombinedInventoryAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package one.laqua.waig.mixin;

import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.List;

@Mixin(PlayerInventory.class)
public interface CombinedInventoryAccessor {

@Accessor
List<DefaultedList<ItemStack>> getCombinedInventory();

}
3 changes: 2 additions & 1 deletion src/main/resources/waig.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"package": "one.laqua.waig.mixin",
"compatibilityLevel": "JAVA_16",
"mixins": [
"BossBarHudAccessor"
"BossBarHudAccessor",
"CombinedInventoryAccessor"
],
"client": [],
"injectors": {
Expand Down

0 comments on commit 1ef38b6

Please sign in to comment.