Search in sources :

Example 1 with IInteractionResponseHandler

use of com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler in project minecolonies by Minecolonies.

the class CitizenData method serializeNBT.

@Override
public CompoundNBT serializeNBT() {
    final CompoundNBT nbtTagCompound = new CompoundNBT();
    nbtTagCompound.putInt(TAG_ID, id);
    nbtTagCompound.putString(TAG_NAME, name);
    nbtTagCompound.putString(TAG_SUFFIX, textureSuffix);
    nbtTagCompound.putBoolean(TAG_FEMALE, female);
    nbtTagCompound.putBoolean(TAG_PAUSED, paused);
    nbtTagCompound.putBoolean(TAG_CHILD, isChild);
    nbtTagCompound.putInt(TAG_TEXTURE, textureId);
    nbtTagCompound.put(TAG_NEW_SKILLS, citizenSkillHandler.write());
    BlockPosUtil.write(nbtTagCompound, TAG_POS, getEntity().isPresent() ? getEntity().get().blockPosition() : lastPosition);
    if (nextRespawnPos != null) {
        BlockPosUtil.write(nbtTagCompound, TAG_RESPAWN_POS, nextRespawnPos);
    }
    nbtTagCompound.putDouble(TAG_SATURATION, saturation);
    if (job != null) {
        @NotNull final INBT jobCompound = job.serializeNBT();
        nbtTagCompound.put("job", jobCompound);
    }
    citizenHappinessHandler.write(nbtTagCompound);
    citizenMournHandler.write(nbtTagCompound);
    nbtTagCompound.put(TAG_INVENTORY, inventory.write(new ListNBT()));
    nbtTagCompound.putInt(TAG_HELD_ITEM_SLOT, inventory.getHeldItemSlot(Hand.MAIN_HAND));
    nbtTagCompound.putInt(TAG_OFFHAND_HELD_ITEM_SLOT, inventory.getHeldItemSlot(Hand.OFF_HAND));
    BlockPosUtil.write(nbtTagCompound, TAG_BEDS, bedPos);
    nbtTagCompound.putBoolean(TAG_ASLEEP, isAsleep);
    nbtTagCompound.putBoolean(TAG_JUST_ATE, justAte);
    @NotNull final ListNBT chatTagList = new ListNBT();
    for (@NotNull final IInteractionResponseHandler entry : citizenChatOptions.values()) {
        @NotNull final CompoundNBT chatOptionCompound = new CompoundNBT();
        chatOptionCompound.put(TAG_CHAT_OPTION, entry.serializeNBT());
        chatTagList.add(chatOptionCompound);
    }
    nbtTagCompound.put(TAG_CHAT_OPTIONS, chatTagList);
    nbtTagCompound.putBoolean(TAG_IDLE, idle);
    nbtTagCompound.putString(TAG_PARENT_A, parents.getA());
    nbtTagCompound.putString(TAG_PARENT_B, parents.getB());
    @NotNull final ListNBT siblingsNBT = new ListNBT();
    for (final int sibling : siblings) {
        siblingsNBT.add(IntNBT.valueOf(sibling));
    }
    nbtTagCompound.put(TAG_SIBLINGS, siblingsNBT);
    @NotNull final ListNBT childrenNBT = new ListNBT();
    for (final int child : children) {
        childrenNBT.add(IntNBT.valueOf(child));
    }
    nbtTagCompound.put(TAG_CHILDREN, childrenNBT);
    nbtTagCompound.putInt(TAG_PARTNER, partner);
    nbtTagCompound.putBoolean(TAG_ACTIVE, this.isWorking);
    return nbtTagCompound;
}
Also used : IInteractionResponseHandler(com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler) ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) INBT(net.minecraft.nbt.INBT) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with IInteractionResponseHandler

use of com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler in project minecolonies by Minecolonies.

the class CitizenData method triggerInteraction.

