use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by Minecolonies.
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()) {
MessageUtils.format(TOOL_GUARD_SCEPTER_TOWER_TOO_FAR).sendTo(playerIn);
return ActionResultType.FAIL;
}
if (hut.getSetting(AbstractBuildingGuards.GUARD_TASK).getValue().equals(GuardTaskSetting.GUARD)) {
MessageUtils.format(TOOL_GUARD_SCEPTER_ADD_GUARD_TARGET, pos).sendTo(playerIn);
tower.setGuardPos(pos);
playerIn.inventory.removeItemNoUpdate(playerIn.inventory.selected);
} else {
if (!compound.getAllKeys().contains(TAG_LAST_POS)) {
tower.resetPatrolTargets();
}
tower.addPatrolTargets(pos);
MessageUtils.format(TOOL_GUARD_SCEPTER_ADD_PATROL_TARGET, pos).sendTo(playerIn);
}
BlockPosUtil.write(compound, TAG_LAST_POS, pos);
return ActionResultType.SUCCESS;
}
use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by Minecolonies.
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 Minecolonies.
the class ItemBannerRallyGuards method useOn.
@NotNull
@Override
public ActionResultType useOn(final ItemUseContext context) {
final PlayerEntity player = context.getPlayer();
if (player == null) {
return ActionResultType.FAIL;
}
final ItemStack banner = context.getPlayer().getItemInHand(context.getHand());
final CompoundNBT compound = checkForCompound(banner);
if (isGuardBuilding(context.getLevel(), context.getClickedPos())) {
if (context.getLevel().isClientSide()) {
return ActionResultType.SUCCESS;
} else {
final IGuardBuilding building = getGuardBuilding(context.getLevel(), context.getClickedPos());
if (!building.getColony().getPermissions().hasPermission(player, Action.RALLY_GUARDS)) {
MessageUtils.format(PERMISSION_DENIED).sendTo(player);
return ActionResultType.FAIL;
}
final ILocation location = building.getLocation();
if (removeGuardTowerAtLocation(banner, location)) {
MessageUtils.format(COM_MINECOLONIES_BANNER_RALLY_GUARDS_DESELECTED, building.getSchematicName(), location.toString()).sendTo(player);
} else {
final ListNBT guardTowers = compound.getList(TAG_RALLIED_GUARDTOWERS, TAG_COMPOUND);
guardTowers.add(StandardFactoryController.getInstance().serialize(location));
MessageUtils.format(COM_MINECOLONIES_BANNER_RALLY_GUARDS_SELECTED, building.getSchematicName(), location.toString()).sendTo(player);
}
}
} else {
handleRightClick(banner, context.getPlayer());
}
return ActionResultType.SUCCESS;
}
use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by ldtteam.
the class ItemBannerRallyGuards method useOn.
@NotNull
@Override
public ActionResultType useOn(final ItemUseContext context) {
final PlayerEntity player = context.getPlayer();
if (player == null) {
return ActionResultType.FAIL;
}
final ItemStack banner = context.getPlayer().getItemInHand(context.getHand());
final CompoundNBT compound = checkForCompound(banner);
if (isGuardBuilding(context.getLevel(), context.getClickedPos())) {
if (context.getLevel().isClientSide()) {
return ActionResultType.SUCCESS;
} else {
final IGuardBuilding building = getGuardBuilding(context.getLevel(), context.getClickedPos());
if (!building.getColony().getPermissions().hasPermission(player, Action.RALLY_GUARDS)) {
LanguageHandler.sendPlayerMessage(player, "com.minecolonies.coremod.permission.no");
return ActionResultType.FAIL;
}
final ILocation location = building.getLocation();
if (removeGuardTowerAtLocation(banner, location)) {
LanguageHandler.sendPlayerMessage(context.getPlayer(), TranslationConstants.COM_MINECOLONIES_BANNER_RALLY_GUARDS_DESELECTED, building.getSchematicName(), location.toString());
} else {
final ListNBT guardTowers = compound.getList(TAG_RALLIED_GUARDTOWERS, TAG_COMPOUND);
guardTowers.add(StandardFactoryController.getInstance().serialize(location));
LanguageHandler.sendPlayerMessage(context.getPlayer(), TranslationConstants.COM_MINECOLONIES_BANNER_RALLY_GUARDS_SELECTED, building.getSchematicName(), location.toString());
}
}
} else {
handleRightClick(banner, context.getPlayer());
}
return ActionResultType.SUCCESS;
}
Aggregations