Skip to content

Commit

Permalink
fix: pet items not being detected correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Morazzer committed Nov 11, 2024
1 parent 078a3b9 commit 06551a0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class CookiesDataComponentTypes {
public static final ComponentType<String> DYE;
@GenerateAccessor
public static final ComponentType<Map<String, Integer>> ATTRIBUTES;
public static final ComponentType<PetInfo> PET_INFO;
public static final ComponentType<String> ENCHANTMENT_ID;

public static final ComponentType<String> CUSTOM_SLOT_TEXT;
Expand Down Expand Up @@ -175,6 +176,11 @@ public class CookiesDataComponentTypes {
}
return map;
});
PET_INFO = register(builder -> builder.codec(new CookiesDataComponent.FakeCodec<>()),
"petInfo",
DataComponentTypes.CUSTOM_DATA,
defaultTest(),
(nbtComponent, s) -> PetInfo.create(nbtComponent.getNbt().getString(s)));
UPGRADE_LEVEL = register(
builder -> builder.codec(Codec.INT),
"upgrade_level",
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/codes/cookies/mod/utils/items/PetInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package codes.cookies.mod.utils.items;

import codes.cookies.mod.repository.RepositoryItem;
import codes.cookies.mod.utils.exceptions.ExceptionHandler;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.Builder;

@Builder
public record PetInfo(String type, boolean active, double exp, RepositoryItem.Tier tier, boolean hideInfo,
int candyUsed, boolean hideRightClick, boolean noMove) {

public static PetInfo EMPTY = new PetInfo("PET", false, 0.0, RepositoryItem.Tier.COMMON, false, 0, false, false);

public static PetInfo create(String s) {
if (s == null) {
return EMPTY;
}
final JsonElement jsonElement = JsonParser.parseString(s);
if (!jsonElement.isJsonObject()) {
return EMPTY;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
final PetInfoBuilder builder = PetInfo.builder();
builder.type(EMPTY.type()).tier(EMPTY.tier());
if (jsonObject.has("type")) {
builder.type(jsonObject.get("type").getAsString());
}
if (jsonObject.has("active")) {
builder.active(jsonObject.get("active").getAsBoolean());
}
if (jsonObject.has("exp")) {
builder.exp(jsonObject.get("exp").getAsDouble());
}
if (jsonObject.has("tier")) {
builder.tier(ExceptionHandler.removeThrowsSilent(
() -> RepositoryItem.Tier.valueOf(jsonObject.get("tier").getAsString()),
RepositoryItem.Tier.COMMON));
}
if (jsonObject.has("hideInfo")) {
builder.hideInfo(jsonObject.get("hideInfo").getAsBoolean());
}
if (jsonObject.has("candyUsed")) {
builder.candyUsed(jsonObject.get("candyUsed").getAsInt());
}
if (jsonObject.has("hideRightClick")) {
builder.hideRightClick(jsonObject.get("hideRightClick").getAsBoolean());
}
if (jsonObject.has("noMove")) {
builder.noMove(jsonObject.get("noMove").getAsBoolean());
}

return builder.build();
}
}
20 changes: 17 additions & 3 deletions src/main/java/codes/cookies/mod/utils/mixins/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codes.cookies.mod.utils.mixins;

import codes.cookies.mod.utils.items.PetInfo;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
Expand All @@ -17,6 +18,7 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;
import net.minecraft.component.ComponentChanges;
import net.minecraft.component.ComponentHolder;
import net.minecraft.component.ComponentMap;
import net.minecraft.component.MergedComponentMap;
import net.minecraft.component.ComponentType;
Expand Down Expand Up @@ -46,7 +48,7 @@
*/
@Mixin(ItemStack.class)
@SuppressWarnings("MissingJavadoc")
public abstract class ItemStackMixin {
public abstract class ItemStackMixin implements ComponentHolder {

@Shadow
@Final
Expand Down Expand Up @@ -76,7 +78,16 @@ public void initializeItemStackWithComponents(
}
}

set(CookiesDataComponentTypes.SELF, (ItemStack) (Object) this);
final PetInfo petInfo = get(CookiesDataComponentTypes.PET_INFO);
if (petInfo != null) {
final RepositoryItem repositoryItem = RepositoryItem.of(petInfo.type() + "/" + petInfo.tier().ordinal());
if (repositoryItem != null) {
set(CookiesDataComponentTypes.REPOSITORY_ITEM, repositoryItem);
set(CookiesDataComponentTypes.SKYBLOCK_ID, repositoryItem.getInternalId());
}
}

set(CookiesDataComponentTypes.SELF, (ItemStack) (Object) this);

ItemStackEvents.EVENT.invoker().create((ItemStack) (Object) this);

Expand Down Expand Up @@ -181,7 +192,10 @@ public void initializeItemStackWithComponents(
@Shadow
public abstract ComponentChanges getComponentChanges();

@WrapOperation(
@Shadow
public abstract Text getName();

@WrapOperation(
method = "getTooltip", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;appendTooltip(Lnet/minecraft/component/ComponentType;" +
Expand Down

0 comments on commit 06551a0

Please sign in to comment.