Search in sources :

Example 16 with TileEntityColonyBuilding

use of com.minecolonies.api.tileentities.TileEntityColonyBuilding in project minecolonies by Minecolonies.

the class AbstractItemScroll method useOn.

@Override
@NotNull
public ActionResultType useOn(ItemUseContext ctx) {
    // Right click on block
    if (ctx.getLevel().isClientSide || !ctx.getPlayer().isShiftKeyDown() || !needsColony()) {
        return ActionResultType.PASS;
    }
    final TileEntity te = ctx.getLevel().getBlockEntity(ctx.getClickedPos());
    final ItemStack scroll = ctx.getPlayer().getItemInHand(ctx.getHand());
    final CompoundNBT compound = checkForCompound(scroll);
    if (te instanceof TileEntityColonyBuilding) {
        compound.putInt(TAG_COLONY_ID, ((AbstractTileEntityColonyBuilding) te).getColonyId());
        compound.putString(TAG_COLONY_DIM, ((AbstractTileEntityColonyBuilding) te).getColony().getWorld().dimension().location().toString());
        BlockPosUtil.write(compound, TAG_BUILDING_POS, ctx.getClickedPos());
        MessageUtils.format(MESSAGE_SCROLL_REGISTERED, ((AbstractTileEntityColonyBuilding) te).getColony().getName()).sendTo(ctx.getPlayer());
    }
    return ActionResultType.SUCCESS;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) CompoundNBT(net.minecraft.nbt.CompoundNBT) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) ItemStack(net.minecraft.item.ItemStack) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with TileEntityColonyBuilding

use of com.minecolonies.api.tileentities.TileEntityColonyBuilding in project minecolonies by Minecolonies.

the class ItemScrollHighlight method useOn.

@Override
@NotNull
public ActionResultType useOn(ItemUseContext ctx) {
    // Right click on block
    if (ctx.getLevel().isClientSide || ctx.getPlayer() == null || !ctx.getPlayer().isShiftKeyDown()) {
        return ActionResultType.PASS;
    }
    final TileEntity te = ctx.getLevel().getBlockEntity(ctx.getClickedPos());
    if (te instanceof TileEntityColonyBuilding) {
        ctx.getItemInHand().shrink(1);
        if (ctx.getLevel().random.nextInt(10) == 0) {
            ctx.getPlayer().displayClientMessage(new TranslationTextComponent("minecolonies.scroll.failed" + (ctx.getLevel().random.nextInt(FAIL_RESPONSES_TOTAL) + 1)).setStyle(Style.EMPTY.withColor(TextFormatting.GOLD)), true);
            ctx.getPlayer().addEffect(new EffectInstance(Effects.GLOWING, TICKS_SECOND * 300));
            SoundUtils.playSoundForPlayer((ServerPlayerEntity) ctx.getPlayer(), SoundEvents.ENDER_CHEST_OPEN, 0.3f, 1.0f);
            return ActionResultType.SUCCESS;
        }
        final TileEntityColonyBuilding building = (TileEntityColonyBuilding) te;
        final Set<ICitizenData> citizens = building.getColony().getBuildingManager().getBuilding(ctx.getClickedPos()).getAllAssignedCitizen();
        for (final ICitizenData citizenData : citizens) {
            if (citizenData.getEntity().isPresent()) {
                citizenData.getEntity().get().addEffect(new EffectInstance(Effects.GLOWING, TICKS_SECOND * 120));
                citizenData.getEntity().get().addEffect(new EffectInstance(Effects.MOVEMENT_SPEED, TICKS_SECOND * 120));
            }
        }
        SoundUtils.playSoundForPlayer((ServerPlayerEntity) ctx.getPlayer(), SoundEvents.PLAYER_LEVELUP, 0.3f, 1.0f);
    }
    return ActionResultType.SUCCESS;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) ICitizenData(com.minecolonies.api.colony.ICitizenData) EffectInstance(net.minecraft.potion.EffectInstance) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with TileEntityColonyBuilding

use of com.minecolonies.api.tileentities.TileEntityColonyBuilding in project minecolonies by Minecolonies.

the class ItemClipboard method useOn.

