use of com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding 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();
for (int i = 0; i < workerInventory.getSlots(); i++) {
if (workerInventory.getStackInSlot(i).isEmpty()) {
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;
}
use of com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding in project minecolonies by ldtteam.
the class AbstractBuilding method onDestroyed.
@Override
public void onDestroyed() {
final AbstractTileEntityColonyBuilding tileEntityNew = this.getTileEntity();
final World world = colony.getWorld();
final Block block = world.getBlockState(this.getPosition()).getBlock();
if (tileEntityNew != null) {
InventoryUtils.dropItemHandler(tileEntityNew.getInventory(), world, tileEntityNew.getPosition().getX(), tileEntityNew.getPosition().getY(), tileEntityNew.getPosition().getZ());
world.updateNeighbourForOutputSignal(this.getPosition(), block);
}
ChunkDataHelper.claimColonyChunks(colony, false, this.getID(), getClaimRadius(getBuildingLevel()));
ConstructionTapeHelper.removeConstructionTape(getCorners(), world);
getModules(IBuildingEventsModule.class).forEach(IBuildingEventsModule::onDestroyed);
}
use of com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding in project minecolonies by ldtteam.
the class ItemResourceScroll method useOn.
/**
* Used when clicking on block in world.
*
* @param ctx the context of use.
* @return the result
*/
@Override
@NotNull
public ActionResultType useOn(ItemUseContext ctx) {
final ItemStack scroll = ctx.getPlayer().getItemInHand(ctx.getHand());
final CompoundNBT compound = checkForCompound(scroll);
TileEntity entity = ctx.getLevel().getBlockEntity(ctx.getClickedPos());
if (entity instanceof TileEntityColonyBuilding) {
compound.putInt(TAG_COLONY_ID, ((AbstractTileEntityColonyBuilding) entity).getColonyId());
BlockPosUtil.write(compound, TAG_BUILDER, ((AbstractTileEntityColonyBuilding) entity).getPosition());
if (!ctx.getLevel().isClientSide) {
LanguageHandler.sendPlayerMessage(ctx.getPlayer(), TranslationConstants.COM_MINECOLONIES_SCROLL_BUILDER_SET, ((AbstractTileEntityColonyBuilding) entity).getColony().getName());
}
} else if (ctx.getLevel().isClientSide) {
openWindow(compound, ctx.getPlayer());
}
return ActionResultType.SUCCESS;
}
use of com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding in project minecolonies by ldtteam.
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) {
LanguageHandler.sendPlayerMessage(ctx.getPlayer(), TranslationConstants.COM_MINECOLONIES_CLIPBOARD_COLONY_SET, ((AbstractTileEntityColonyBuilding) entity).getColony().getName());
}
} else if (ctx.getLevel().isClientSide) {
openWindow(compound, ctx.getLevel(), ctx.getPlayer());
}
return ActionResultType.SUCCESS;
}
use of com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding in project minecolonies by ldtteam.
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()) {
LanguageHandler.sendPlayerMessage(sender, "com.minecolonies.coremod.supplyneed");
return;
}
final IColony colony = IColonyManager.getInstance().getClosestColony(world, townHall);
String style = Constants.DEFAULT_STYLE;
final TileEntity tileEntity = world.getBlockEntity(townHall);
if (!(tileEntity instanceof TileEntityColonyBuilding)) {
LanguageHandler.sendPlayerMessage(sender, "com.minecolonies.coremod.gui.colony.create.notileentity");
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) {
LanguageHandler.sendPlayerMessage(sender, CANT_PLACE_COLONY_TOO_CLOSE_TO_SPAWN, MineColonies.getConfig().getServer().minDistanceFromWorldSpawn.get());
}
return;
} else if (spawnDistance > MineColonies.getConfig().getServer().maxDistanceFromWorldSpawn.get()) {
if (!world.isClientSide) {
LanguageHandler.sendPlayerMessage(sender, CANT_PLACE_COLONY_TOO_FAR_FROM_SPAWN, MineColonies.getConfig().getServer().maxDistanceFromWorldSpawn.get());
}
return;
}
}
if (colony != null && !IColonyManager.getInstance().isFarEnoughFromColonies(world, townHall)) {
LanguageHandler.sendPlayerMessage(sender, "com.minecolonies.coremod.gui.colony.denied.tooclose", colony.getName());
return;
}
final IColony ownedColony = IColonyManager.getInstance().getIColonyByOwner(world, sender);
if (ownedColony == null) {
IColonyManager.getInstance().createColony(world, townHall, sender, style);
IColonyManager.getInstance().getIColonyByOwner(world, sender).getBuildingManager().addNewBuilding((TileEntityColonyBuilding) tileEntity, world);
LanguageHandler.sendPlayerMessage((PlayerEntity) sender, "com.minecolonies.coremod.progress.colony_founded");
return;
}
LanguageHandler.sendPlayerMessage((PlayerEntity) sender, "com.minecolonies.coremod.gui.colony.create.failed");
}
Aggregations