use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen 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.entity.citizen.AbstractEntityCitizen 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.entity.citizen.AbstractEntityCitizen in project minecolonies by Minecolonies.
the class ColonyPermissionEventHandler method on.
/**
* AttackEntityEvent handler.
* <p>
* Check, if a player tries to attack an entity.. Deny if: - If the attacking happens in the colony - Player is less than officer to the colony.
*
* @param event ItemEntityPickupEvent
*/
@SubscribeEvent
public void on(final AttackEntityEvent event) {
if (event.getTarget() instanceof MonsterEntity) {
return;
}
@NotNull final PlayerEntity player = EntityUtils.getPlayerOfFakePlayer(event.getPlayer(), event.getPlayer().getCommandSenderWorld());
if (MineColonies.getConfig().getServer().enableColonyProtection.get() && colony.isCoordInColony(player.getCommandSenderWorld(), new BlockPos(player.position()))) {
final Permissions perms = colony.getPermissions();
if (event.getTarget() instanceof EntityCitizen) {
final AbstractEntityCitizen citizen = (AbstractEntityCitizen) event.getTarget();
if (citizen.getCitizenJobHandler().getColonyJob() instanceof AbstractJobGuard && perms.hasPermission(event.getPlayer(), Action.GUARDS_ATTACK)) {
return;
}
if (perms.hasPermission(event.getPlayer(), Action.ATTACK_CITIZEN)) {
return;
}
cancelEvent(event, event.getPlayer(), colony, Action.ATTACK_CITIZEN, new BlockPos(event.getTarget().position()));
return;
}
if (!(event.getTarget() instanceof MobEntity) && !perms.hasPermission(event.getPlayer(), Action.ATTACK_ENTITY)) {
cancelEvent(event, event.getPlayer(), colony, Action.ATTACK_ENTITY, new BlockPos(event.getTarget().position()));
}
}
}
use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen 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);
}
}
}
use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by Minecolonies.
the class EventListener method onUpdateEntityRadar.
@SubscribeEvent
public void onUpdateEntityRadar(@NotNull final EntityRadarUpdateEvent event) {
final WrappedEntity wrapper = event.getWrappedEntity();
final LivingEntity entity = wrapper.getEntityLivingRef().get();
if (entity instanceof AbstractEntityCitizen) {
final boolean isVisitor = entity instanceof VisitorCitizen;
TextComponent jobName;
if (isVisitor) {
if (!JourneymapOptions.getShowVisitors(this.jmap.getOptions())) {
wrapper.setDisable(true);
return;
}
jobName = new TranslationTextComponent(PARTIAL_JOURNEY_MAP_INFO + "visitor");
} else {
final String jobId = entity.getEntityData().get(DATA_JOB);
final JobEntry jobEntry = IJobRegistry.getInstance().getValue(new ResourceLocation(jobId));
final IJob<?> job = jobEntry == null ? null : jobEntry.produceJob(null);
if (job instanceof AbstractJobGuard ? !JourneymapOptions.getShowGuards(this.jmap.getOptions()) : !JourneymapOptions.getShowCitizens(this.jmap.getOptions())) {
wrapper.setDisable(true);
return;
}
jobName = new TranslationTextComponent(jobEntry == null ? PARTIAL_JOURNEY_MAP_INFO + "unemployed" : jobEntry.getTranslationKey());
}
if (JourneymapOptions.getShowColonistTooltip(this.jmap.getOptions())) {
ITextComponent name = entity.getCustomName();
if (name != null) {
wrapper.setEntityToolTips(Arrays.asList(name, jobName.setStyle(JOB_TOOLTIP)));
}
}
final boolean showName = event.getActiveUiState().ui.equals(Context.UI.Minimap) ? JourneymapOptions.getShowColonistNameMinimap(this.jmap.getOptions()) : JourneymapOptions.getShowColonistNameFullscreen(this.jmap.getOptions());
if (!showName) {
wrapper.setCustomName("");
}
if (!isVisitor && JourneymapOptions.getShowColonistTeamColour(this.jmap.getOptions())) {
wrapper.setColor(entity.getTeamColor());
}
} else if (entity instanceof AbstractEntityMinecoloniesMob) {
final JourneymapOptions.RaiderColor color = JourneymapOptions.getRaiderColor(this.jmap.getOptions());
if (JourneymapOptions.RaiderColor.NONE.equals(color)) {
wrapper.setDisable(true);
} else if (!JourneymapOptions.RaiderColor.HOSTILE.equals(color)) {
wrapper.setColor(color.getColor().getValue());
}
}
}
Aggregations