Skip to content

Commit

Permalink
Rework diseases to become datapacks (#10361)
Browse files Browse the repository at this point in the history
Diseases use datapacks in favor of configuration values
Upgraded the RandomCollection introduced in Rework lucky ores to become datapacks #10343 to allow for key based values
Improved code regarding diseases by using more up-to-date code
  • Loading branch information
Thodor12 authored Dec 1, 2024
1 parent 8a5def5 commit 3f76f96
Show file tree
Hide file tree
Showing 22 changed files with 506 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,9 @@ public class CompatibilityManager implements ICompatibilityManager
private ImmutableSet<ItemStorage> beekeeperflowers = ImmutableSet.of();

/**
* Set of all possible diseases.
* List of lucky oreBlocks which get dropped by the miner.
*/
private final Map<String, Disease> diseases = new HashMap<>();

/**
* List of diseases including the random factor.
*/
private final List<String> diseaseList = new ArrayList<>();
private final Map<Integer, List<ItemStorage>> luckyOres = new HashMap<>();

/**
* The items and weights of the recruitment.
Expand Down Expand Up @@ -170,8 +165,6 @@ private void clear()
compostRecipes.clear();

recruitmentCostsWeights.clear();
diseases.clear();
diseaseList.clear();
monsters = ImmutableSet.of();
creativeModeTabMap.clear();
}
Expand All @@ -188,7 +181,6 @@ public void discover(@NotNull final RecipeManager recipeManager, final Level lev
discoverAllItems(level);

discoverRecruitCosts();
discoverDiseases();
discoverModCompat();

discoverCompostRecipes(recipeManager);
Expand Down Expand Up @@ -243,7 +235,6 @@ public void deserialize(@NotNull final FriendlyByteBuf buf, final ClientLevel le

// the below are loaded from config files, which have been synched already by this point
discoverRecruitCosts();
discoverDiseases();
discoverModCompat();
}

Expand Down Expand Up @@ -460,24 +451,6 @@ public Set<ItemStorage> getImmutableFlowers()
return beekeeperflowers;
}

@Override
public String getRandomDisease()
{
return diseaseList.get(random.nextInt(diseaseList.size()));
}

@Override
public Disease getDisease(final String disease)
{
return diseases.get(disease);
}

@Override
public List<Disease> getDiseases()
{
return new ArrayList<>(diseases.values());
}

@Override
public List<Tuple<Item, Integer>> getRecruitmentCostsWeights()
{
Expand Down Expand Up @@ -802,57 +775,6 @@ private void discoverRecruitCosts()
Log.getLogger().info("Finished discovering recruitment costs");
}

/**
* Go through the disease config and setup all possible diseases.
*/
private void discoverDiseases()
{
if (diseases.isEmpty())
{
for (final String disease : MinecoloniesAPIProxy.getInstance().getConfig().getServer().diseases.get())
{
final String[] split = disease.split(",");
if (split.length < 3)
{
Log.getLogger().warn("Wrongly configured disease: " + disease);
continue;
}

try
{
final String name = split[0];
final int rarity = Integer.parseInt(split[1]);

final List<ItemStack> cure = new ArrayList<>();

for (int i = 2; i < split.length; i++)
{
final String[] theItem = split[i].split(":");
final Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(theItem[0], theItem[1]));
if (item == null || item == Items.AIR)
{
Log.getLogger().warn("Invalid cure item: " + disease);
continue;
}

final ItemStack stack = new ItemStack(item, 1);
cure.add(stack);
}
diseases.put(name, new Disease(name, rarity, cure));
for (int i = 0; i < rarity; i++)
{
diseaseList.add(name);
}
}
catch (final NumberFormatException e)
{
Log.getLogger().warn("Wrongly configured disease: " + disease);
}
}
}
Log.getLogger().info("Finished discovering diseases");
}

