Skip to content

Commit

Permalink
Stateless citizen item utils (#10484)
Browse files Browse the repository at this point in the history
Move citizen item handler util class to stateless util
  • Loading branch information
Raycoms authored Nov 27, 2024
1 parent 914b3be commit cb84d4d
Show file tree
Hide file tree
Showing 33 changed files with 122 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -623,13 +623,6 @@ public boolean checkCanDropLoot()
*/
public abstract ICitizenExperienceHandler getCitizenExperienceHandler();

/**
* The Handler for all item related methods.
*
* @return the instance of the handler.
*/
public abstract ICitizenItemHandler getCitizenItemHandler();

/**
* The Handler for all inventory related methods.
*
Expand Down Expand Up @@ -672,8 +665,6 @@ public boolean checkCanDropLoot()

public abstract void setCitizenJobHandler(ICitizenJobHandler citizenJobHandler);

public abstract void setCitizenItemHandler(ICitizenItemHandler citizenItemHandler);

public abstract void setCitizenExperienceHandler(ICitizenExperienceHandler citizenExperienceHandler);

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.minecolonies.core.colony.jobs;

import com.minecolonies.core.util.citizenutils.CitizenItemUtils;
import net.minecraft.core.component.DataComponents;
import net.minecraft.resources.ResourceLocation;
import com.minecolonies.api.client.render.modeltype.ModModelTypes;
Expand Down Expand Up @@ -83,7 +84,7 @@ public boolean ignoresDamage(@NotNull final DamageSource damageSource)
return true;
}
final AbstractEntityCitizen worker = this.getCitizen().getEntity().get();
worker.getCitizenItemHandler().setHeldItem(InteractionHand.OFF_HAND, InventoryUtils.findFirstSlotInItemHandlerWith(this.getCitizen().getInventory(), Items.SHIELD));
CitizenItemUtils.setHeldItem(worker, InteractionHand.OFF_HAND, InventoryUtils.findFirstSlotInItemHandlerWith(this.getCitizen().getInventory(), Items.SHIELD));
worker.startUsingItem(InteractionHand.OFF_HAND);

ItemStack shieldStack = worker.getInventoryCitizen().getHeldItem(InteractionHand.OFF_HAND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.minecolonies.core.colony.jobs.AbstractJob;
import com.minecolonies.core.colony.jobs.JobDeliveryman;
import com.minecolonies.core.colony.requestsystem.resolvers.StationRequestResolver;
import com.minecolonies.core.util.citizenutils.CitizenItemUtils;
import com.minecolonies.core.entity.pathfinding.proxy.EntityCitizenWalkToProxy;
import com.minecolonies.core.tileentities.TileEntityRack;
import com.minecolonies.core.util.WorkerUtil;
Expand Down Expand Up @@ -520,7 +521,7 @@ private boolean waitingForSomething()
currentWorkingLocation.getZ(),
DEFAULT_RANGE_FOR_DELAY))
{
worker.getCitizenItemHandler().hitBlockWithToolInHand(currentWorkingLocation);
CitizenItemUtils.hitBlockWithToolInHand(worker, currentWorkingLocation);
}
delay -= getTickRate();
if (delay <= 0)
Expand Down Expand Up @@ -1278,13 +1279,13 @@ public final boolean holdEfficientTool(@NotNull final BlockState target, final B
if (bestSlot >= 0)
{
worker.getCitizenData().setIdleAtJob(false);
worker.getCitizenItemHandler().setHeldItem(InteractionHand.MAIN_HAND, bestSlot);
CitizenItemUtils.setHeldItem(worker, InteractionHand.MAIN_HAND, bestSlot);
return true;
}
else if (bestSlot == NO_TOOL)
{
worker.getCitizenData().setIdleAtJob(false);
worker.getCitizenItemHandler().removeHeldItem();
CitizenItemUtils.removeHeldItem(worker);
return true;
}
requestTool(target, pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.minecolonies.core.MineColonies;
import com.minecolonies.core.colony.buildings.AbstractBuilding;
import com.minecolonies.core.colony.jobs.AbstractJob;
import com.minecolonies.core.util.citizenutils.CitizenItemUtils;
import com.minecolonies.core.entity.pathfinding.pathresults.PathResult;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.item.ItemEntity;
Expand Down Expand Up @@ -217,7 +218,7 @@ protected final boolean mineBlock(
if (blockBreakAction == null)
{
//Break the block
worker.getCitizenItemHandler().breakBlockWithToolInHand(blockToMine);
CitizenItemUtils.breakBlockWithToolInHand(worker, blockToMine);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.minecolonies.core.colony.jobs.AbstractJobCrafter;
import com.minecolonies.core.entity.ai.workers.AbstractEntityAIInteract;
import com.minecolonies.core.entity.citizen.EntityCitizen;
import com.minecolonies.core.util.citizenutils.CitizenItemUtils;
import com.minecolonies.core.network.messages.client.BlockParticleEffectMessage;
import com.minecolonies.core.network.messages.client.LocalizedParticleEffectMessage;
import net.minecraft.core.BlockPos;
Expand All @@ -34,7 +35,6 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -449,7 +449,7 @@ protected IAIState craft()
job.setCraftCounter(job.getCraftCounter() + 1);
if (toolSlot != -1)
{
worker.getCitizenItemHandler().damageItemInHand(InteractionHand.MAIN_HAND, 1);
CitizenItemUtils.damageItemInHand(worker, InteractionHand.MAIN_HAND, 1);
}

if (job.getCraftCounter() >= job.getMaxCraftingCount())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.minecolonies.core.colony.interactionhandling.StandardInteraction;
import com.minecolonies.core.colony.jobs.AbstractJobCrafter;
import com.minecolonies.core.entity.ai.workers.AbstractEntityAIBasic;
import com.minecolonies.core.util.citizenutils.CitizenItemUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -740,7 +741,7 @@ private IAIState fillUpFurnace()
{
return getState();
}
worker.getCitizenItemHandler().hitBlockWithToolInHand(walkTo);
CitizenItemUtils.hitBlockWithToolInHand(worker, walkTo);
InventoryUtils.transferXInItemHandlerIntoSlotInItemHandler(
worker.getInventoryCitizen(),
smeltable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingAlchemist;
import com.minecolonies.core.colony.interactionhandling.StandardInteraction;
import com.minecolonies.core.colony.jobs.JobAlchemist;
import com.minecolonies.core.util.citizenutils.CitizenItemUtils;
import com.minecolonies.core.network.messages.client.BlockParticleEffectMessage;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -247,7 +248,7 @@ private IAIState harvestMistleToe()

final int slot =
InventoryUtils.getFirstSlotOfItemHandlerContainingEquipment(worker.getInventoryCitizen(), ModEquipmentTypes.shears.get(), TOOL_LEVEL_WOOD_OR_GOLD, building.getMaxEquipmentLevel());
worker.getCitizenItemHandler().setHeldItem(InteractionHand.MAIN_HAND, slot);
CitizenItemUtils.setHeldItem(worker, InteractionHand.MAIN_HAND, slot);

worker.swing(InteractionHand.MAIN_HAND);
world.playSound(null,
Expand All @@ -262,7 +263,7 @@ private IAIState harvestMistleToe()
worker.decreaseSaturationForContinuousAction();
InventoryUtils.addItemStackToItemHandler(worker.getInventoryCitizen(), new ItemStack(ModItems.mistletoe, 1));
walkTo = null;
worker.getCitizenItemHandler().damageItemInHand(InteractionHand.MAIN_HAND, 1);
CitizenItemUtils.damageItemInHand(worker, InteractionHand.MAIN_HAND, 1);
return INVENTORY_FULL;
}
}
Expand Down Expand Up @@ -929,7 +930,7 @@ private IAIState fillUpBrewingStand()
{
return getState();
}
worker.getCitizenItemHandler().hitBlockWithToolInHand(walkTo);
CitizenItemUtils.hitBlockWithToolInHand(worker, walkTo);
InventoryUtils.transferXInItemHandlerIntoSlotInItemHandler(
worker.getInventoryCitizen(),
potion,
Expand Down Expand Up @@ -988,7 +989,7 @@ else if (isEmpty(((BrewingStandBlockEntity) entity).getItem(INGREDIENT_SLOT)))
{
return getState();
}
worker.getCitizenItemHandler().hitBlockWithToolInHand(walkTo);
CitizenItemUtils.hitBlockWithToolInHand(worker, walkTo);
InventoryUtils.transferXInItemHandlerIntoSlotInItemHandler(
worker.getInventoryCitizen(),
ingredient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.minecolonies.api.entity.ai.statemachine.AITarget;
import com.minecolonies.api.entity.ai.statemachine.states.IAIState;
import com.minecolonies.api.entity.citizen.VisibleCitizenStatus;
import com.minecolonies.core.entity.pathfinding.navigation.AbstractAdvancedPathNavigate;
import com.minecolonies.core.util.citizenutils.CitizenItemUtils;
import com.minecolonies.api.util.SoundUtils;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingCrusher;
Expand Down Expand Up @@ -218,7 +218,7 @@ protected IAIState craft()
worker.setItemInHand(InteractionHand.MAIN_HAND,
currentRecipeStorage.getCleanedInput().get(worker.getRandom().nextInt(currentRecipeStorage.getCleanedInput().size())).getItemStack().copy());
worker.setItemInHand(InteractionHand.OFF_HAND, currentRecipeStorage.getPrimaryOutput().copy());
worker.getCitizenItemHandler().hitBlockWithToolInHand(building.getPosition());
CitizenItemUtils.hitBlockWithToolInHand(worker, building.getPosition());

currentRequest = job.getCurrentTask();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.minecolonies.core.colony.buildings.AbstractBuildingGuards;
import com.minecolonies.core.colony.jobs.AbstractJobGuard;
import com.minecolonies.core.entity.ai.workers.AbstractEntityAIInteract;
import com.minecolonies.core.util.citizenutils.CitizenItemUtils;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ArmorItem;
Expand Down Expand Up @@ -125,7 +126,7 @@ private IAIState prepare()
stack -> !ItemStackUtils.isEmpty(stack)
&& ItemStackUtils.doesItemServeAsWeapon(stack)
&& ItemStackUtils.hasEquipmentLevel(stack, tool, 0, building.getMaxEquipmentLevel()),
itemStack -> worker.getCitizenItemHandler().setMainHeldItem(itemStack));
itemStack -> CitizenItemUtils.setMainHeldItem(worker, itemStack));
}

equipInventoryArmor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.minecolonies.api.equipment.ModEquipmentTypes;
import com.minecolonies.api.util.*;
import com.minecolonies.api.util.constant.ColonyConstants;
import com.minecolonies.core.util.citizenutils.CitizenItemUtils;
import com.minecolonies.core.entity.pathfinding.pathresults.PathResult;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.core.MineColonies;
Expand Down Expand Up @@ -108,7 +109,7 @@ protected IAIState attackProtect()
if (shieldSlot != -1 && target != null && target.isAlive() && nextAttackTime - user.level().getGameTime() >= MIN_TIME_TO_ATTACK &&
user.getCitizenColonyHandler().getColony().getResearchManager().getResearchEffects().getEffectStrength(SHIELD_USAGE) > 0)
{
user.getCitizenItemHandler().setHeldItem(InteractionHand.OFF_HAND, shieldSlot);
CitizenItemUtils.setHeldItem(user, InteractionHand.OFF_HAND, shieldSlot);
user.startUsingItem(InteractionHand.OFF_HAND);

// Apply the colony Flag to the shield
Expand All @@ -133,7 +134,7 @@ public boolean canAttack()

if (weaponSlot != -1)
{
user.getCitizenItemHandler().setHeldItem(InteractionHand.MAIN_HAND, weaponSlot);
CitizenItemUtils.setHeldItem(user, InteractionHand.MAIN_HAND, weaponSlot);
return true;
}

Expand Down Expand Up @@ -184,7 +185,7 @@ protected void doAttack(final LivingEntity target)
user.stopUsingItem();
user.decreaseSaturationForContinuousAction();
user.getCitizenData().setVisibleStatus(KNIGHT_COMBAT);
user.getCitizenItemHandler().damageItemInHand(InteractionHand.MAIN_HAND, 1);
CitizenItemUtils.damageItemInHand(user, InteractionHand.MAIN_HAND, 1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.minecolonies.api.equipment.ModEquipmentTypes;
import com.minecolonies.api.util.*;
import com.minecolonies.api.util.constant.ColonyConstants;
import com.minecolonies.core.util.citizenutils.CitizenItemUtils;
import com.minecolonies.core.entity.pathfinding.PathfindingUtils;
import com.minecolonies.core.entity.pathfinding.pathresults.PathResult;
import com.minecolonies.core.entity.pathfinding.PathingOptions;
Expand All @@ -18,11 +19,8 @@
import com.minecolonies.core.entity.ai.combat.CombatUtils;
import com.minecolonies.core.entity.citizen.EntityCitizen;
import com.minecolonies.core.entity.other.CustomArrowEntity;
import com.minecolonies.core.entity.pathfinding.PathfindingUtils;
import com.minecolonies.core.entity.pathfinding.PathingOptions;
import com.minecolonies.core.entity.pathfinding.navigation.MinecoloniesAdvancedPathNavigate;
import com.minecolonies.core.entity.pathfinding.pathjobs.*;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -35,7 +33,6 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.neoforged.neoforge.registries.DeferredRegister;

import static com.minecolonies.api.research.util.ResearchConstants.*;
import static com.minecolonies.api.util.constant.GuardConstants.*;
Expand Down Expand Up @@ -111,7 +108,7 @@ public boolean canAttack()

if (weaponSlot != -1)
{
user.getCitizenItemHandler().setHeldItem(InteractionHand.MAIN_HAND, weaponSlot);
CitizenItemUtils.setHeldItem(user, InteractionHand.MAIN_HAND, weaponSlot);
if (nextAttackTime - BOW_HOLDING_DELAY >= user.level().getGameTime() && !user.isUsingItem())
{
user.startUsingItem(InteractionHand.MAIN_HAND);
Expand Down Expand Up @@ -190,7 +187,7 @@ protected void doAttack(final LivingEntity target)
}

target.setLastHurtByMob(user);
user.getCitizenItemHandler().damageItemInHand(InteractionHand.MAIN_HAND, 1);
CitizenItemUtils.damageItemInHand(user, InteractionHand.MAIN_HAND, 1);
user.stopUsingItem();
user.decreaseSaturationForContinuousAction();
}
Expand Down
Loading

0 comments on commit cb84d4d

Please sign in to comment.