Skip to content

Commit

Permalink
Simplify damage type advancement deserialization and fix having broke…
Browse files Browse the repository at this point in the history
…n syncing of first element in previous commit
  • Loading branch information
pupnewfster committed Jun 28, 2023
1 parent 1ca4df0 commit bf68f47
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static ConfiguredFeature<ResizableOreFeatureConfig, ResizableOreFeature>
GenerationStep.Decoration.UNDERGROUND_ORES));
})
.add(Registries.DAMAGE_TYPE, context -> {
for (MekanismDamageType damageType : MekanismDamageTypes.DAMAGE_TYPES) {
for (MekanismDamageType damageType : MekanismDamageTypes.DAMAGE_TYPES.values()) {
context.register(damageType.key(), new DamageType(damageType.getMsgId(), damageType.exhaustion()));
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ public ResourceLocation getId() {
@NotNull
@Override
protected TriggerInstance createInstance(@NotNull JsonObject json, @NotNull ContextAwarePredicate playerPredicate, @NotNull DeserializationContext context) {
String damageType = GsonHelper.getAsString(json, JsonConstants.DAMAGE);
//TODO - 1.20: Better filter compare
MekanismDamageType damageSource = MekanismDamageTypes.DAMAGE_TYPES.stream()
.filter(typeRO -> typeRO.registryName().toString().equals(damageType)).findFirst()
.orElseThrow(() -> new JsonSyntaxException("Expected " + JsonConstants.DAMAGE + " to represent a Mekanism damage source."));
return new TriggerInstance(playerPredicate, damageSource, GsonHelper.getAsBoolean(json, JsonConstants.KILLED));
String damage = GsonHelper.getAsString(json, JsonConstants.DAMAGE);
MekanismDamageType damageType = MekanismDamageTypes.DAMAGE_TYPES.get(damage);
if (damageType == null) {
throw new JsonSyntaxException("Expected " + JsonConstants.DAMAGE + " to represent a Mekanism damage type.");
}
return new TriggerInstance(playerPredicate, damageType, GsonHelper.getAsBoolean(json, JsonConstants.KILLED));
}

public void trigger(ServerPlayer player, MekanismDamageType damageSource, boolean hardcoreTotem) {
public void trigger(ServerPlayer player, MekanismDamageType damageType, boolean hardcoreTotem) {
this.trigger(player, instance -> {
//If it is just any damage regardless of killed or the player is dead (or is on hardcore and used up a totem of undying)
if (!instance.killed || player.isDeadOrDying() || hardcoreTotem) {
//And the damage source matches
return instance.damageType.key() == damageSource.key();
return instance.damageType.key() == damageType.key();
}
return false;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public void trackArray(boolean[][] arrayIn) {
@Nullable
private ISyncableData getTrackedData(short property) {
//In theory the property indexing should always be valid but in case we get something that is out of bounds handle it gracefully
if (property > 0 && property < trackedData.size()) {
if (property >= 0 && property < trackedData.size()) {
return trackedData.get(property);
}
Mekanism.logger.warn("Received out of bounds window property {} for container {}. There are currently {} tracked properties.", property,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package mekanism.common.registries;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import mekanism.api.text.IHasTranslationKey;
import mekanism.common.Mekanism;
import net.minecraft.core.RegistryAccess;
Expand All @@ -16,16 +16,16 @@

public class MekanismDamageTypes {

private static final List<MekanismDamageType> INTERNAL_DAMAGE_TYPES = new ArrayList<>();
public static final List<MekanismDamageType> DAMAGE_TYPES = Collections.unmodifiableList(INTERNAL_DAMAGE_TYPES);
private static final Map<String, MekanismDamageType> INTERNAL_DAMAGE_TYPES = new HashMap<>();
public static final Map<String, MekanismDamageType> DAMAGE_TYPES = Collections.unmodifiableMap(INTERNAL_DAMAGE_TYPES);

public static final MekanismDamageType LASER = new MekanismDamageType("laser", 0.1F);
public static final MekanismDamageType RADIATION = new MekanismDamageType("radiation");

public record MekanismDamageType(ResourceKey<DamageType> key, float exhaustion) implements IHasTranslationKey {

public MekanismDamageType {
INTERNAL_DAMAGE_TYPES.add(this);
INTERNAL_DAMAGE_TYPES.put(key.location().toString(), this);
}

private MekanismDamageType(String name) {
Expand Down

0 comments on commit bf68f47

Please sign in to comment.