Search in sources :

Example 1 with Permissions

use of com.minecolonies.coremod.colony.permissions.Permissions in project minecolonies by Minecolonies.

the class Colony method sendPermissionsPackets.

/**
     * Sends packages to update the permissions.
     *
     * @param oldSubscribers    the existing subscribers.
     * @param hasNewSubscribers the new subscribers.
     */
private void sendPermissionsPackets(@NotNull final Set<EntityPlayerMP> oldSubscribers, final boolean hasNewSubscribers) {
    if (permissions.isDirty() || hasNewSubscribers) {
        subscribers.stream().filter(player -> permissions.isDirty() || !oldSubscribers.contains(player)).forEach(player -> {
            final Rank rank = getPermissions().getRank(player);
            MineColonies.getNetwork().sendTo(new PermissionsMessage.View(this, rank), player);
        });
    }
}
Also used : Permissions(com.minecolonies.coremod.colony.permissions.Permissions) AchievementUtils(com.minecolonies.coremod.util.AchievementUtils) java.util(java.util) ColonyUtils(com.minecolonies.coremod.util.ColonyUtils) Achievement(net.minecraft.stats.Achievement) com.minecolonies.coremod.network.messages(com.minecolonies.coremod.network.messages) NBTTagString(net.minecraft.nbt.NBTTagString) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) Rank(com.minecolonies.api.colony.permissions.Rank) EntityCitizen(com.minecolonies.coremod.entity.EntityCitizen) NBTTagList(net.minecraft.nbt.NBTTagList) ModAchievements(com.minecolonies.coremod.achievements.ModAchievements) Block(net.minecraft.block.Block) ServerUtils(com.minecolonies.coremod.util.ServerUtils) ConstructionTapeHelper(com.minecolonies.coremod.entity.ai.citizen.builder.ConstructionTapeHelper) Field(com.minecolonies.coremod.entity.ai.citizen.farmer.Field) com.minecolonies.api.util(com.minecolonies.api.util) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) StatList(net.minecraft.stats.StatList) World(net.minecraft.world.World) StatBase(net.minecraft.stats.StatBase) BlockPos(net.minecraft.util.math.BlockPos) InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) TileEntityColonyBuilding(com.minecolonies.coremod.tileentities.TileEntityColonyBuilding) Collectors(java.util.stream.Collectors) com.minecolonies.coremod.colony.buildings(com.minecolonies.coremod.colony.buildings) IBlockState(net.minecraft.block.state.IBlockState) Nullable(org.jetbrains.annotations.Nullable) MinecraftForge(net.minecraftforge.common.MinecraftForge) ColonyPermissionEventHandler(com.minecolonies.coremod.permissions.ColonyPermissionEventHandler) NBT(net.minecraftforge.common.util.Constants.NBT) MineColonies(com.minecolonies.coremod.MineColonies) EntityPlayer(net.minecraft.entity.player.EntityPlayer) AbstractWorkOrder(com.minecolonies.coremod.colony.workorders.AbstractWorkOrder) TickEvent(net.minecraftforge.fml.common.gameevent.TickEvent) NotNull(org.jetbrains.annotations.NotNull) ScarecrowTileEntity(com.minecolonies.coremod.tileentities.ScarecrowTileEntity) NBTUtil(net.minecraft.nbt.NBTUtil) Configurations(com.minecolonies.api.configuration.Configurations) Rank(com.minecolonies.api.colony.permissions.Rank)

Example 2 with Permissions

use of com.minecolonies.coremod.colony.permissions.Permissions 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 EntityItemPickupEvent
 */
@SubscribeEvent
public void on(final AttackEntityEvent event) {
    if (event.getTarget() instanceof EntityMob) {
        return;
    }
    @NotNull final EntityPlayer player = EntityUtils.getPlayerOfFakePlayer(event.getEntityPlayer(), event.getEntityPlayer().getEntityWorld());
    if (Configurations.gameplay.enableColonyProtection && colony.isCoordInColony(player.getEntityWorld(), player.getPosition())) {
        final Permissions perms = colony.getPermissions();
        if (event.getTarget() instanceof EntityCitizen) {
            final EntityCitizen citizen = (EntityCitizen) event.getTarget();
            if (citizen.getColonyJob() instanceof JobGuard && perms.hasPermission(event.getEntityPlayer(), Action.GUARDS_ATTACK)) {
                return;
            }
            if (perms.hasPermission(event.getEntityPlayer(), Action.ATTACK_CITIZEN)) {
                return;
            }
            cancelEvent(event, event.getEntityPlayer(), colony, Action.ATTACK_CITIZEN, event.getTarget().getPosition());
            return;
        }
        if (!(event.getTarget() instanceof EntityMob) && !perms.hasPermission(event.getEntityPlayer(), Action.ATTACK_ENTITY)) {
            cancelEvent(event, event.getEntityPlayer(), colony, Action.ATTACK_ENTITY, event.getTarget().getPosition());
        }
    }
}
Also used : EntityMob(net.minecraft.entity.monster.EntityMob) Permissions(com.minecolonies.coremod.colony.permissions.Permissions) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityCitizen(com.minecolonies.coremod.entity.EntityCitizen) JobGuard(com.minecolonies.coremod.colony.jobs.JobGuard) NotNull(org.jetbrains.annotations.NotNull) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 3 with Permissions