@Override
@NotNull
public ActionResultType useOn(final ItemUseContext ctx) {
    final ItemStack clipboard = ctx.getPlayer().getItemInHand(ctx.getHand());
    final CompoundNBT compound = checkForCompound(clipboard);
    final TileEntity entity = ctx.getLevel().getBlockEntity(ctx.getClickedPos());
    if (entity instanceof TileEntityColonyBuilding) {
        compound.putInt(TAG_COLONY, ((AbstractTileEntityColonyBuilding) entity).getColonyId());
        if (!ctx.getLevel().isClientSide) {
            MessageUtils.format(COM_MINECOLONIES_CLIPBOARD_COLONY_SET, ((TileEntityColonyBuilding) entity).getColony().getName()).sendTo(ctx.getPlayer());
        }
    } else if (ctx.getLevel().isClientSide) {
        openWindow(compound, ctx.getLevel(), ctx.getPlayer());
    }
    return ActionResultType.SUCCESS;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) CompoundNBT(net.minecraft.nbt.CompoundNBT) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) ItemStack(net.minecraft.item.ItemStack) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with TileEntityColonyBuilding

use of com.minecolonies.api.tileentities.TileEntityColonyBuilding in project minecolonies by Minecolonies.

the class EntityAIWorkDeliveryman method deliver.

/**
 * Deliver the items to the hut. TODO: Current precondition: The dman's inventory may only consist of the requested itemstack.
 *
 * @return the next state.
 */
