org.spongepowered.api.event.entity.InteractEntityEvent Java Examples

The following examples show how to use org.spongepowered.api.event.entity.InteractEntityEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlayerInteractListener.java    From EagleFactions with MIT License 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onEntityInteract(final InteractEntityEvent event, @Root final Player player)
{
    final Entity targetEntity = event.getTargetEntity();
    final Optional<Vector3d> optionalInteractionPoint = event.getInteractionPoint();

    if((targetEntity instanceof Living) && !(targetEntity instanceof ArmorStand))
        return;

    final Vector3d blockPosition = optionalInteractionPoint.orElseGet(() -> targetEntity.getLocation().getPosition());
    final Location<World> location = new Location<>(targetEntity.getWorld(), blockPosition);
    boolean canInteractWithEntity = super.getPlugin().getProtectionManager().canInteractWithBlock(location, player, true).hasAccess();
    if(!canInteractWithEntity)
    {
        event.setCancelled(true);
        return;
    }
}
 
Example #2
Source File: EntityListener.java    From RedProtect with GNU General Public License v3.0 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onInteractEvent(InteractEntityEvent.Secondary e, @First Player p) {
    Entity et = e.getTargetEntity();
    Location<World> l = et.getLocation();
    Region r = RedProtect.get().rm.getTopRegion(l, this.getClass().getName());

    RedProtect.get().logger.debug(LogLevel.ENTITY, "EntityListener - InteractEntityEvent.Secondary entity " + et.getType().getName());

    if (r != null && !r.canInteractPassives(p) && (et instanceof Animal || et instanceof Villager || et instanceof Golem || et instanceof Ambient)) {
        if (RedProtect.get().getVersionHelper().checkHorseOwner(et, p)) {
            return;
        }
        e.setCancelled(true);
        RedProtect.get().lang.sendMessage(p, "entitylistener.region.cantinteract");
    }
}
 
Example #3
Source File: RequiredInteractListener.java    From Prism with MIT License 6 votes vote down vote up
/**
 * Listens for interactions by Players with active inspection wands.
 * <br>
 * This listener is required and does not track any events.
 *
 * @param event  InteractEntityEvent
 * @param player Player
 */
@Listener(order = Order.EARLY)
public void onInteractEntity(InteractEntityEvent event, @First Player player) {
    // Wand support
    if (!Prism.getInstance().getActiveWands().contains(player.getUniqueId())) {
        return;
    }

    event.setCancelled(true);

    // Ignore OffHand events
    if (event instanceof InteractEntityEvent.Primary.OffHand || event instanceof InteractEntityEvent.Secondary.OffHand) {
        return;
    }

    player.sendMessage(Format.error(Text.of("Cannot interact with entities while inspection is active!")));
}
 
Example #4
Source File: InteractPermListener.java    From Nations with MIT License 5 votes vote down vote up
@Listener(order=Order.FIRST, beforeModifications = true)
public void onInteract(InteractEntityEvent event, @First Player player)
{
	if (!ConfigHandler.getNode("worlds").getNode(player.getWorld().getName()).getNode("enabled").getBoolean())
	{
		return;
	}
	if (player.hasPermission("nations.admin.bypass.perm.interact"))
	{
		return;
	}
	Entity target = event.getTargetEntity();
	if (target instanceof Player || target instanceof Monster)
	{
		return;
	}
	if (target instanceof ItemFrame || target instanceof ArmorStand)
	{
		if (player.hasPermission("nations.admin.bypass.perm.build"))
		{
			return;
		}
		if (!DataHandler.getPerm("build", player.getUniqueId(), event.getTargetEntity().getLocation()))
		{
			event.setCancelled(true);
			player.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PERM_BUILD));
		}
		return;
	}
	if (!DataHandler.getPerm("interact", player.getUniqueId(), event.getTargetEntity().getLocation()))
	{
		event.setCancelled(true);
		player.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PERM_INTERACT));
	}
}
 
