use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class CommandCitizenKill method onExecute.
/**
* What happens when the command is executed after preConditions are successful.
*
* @param context the context of the command execution
*/
@Override
public int onExecute(final CommandContext<CommandSource> context) {
// Colony
final int colonyID = IntegerArgumentType.getInteger(context, COLONYID_ARG);
final IColony colony = IColonyManager.getInstance().getColonyByDimension(colonyID, context.getSource().getLevel().dimension());
if (colony == null) {
context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_COLONY_ID_NOT_FOUND, colonyID), true);
return 0;
}
if (!context.getSource().hasPermission(OP_PERM_LEVEL) && !MineColonies.getConfig().getServer().canPlayerUseKillCitizensCommand.get()) {
context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_DISABLED_IN_CONFIG), true);
return 0;
}
final ICitizenData citizenData = colony.getCitizenManager().getCivilian(IntegerArgumentType.getInteger(context, CITIZENID_ARG));
if (citizenData == null) {
context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_CITIZEN_NOT_FOUND), true);
return 0;
}
final Optional<AbstractEntityCitizen> optionalEntityCitizen = citizenData.getEntity();
if (!optionalEntityCitizen.isPresent()) {
context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_CITIZEN_NOT_LOADED), true);
return 0;
}
context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_CITIZEN_INFO, citizenData.getId(), citizenData.getName()), true);
final BlockPos position = optionalEntityCitizen.get().blockPosition();
context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_CITIZEN_INFO_POSITION, position.getX(), position.getY(), position.getZ()), true);
context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_CITIZEN_KILL_SUCCESS, position.getX(), position.getY(), position.getZ()), true);
optionalEntityCitizen.get().die(CONSOLE_DAMAGE_SOURCE);
return 1;
}
use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class RestartCitizenMessage method onExecute.
@Override
protected void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony) {
final ServerPlayerEntity player = ctxIn.getSender();
if (player == null) {
return;
}
final ICitizenData citizen = colony.getCitizenManager().getCivilian(citizenID);
// Restart also worker building and AI
citizen.scheduleRestart(player);
MessageUtils.format(MESSAGE_CITIZEN_RESTART_SCHEDULED, citizen.getName()).sendTo(player);
}
use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class TransferItemsToCitizenRequestMessage method onExecute.
@Override
protected void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony) {
final ICitizenData citizenData = colony.getCitizenManager().getCivilian(citizenId);
if (citizenData == null) {
Log.getLogger().warn("TransferItemsRequestMessage citizenData is null");
return;
}
final Optional<AbstractEntityCitizen> optionalEntityCitizen = citizenData.getEntity();
if (!optionalEntityCitizen.isPresent()) {
Log.getLogger().warn("TransferItemsRequestMessage entity citizen is null");
return;
}
final PlayerEntity player = ctxIn.getSender();
if (player == null) {
return;
}
final boolean isCreative = player.isCreative();
if (quantity <= 0 && !isCreative) {
Log.getLogger().warn("TransferItemsRequestMessage quantity below 0");
return;
}
// Inventory content before
Map<ItemStorage, ItemStorage> previousContent = null;
final int amountToTake;
if (isCreative) {
amountToTake = quantity;
} else {
amountToTake = Math.min(quantity, InventoryUtils.getItemCountInItemHandler(new InvWrapper(player.inventory), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, itemStack)));
}
final List<ItemStack> itemsToPut = new ArrayList<>();
int tempAmount = amountToTake;
while (tempAmount > 0) {
int count = Math.min(itemStack.getMaxStackSize(), tempAmount);
ItemStack stack = itemStack.copy();
stack.setCount(count);
itemsToPut.add(stack);
tempAmount -= count;
}
final AbstractEntityCitizen citizen = optionalEntityCitizen.get();
if (!isCreative && MineColonies.getConfig().getServer().debugInventories.get()) {
previousContent = InventoryUtils.getAllItemsForProviders(citizen.getInventoryCitizen(), new InvWrapper(player.inventory));
}
tempAmount = 0;
for (final ItemStack insertStack : itemsToPut) {
final ItemStack remainingItemStack = InventoryUtils.addItemStackToItemHandlerWithResult(citizen.getInventoryCitizen(), insertStack);
if (!ItemStackUtils.isEmpty(remainingItemStack)) {
tempAmount += (insertStack.getCount() - remainingItemStack.getCount());
break;
}
tempAmount += insertStack.getCount();
}
if (!isCreative) {
int amountToRemoveFromPlayer = tempAmount;
while (amountToRemoveFromPlayer > 0) {
final int slot = InventoryUtils.findFirstSlotInItemHandlerWith(new InvWrapper(player.inventory), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, itemStack));
final ItemStack itemsTaken = player.inventory.removeItem(slot, amountToRemoveFromPlayer);
amountToRemoveFromPlayer -= ItemStackUtils.getSize(itemsTaken);
}
}
if (!isCreative && previousContent != null && MineColonies.getConfig().getServer().debugInventories.get()) {
InventoryUtils.doStorageSetsMatch(previousContent, InventoryUtils.getAllItemsForProviders(citizen.getInventoryCitizen(), new InvWrapper(player.inventory)), true);
}
}
use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class AssignUnassignMessage method onExecute.
@Override
public void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony, final DefaultBuildingInstance building) {
final ICitizenData citizen = colony.getCitizenManager().getCivilian(citizenID);
final AbstractAssignedCitizenModule module;
if (jobEntry == null) {
module = building.getFirstModuleOccurance(LivingBuildingModule.class);
} else {
module = building.getModuleMatching(WorkerBuildingModule.class, m -> m.getJobEntry() == jobEntry);
}
if (assign && !module.isFull() && !building.equals(citizen.getHomeBuilding())) {
if (citizen.getHomeBuilding() != null) {
citizen.getHomeBuilding().getFirstModuleOccurance(LivingBuildingModule.class).removeCitizen(citizen);
}
module.assignCitizen(citizen);
} else if (module.hasAssignedCitizen(citizen)) {
module.removeCitizen(citizen);
}
}
use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class RecallCitizenMessage method onExecute.
@Override
protected void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony, final IBuilding building) {
final List<ICitizenData> citizens = new ArrayList<>(building.getAllAssignedCitizen());
for (int i = 0; i < building.getAllAssignedCitizen().size(); i++) {
Optional<AbstractEntityCitizen> optionalEntityCitizen = citizens.get(i).getEntity();
final ICitizenData citizenData = citizens.get(i);
if (!optionalEntityCitizen.isPresent()) {
if (citizenData != null) {
Log.getLogger().warn(String.format("Citizen #%d:%d has gone AWOL, respawning them!", colony.getID(), citizenData.getId()));
citizenData.setNextRespawnPosition(EntityUtils.getSpawnPoint(colony.getWorld(), building.getPosition()));
citizenData.updateEntityIfNecessary();
} else {
Log.getLogger().warn("Citizen is AWOL and citizenData is null!");
return;
}
} else if (optionalEntityCitizen.get().getTicksExisted() == 0) {
citizenData.getEntity().ifPresent(Entity::remove);
citizenData.updateEntityIfNecessary();
}
final BlockPos loc = building.getPosition();
if (optionalEntityCitizen.isPresent() && !TeleportHelper.teleportCitizen(optionalEntityCitizen.get(), colony.getWorld(), loc)) {
final PlayerEntity player = ctxIn.getSender();
if (player == null) {
return;
}
MessageUtils.format(WARNING_CITIZEN_RECALL_FAILED).sendTo(player);
}
}
}
Aggregations