use of com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler in project minecolonies by Minecolonies.
the class WindowInteraction method setupInteraction.
/**
* Setup the current interaction.
*/
private void setupInteraction() {
if (currentInteraction >= interactions.size()) {
close();
return;
}
final IInteractionResponseHandler handler = interactions.get(currentInteraction);
final Box group = findPaneOfTypeByID(RESPONSE_BOX_ID, Box.class);
int y = 0;
int x = 0;
final Text chatText = findPaneOfTypeByID(CHAT_LABEL_ID, Text.class);
chatText.setTextAlignment(Alignment.TOP_LEFT);
chatText.setAlignment(Alignment.TOP_LEFT);
chatText.setText(citizen.getName() + ": " + handler.getInquiry().getString());
int responseIndex = 1;
for (final ITextComponent component : handler.getPossibleResponses()) {
final ButtonImage button = new ButtonImage();
button.setImage(new ResourceLocation(Constants.MOD_ID, MEDIUM_SIZED_BUTTON_RES));
button.setSize(BUTTON_LENGTH, BUTTON_HEIGHT);
button.setColors(SLIGHTLY_BLUE);
button.setPosition(x, y);
button.setID(BUTTON_RESPONSE_ID + responseIndex);
button.setTextRenderBox(BUTTON_LENGTH, BUTTON_HEIGHT);
button.setTextAlignment(Alignment.MIDDLE);
button.setText(component);
group.addChild(button);
y += button.getHeight();
if (y + button.getHeight() >= group.getHeight()) {
y = 0;
x += BUTTON_HEIGHT + BUTTON_BUFFER + button.getWidth();
}
responseIndex++;
}
handler.onWindowOpened(this, citizen);
}
use of com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler in project minecolonies by Minecolonies.
the class WindowInteraction method onButtonClicked.
/**
* Called when a button in the citizen has been clicked.
*
* @param button the clicked button.
*/
@Override
public void onButtonClicked(@NotNull final Button button) {
if (!interactions.isEmpty()) {
final IInteractionResponseHandler handler = interactions.get(currentInteraction);
for (final ITextComponent component : handler.getPossibleResponses()) {
if (component.getString().equals(button.getTextAsString())) {
if (handler.onClientResponseTriggered(component, Minecraft.getInstance().player, citizen, this)) {
currentInteraction++;
setupInteraction();
return;
}
}
}
}
}
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());
}
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());
}
}
}
}
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;
}
Aggregations