Skip to content

Commit

Permalink
Made the apoli:powers item component lenient
Browse files Browse the repository at this point in the history
  • Loading branch information
eggohito committed Oct 2, 2024
1 parent 5833724 commit 9e0946e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void appendTooltip(AttributeModifierSlot modifierSlot, Item.TooltipContex

for (Entry entry : entries) {

Power power = PowerManager.getOptional(entry.powerId()).orElse(null);
Power power = PowerManager.getNullable(entry.powerId());
if (power == null || entry.hidden() || !entry.slot().equals(modifierSlot)) {
continue;
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public static void onChangeEquipment(LivingEntity entity, EquipmentSlot equipmen
public record Entry(Identifier powerId, AttributeModifierSlot slot, boolean hidden, boolean negative) {

public static final MapCodec<Entry> MAP_CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
PowerManager.VALIDATING_CODEC.fieldOf("power").forGetter(Entry::powerId),
Identifier.CODEC.fieldOf("power").forGetter(Entry::powerId),
AttributeModifierSlot.CODEC.fieldOf("slot").forGetter(Entry::slot),
Codec.BOOL.optionalFieldOf("hidden", false).forGetter(Entry::hidden),
Codec.BOOL.optionalFieldOf("negative", false).forGetter(Entry::negative)
Expand Down
31 changes: 14 additions & 17 deletions src/main/java/io/github/apace100/apoli/power/PowerManager.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package io.github.apace100.apoli.power;

import com.google.gson.*;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import io.github.apace100.apoli.Apoli;
import io.github.apace100.apoli.component.PowerHolderComponent;
import io.github.apace100.apoli.integration.*;
import io.github.apace100.apoli.networking.packet.s2c.SyncPowersS2CPacket;
import io.github.apace100.apoli.power.factory.PowerTypeFactory;
import io.github.apace100.apoli.power.type.PowerTypes;
import io.github.apace100.apoli.power.type.PowerType;
import io.github.apace100.apoli.power.type.PowerTypes;
import io.github.apace100.apoli.registry.ApoliRegistries;
import io.github.apace100.calio.CalioServer;
import io.github.apace100.calio.data.IdentifiableMultiJsonDataLoader;
import io.github.apace100.calio.data.MultiJsonDataContainer;
import io.github.apace100.calio.data.SerializableData;
import it.unimi.dsi.fastutil.objects.*;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
Expand Down Expand Up @@ -44,13 +45,10 @@

import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;

@SuppressWarnings("unused")
public class PowerManager extends IdentifiableMultiJsonDataLoader implements IdentifiableResourceReloadListener {

public static final Codec<Identifier> VALIDATING_CODEC = Identifier.CODEC.comapFlatMap(id -> contains(id) ? DataResult.success(id) : DataResult.error(() -> "Couldn't get power from ID \"" + id + "\", as it wasn't registered!"), Function.identity());

public static final Set<Identifier> DEPENDENCIES = new HashSet<>();
public static final Identifier ID = Apoli.identifier("powers");

Expand Down Expand Up @@ -564,23 +562,22 @@ public static void receive(SyncPowersS2CPacket packet, ClientPlayNetworking.Cont
}

public static DataResult<Power> getResult(Identifier id) {

try {
return DataResult.success(get(id));
}

catch (Exception e) {
return DataResult.error(e::getMessage);
}

return contains(id)
? DataResult.success(POWERS_BY_ID.get(id))
: DataResult.error(() -> "Couldn't get power from ID \"" + id + "\", as it wasn't registered!");
}

public static Optional<Power> getOptional(Identifier id) {
return Optional.ofNullable(POWERS_BY_ID.get(id));
return getResult(id).result();
}

@Nullable
public static Power getNullable(Identifier id) {
return POWERS_BY_ID.get(id);
}

public static Power get(Identifier id) {
return getOptional(id).orElseThrow(() -> new IllegalArgumentException("Couldn't get power from ID \"" + id + "\", as it wasn't registered!"));
return getResult(id).getOrThrow();
}

public static Set<Map.Entry<Identifier, Power>> entrySet() {
Expand Down

0 comments on commit 9e0946e

Please sign in to comment.