use of com.minecolonies.coremod.colony.permissions.Permissions in project minecolonies by Minecolonies.

the class ColonyPermissionEventHandler method on.

/**
 * PlayerInteractEvent handler.
 * <p>
 * Check, if a player right clicked a block.
 * Deny if:
 * - If the block is in colony
 * - block is AbstractBlockHut
 * - player has not permission
 *
 * @param event PlayerInteractEvent
 */
@SubscribeEvent
public void on(final PlayerInteractEvent event) {
    if (colony.isCoordInColony(event.getWorld(), event.getPos()) && !(event instanceof PlayerInteractEvent.EntityInteract || event instanceof PlayerInteractEvent.EntityInteractSpecific)) {
        final Block block = event.getWorld().getBlockState(event.getPos()).getBlock();
        // Huts
        if (block instanceof AbstractBlockHut && !colony.getPermissions().hasPermission(event.getEntityPlayer(), Action.ACCESS_HUTS)) {
            cancelEvent(event, event.getEntityPlayer(), colony, Action.ACCESS_HUTS, event.getPos());
            return;
        }
        final Permissions perms = colony.getPermissions();
        if (isFreeToInteractWith(block, event.getPos()) && perms.hasPermission(event.getEntityPlayer(), Action.ACCESS_FREE_BLOCKS)) {
            return;
        }
        if (Configurations.gameplay.enableColonyProtection) {
            if (!perms.hasPermission(event.getEntityPlayer(), Action.RIGHTCLICK_BLOCK) && block != null && block != Blocks.AIR) {
                cancelEvent(event, event.getEntityPlayer(), colony, Action.RIGHTCLICK_BLOCK, event.getPos());
                return;
            }
            if (block instanceof BlockContainer && !perms.hasPermission(event.getEntityPlayer(), Action.OPEN_CONTAINER)) {
                cancelEvent(event, event.getEntityPlayer(), colony, Action.OPEN_CONTAINER, event.getPos());
                return;
            }
            if (event.getWorld().getTileEntity(event.getPos()) != null && !perms.hasPermission(event.getEntityPlayer(), Action.RIGHTCLICK_ENTITY)) {
                cancelEvent(event, event.getEntityPlayer(), colony, Action.RIGHTCLICK_ENTITY, event.getPos());
                return;
            }
            final ItemStack stack = event.getItemStack();
            if (ItemStackUtils.isEmpty(stack) || stack.getItem() instanceof ItemFood) {
                return;
            }
            if (stack.getItem() instanceof ItemPotion && !perms.hasPermission(event.getEntityPlayer(), Action.THROW_POTION)) {
                cancelEvent(event, event.getEntityPlayer(), colony, Action.THROW_POTION, event.getPos());
                return;
            }
            if (stack.getItem() instanceof ItemScanTool && !perms.hasPermission(event.getEntityPlayer(), Action.USE_SCAN_TOOL)) {
                cancelEvent(event, event.getEntityPlayer(), colony, Action.USE_SCAN_TOOL, event.getPos());
                return;
            }
        }
    }
}
Also used : ItemPotion(net.minecraft.item.ItemPotion) ItemFood(net.minecraft.item.ItemFood) ItemScanTool(com.minecolonies.coremod.items.ItemScanTool) BlockContainer(net.minecraft.block.BlockContainer) Permissions(com.minecolonies.coremod.colony.permissions.Permissions) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack) AbstractBlockHut(com.minecolonies.coremod.blocks.AbstractBlockHut) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 4 with Permissions

use of com.minecolonies.coremod.colony.permissions.Permissions in project minecolonies by Minecolonies.

the class PermissionUtils method getPlayersWithAtLeastRank.

/**
 * Creates a list of players that have the given rank or higher.
 * <p>
 * This is using the enums ordinal method for comparison.
 *
 * @param colony The colony to get the players
 * @param rank   The rank to check
 * @return The list with online players that has the rank or higher
 */
