Skip to content

Commit

Permalink
Update to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Thodor12 committed Nov 17, 2024
1 parent 7c911ed commit cd0c90b
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 185 deletions.
60 changes: 32 additions & 28 deletions src/main/java/com/minecolonies/api/crafting/ItemStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
*/
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 @@ -119,15 +122,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 @@ -137,16 +140,23 @@ 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();
this.shouldIgnoreNBTValue = matchType.equals(MATCH_NBTIGNORE);
if(matchType.equals(MATCH_NBTIGNORE))
{
this.shouldIgnoreNBTValue = true;
}
else // includes "exact"
{
this.shouldIgnoreNBTValue = false;
}
}
else
{
this.shouldIgnoreNBTValue = false;
}
this.shouldIgnoreDamageValue = true;
this.shouldIgnoreDamageValue= true;
}
else
{
Expand Down Expand Up @@ -226,6 +236,14 @@ 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 @@ -246,30 +264,18 @@ 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));
}

@Override
public String toString()
{
final ItemStack stack = this.stack.copy();
stack.setCount(this.amount);
return stack.toString();
return ItemStackUtils.compareItemStacksIgnoreStackSize(that.getItemStack(), this.getItemStack(), !(this.shouldIgnoreDamageValue || that.shouldIgnoreDamageValue), !(this.shouldIgnoreNBTValue || that.shouldIgnoreNBTValue));
}

/**
* 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 @@ -305,7 +311,7 @@ public int getRemainingDurablityValue()

/**
* Is this an empty ItemStorage
*
*
* @return true if empty
*/
public boolean isEmpty()
Expand All @@ -315,19 +321,17 @@ 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;
}
}

/**
* 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,7 +1,7 @@
package com.minecolonies.api.entity.citizen.citizenhandlers;

import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.core.datalistener.DiseasesListener;
import com.minecolonies.core.datalistener.model.Disease;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -43,7 +43,7 @@ public interface ICitizenDiseaseHandler
* @return the disease instance.
*/
@Nullable
DiseasesListener.Disease getDisease();
Disease getDisease();

/**
* Cure the citizen.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.google.common.collect.ImmutableList;
import com.minecolonies.api.colony.ICitizenData;
import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.util.BlockPosUtil;
import com.minecolonies.api.util.ItemStackUtils;
import com.minecolonies.api.util.constant.NbtTagConstants;
import com.minecolonies.core.colony.buildings.AbstractBuilding;
import com.minecolonies.core.datalistener.model.Disease;
import com.minecolonies.core.datalistener.DiseasesListener;
import com.minecolonies.core.entity.ai.workers.util.Patient;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -206,9 +207,14 @@ public Map<Predicate<ItemStack>, Tuple<Integer, Boolean>> getRequiredItemsAndAmo
*/
private static boolean isCureItem(final ItemStack stack)
{
return DiseasesListener.getDiseases().stream()
.flatMap(m -> m.cureItems().stream())
.anyMatch(f -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, f.getItemStack(), !f.ignoreDamageValue(), !f.ignoreNBT()));
for (final Disease disease : DiseasesListener.getDiseases())
{
for (final ItemStorage cureItem : disease.cureItems())
{
return Disease.isCureItem(stack, cureItem);
}
}
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,27 @@
import com.google.gson.JsonObject;
import com.minecolonies.api.colony.requestsystem.StandardFactoryController;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.util.ItemStackUtils;
import com.minecolonies.core.Network;
import com.minecolonies.core.datalistener.model.Disease;
import com.minecolonies.core.network.messages.client.colony.GlobalDiseaseSyncMessage;
import com.minecolonies.core.util.RandomCollection;
import io.netty.buffer.Unpooled;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;
import net.minecraft.util.GsonHelper;
import net.minecraft.util.RandomSource;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
* Loads and listens to diseases data.
Expand All @@ -53,37 +50,6 @@ public class DiseasesListener extends SimpleJsonResourceReloadListener
*/
private static RandomCollection<ResourceLocation, Disease> DISEASES = new RandomCollection<>();

