Skip to content

Commit

Permalink
Sync and other improvements (#10135)
Browse files Browse the repository at this point in the history
Sync and other improvements
  • Loading branch information
Raycoms committed Aug 20, 2024
1 parent 1b510b9 commit f4328d2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public abstract class AbstractCraftingBuildingModule extends AbstractBuildingMod
*/
protected AbstractBuilding building;

/**
* Dirty flag for recipes
*/
private boolean recipesDirty = true;

/**
* Create a new module.
* @param jobEntry the entry of the job.
Expand Down Expand Up @@ -249,7 +254,7 @@ public void deserializeNBT(@NotNull final HolderLookup.Provider provider, Compou
}

@Override
public void serializeToView(@NotNull final RegistryFriendlyByteBuf buf)
public void serializeToView(@NotNull final FriendlyByteBuf buf, final boolean fullSync)
{
if (jobEntry != null)
{
Expand All @@ -268,39 +273,46 @@ public void serializeToView(@NotNull final RegistryFriendlyByteBuf buf)
buf.writeById(MinecoloniesAPIProxy.getInstance().getCraftingTypeRegistry()::getIdOrThrow, type);
}

final List<IRecipeStorage> storages = new ArrayList<>();
final List<IRecipeStorage> disabledStorages = new ArrayList<>();
final Map<ResourceLocation, CustomRecipe> crafterRecipes = CustomRecipeManager.getInstance().getAllRecipes().getOrDefault(getCustomRecipeKey(), Collections.emptyMap());
for (final IToken<?> token : new ArrayList<>(recipes))
buf.writeBoolean(recipesDirty || fullSync);
if (recipesDirty || fullSync)
{
final IRecipeStorage storage = IColonyManager.getInstance().getRecipeManager().getRecipes().get(token);

if (storage == null || (storage.getRecipeSource() != null && !crafterRecipes.containsKey(storage.getRecipeSource())) || (!isRecipeCompatibleWithCraftingModule(token) && !isPreTaughtRecipe(storage, crafterRecipes)))
{
removeRecipe(token);
}
else
final List<IRecipeStorage> storages = new ArrayList<>();
final List<IRecipeStorage> disabledStorages = new ArrayList<>();
final Map<ResourceLocation, CustomRecipe> crafterRecipes = CustomRecipeManager.getInstance().getAllRecipes().getOrDefault(getCustomRecipeKey(), Collections.emptyMap());
for (final IToken<?> token : new ArrayList<>(recipes))
{
storages.add(storage);
if (disabledRecipes.contains(token))
final IRecipeStorage storage = IColonyManager.getInstance().getRecipeManager().getRecipes().get(token);

if (storage == null || (storage.getRecipeSource() != null && !crafterRecipes.containsKey(storage.getRecipeSource())) || (
!isRecipeCompatibleWithCraftingModule(token) && !isPreTaughtRecipe(storage, crafterRecipes)))
{
disabledStorages.add(storage);
removeRecipe(token);
}
else
{
storages.add(storage);
if (disabledRecipes.contains(token))
{
disabledStorages.add(storage);
}
}
}
}

buf.writeInt(storages.size());
for (final IRecipeStorage storage : storages)
{
StandardFactoryController.getInstance().serialize(buf, storage);
}
buf.writeInt(storages.size());
for (final IRecipeStorage storage : storages)
{
buf.writeNbt(StandardFactoryController.getInstance().serialize(storage));
}

buf.writeInt(disabledStorages.size());
for (final IRecipeStorage storage : disabledStorages)
{
StandardFactoryController.getInstance().serialize(buf, storage);
buf.writeInt(disabledStorages.size());
for (final IRecipeStorage storage : disabledStorages)
{
buf.writeNbt(StandardFactoryController.getInstance().serialize(storage));
}
}

recipesDirty = false;

buf.writeInt(getMaxRecipes());
buf.writeUtf(getId());
buf.writeBoolean(isVisible());
Expand Down Expand Up @@ -559,6 +571,7 @@ else if((forceReplace || newRecipe.getMustExist()) && !(duplicateFound.equals(re
public void clearRecipes()
{
recipes.clear();
recipesDirty = true;
}

@Override
Expand Down Expand Up @@ -823,6 +836,7 @@ public void replaceRecipe(final IToken<?> oldRecipe, final IToken<?> newRecipe)
{
if (recipes.contains(oldRecipe))
{
recipesDirty = true;
int oldIndex = recipes.indexOf(oldRecipe);
recipes.add(oldIndex, newRecipe);
recipes.remove(oldRecipe);
Expand All @@ -835,6 +849,7 @@ public void removeRecipe(final IToken<?> token)
{
if(recipes.remove(token))
{
recipesDirty = true;
disabledRecipes.remove(token);
markDirty();
}
Expand All @@ -850,6 +865,7 @@ public void addRecipeToList(final IToken<?> token, boolean atTop)
{
if (!recipes.contains(token))
{
recipesDirty = true;
if(atTop)
{
recipes.add(0, token);
Expand All @@ -864,6 +880,7 @@ public void addRecipeToList(final IToken<?> token, boolean atTop)
@Override
public void switchOrder(final int i, final int j, final boolean fullMove)
{
recipesDirty = true;
if (fullMove)
{
if (i > j)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,29 @@ public void deserialize(@NotNull RegistryFriendlyByteBuf buf)
}
}

recipes.clear();
disabledRecipes.clear();

final int recipesSize = buf.readInt();
for (int i = 0; i < recipesSize; i++)
if (buf.readBoolean())
{
final IRecipeStorage storage = StandardFactoryController.getInstance().deserialize(buf);
if (storage != null)
recipes.clear();
disabledRecipes.clear();

final int recipesSize = buf.readInt();
for (int i = 0; i < recipesSize; i++)
{
recipes.add(storage);
final IRecipeStorage storage = StandardFactoryController.getInstance().deserialize(buf.readNbt());
if (storage != null)
{
recipes.add(storage);
}
}
}

final int disabledRecipeSize = buf.readInt();
for (int i = 0; i < disabledRecipeSize; i++)
{
final IRecipeStorage storage = StandardFactoryController.getInstance().deserialize(buf);
if (storage != null)
final int disabledRecipeSize = buf.readInt();
for (int i = 0; i < disabledRecipeSize; i++)
{
disabledRecipes.add(storage);
final IRecipeStorage storage = StandardFactoryController.getInstance().deserialize(buf.readNbt());
if (storage != null)
{
disabledRecipes.add(storage);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ public PathResult<AbstractPathJob> setPathJob(
return null;
}

if (PathfindingUtils.trackingMap.containsValue(ourEntity.getUUID()))
{
Log.getLogger().info(ourEntity + " started pathjob to:" + dest + " job type:" + job.getClass().getSimpleName());
}

stop();

this.destination = dest;
Expand Down Expand Up @@ -984,7 +989,7 @@ protected void followThePath()
}
}

if (isTracking)
if (isTracking && reached != null)
{
PathfindingUtils.syncDebugReachedPositions(reached, ourEntity);
reached.clear();
Expand Down Expand Up @@ -1028,7 +1033,7 @@ else if (isTracking)
}
}

if (isTracking)
if (isTracking && reached != null)
{
PathfindingUtils.syncDebugReachedPositions(reached, ourEntity);
reached.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2671,7 +2671,7 @@
"com.minecolonies.alchemyquestpotion.name": "Suspicious Potion",
"com.minecolonies.alchemyquestpotion.lore": "A present from the Alchemist",

"item.minecolonies.large_empty_bottle": "Large Empty Bottle",
"item.minecolonies.large_empty_bottle": "Large Bottle",
"item.minecolonies.large_water_bottle": "Large Water Bottle",
"item.minecolonies.large_milk_bottle": "Large Milk Bottle",
"item.minecolonies.large_soy_milk_bottle": "Large Soy Milk Bottle",
Expand Down

0 comments on commit f4328d2

Please sign in to comment.