@Override
public void triggerInteraction(@NotNull final IInteractionResponseHandler handler) {
    if (!this.citizenChatOptions.containsKey(handler.getInquiry())) {
        this.citizenChatOptions.put(handler.getInquiry(), handler);
        for (final IInteractionResponseHandler childHandler : handler.genChildInteractions()) {
            this.citizenChatOptions.put(childHandler.getInquiry(), (ServerCitizenInteraction) childHandler);
        }
        markDirty();
    }
}
Also used : IInteractionResponseHandler(com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler)

Example 3 with IInteractionResponseHandler

use of com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler in project minecolonies by Minecolonies.

the class CitizenData method serializeViewNetworkData.

@Override
public void serializeViewNetworkData(@NotNull final PacketBuffer buf) {
    buf.writeUtf(name);
    buf.writeBoolean(female);
    buf.writeInt(getEntity().map(AbstractEntityCitizen::getId).orElse(-1));
    buf.writeBoolean(paused);
    buf.writeBoolean(isChild);
    buf.writeBoolean(homeBuilding != null);
    if (homeBuilding != null) {
        buf.writeBlockPos(homeBuilding.getID());
    }
    buf.writeBoolean(workBuilding != null);
    if (workBuilding != null) {
        buf.writeBlockPos(workBuilding.getID());
    }
    // If the entity is not present we assumes standard values.
    buf.writeFloat(getEntity().map(AbstractEntityCitizen::getHealth).orElse(MAX_HEALTH));
    buf.writeFloat(getEntity().map(AbstractEntityCitizen::getMaxHealth).orElse(MAX_HEALTH));
    buf.writeDouble(getSaturation());
    buf.writeDouble(citizenHappinessHandler.getHappiness(getColony()));
    buf.writeNbt(citizenSkillHandler.write());
    buf.writeUtf((job != null) ? job.getJobRegistryEntry().getTranslationKey() : "");
    buf.writeInt(colony.getID());
    final CompoundNBT compound = new CompoundNBT();
    compound.put("inventory", inventory.write(new ListNBT()));
    buf.writeNbt(compound);
    buf.writeBlockPos(lastPosition);
    if (colony.getWorld() != null) {
        final List<IInteractionResponseHandler> subInteractions = citizenChatOptions.values().stream().filter(e -> e.isVisible(colony.getWorld())).collect(Collectors.toList());
        buf.writeInt(subInteractions.size());
        for (final IInteractionResponseHandler interactionHandler : subInteractions) {
            buf.writeNbt(interactionHandler.serializeNBT());
        }
    } else {
        buf.writeInt(0);
    }
    final CompoundNBT happinessCompound = new CompoundNBT();
    citizenHappinessHandler.write(happinessCompound);
    buf.writeNbt(happinessCompound);
    buf.writeInt(status != null ? status.getId() : -1);
    buf.writeBoolean(job != null);
    if (job != null) {
        job.serializeToView(buf);
    }
    if (colony.getCitizenManager().getCivilian(partner) == null) {
        partner = 0;
    }
    siblings.removeIf(s -> colony.getCitizenManager().getCivilian(s) == null);
    children.removeIf(c -> colony.getCitizenManager().getCivilian(c) == null);
    buf.writeInt(partner);
    buf.writeInt(siblings.size());
    for (int sibling : siblings) {
        buf.writeInt(sibling);
    }
    buf.writeInt(children.size());
    for (int child : children) {
        buf.writeInt(child);
    }
    buf.writeUtf(parents.getA());
    buf.writeUtf(parents.getB());
}
Also used : LanguageHandler(com.ldtteam.structurize.util.LanguageHandler) Constants(net.minecraftforge.common.util.Constants) WALKING(com.minecolonies.api.research.util.ResearchConstants.WALKING) CompoundNBT(net.minecraft.nbt.CompoundNBT) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) MinecoloniesAPIProxy(com.minecolonies.api.MinecoloniesAPIProxy) ICitizenSkillHandler(com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenSkillHandler) AttributeModifierUtils(com.minecolonies.coremod.util.AttributeModifierUtils) StringTextComponent(net.minecraft.util.text.StringTextComponent) CAN_EAT(com.minecolonies.api.util.ItemStackUtils.CAN_EAT) CitizenConstants(com.minecolonies.api.util.constant.CitizenConstants) CitizenSkillHandler(com.minecolonies.coremod.entity.citizen.citizenhandlers.CitizenSkillHandler) ListNBT(net.minecraft.nbt.ListNBT) HEALTH_BOOST(com.minecolonies.api.research.util.ResearchConstants.HEALTH_BOOST) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Skill(com.minecolonies.api.entity.citizen.Skill) AttributeModifier(net.minecraft.entity.ai.attributes.AttributeModifier) ChatPriority(com.minecolonies.api.colony.interactionhandling.ChatPriority) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) Collectors(java.util.stream.Collectors) AbstractCivilianEntity(com.minecolonies.api.entity.citizen.AbstractCivilianEntity) IntNBT(net.minecraft.nbt.IntNBT) Nullable(org.jetbrains.annotations.Nullable) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) WorkerBuildingModule(com.minecolonies.coremod.colony.buildings.modules.WorkerBuildingModule) MineColonies(com.minecolonies.coremod.MineColonies) ServerCitizenInteraction(com.minecolonies.coremod.colony.interactionhandling.ServerCitizenInteraction) NotNull(org.jetbrains.annotations.NotNull) CitizenHappinessHandler(com.minecolonies.coremod.entity.citizen.citizenhandlers.CitizenHappinessHandler) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) CITIZEN_NOT_GUARD_NEAR_WORK(com.minecolonies.api.util.constant.TranslationConstants.CITIZEN_NOT_GUARD_NEAR_WORK) java.util(java.util) Suppression(com.minecolonies.api.util.constant.Suppression) TAG_ACTIVE(com.minecolonies.api.util.constant.BuildingConstants.TAG_ACTIVE) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) ICitizenData(com.minecolonies.api.colony.ICitizenData) ITextComponent(net.minecraft.util.text.ITextComponent) ItemStack(net.minecraft.item.ItemStack) Attributes(net.minecraft.entity.ai.attributes.Attributes) CitizenMournHandler(com.minecolonies.coremod.entity.citizen.citizenhandlers.CitizenMournHandler) AbstractAISkeleton(com.minecolonies.coremod.entity.ai.basic.AbstractAISkeleton) IJobDataManager(com.minecolonies.api.colony.jobs.registry.IJobDataManager) IInteractionResponseHandler(com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler) LivingBuildingModule(com.minecolonies.coremod.colony.buildings.modules.LivingBuildingModule) Hand(net.minecraft.util.Hand) com.minecolonies.api.util(com.minecolonies.api.util) WeakReference(java.lang.ref.WeakReference) INBT(net.minecraft.nbt.INBT) Entity(net.minecraft.entity.Entity) VisibleCitizenStatus(com.minecolonies.api.entity.citizen.VisibleCitizenStatus) BlockPos(net.minecraft.util.math.BlockPos) CITIZEN_NOT_GUARD_NEAR_HOME(com.minecolonies.api.util.constant.TranslationConstants.CITIZEN_NOT_GUARD_NEAR_HOME) IRequestable(com.minecolonies.api.colony.requestsystem.requestable.IRequestable) InventoryCitizen(com.minecolonies.api.inventory.InventoryCitizen) EquipmentSlotType(net.minecraft.inventory.EquipmentSlotType) EntityCitizen(com.minecolonies.coremod.entity.citizen.EntityCitizen) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) NbtTagConstants(com.minecolonies.api.util.constant.NbtTagConstants) IColony(com.minecolonies.api.colony.IColony) PacketBuffer(net.minecraft.network.PacketBuffer) IJob(com.minecolonies.api.colony.jobs.IJob) IInteractionResponseHandler(com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler) ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen)