Example #5
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractEntity(InteractEntityEvent.Primary event, @First Player player) {
    if (!GPFlags.INTERACT_ENTITY_PRIMARY || !GriefPreventionPlugin.instance.claimsEnabledForWorld(player.getWorld().getProperties())) {
        return;
    }

    final Entity targetEntity = event.getTargetEntity();
    final HandType handType = event.getHandType();
    final ItemStack itemInHand = player.getItemInHand(handType).orElse(ItemStack.empty());
    final Object source = !itemInHand.isEmpty() ? itemInHand : player;
    if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.INTERACT_ENTITY_PRIMARY.toString(), targetEntity, player.getWorld().getProperties())) {
        return;
    }

    GPTimings.PLAYER_INTERACT_ENTITY_PRIMARY_EVENT.startTimingIfSync();
    Location<World> location = targetEntity.getLocation();
    GPClaim claim = this.dataStore.getClaimAt(location);
    if (event.isCancelled() && claim.getData().getPvpOverride() == Tristate.TRUE && targetEntity instanceof Player) {
        event.setCancelled(false);
    }

    Tristate result = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.INTERACT_ENTITY_PRIMARY, source, targetEntity, player, TrustType.ACCESSOR, true);
    if (result == Tristate.FALSE) {
        final Text message = GriefPreventionPlugin.instance.messageData.claimProtectedEntity
                .apply(ImmutableMap.of(
                "owner", claim.getOwnerName())).build();
        GriefPreventionPlugin.sendMessage(player, message);
        event.setCancelled(true);
        this.sendInteractEntityDenyMessage(itemInHand, targetEntity, claim, player, handType);
        GPTimings.PLAYER_INTERACT_ENTITY_PRIMARY_EVENT.stopTimingIfSync();
        return;
    }
}
 
Example #6
Source File: PlayerClickListener.java    From EssentialCmds with MIT License 5 votes vote down vote up
@Listener
public void onPlayerClickEntity(InteractEntityEvent event, @First Player player)
{
	if (EssentialCmds.frozenPlayers.contains(player.getUniqueId()))
	{
		player.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You cannot interact with entities while frozen."));
		event.setCancelled(true);
		return;
	}
}
 
Example #7
Source File: EntityListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onAttemptInteractAS(InteractEntityEvent e, @First Player p) {

    Entity ent = e.getTargetEntity();
    Location<World> l = ent.getLocation();
    Region r = RedProtect.get().rm.getTopRegion(l, this.getClass().getName());

    if (r == null) {
        //global flags
        if (ent.getType().equals(EntityTypes.ARMOR_STAND)) {
            if (!RedProtect.get().config.globalFlagsRoot().worlds.get(l.getExtent().getName()).build) {
                e.setCancelled(true);
                return;
            }
        }
        return;
    }

    ItemType itemInHand = RedProtect.get().getVersionHelper().getItemInHand(p);

    if (!itemInHand.equals(ItemTypes.NONE) && itemInHand.getType().equals(ItemTypes.ARMOR_STAND)) {
        if (!r.canBuild(p)) {
            e.setCancelled(true);
            RedProtect.get().lang.sendMessage(p, "blocklistener.region.cantbuild");
            return;
        }
    }

    //TODO Not working!
    if (ent.getType().equals(EntityTypes.ARMOR_STAND)) {
        if (!r.canBuild(p)) {
            if (!RedProtect.get().ph.hasPerm(p, "redprotect.bypass")) {
                RedProtect.get().lang.sendMessage(p, "playerlistener.region.cantedit");
                e.setCancelled(true);
            }
        }
    }
}
 
Example #8
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractEntity(InteractEntityEvent.Secondary event, @First Player player) {
    if (!GDFlags.INTERACT_ENTITY_SECONDARY || !GriefDefenderPlugin.getInstance().claimsEnabledForWorld(player.getWorld().getUniqueId())) {
        return;
    }

    final Entity targetEntity = event.getTargetEntity();
    final HandType handType = event.getHandType();
    final ItemStack itemInHand = player.getItemInHand(handType).orElse(ItemStack.empty());
    final Object source = player;
    if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.INTERACT_ENTITY_SECONDARY.getName(), targetEntity, player.getWorld().getProperties())) {
        return;
    }

    GDTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.startTimingIfSync();
    final Location<World> location = targetEntity.getLocation();
    final GDClaim claim = this.dataStore.getClaimAt(location);
    final GDPlayerData playerData = this.dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());
    if (targetEntity instanceof Living && targetEntity.get(Keys.TAMED_OWNER).isPresent()) {
        final UUID ownerID = targetEntity.get(Keys.TAMED_OWNER).get().orElse(null);
        if (ownerID != null) {
            // always allow owner to interact with their pets
            if (player.getUniqueId().equals(ownerID) || playerData.canIgnoreClaim(claim)) {
                if (playerData.petRecipientUniqueId != null) {
                    targetEntity.offer(Keys.TAMED_OWNER, Optional.of(playerData.petRecipientUniqueId));
                    playerData.petRecipientUniqueId = null;
                    GriefDefenderPlugin.sendMessage(player, MessageCache.getInstance().COMMAND_PET_CONFIRMATION);
                    event.setCancelled(true);
                }
                GDTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
                return;
            }
            // If pet protection is enabled, deny the interaction
            if (GriefDefenderPlugin.getActiveConfig(player.getWorld().getProperties()).getConfig().claim.protectTamedEntities) {
                final GDPermissionUser user = PermissionHolderCache.getInstance().getOrCreateUser(ownerID);
                final Component message = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.CLAIM_PROTECTED_ENTITY,
                        ImmutableMap.of(
                        "player", user.getName()));
                GriefDefenderPlugin.sendMessage(player, message);
                event.setCancelled(true);
                GDTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
                return;
            }
        }
    }

    if (playerData.canIgnoreClaim(claim)) {
        GDTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
        return;
    }

    Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.INTERACT_ENTITY_SECONDARY, source, targetEntity, player, TrustTypes.ACCESSOR, true);
    if (result == Tristate.TRUE && targetEntity instanceof ArmorStand) {
        result = GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.INTERACT_INVENTORY, source, targetEntity, player, TrustTypes.CONTAINER, false);
    }
    if (result == Tristate.FALSE) {
        event.setCancelled(true);
        this.sendInteractEntityDenyMessage(itemInHand, targetEntity, claim, player, handType);
        GDTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
    }
}
 
