Skip to content

Commit

Permalink
Ray fixes (#10201)
Browse files Browse the repository at this point in the history
No more ping pong between warehouses for min stock requests. Extract this into its own request. Can in the future also use this to process these at lower priority for example and hide them from the clipboard.
Fix recruitment message formatting
Make ticketed chunks randomTick
  • Loading branch information
Raycoms authored Sep 8, 2024
1 parent 2193757 commit 3227767
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package com.minecolonies.api.colony.requestsystem.requestable;

import com.google.common.reflect.TypeToken;
import com.minecolonies.api.colony.requestsystem.factory.IFactoryController;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.util.ItemStackUtils;
import com.minecolonies.api.util.ReflectionUtils;
import com.minecolonies.api.util.constant.TypeConstants;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.Set;
import java.util.stream.Collectors;

/**
* Minimum stack request type.
*/
public class MinimumStack extends Stack
{
/**
* Set of type tokens belonging to this class.
*/
private final static Set<TypeToken<?>>
TYPE_TOKENS = ReflectionUtils.getSuperClasses(TypeToken.of(MinimumStack.class)).stream().filter(type -> !type.equals(TypeConstants.OBJECT)).collect(Collectors.toSet());

/**
* Create a Stack deliverable.
*
* @param stack the required stack.
*/
public MinimumStack(@NotNull final ItemStack stack)
{
this(stack, true);
}

/**
* Create a Stack deliverable.
*
* @param stack the required stack.
* @param matchDurability whether or not to match Durability
*/
public MinimumStack(@NotNull final ItemStack stack, boolean matchDurability)
{
this(stack, matchDurability, true, ItemStackUtils.EMPTY, Math.min(stack.getCount(), stack.getMaxStackSize()), Math.min(stack.getCount(), stack.getMaxStackSize()));
}


/**
* Create a Stack deliverable.
*
* @param stack the required stack.
* @param count the count.
* @param minCount the min count.
*/
public MinimumStack(@NotNull final ItemStack stack, final int count, final int minCount)
{
this(stack, count, minCount, true);
}

/**
* Transform an itemStorage into this predicate.
*
* @param itemStorage the storage to use.
*/
public MinimumStack(@NotNull final ItemStorage itemStorage)
{
this(itemStorage.getItemStack(), !itemStorage.ignoreDamageValue(), !itemStorage.ignoreNBT(), ItemStackUtils.EMPTY, itemStorage.getAmount(), itemStorage.getAmount());
}

/**
* Create a Stack deliverable with variable nbt.
* @param stack the stack to deliver.
* @param count the count.
* @param minCount the min count.
* @param matchNBT if nbt has to match.
*/
public MinimumStack(@NotNull final ItemStack stack, final int count, final int minCount, final boolean matchNBT)
{
this(stack, true, matchNBT, ItemStackUtils.EMPTY, count, minCount);
}

/**
* Create a Stack deliverable.
*
* @param stack the required stack.
* @param matchDamage if damage has to be matched.
* @param matchNBT if NBT has to be matched.
* @param result the result stack.
* @param count the count.
* @param minCount the min count.
*/
public MinimumStack(
@NotNull final ItemStack stack,
final boolean matchDamage,
final boolean matchNBT,
@NotNull final ItemStack result,
final int count,
final int minCount)
{
this(stack, matchDamage, matchNBT, ItemStackUtils.EMPTY, count, minCount, true);
}

/**
* Create a Stack deliverable.
*
* @param stack the required stack.
* @param matchDamage if damage has to be matched.
* @param matchNBT if NBT has to be matched.
* @param result the result stack.
* @param count the count.
* @param minCount the min count.
* @param canBeResolvedByBuilding if can be resolved by building.
*/
public MinimumStack(final ItemStack stack, final boolean matchDamage, final boolean matchNBT, final ItemStack result, final int count, final int minCount, final boolean canBeResolvedByBuilding)
{
super(stack, matchDamage, matchNBT, result, count, minCount, canBeResolvedByBuilding);
}

/**
* Deserialize the deliverable.
*
* @param controller the controller.
* @param compound the compound.
* @return the deliverable.
*/
public static MinimumStack deserialize(@NotNull final HolderLookup.Provider provider, final IFactoryController controller, final CompoundTag compound)
{
final Stack stack = Stack.deserialize(provider, controller, compound);
return new MinimumStack(stack.getStack(), stack.matchDamage(), stack.matchNBT(), stack.getResult(), stack.getCount(), stack.getMinimumCount(), stack.canBeResolvedByBuilding());
}

/**
* Deserialize the deliverable.
*
* @param controller the controller.
* @param buffer the buffer to read.
* @return the deliverable.
*/
public static MinimumStack deserialize(final IFactoryController controller, final RegistryFriendlyByteBuf buffer)
{
final Stack stack = Stack.deserialize(controller, buffer);
return new MinimumStack(stack.getStack(), stack.matchDamage(), stack.matchNBT(), stack.getResult(), stack.getCount(), stack.getMinimumCount(), stack.canBeResolvedByBuilding());
}

@Override
public boolean equals(final Object o)
{
if (this == o)
{
return true;
}
if (!(o instanceof MinimumStack))
{
return false;
}
return super.equals(o);
}

@Override
public Set<TypeToken<?>> getSuperClasses()
{
return TYPE_TOKENS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -34,7 +33,6 @@ public class Stack implements IConcreteDeliverable
private static final String NBT_STACK = "Stack";
private static final String NBT_MATCHMETA = "MatchMeta";
private static final String NBT_MATCHNBT = "MatchNBT";
private static final String NBT_MATCHOREDIC = "MatchOreDic";
private static final String NBT_BUILDING_RES = "BuildingRes";
private static final String NBT_RESULT = "Result";
private static final String NBT_COUNT = "Count";
Expand All @@ -54,11 +52,6 @@ public class Stack implements IConcreteDeliverable
*/
private boolean matchNBT;

/**
* If oredict should match.
*/
private boolean matchOreDic;

/**
* The required count.
*/
Expand Down Expand Up @@ -95,7 +88,7 @@ public Stack(@NotNull final ItemStack stack)
*/
public Stack(@NotNull final ItemStack stack, boolean matchDurability)
{
this(stack, matchDurability, true, false, ItemStackUtils.EMPTY, Math.min(stack.getCount(), stack.getMaxStackSize()), Math.min(stack.getCount(), stack.getMaxStackSize()));
this(stack, matchDurability, true, ItemStackUtils.EMPTY, Math.min(stack.getCount(), stack.getMaxStackSize()), Math.min(stack.getCount(), stack.getMaxStackSize()));
}


Expand All @@ -118,7 +111,7 @@ public Stack(@NotNull final ItemStack stack, final int count, final int minCount
*/
public Stack(@NotNull final ItemStorage itemStorage)
{
this(itemStorage.getItemStack(), !itemStorage.ignoreDamageValue(), !itemStorage.ignoreNBT(), false, ItemStackUtils.EMPTY, itemStorage.getAmount(), itemStorage.getAmount());
this(itemStorage.getItemStack(), !itemStorage.ignoreDamageValue(), !itemStorage.ignoreNBT(), ItemStackUtils.EMPTY, itemStorage.getAmount(), itemStorage.getAmount());
}

/**
Expand All @@ -130,7 +123,7 @@ public Stack(@NotNull final ItemStorage itemStorage)
*/
public Stack(@NotNull final ItemStack stack, final int count, final int minCount, final boolean matchNBT)
{
this(stack, true, matchNBT, false, ItemStackUtils.EMPTY, count, minCount);
this(stack, true, matchNBT, ItemStackUtils.EMPTY, count, minCount);
}

/**
Expand All @@ -139,7 +132,6 @@ public Stack(@NotNull final ItemStack stack, final int count, final int minCount
* @param stack the required stack.
* @param matchDamage if damage has to be matched.
* @param matchNBT if NBT has to be matched.
* @param matchOreDic if the oredict has to be matched.
* @param result the result stack.
* @param count the count.
* @param minCount the min count.
Expand All @@ -148,12 +140,11 @@ public Stack(
@NotNull final ItemStack stack,
final boolean matchDamage,
final boolean matchNBT,
final boolean matchOreDic,
@NotNull final ItemStack result,
final int count,
final int minCount)
{
this(stack, matchDamage, matchNBT, matchOreDic, ItemStackUtils.EMPTY, count, minCount, true);
this(stack, matchDamage, matchNBT, ItemStackUtils.EMPTY, count, minCount, true);
}

/**
Expand All @@ -162,13 +153,12 @@ public Stack(
* @param stack the required stack.
* @param matchDamage if damage has to be matched.
* @param matchNBT if NBT has to be matched.
* @param matchOreDic if the oredict has to be matched.
* @param result the result stack.
* @param count the count.
* @param minCount the min count.
* @param canBeResolvedByBuilding if can be resolved by building.
*/
public Stack(final ItemStack stack, final boolean matchDamage, final boolean matchNBT, final boolean matchOreDic, final ItemStack result, final int count, final int minCount, final boolean canBeResolvedByBuilding)
public Stack(final ItemStack stack, final boolean matchDamage, final boolean matchNBT, final ItemStack result, final int count, final int minCount, final boolean canBeResolvedByBuilding)
{
if (ItemStackUtils.isEmpty(stack))
{
Expand All @@ -178,7 +168,6 @@ public Stack(final ItemStack stack, final boolean matchDamage, final boolean mat
this.theStack = stack.copy();
this.matchDamage = matchDamage;
this.matchNBT = matchNBT;
this.matchOreDic = matchOreDic;
this.result = result;
this.count = count;
this.minCount = Math.min(minCount, count);
Expand All @@ -198,7 +187,6 @@ public static CompoundTag serialize(@NotNull final HolderLookup.Provider provide
compound.put(NBT_STACK, input.theStack.saveOptional(provider));
compound.putBoolean(NBT_MATCHMETA, input.matchDamage);
compound.putBoolean(NBT_MATCHNBT, input.matchNBT);
compound.putBoolean(NBT_MATCHOREDIC, input.matchOreDic);
compound.putBoolean(NBT_BUILDING_RES, input.canBeResolvedByBuilding);

if (!ItemStackUtils.isEmpty(input.result))
Expand All @@ -223,7 +211,6 @@ public static Stack deserialize(@NotNull final HolderLookup.Provider provider, f
final ItemStack stack = ItemStackUtils.deserializeFromNBT(compound.getCompound(NBT_STACK), provider);
final boolean matchMeta = compound.getBoolean(NBT_MATCHMETA);
final boolean matchNBT = compound.getBoolean(NBT_MATCHNBT);
final boolean matchOreDic = compound.getBoolean(NBT_MATCHOREDIC);
final boolean canBeResolved = compound.contains(NBT_BUILDING_RES) ? compound.getBoolean(NBT_BUILDING_RES) : true;

final ItemStack result = compound.contains(NBT_RESULT) ? ItemStackUtils.deserializeFromNBT(compound.getCompound(NBT_RESULT), provider) : ItemStackUtils.EMPTY;
Expand All @@ -236,7 +223,7 @@ public static Stack deserialize(@NotNull final HolderLookup.Provider provider, f
minCount = compound.getInt(NBT_MINCOUNT);
}

return new Stack(stack, matchMeta, matchNBT, matchOreDic, result, count, minCount, canBeResolved);
return new Stack(stack, matchMeta, matchNBT, result, count, minCount, canBeResolved);
}

/**
Expand All @@ -251,7 +238,6 @@ public static void serialize(final IFactoryController controller, final Registry
Utils.serializeCodecMess(buffer, input.theStack);
buffer.writeBoolean(input.matchDamage);
buffer.writeBoolean(input.matchNBT);
buffer.writeBoolean(input.matchOreDic);
buffer.writeBoolean(input.canBeResolvedByBuilding);

buffer.writeBoolean(!ItemStackUtils.isEmpty(input.result));
Expand All @@ -275,27 +261,18 @@ public static Stack deserialize(final IFactoryController controller, final Regis
final ItemStack stack = Utils.deserializeCodecMess(buffer);
final boolean matchMeta = buffer.readBoolean();
final boolean matchNBT = buffer.readBoolean();
final boolean matchOreDic = buffer.readBoolean();
final boolean canBeResolved = buffer.readBoolean();

final ItemStack result = buffer.readBoolean() ? Utils.deserializeCodecMess(buffer) : ItemStack.EMPTY;

int count = buffer.readInt();
int minCount = buffer.readInt();
return new Stack(stack, matchMeta, matchNBT, matchOreDic, result, count, minCount, canBeResolved);
return new Stack(stack, matchMeta, matchNBT, result, count, minCount, canBeResolved);
}

@Override
public boolean matches(@NotNull final ItemStack stack)
{
if (matchOreDic)
{
if (!Collections.disjoint(stack.getTags().toList(), theStack.getTags().toList()))
{
return true;
}
}

return ItemStackUtils.compareItemStacksIgnoreStackSize(getStack(), stack, matchDamage, matchNBT);
}

Expand Down Expand Up @@ -344,7 +321,7 @@ public boolean matchDamage()
@Override
public IDeliverable copyWithCount(final int newCount)
{
return new Stack(this.theStack, this.matchDamage, this.matchNBT, this.matchOreDic, this.result, newCount, this.minCount);
return new Stack(this.theStack, this.matchDamage, this.matchNBT, this.result, newCount, this.minCount);
}

@NotNull
Expand Down Expand Up @@ -380,10 +357,7 @@ public boolean equals(final Object o)
{
return false;
}
if (matchOreDic != stack1.matchOreDic)
{
return false;
}

if (!ItemStackUtils.compareItemStacksIgnoreStackSize(getStack(), stack1.getStack()))
{
return false;
Expand All @@ -397,7 +371,6 @@ public int hashCode()
int result1 = getStack().hashCode();
result1 = 31 * result1 + (matchDamage ? 1 : 0);
result1 = 31 * result1 + (matchNBT ? 1 : 0);
result1 = 31 * result1 + (matchOreDic ? 1 : 0);
result1 = 31 * result1 + (canBeResolvedByBuilding ? 1 : 0);
result1 = 31 * result1 + getResult().hashCode();
return result1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class SerializationIdentifierConstants
public static final short DYNAMIC_TREES_SETTINGS_ID = 57;
public static final short STATION_REQUEST_RESOLVER_ID = 58;
public static final short CRAFTING_SETTINGS_ID = 59;
public static final short MIN_STACK_REQUEST_ID = 60;

private SerializationIdentifierConstants() {}
}
2 changes: 1 addition & 1 deletion src/main/java/com/minecolonies/core/colony/Colony.java
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ private void checkChunkAndRegisterTicket(final long chunkPos, final LevelChunk c
{
ticketedChunks.add(chunkPos);
ticketedChunksDirty = true;
((ServerChunkCache) world.getChunkSource()).addRegionTicket(KEEP_LOADED_TYPE, chunk.getPos(), 2, chunk.getPos());
world.getChunkSource().addRegionTicket(KEEP_LOADED_TYPE, chunk.getPos(), 2, chunk.getPos(), true);
}
}
}
Expand Down
Loading

0 comments on commit 3227767

Please sign in to comment.