private static CompoundTag writeLeafSaplingEntryToNBT(final BlockState state, final ItemStorage storage)
{
final CompoundTag compound = NbtUtils.writeBlockState(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.collect.ImmutableSet;
import com.minecolonies.api.crafting.CompostRecipe;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.util.Disease;
import com.minecolonies.api.util.Tuple;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -139,28 +138,6 @@ public interface ICompatibilityManager
*/
ImmutableSet<ResourceLocation> getAllMonsters();

/**
* Get a random disease of the compat manager.
*
* @return a randomly chosen disease.
*/
String getRandomDisease();

/**
* Get a disease by the ID.
*
* @param disease the id.
* @return the disease.
*/
Disease getDisease(String disease);

/**
* Get the list of diseases.
*
* @return a copy of the list.
*/
List<Disease> getDiseases();

/**
* Gets the list of recruitment costs with weights
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public class ServerConfiguration extends AbstractConfiguration
* -------------------------------------------------------------------------------- */

public final ForgeConfigSpec.ConfigValue<List<? extends String>> configListRecruitmentItems;
public final ForgeConfigSpec.ConfigValue<List<? extends String>> diseases;
public final ForgeConfigSpec.BooleanValue auditCraftingTags;
public final ForgeConfigSpec.BooleanValue debugInventories;
public final ForgeConfigSpec.BooleanValue blueprintBuildMode;
Expand Down Expand Up @@ -208,12 +207,6 @@ protected ServerConfiguration(final ForgeConfigSpec.Builder builder)
"minecraft:quartz;3"),
s -> s instanceof String);

diseases = defineList(builder, "diseases",
Arrays.asList("Influenza,100,minecraft:carrot,minecraft:potato",
"Measles,10,minecraft:dandelion,minecraft:kelp,minecraft:poppy",
"Smallpox,1,minecraft:honey_bottle,minecraft:golden_apple"),
s -> s instanceof String);

auditCraftingTags = defineBoolean(builder, "auditcraftingtags", false);
debugInventories = defineBoolean(builder, "debuginventories", false);
blueprintBuildMode = defineBoolean(builder, "blueprintbuildmode", false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.minecolonies.api.entity.citizen.citizenhandlers;

import com.minecolonies.core.datalistener.model.Disease;
import com.minecolonies.api.colony.ICitizenData;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.Nullable;

/**
* Citizen disease handler interface.
Expand Down Expand Up @@ -35,11 +37,12 @@ public interface ICitizenDiseaseHandler
void read(final CompoundTag compound);

/**
* get the disease identifier.
* Get the current disease, if any.
*
* @return the disease identifier.
* @return the disease instance.
*/
String getDisease();
@Nullable
Disease getDisease();

/**
* Cure the citizen.
Expand Down Expand Up @@ -70,7 +73,9 @@ public interface ICitizenDiseaseHandler

/**
* Set a disease on the citizen.
*
* @param disease to set.
* @return true if they actually became sick.
*/
void setDisease(String disease);
boolean setDisease(Disease disease);
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public BlockPos getPos()
}

/**
* Get for the remaining items.
* Get for the remaining items.
* @return
*/
public List<ItemStack> getRemainingItems()
Expand Down
88 changes: 0 additions & 88 deletions src/main/java/com/minecolonies/api/util/Disease.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static void init()
citizen -> citizen.getColony() != null && citizen.getEntity().isPresent() && citizen.getCitizenDiseaseHandler().isSick()
&& citizen.getColony().getBuildingManager().getBestBuilding(citizen.getEntity().get(), BuildingHospital.class) == null);
InteractionValidatorRegistry.registerStandardPredicate(Component.translatable(WAITING_FOR_CURE),
citizen -> citizen.getColony() != null && citizen.getEntity().isPresent() && !citizen.getCitizenDiseaseHandler().getDisease().isEmpty());
citizen -> citizen.getColony() != null && citizen.getEntity().isPresent() && citizen.getCitizenDiseaseHandler().getDisease() != null);

InteractionValidatorRegistry.registerPosBasedPredicate(Component.translatable(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL),
(citizen, pos) ->
Expand Down
Loading

0 comments on commit 3f76f96

Please sign in to comment.