use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by ldtteam.
the class ItemBannerRallyGuards method broadcastPlayerToRally.
/**
* Broadcasts the player all the guardtowers rallied by the item are supposed to follow.
*
* @param banner The banner that should broadcast
* @param playerIn The player to follow. Can be null, if the towers should revert to "normal" mode
* @return The number of guards rallied
*/
public static int broadcastPlayerToRally(final ItemStack banner, final World worldIn, @Nullable final PlayerEntity playerIn) {
if (worldIn.isClientSide()) {
Log.getLogger().error("Tried to run server-side function #broadcastPlayerToRally() on the client-side!");
return 0;
}
@Nullable ILocation rallyTarget = null;
if (!isActive(banner) || playerIn == null) {
rallyTarget = null;
} else {
rallyTarget = new EntityLocation(playerIn.getUUID());
}
int numGuards = 0;
for (final ILocation guardTowerLocation : getGuardTowerLocations(banner)) {
// Note: getCurrentServer().getWorld() must be used here because MineColonies.proxy.getWorld() fails on single player worlds
// We are sure we are on the server-side in this function though, so it's fine.
final IGuardBuilding building = getGuardBuilding(ServerLifecycleHooks.getCurrentServer().getLevel(guardTowerLocation.getDimension()), guardTowerLocation.getInDimensionLocation());
// Safely ignore this case, the player must remove the tower from the rallying list manually.
if (building != null && (playerIn == null || building.getColony().getPermissions().hasPermission(playerIn, Action.RALLY_GUARDS))) {
building.setRallyLocation(rallyTarget);
numGuards += building.getAllAssignedCitizen().size();
}
}
return numGuards;
}
use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by ldtteam.
the class ItemScepterGuard method handleItemUsage.
/**
* Handles the usage of the item.
*
* @param worldIn the world it is used in.
* @param pos the position.
* @param compound the compound.
* @param playerIn the player using it.
* @return if it has been successful.
*/
@NotNull
private static ActionResultType handleItemUsage(final World worldIn, final BlockPos pos, final CompoundNBT compound, final PlayerEntity playerIn) {
if (!compound.getAllKeys().contains(TAG_ID)) {
return ActionResultType.FAIL;
}
final IColony colony = IColonyManager.getInstance().getColonyByWorld(compound.getInt(TAG_ID), worldIn);
if (colony == null) {
return ActionResultType.FAIL;
}
final BlockPos guardTower = BlockPosUtil.read(compound, TAG_POS);
final IBuilding hut = colony.getBuildingManager().getBuilding(guardTower);
if (!(hut instanceof AbstractBuildingGuards)) {
return ActionResultType.FAIL;
}
final IGuardBuilding tower = (IGuardBuilding) hut;
if (BlockPosUtil.getDistance2D(pos, guardTower) > tower.getPatrolDistance()) {
LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolClickGuardTooFar");
return ActionResultType.FAIL;
}
if (hut.getSetting(AbstractBuildingGuards.GUARD_TASK).getValue().equals(GuardTaskSetting.GUARD)) {
LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolclickguard", pos);
tower.setGuardPos(pos);
playerIn.inventory.removeItemNoUpdate(playerIn.inventory.selected);
} else {
if (!compound.getAllKeys().contains(TAG_LAST_POS)) {
tower.resetPatrolTargets();
}
tower.addPatrolTargets(pos);
LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolclickpatrol", pos);
}
BlockPosUtil.write(compound, TAG_LAST_POS, pos);
return ActionResultType.SUCCESS;
}
use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by ldtteam.
the class AbstractEntityAIFight method atBuildingActions.
/**
* Task to do when at the own building, as guards only go there on requests and on dump
*/
protected void atBuildingActions() {
final IGuardBuilding building = getOwnBuilding();
for (final List<GuardGear> itemList : itemsNeeded) {
for (final GuardGear item : itemList) {
if (!(building.getBuildingLevel() >= item.getMinBuildingLevelRequired() && building.getBuildingLevel() <= item.getMaxBuildingLevelRequired())) {
continue;
}
if (item.getItemNeeded() == ToolType.SHIELD && worker.getCitizenColonyHandler().getColony().getResearchManager().getResearchEffects().getEffectStrength(SHIELD_USAGE) <= 0) {
continue;
}
int bestSlot = -1;
int bestLevel = -1;
IItemHandler bestHandler = null;
if (!ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType()))) {
bestLevel = ItemStackUtils.getMiningLevel(worker.getItemBySlot(item.getType()), item.getItemNeeded());
}
final Map<IItemHandler, List<Integer>> items = InventoryUtils.findAllSlotsInProviderWith(building, item::test);
if (items.isEmpty()) {
// None found, check for equipped
if (ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType()))) {
// create request
checkForToolorWeaponASync(item.getItemNeeded(), item.getMinArmorLevel(), item.getMaxArmorLevel());
}
} else {
// Compare levels
for (Map.Entry<IItemHandler, List<Integer>> entry : items.entrySet()) {
for (final Integer slot : entry.getValue()) {
final ItemStack stack = entry.getKey().getStackInSlot(slot);
if (ItemStackUtils.isEmpty(stack)) {
continue;
}
int currentLevel = ItemStackUtils.getMiningLevel(stack, item.getItemNeeded());
if (currentLevel > bestLevel) {
bestLevel = currentLevel;
bestSlot = slot;
bestHandler = entry.getKey();
}
}
}
}
// Transfer if needed
if (bestHandler != null) {
if (!ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType()))) {
final int slot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getInventoryCitizen(), stack -> stack == worker.getItemBySlot(item.getType()));
if (slot > -1) {
InventoryUtils.transferItemStackIntoNextFreeSlotInProvider(worker.getInventoryCitizen(), slot, building);
}
}
// Used for further comparisons, set to the right inventory slot afterwards
worker.setItemSlot(item.getType(), bestHandler.getStackInSlot(bestSlot));
InventoryUtils.transferItemStackIntoNextFreeSlotInItemHandler(bestHandler, bestSlot, worker.getInventoryCitizen());
}
}
}
equipInventoryArmor();
}
use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by ldtteam.
the class AbstractEntityAIFight method equipInventoryArmor.
/**
* Equips armor existing in inventory
*/
public void equipInventoryArmor() {
cleanArmor();
final IGuardBuilding building = getOwnBuilding();
for (final List<GuardGear> itemList : itemsNeeded) {
for (final GuardGear item : itemList) {
if (ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType())) && building.getBuildingLevel() >= item.getMinBuildingLevelRequired() && building.getBuildingLevel() <= item.getMaxBuildingLevelRequired()) {
int slot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getInventoryCitizen(), item::test);
if (slot > -1) {
worker.setItemSlot(item.getType(), worker.getInventoryCitizen().getStackInSlot(slot));
}
}
}
}
}
use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by Minecolonies.
the class AbstractEntityAIFight method equipInventoryArmor.
/**
* Equips armor existing in inventory
*/
public void equipInventoryArmor() {
cleanArmor();
final IGuardBuilding building = getOwnBuilding();
for (final List<GuardGear> itemList : itemsNeeded) {
for (final GuardGear item : itemList) {
if (ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType())) && building.getBuildingLevel() >= item.getMinBuildingLevelRequired() && building.getBuildingLevel() <= item.getMaxBuildingLevelRequired()) {
int slot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getInventoryCitizen(), item::test);
if (slot > -1) {
worker.setItemSlot(item.getType(), worker.getInventoryCitizen().getStackInSlot(slot));
}
}
}
}
}
Aggregations