/**
* A possible disease.
*
* @param id the id of the disease.
* @param name the name of the disease.
* @param rarity the rarity of the disease.
* @param cureItems the list of items needed to heal.
*/
public record Disease(ResourceLocation id, Component name, int rarity, List<ItemStorage> cureItems)
{
/**
* Get the cure string containing all items required for the cure.
*
* @return the cure string.
*/
public Component getCureString()
{
final MutableComponent cureString = Component.literal("");
for (int i = 0; i < cureItems.size(); i++)
{
final ItemStorage cureStack = cureItems.get(i);
cureString.append(String.valueOf(cureStack.getItemStack().getCount())).append(" ").append(cureStack.getItemStack().getHoverName());
if (i != cureItems.size() - 1)
{
cureString.append(" + ");
}
}
return cureString;
}
}

/**
* Default constructor.
*/
Expand All @@ -103,10 +69,10 @@ public static void sendGlobalDiseasesPackets(final ServerPlayer player)
byteBuf.writeInt(DISEASES.getAll().size());
for (final Disease disease : DISEASES.getAll())
{
byteBuf.writeResourceLocation(disease.id);
byteBuf.writeComponent(disease.name);
byteBuf.writeInt(disease.rarity);
for (final ItemStorage cureItem : disease.cureItems)
byteBuf.writeResourceLocation(disease.id());
byteBuf.writeComponent(disease.name());
byteBuf.writeInt(disease.rarity());
for (final ItemStorage cureItem : disease.cureItems())
{
StandardFactoryController.getInstance().serialize(byteBuf, cureItem);
}
Expand Down Expand Up @@ -176,29 +142,6 @@ public static Disease getRandomDisease(final RandomSource random)
return DISEASES.next(random);
}

/**
* Predicate for the different usages to check if inventory contains a cure.
*
* @param cure the expected cure item.
* @return the predicate for checking if the cure exists.
*/
public static Predicate<ItemStack> hasCureItem(final ItemStorage cure)
{
return stack -> isCureItem(stack, cure);
}

/**
* Check if the given item is a cure item.
*
* @param stack the input stack.
* @param cure the cure item.
* @return true if so.
*/
public static boolean isCureItem(final ItemStack stack, final ItemStorage cure)
{
return ItemStackUtils.compareItemStacksIgnoreStackSize(stack, cure.getItemStack(), !cure.ignoreDamageValue(), !cure.ignoreNBT());
}

@Override
protected void apply(
final @NotNull Map<ResourceLocation, JsonElement> jsonElementMap,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.minecolonies.core.datalistener.model;

import com.minecolonies.api.crafting.ItemStorage;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;

import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

/**
* A possible disease.
*
* @param id the id of the disease.
* @param name the name of the disease.
* @param rarity the rarity of the disease.
* @param cureItems the list of items needed to heal.
*/
public record Disease(ResourceLocation id, Component name, int rarity, List<ItemStorage> cureItems)
{
/**
* Predicate for the different usages to check if inventory contains a cure.
*
* @param cure the expected cure item.
* @return the predicate for checking if the cure exists.
*/
public static Predicate<ItemStack> hasCureItem(final ItemStorage cure)
{
return stack -> isCureItem(stack, cure);
}

/**
* Check if the given item is a cure item.
*
* @param stack the input stack.
* @param cure the cure item.
* @return true if so.
*/
public static boolean isCureItem(final ItemStack stack, final ItemStorage cure)
{
return Objects.equals(new ItemStorage(stack), cure);
}

/**
* Get the cure string containing all items required for the cure.
*
* @return the cure string.
*/
public Component getCureString()
{
final MutableComponent cureString = Component.literal("");
for (int i = 0; i < cureItems.size(); i++)
{
final ItemStorage cureStack = cureItems.get(i);
cureString.append(String.valueOf(cureStack.getItemStack().getCount())).append(" ").append(cureStack.getItemStack().getHoverName());
if (i != cureItems.size() - 1)
{
cureString.append(" + ");
}
}
return cureString;
}
}
Loading

0 comments on commit cd0c90b

Please sign in to comment.