Search in sources :

Example 6 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.

the class ItemScrollGuardHelp method useOn.

@Override
@NotNull
public ActionResultType useOn(ItemUseContext ctx) {
    final ActionResultType result = super.useOn(ctx);
    if (ctx.getLevel().isClientSide) {
        return result;
    }
    final TileEntity te = ctx.getLevel().getBlockEntity(ctx.getClickedPos());
    if (te instanceof TileEntityColonyBuilding && ctx.getPlayer() != null) {
        final IBuilding building = ((TileEntityColonyBuilding) te).getColony().getBuildingManager().getBuilding(ctx.getClickedPos());
        if (!(building instanceof AbstractBuildingGuards)) {
            LanguageHandler.sendPlayerMessage(ctx.getPlayer(), "minecolonies.scroll.noguardbuilding");
        }
    }
    return result;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) AbstractBuildingGuards(com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards) ActionResultType(net.minecraft.util.ActionResultType) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding 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;
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) AbstractBuildingGuards(com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IColony(com.minecolonies.api.colony.IColony) BlockPos(net.minecraft.util.math.BlockPos) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.

the class BuildingManager method read.

@Override
public void read(@NotNull final CompoundNBT compound) {
    buildings = ImmutableMap.of();
    maxChunkX = colony.getCenter().getX() >> 4;
    minChunkX = colony.getCenter().getX() >> 4;
    maxChunkZ = colony.getCenter().getZ() >> 4;
    minChunkZ = colony.getCenter().getZ() >> 4;
    // Buildings
    final ListNBT buildingTagList = compound.getList(TAG_BUILDINGS, Constants.NBT.TAG_COMPOUND);
    for (int i = 0; i < buildingTagList.size(); ++i) {
        final CompoundNBT buildingCompound = buildingTagList.getCompound(i);
        @Nullable final IBuilding b = IBuildingDataManager.getInstance().createFrom(colony, buildingCompound);
        if (b != null) {
            addBuilding(b);
            setMaxChunk(b);
        }
    }
    if (compound.getAllKeys().contains(TAG_NEW_FIELDS)) {
        // Fields before Buildings, because the Farmer needs them.
        final ListNBT fieldTagList = compound.getList(TAG_NEW_FIELDS, Constants.NBT.TAG_COMPOUND);
        for (int i = 0; i < fieldTagList.size(); ++i) {
            addField(BlockPosUtil.read(fieldTagList.getCompound(i), TAG_POS));
        }
    }
}
Also used : ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.

the class BuildingManager method removeBuilding.

@Override
public void removeBuilding(@NotNull final IBuilding building, final Set<ServerPlayerEntity> subscribers) {
    if (buildings.containsKey(building.getID())) {
        final ImmutableMap.Builder<BlockPos, IBuilding> builder = new ImmutableMap.Builder<>();
        for (final IBuilding tbuilding : buildings.values()) {
            if (tbuilding != building) {
                builder.put(tbuilding.getID(), tbuilding);
            }
        }
        buildings = builder.build();
        for (final ServerPlayerEntity player : subscribers) {
            Network.getNetwork().sendToPlayer(new ColonyViewRemoveBuildingMessage(colony, building.getID()), player);
        }
        Log.getLogger().info(String.format("Colony %d - removed AbstractBuilding %s of type %s", colony.getID(), building.getID(), building.getSchematicName()));
    }
    if (building instanceof BuildingTownHall) {
        townHall = null;
    } else if (building instanceof BuildingWareHouse) {
        wareHouses.remove(building);
    } else if (building instanceof BuildingMysticalSite) {
        mysticalSites.remove(building);
    }
    // Allow Citizens to fix up any data that wasn't fixed up by the AbstractBuilding's own onDestroyed
    for (@NotNull final ICitizenData citizen : colony.getCitizenManager().getCitizens()) {
        citizen.onRemoveBuilding(building);
        building.cancelAllRequestsOfCitizen(citizen);
    }
    colony.getRequestManager().onProviderRemovedFromColony(building);
    colony.getRequestManager().onRequesterRemovedFromColony(building.getRequester());
    colony.getCitizenManager().calculateMaxCitizens();
}
Also used : BuildingMysticalSite(com.minecolonies.coremod.colony.buildings.BuildingMysticalSite) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ICitizenData(com.minecolonies.api.colony.ICitizenData) BlockPos(net.minecraft.util.math.BlockPos) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ColonyViewRemoveBuildingMessage(com.minecolonies.coremod.network.messages.client.colony.ColonyViewRemoveBuildingMessage) NotNull(org.jetbrains.annotations.NotNull) ImmutableMap(com.google.common.collect.ImmutableMap) BuildingTownHall(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingTownHall) BuildingWareHouse(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingWareHouse)

Example 10 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.

the class BuildingManager method sendBuildingPackets.

/**
 * Sends packages to update the buildings.
 *
 * @param closeSubscribers the current event subscribers.
 * @param newSubscribers   the new event subscribers.
 */
private void sendBuildingPackets(final Set<ServerPlayerEntity> closeSubscribers, final Set<ServerPlayerEntity> newSubscribers) {
    if (isBuildingsDirty || !newSubscribers.isEmpty()) {
        final Set<ServerPlayerEntity> players = new HashSet<>();
        if (isBuildingsDirty) {
            players.addAll(closeSubscribers);
        }
        players.addAll(newSubscribers);
        for (@NotNull final IBuilding building : buildings.values()) {
            if (building.isDirty() || !newSubscribers.isEmpty()) {
                players.forEach(player -> Network.getNetwork().sendToPlayer(new ColonyViewBuildingViewMessage(building), player));
            }
        }
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) NotNull(org.jetbrains.annotations.NotNull) ColonyViewBuildingViewMessage(com.minecolonies.coremod.network.messages.client.colony.ColonyViewBuildingViewMessage)

Aggregations

IBuilding (com.minecolonies.api.colony.buildings.IBuilding)187 BlockPos (net.minecraft.util.math.BlockPos)71 NotNull (org.jetbrains.annotations.NotNull)45 IColony (com.minecolonies.api.colony.IColony)37 Nullable (org.jetbrains.annotations.Nullable)26 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)24 ICitizenData (com.minecolonies.api.colony.ICitizenData)22 World (net.minecraft.world.World)20 ItemStack (net.minecraft.item.ItemStack)19 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)19 TileEntity (net.minecraft.tileentity.TileEntity)17 CompoundNBT (net.minecraft.nbt.CompoundNBT)15 ArrayList (java.util.ArrayList)14 AbstractBuildingGuards (com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards)10 ItemStorage (com.minecolonies.api.crafting.ItemStorage)9 ResourceLocation (net.minecraft.util.ResourceLocation)9 IItemHandler (net.minecraftforge.items.IItemHandler)9 InventoryUtils (com.minecolonies.api.util.InventoryUtils)8 ItemStackUtils (com.minecolonies.api.util.ItemStackUtils)8 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)8