Example 4 with IInteractionResponseHandler

use of com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler in project minecolonies by Minecolonies.

the class CitizenData method tick.

@Override
public void tick() {
    if (!getEntity().isPresent() || !getEntity().get().isAlive()) {
        return;
    }
    if (!isWorking && job != null && inactivityTimer != DISABLED && ++inactivityTimer >= job.getInactivityLimit()) {
        job.triggerActivityChangeAction(this.isWorking);
        inactivityTimer = DISABLED;
    }
    final List<IInteractionResponseHandler> toRemove = new ArrayList<>();
    for (final IInteractionResponseHandler handler : citizenChatOptions.values()) {
        try {
            if (!handler.isValid(this)) {
                toRemove.add(handler);
            }
        } catch (final Exception e) {
            Log.getLogger().warn("Error during validation of handler: " + handler.getInquiry(), e);
            // If anything goes wrong in checking validity, remove handler.
            toRemove.add(handler);
        }
    }
    if (!toRemove.isEmpty()) {
        markDirty();
    }
    for (final IInteractionResponseHandler handler : toRemove) {
        citizenChatOptions.remove(handler.getInquiry());
        for (final ITextComponent comp : handler.getPossibleResponses()) {
            if (citizenChatOptions.containsKey(handler.getResponseResult(comp))) {
                citizenChatOptions.get(handler.getResponseResult(comp)).removeParent(handler.getInquiry());
            }
        }
    }
}
Also used : IInteractionResponseHandler(com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler) ITextComponent(net.minecraft.util.text.ITextComponent)