private IAIState deliver() {
    final IRequest<? extends IDeliverymanRequestable> currentTask = job.getCurrentTask();
    if (!(currentTask instanceof DeliveryRequest)) {
        // Since prepareDelivery() was called earlier, go dumping first and then restart.
        return DUMPING;
    }
    worker.getCitizenData().setVisibleStatus(DELIVERING);
    worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.delivering"));
    final ILocation targetBuildingLocation = ((Delivery) currentTask.getRequest()).getTarget();
    if (!targetBuildingLocation.isReachableFromLocation(worker.getLocation())) {
        Log.getLogger().info(worker.getCitizenColonyHandler().getColony().getName() + ": " + worker.getName() + ": Can't inter dimension yet: ");
        return START_WORKING;
    }
    if (!worker.isWorkerAtSiteWithMove(targetBuildingLocation.getInDimensionLocation(), MIN_DISTANCE_TO_WAREHOUSE)) {
        setDelay(WALK_DELAY);
        return DELIVERY;
    }
    final TileEntity tileEntity = world.getBlockEntity(targetBuildingLocation.getInDimensionLocation());
    if (!(tileEntity instanceof TileEntityColonyBuilding)) {
        // TODO: Non-Colony deliveries are unsupported yet. Fix that at some point in time.
        job.finishRequest(true);
        return START_WORKING;
    }
    final IBuilding targetBuilding = ((AbstractTileEntityColonyBuilding) tileEntity).getBuilding();
    boolean success = true;
    boolean extracted = false;
    final IItemHandler workerInventory = worker.getInventoryCitizen();
    final List<ItemStorage> itemsToDeliver = job.getTaskListWithSameDestination((IRequest<? extends Delivery>) currentTask).stream().map(r -> new ItemStorage(r.getRequest().getStack())).collect(Collectors.toList());
    for (int i = 0; i < workerInventory.getSlots(); i++) {
        if (workerInventory.getStackInSlot(i).isEmpty()) {
            continue;
        }
        if (!itemsToDeliver.contains(new ItemStorage(workerInventory.getStackInSlot(i)))) {
            continue;
        }
        final ItemStack stack = workerInventory.extractItem(i, Integer.MAX_VALUE, false);
        if (ItemStackUtils.isEmpty(stack)) {
            continue;
        }
        extracted = true;
        final ItemStack insertionResultStack;
        // TODO: Please only push items into the target that were actually requested.
        if (targetBuilding instanceof AbstractBuilding) {
            insertionResultStack = InventoryUtils.forceItemStackToItemHandler(targetBuilding.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack, ((IBuilding) targetBuilding)::isItemStackInRequest);
        } else {
            // Buildings that are not inherently part of the request system, but just receive a delivery, cannot have their items replaced.
            // Therefore, the keep-predicate always returns true.
            insertionResultStack = InventoryUtils.forceItemStackToItemHandler(targetBuilding.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack, itemStack -> true);
        }
        if (!ItemStackUtils.isEmpty(insertionResultStack)) {
            if (ItemStack.matches(insertionResultStack, stack) && worker.getCitizenData() != null) {
                // The replaced stack is the same as the one we tried to put into the inventory.
                // Meaning, replacing failed.
                success = false;
                if (targetBuilding.hasModule(WorkerBuildingModule.class)) {
                    worker.getCitizenData().triggerInteraction(new PosBasedInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_NAMEDCHESTFULL, targetBuilding.getFirstModuleOccurance(WorkerBuildingModule.class).getFirstCitizen().getName()), ChatPriority.IMPORTANT, new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL), targetBuilding.getID()));
                } else {
                    worker.getCitizenData().triggerInteraction(new PosBasedInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL, new StringTextComponent(" :" + targetBuilding.getSchematicName())), ChatPriority.IMPORTANT, new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL), targetBuildingLocation.getInDimensionLocation()));
                }
            }
            // Insert the result back into the inventory so we do not lose it.
            workerInventory.insertItem(i, insertionResultStack, false);
        }
    }
    if (!extracted) {
        // This can only happen if the dman's inventory was completely empty.
        // Let the retry-system handle this case.
        worker.decreaseSaturationForContinuousAction();
        worker.getCitizenItemHandler().setHeldItem(Hand.MAIN_HAND, SLOT_HAND);
        job.finishRequest(false);
        // No need to go dumping in this case.
        return START_WORKING;
    }
    worker.getCitizenExperienceHandler().addExperience(1.5D);
    worker.decreaseSaturationForContinuousAction();
    worker.getCitizenItemHandler().setHeldItem(Hand.MAIN_HAND, SLOT_HAND);
    job.finishRequest(true);
    return success ? START_WORKING : DUMPING;
}
Also used : TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) IItemHandler(net.minecraftforge.items.IItemHandler) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) Item(net.minecraft.item.Item) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) DeliveryRequest(com.minecolonies.coremod.colony.requestsystem.requests.StandardRequests.DeliveryRequest) IWareHouse(com.minecolonies.api.colony.buildings.workerbuildings.IWareHouse) HashMap(java.util.HashMap) PickupRequest(com.minecolonies.coremod.colony.requestsystem.requests.StandardRequests.PickupRequest) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) PosBasedInteraction(com.minecolonies.coremod.colony.interactionhandling.PosBasedInteraction) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ArrayList(java.util.ArrayList) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) ItemStack(net.minecraft.item.ItemStack) IDeliverymanRequestable(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.IDeliverymanRequestable) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) Map(java.util.Map) StringTextComponent(net.minecraft.util.text.StringTextComponent) Hand(net.minecraft.util.Hand) ITEM_HANDLER_CAPABILITY(net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) Log(com.minecolonies.api.util.Log) TICKS_SECOND(com.minecolonies.api.util.constant.Constants.TICKS_SECOND) Constants(com.minecolonies.api.util.constant.Constants) AbstractEntityAIInteract(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIInteract) TranslationConstants(com.minecolonies.api.util.constant.TranslationConstants) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) VisibleCitizenStatus(com.minecolonies.api.entity.citizen.VisibleCitizenStatus) ChatPriority(com.minecolonies.api.colony.interactionhandling.ChatPriority) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) BlockPos(net.minecraft.util.math.BlockPos) IRequestable(com.minecolonies.api.colony.requestsystem.requestable.IRequestable) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) WorkerBuildingModule(com.minecolonies.coremod.colony.buildings.modules.WorkerBuildingModule) BuildingDeliveryman(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingDeliveryman) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) InventoryUtils(com.minecolonies.api.util.InventoryUtils) ResourceLocation(net.minecraft.util.ResourceLocation) TileEntity(net.minecraft.tileentity.TileEntity) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) DeliveryRequest(com.minecolonies.coremod.colony.requestsystem.requests.StandardRequests.DeliveryRequest) IItemHandler(net.minecraftforge.items.IItemHandler) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) WorkerBuildingModule(com.minecolonies.coremod.colony.buildings.modules.WorkerBuildingModule) PosBasedInteraction(com.minecolonies.coremod.colony.interactionhandling.PosBasedInteraction) ItemStorage(com.minecolonies.api.crafting.ItemStorage) TileEntity(net.minecraft.tileentity.TileEntity) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) StringTextComponent(net.minecraft.util.text.StringTextComponent) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) ItemStack(net.minecraft.item.ItemStack) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding)

Example 20 with TileEntityColonyBuilding

use of com.minecolonies.api.tileentities.TileEntityColonyBuilding in project minecolonies by Minecolonies.

the class CreateColonyMessage method onExecute.