Example #9
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractEntity(InteractEntityEvent.Secondary event, @First Player player) {
    if (!GPFlags.INTERACT_ENTITY_SECONDARY || !GriefPreventionPlugin.instance.claimsEnabledForWorld(player.getWorld().getProperties())) {
        return;
    }

    final Entity targetEntity = event.getTargetEntity();
    final HandType handType = event.getHandType();
    final ItemStack itemInHand = player.getItemInHand(handType).orElse(ItemStack.empty());
    final Object source = !itemInHand.isEmpty() ? itemInHand : player;
    if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.INTERACT_ENTITY_SECONDARY.toString(), targetEntity, player.getWorld().getProperties())) {
        return;
    }

    GPTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.startTimingIfSync();
    Location<World> location = targetEntity.getLocation();
    GPClaim claim = this.dataStore.getClaimAt(location);
    GPPlayerData playerData = this.dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());

    // if entity is living and has an owner, apply special rules
    if (targetEntity instanceof Living) {
        EntityBridge spongeEntity = (EntityBridge) targetEntity;
        Optional<User> owner = ((OwnershipTrackedBridge) spongeEntity).tracked$getOwnerReference();
        if (owner.isPresent()) {
            UUID ownerID = owner.get().getUniqueId();
            // if the player interacting is the owner or an admin in ignore claims mode, always allow
            if (player.getUniqueId().equals(ownerID) || playerData.canIgnoreClaim(claim)) {
                // if giving away pet, do that instead
                if (playerData.petGiveawayRecipient != null) {
                    SpongeEntityType spongeEntityType = (SpongeEntityType) ((Entity) spongeEntity).getType();
                    if (spongeEntityType.equals(EntityTypes.UNKNOWN) || !spongeEntityType.getModId().equalsIgnoreCase("minecraft")) {
                        final Text message = GriefPreventionPlugin.instance.messageData.commandPetInvalid
                                .apply(ImmutableMap.of(
                                "type", spongeEntityType.getId())).build();
                        GriefPreventionPlugin.sendMessage(player, message);
                        playerData.petGiveawayRecipient = null;
                        GPTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
                        return;
                    }
                    ((Entity) spongeEntity).setCreator(playerData.petGiveawayRecipient.getUniqueId());
                    if (targetEntity instanceof EntityTameable) {
                        EntityTameable tameable = (EntityTameable) targetEntity;
                        tameable.setOwnerId(playerData.petGiveawayRecipient.getUniqueId());
                    } else if (targetEntity instanceof EntityHorse) {
                        EntityHorse horse = (EntityHorse) targetEntity;
                        horse.setOwnerUniqueId(playerData.petGiveawayRecipient.getUniqueId());
                        horse.setHorseTamed(true);
                    }
                    playerData.petGiveawayRecipient = null;
                    GriefPreventionPlugin.sendMessage(player, GriefPreventionPlugin.instance.messageData.commandPetConfirmation.toText());
                    event.setCancelled(true);
                    this.sendInteractEntityDenyMessage(itemInHand, targetEntity, claim, player, handType);
                }
                GPTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
                return;
            }
        }
    }

    if (playerData.canIgnoreClaim(claim)) {
        GPTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
        return;
    }

    Tristate result = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.INTERACT_ENTITY_SECONDARY, source, targetEntity, player, TrustType.ACCESSOR, true);
    if (result == Tristate.TRUE && targetEntity instanceof ArmorStand) {
        result = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.INVENTORY_OPEN, source, targetEntity, player, TrustType.CONTAINER, false);
    }
    if (result == Tristate.FALSE) {
        event.setCancelled(true);
        this.sendInteractEntityDenyMessage(itemInHand, targetEntity, claim, player, handType);
        GPTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
    }
}
 
Example #10
Source File: PreventListener.java    From FlexibleLogin with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractEntity(InteractEntityEvent interactEntityEvent, @First Player player) {
    checkLoginStatus(interactEntityEvent, player);
}