Example 5 with IInteractionResponseHandler

use of com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler in project minecolonies by Minecolonies.

the class InteractionResponseHandlerManager method createFrom.

@Nullable
@Override
public IInteractionResponseHandler createFrom(@NotNull final ICitizen citizen, @NotNull final CompoundNBT compound) {
    final ResourceLocation handlerType = compound.getAllKeys().contains(NbtTagConstants.TAG_HANDLER_TYPE) ? new ResourceLocation(Constants.MOD_ID, compound.getString(NbtTagConstants.TAG_HANDLER_TYPE)) : ModInteractionResponseHandlers.STANDARD;
    final IInteractionResponseHandler handler = IInteractionResponseHandlerRegistry.getInstance().getValue(handlerType).getProducer().apply(citizen);
    if (handler != null) {
        try {
            handler.deserializeNBT(compound);
        } catch (final RuntimeException ex) {
            Log.getLogger().error(String.format("An Interaction %s has thrown an exception during loading, its state cannot be restored. Report this to the mod author", handlerType), ex);
            return null;
        }
    } else {
        Log.getLogger().warn(String.format("Unknown Interaction type '%s' or missing constructor of proper format.", handlerType));
    }
    return handler;
}
Also used : IInteractionResponseHandler(com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler) ResourceLocation(net.minecraft.util.ResourceLocation) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IInteractionResponseHandler (com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler)14 ITextComponent (net.minecraft.util.text.ITextComponent)7 ResourceLocation (net.minecraft.util.ResourceLocation)4 CompoundNBT (net.minecraft.nbt.CompoundNBT)3 INBT (net.minecraft.nbt.INBT)3 ListNBT (net.minecraft.nbt.ListNBT)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 ButtonImage (com.ldtteam.blockout.controls.ButtonImage)2 Text (com.ldtteam.blockout.controls.Text)2 Box (com.ldtteam.blockout.views.Box)2 LanguageHandler (com.ldtteam.structurize.util.LanguageHandler)2 MinecoloniesAPIProxy (com.minecolonies.api.MinecoloniesAPIProxy)2 ICitizenData (com.minecolonies.api.colony.ICitizenData)2 IColony (com.minecolonies.api.colony.IColony)2 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 ChatPriority (com.minecolonies.api.colony.interactionhandling.ChatPriority)2 IJob (com.minecolonies.api.colony.jobs.IJob)2 IJobDataManager (com.minecolonies.api.colony.jobs.registry.IJobDataManager)2 IRequestable (com.minecolonies.api.colony.requestsystem.requestable.IRequestable)2