Skip to content

Commit

Permalink
Better validation of blocks & fluids in heat properties recipe
Browse files Browse the repository at this point in the history
There does not necessarily need to be a mod loaded for the domain part
of the block/fluid resource location; e.g. KubeJS could be used to
register blocks in a custom namespace.  So check for the existence of
the block/fluid in the corresponding registry before do any mod-loaded
checks.

#1236
  • Loading branch information
desht committed Oct 3, 2023
1 parent a40d06f commit ed328bc
Showing 1 changed file with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,7 @@ public void write(FriendlyByteBuf buffer) {
PacketUtil.writeNullableBlockState(buffer, transformCold);
PacketUtil.writeNullableBlockState(buffer, transformHotFlowing);
PacketUtil.writeNullableBlockState(buffer, transformColdFlowing);
buffer.writeVarInt(predicates.size());
predicates.forEach((key, val) -> {
buffer.writeUtf(key);
buffer.writeUtf(val);
});
buffer.writeMap(predicates, FriendlyByteBuf::writeUtf, FriendlyByteBuf::writeUtf);
buffer.writeInt(temperature);
buffer.writeInt(heatCapacity);
buffer.writeDouble(thermalResistance);
Expand Down Expand Up @@ -238,25 +234,21 @@ public T fromJson(ResourceLocation recipeId, JsonObject json) {
if (blockId.toString().equals("minecraft:air")) {
throw new JsonSyntaxException("minecraft:air block heat properties may not be changed!");
}
if (!ModList.get().isLoaded(blockId.getNamespace())) {
Log.info("ignoring heat properties for block %s: mod not loaded", blockId);
block = ForgeRegistries.BLOCKS.getValue(blockId);
if (!validateBlock(blockId, block)) {
return null;
}
block = ForgeRegistries.BLOCKS.getValue(blockId);
validateBlock(blockId, block);
fluid = Objects.requireNonNull(block).defaultBlockState().getFluidState().getType(); // ok if this is absent
} else if (json.has("fluid")) {
ResourceLocation fluidId = new ResourceLocation(GsonHelper.getAsString(json, "fluid"));
if (!ModList.get().isLoaded(fluidId.getNamespace())) {
Log.info("ignoring heat properties for fluid %s: mod not loaded", fluidId);
fluid = ForgeRegistries.FLUIDS.getValue(fluidId);
if (!validateFluid(fluidId, fluid)) {
return null;
}
fluid = ForgeRegistries.FLUIDS.getValue(fluidId);
if (fluid == null || fluid == Fluids.EMPTY) {
throw new JsonSyntaxException("unknown fluid " + fluidId);
block = Objects.requireNonNull(fluid).defaultFluidState().createLegacyBlock().getBlock(); // not ok if this is absent!
if (!validateBlock(fluidId, block)) {
return null;
}
block = fluid.defaultFluidState().createLegacyBlock().getBlock(); // not ok if this is absent!
validateBlock(fluidId, block);
} else {
throw new JsonSyntaxException("heat properties entry must have a \"block\" or \"fluid\" field!");
}
Expand Down Expand Up @@ -320,19 +312,15 @@ public T fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer) {
BlockState transformCold = PacketUtil.readNullableBlockState(buffer);
BlockState transformHotFlowing = PacketUtil.readNullableBlockState(buffer);
BlockState transformColdFlowing = PacketUtil.readNullableBlockState(buffer);
ImmutableMap.Builder<String,String> predBuilder = ImmutableMap.builder();
int nPredicates = buffer.readVarInt();
for (int i = 0; i < nPredicates; i++) {
predBuilder.put(buffer.readUtf(), buffer.readUtf());
}
Map<String,String> predicates = buffer.readMap(FriendlyByteBuf::readUtf, FriendlyByteBuf::readUtf);
int temperature = buffer.readInt();
int heatCapacity = buffer.readInt();
double thermalResistance = buffer.readDouble();
String descriptionKey = buffer.readUtf();

return factory.create(recipeId, block,
transformHot, transformHotFlowing, transformCold, transformColdFlowing,
heatCapacity, temperature, thermalResistance, predBuilder.build(), descriptionKey
heatCapacity, temperature, thermalResistance, predicates, descriptionKey
);
}

Expand All @@ -341,10 +329,29 @@ public void toNetwork(FriendlyByteBuf buffer, T recipe) {
recipe.write(buffer);
}

private void validateBlock(ResourceLocation blockId, Block block) {
private boolean validateFluid(ResourceLocation fluidId, Fluid fluid) {
if (fluid == null || fluid == Fluids.EMPTY) {
if (!ModList.get().isLoaded(fluidId.getNamespace())) {
Log.info("ignoring heat properties for fluid %s: mod not loaded", fluidId);
} else {
throw new JsonSyntaxException("unknown fluid id: " + fluidId);
}
return false;
}
return true;
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean validateBlock(ResourceLocation blockId, Block block) {
if (block == null || block == Blocks.AIR) {
throw new JsonSyntaxException("unknown block id: " + blockId.toString());
if (!ModList.get().isLoaded(blockId.getNamespace())) {
Log.info("ignoring heat properties for block %s: mod not loaded", blockId);
} else {
throw new JsonSyntaxException("unknown block id: " + blockId);
}
return false;
}
return true;
}

private BlockState parseBlockState(String str) {
Expand Down

0 comments on commit ed328bc

Please sign in to comment.