@NotNull
public static List<Player> getPlayersWithAtLeastRank(@NotNull final Colony colony, @NotNull final Rank rank) {
    @NotNull final List<Player> playersWithAtLeastRank = new ArrayList<>();
    @NotNull final Permissions permissions = colony.getPermissions();
    @NotNull final Map<UUID, Player> players = permissions.getPlayers();
    for (@NotNull final Player player : players.values()) {
        if (player.getRank().ordinal() <= rank.ordinal()) {
            playersWithAtLeastRank.add(player);
        }
    }
    return playersWithAtLeastRank;
}
Also used : Player(com.minecolonies.api.colony.permissions.Player) ArrayList(java.util.ArrayList) Permissions(com.minecolonies.coremod.colony.permissions.Permissions) UUID(java.util.UUID) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with Permissions

use of com.minecolonies.coremod.colony.permissions.Permissions in project minecolonies by Minecolonies.

the class ColonyPackageManager method sendPermissionsPackets.

@Override
public void sendPermissionsPackets(@NotNull final Set<EntityPlayerMP> oldSubscribers, final boolean hasNewSubscribers) {
    final Permissions permissions = colony.getPermissions();
    if (permissions.isDirty() || hasNewSubscribers || colony.getWorld().rand.nextInt(CHANCE_TO_UPDATE) <= 1) {
        subscribers.stream().filter(player -> permissions.isDirty() || !oldSubscribers.contains(player)).forEach(player -> {
            final Rank rank = permissions.getRank(player);
            MineColonies.getNetwork().sendTo(new PermissionsMessage.View(colony, rank), player);
        });
    }
}
Also used : Permissions(com.minecolonies.coremod.colony.permissions.Permissions) MAX_SQ_DIST_OLD_SUBSCRIBER_UPDATE(com.minecolonies.api.util.constant.ColonyConstants.MAX_SQ_DIST_OLD_SUBSCRIBER_UPDATE) ColonyUtils(com.minecolonies.coremod.util.ColonyUtils) Colony(com.minecolonies.coremod.colony.Colony) World(net.minecraft.world.World) TICKS_HOUR(com.minecolonies.api.util.constant.Constants.TICKS_HOUR) ColonyViewMessage(com.minecolonies.coremod.network.messages.ColonyViewMessage) Set(java.util.Set) MAX_SQ_DIST_SUBSCRIBER_UPDATE(com.minecolonies.api.util.constant.ColonyConstants.MAX_SQ_DIST_SUBSCRIBER_UPDATE) ColonyStylesMessage(com.minecolonies.coremod.network.messages.ColonyStylesMessage) Structures(com.minecolonies.coremod.colony.Structures) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) HashSet(java.util.HashSet) Rank(com.minecolonies.api.colony.permissions.Rank) List(java.util.List) MineColonies(com.minecolonies.coremod.MineColonies) WorkManager(com.minecolonies.coremod.colony.WorkManager) EntityPlayer(net.minecraft.entity.player.EntityPlayer) AbstractWorkOrder(com.minecolonies.coremod.colony.workorders.AbstractWorkOrder) NotNull(org.jetbrains.annotations.NotNull) ColonyViewWorkOrderMessage(com.minecolonies.coremod.network.messages.ColonyViewWorkOrderMessage) PermissionsMessage(com.minecolonies.coremod.network.messages.PermissionsMessage) PermissionsMessage(com.minecolonies.coremod.network.messages.PermissionsMessage) Permissions(com.minecolonies.coremod.colony.permissions.Permissions) Rank(com.minecolonies.api.colony.permissions.Rank)

Aggregations

Permissions (com.minecolonies.coremod.colony.permissions.Permissions)5 NotNull (org.jetbrains.annotations.NotNull)4 EntityPlayer (net.minecraft.entity.player.EntityPlayer)3 Rank (com.minecolonies.api.colony.permissions.Rank)2 MineColonies (com.minecolonies.coremod.MineColonies)2 AbstractWorkOrder (com.minecolonies.coremod.colony.workorders.AbstractWorkOrder)2 EntityCitizen (com.minecolonies.coremod.entity.EntityCitizen)2 ColonyUtils (com.minecolonies.coremod.util.ColonyUtils)2 Block (net.minecraft.block.Block)2 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)2 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)2 Player (com.minecolonies.api.colony.permissions.Player)1 Configurations (com.minecolonies.api.configuration.Configurations)1 com.minecolonies.api.util (com.minecolonies.api.util)1 MAX_SQ_DIST_OLD_SUBSCRIBER_UPDATE (com.minecolonies.api.util.constant.ColonyConstants.MAX_SQ_DIST_OLD_SUBSCRIBER_UPDATE)1 MAX_SQ_DIST_SUBSCRIBER_UPDATE (com.minecolonies.api.util.constant.ColonyConstants.MAX_SQ_DIST_SUBSCRIBER_UPDATE)1 TICKS_HOUR (com.minecolonies.api.util.constant.Constants.TICKS_HOUR)1 ModAchievements (com.minecolonies.coremod.achievements.ModAchievements)1 AbstractBlockHut (com.minecolonies.coremod.blocks.AbstractBlockHut)1 Colony (com.minecolonies.coremod.colony.Colony)1