Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework diseases to become datapacks #10361

Merged
merged 13 commits into from
Dec 1, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,6 @@ public class CompatibilityManager implements ICompatibilityManager
*/
private ImmutableSet<ItemStorage> beekeeperflowers = ImmutableSet.of();

/**
* Set of all possible diseases.
*/
private final Map<String, Disease> diseases = new HashMap<>();

/**
* List of diseases including the random factor.
*/
private final List<String> diseaseList = new ArrayList<>();

/**
* List of lucky oreBlocks which get dropped by the miner.
*/
Expand Down Expand Up @@ -177,8 +167,6 @@ private void clear()

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

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

discoverCompostRecipes(recipeManager);
Expand Down Expand Up @@ -252,7 +239,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
discoverLuckyOres();
discoverRecruitCosts();
discoverDiseases();
discoverModCompat();
}

Expand Down Expand Up @@ -469,24 +455,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 @@ -897,57 +865,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 @@ -99,7 +99,6 @@ public class ServerConfiguration extends AbstractConfiguration
public final ForgeConfigSpec.ConfigValue<List<? extends String>> configListStudyItems;
public final ForgeConfigSpec.ConfigValue<List<? extends String>> configListRecruitmentItems;
public final ForgeConfigSpec.ConfigValue<List<? extends String>> luckyOres;
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 @@ -227,12 +226,6 @@ protected ServerConfiguration(final ForgeConfigSpec.Builder builder)
"minecraft:emerald_ore!1"),
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
60 changes: 28 additions & 32 deletions src/main/java/com/minecolonies/api/crafting/ItemStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@
*/
public class ItemStorage
{
/**
* The stack to store.
*/
private final ItemStack stack;

/**
* Set this to ignore the damage value in comparisons.
*/
protected final boolean shouldIgnoreDamageValue;

/**
* Set this to ignore the damage value in comparisons.
*/
protected final boolean shouldIgnoreNBTValue;

/**
* The stack to store.
*/
private final ItemStack stack;
/**
* Amount of the storage.
*/
Expand Down Expand Up @@ -122,15 +119,15 @@ public ItemStorage(@NotNull final Item item)

/**
* Creates an instance of the storage from JSON
*
*
* @param jObject the JSON Object to parse
*/
public ItemStorage(@NotNull final JsonObject jObject)
{
if (jObject.has(ITEM_PROP))
{
final ItemStack parsedStack = ItemStackUtils.idToItemStack(jObject.get(ITEM_PROP).getAsString());
if(jObject.has(COUNT_PROP))
if (jObject.has(COUNT_PROP))
{
parsedStack.setCount(jObject.get(COUNT_PROP).getAsInt());
this.amount = jObject.get(COUNT_PROP).getAsInt();
Expand All @@ -140,23 +137,16 @@ public ItemStorage(@NotNull final JsonObject jObject)
this.amount = parsedStack.getCount();
}
this.stack = parsedStack;
if(jObject.has(MATCHTYPE_PROP))
if (jObject.has(MATCHTYPE_PROP))
{
String matchType = jObject.get(MATCHTYPE_PROP).getAsString();
if(matchType.equals(MATCH_NBTIGNORE))
{
this.shouldIgnoreNBTValue = true;
}
else // includes "exact"
{
this.shouldIgnoreNBTValue = false;
}
this.shouldIgnoreNBTValue = matchType.equals(MATCH_NBTIGNORE);
}
else
{
this.shouldIgnoreNBTValue = false;
}
this.shouldIgnoreDamageValue= true;
this.shouldIgnoreDamageValue = true;
}
else
{
Expand Down Expand Up @@ -236,14 +226,6 @@ public boolean ignoreNBT()
return shouldIgnoreNBTValue;
}

@Override
public String toString()
{
final ItemStack stack = this.stack.copy();
stack.setCount(this.amount);
return stack.toString();
}

@Override
public int hashCode()
{
Expand All @@ -264,18 +246,30 @@ public boolean equals(final Object o)
}

final ItemStorage that = (ItemStorage) o;
return ItemStackUtils.compareItemStacksIgnoreStackSize(that.getItemStack(), this.getItemStack(), !(this.shouldIgnoreDamageValue || that.shouldIgnoreDamageValue), !(this.shouldIgnoreNBTValue || that.shouldIgnoreNBTValue));
return ItemStackUtils.compareItemStacksIgnoreStackSize(that.getItemStack(),
this.getItemStack(),
!(this.shouldIgnoreDamageValue || that.shouldIgnoreDamageValue),
!(this.shouldIgnoreNBTValue || that.shouldIgnoreNBTValue));
}

@Override
public String toString()
{
final ItemStack stack = this.stack.copy();
stack.setCount(this.amount);
return stack.toString();
}

/**
* Ensure that two ItemStorage have the same comparison defintion
*
* @param that the item to compare to
* @return true if the comparisons match
*/
public boolean matchDefinitionEquals(ItemStorage that)
{
return this.shouldIgnoreDamageValue == that.shouldIgnoreDamageValue
&& this.shouldIgnoreNBTValue == that.shouldIgnoreNBTValue;
return this.shouldIgnoreDamageValue == that.shouldIgnoreDamageValue
&& this.shouldIgnoreNBTValue == that.shouldIgnoreNBTValue;
}

/**
Expand Down Expand Up @@ -311,7 +305,7 @@ public int getRemainingDurablityValue()

/**
* Is this an empty ItemStorage
*
*
* @return true if empty
*/
public boolean isEmpty()
Expand All @@ -321,17 +315,19 @@ public boolean isEmpty()

/**
* Make a copy of the ItemStorage
*
* @return a copy
*/
public ItemStorage copy()
{
ItemStorage newInstance = new ItemStorage(stack.copy(), shouldIgnoreDamageValue, shouldIgnoreNBTValue);
newInstance.setAmount(amount);
return newInstance;
}
}

Thodor12 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Get an immutable version of this item storage
*
* @return immutable wrapper
*/
public ImmutableItemStorage toImmutable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.minecolonies.api.entity.citizen.citizenhandlers;

import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.core.datalistener.DiseasesListener;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Citizen disease handler interface.
Expand Down Expand Up @@ -36,11 +38,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
DiseasesListener.Disease getDisease();
Thodor12 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Cure the citizen.
Expand Down
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
Loading