@Override
public void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer) {
    final ServerPlayerEntity sender = ctxIn.getSender();
    final World world = ctxIn.getSender().level;
    if (sender == null) {
        return;
    }
    if (sender.getStats().getValue(Stats.ITEM_USED.get(ModItems.supplyChest)) <= 0 && !sender.isCreative()) {
        MessageUtils.format(MESSAGE_COLONY_START_SUPPLY_NEED).sendTo(sender);
        return;
    }
    final IColony colony = IColonyManager.getInstance().getClosestColony(world, townHall);
    String style = Constants.DEFAULT_STYLE;
    final TileEntity tileEntity = world.getBlockEntity(townHall);
    if (!(tileEntity instanceof TileEntityColonyBuilding)) {
        MessageUtils.format(WARNING_TOWN_HALL_NO_TILE_ENTITY).with(TextFormatting.BOLD, TextFormatting.DARK_RED).sendTo(sender);
        return;
    }
    if (!((AbstractTileEntityColonyBuilding) tileEntity).getStyle().isEmpty()) {
        style = ((AbstractTileEntityColonyBuilding) tileEntity).getStyle();
    }
    if (MineColonies.getConfig().getServer().restrictColonyPlacement.get()) {
        final double spawnDistance = Math.sqrt(BlockPosUtil.getDistanceSquared2D(townHall, ((ServerWorld) world).getSharedSpawnPos()));
        if (spawnDistance < MineColonies.getConfig().getServer().minDistanceFromWorldSpawn.get()) {
            if (!world.isClientSide) {
                MessageUtils.format(CANT_PLACE_COLONY_TOO_CLOSE_TO_SPAWN, MineColonies.getConfig().getServer().minDistanceFromWorldSpawn.get()).sendTo(sender);
            }
            return;
        } else if (spawnDistance > MineColonies.getConfig().getServer().maxDistanceFromWorldSpawn.get()) {
            if (!world.isClientSide) {
                MessageUtils.format(CANT_PLACE_COLONY_TOO_FAR_FROM_SPAWN, MineColonies.getConfig().getServer().maxDistanceFromWorldSpawn.get()).sendTo(sender);
            }
            return;
        }
    }
    if (colony != null && !IColonyManager.getInstance().isFarEnoughFromColonies(world, townHall)) {
        MessageUtils.format(MESSAGE_COLONY_CREATE_DENIED_TOO_CLOSE, colony.getName()).sendTo(sender);
        return;
    }
    final IColony ownedColony = IColonyManager.getInstance().getIColonyByOwner(world, sender);
    if (ownedColony == null) {
        IColonyManager.getInstance().createColony(world, townHall, sender, colonyName, style);
        IColonyManager.getInstance().getIColonyByOwner(world, sender).getBuildingManager().addNewBuilding((TileEntityColonyBuilding) tileEntity, world);
        MessageUtils.format(MESSAGE_COLONY_FOUNDED).with(TextFormatting.GOLD).sendTo(sender);
        return;
    }
    MessageUtils.format(WARNING_COLONY_FOUNDING_FAILED).with(TextFormatting.BOLD, TextFormatting.DARK_RED).sendTo(sender);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ServerWorld(net.minecraft.world.server.ServerWorld) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) IColony(com.minecolonies.api.colony.IColony) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World)

Aggregations

TileEntityColonyBuilding (com.minecolonies.api.tileentities.TileEntityColonyBuilding)26 TileEntity (net.minecraft.tileentity.TileEntity)22 AbstractTileEntityColonyBuilding (com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding)18 NotNull (org.jetbrains.annotations.NotNull)14 ItemStack (net.minecraft.item.ItemStack)9 Nullable (org.jetbrains.annotations.Nullable)8 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)6 CompoundNBT (net.minecraft.nbt.CompoundNBT)6 AbstractBlockHut (com.minecolonies.api.blocks.AbstractBlockHut)4 IColony (com.minecolonies.api.colony.IColony)4 TileEntityRack (com.minecolonies.api.tileentities.TileEntityRack)4 ICitizenData (com.minecolonies.api.colony.ICitizenData)2 IWareHouse (com.minecolonies.api.colony.buildings.workerbuildings.IWareHouse)2 ChatPriority (com.minecolonies.api.colony.interactionhandling.ChatPriority)2 ILocation (com.minecolonies.api.colony.requestsystem.location.ILocation)2 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)2 IRequestable (com.minecolonies.api.colony.requestsystem.requestable.IRequestable)2 Delivery (com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery)2 IDeliverymanRequestable (com.minecolonies.api.colony.requestsystem.requestable.deliveryman.IDeliverymanRequestable)2 ItemStorage (com.minecolonies.api.crafting.ItemStorage)2