Java Code Examples for org.spongepowered.api.entity.Entity#getLocation()

The following examples show how to use org.spongepowered.api.entity.Entity#getLocation() . 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: CachedEntity.java    From Web-API with MIT License 6 votes vote down vote up
public CachedEntity(Entity entity) {
    super(entity);

    this.type = new CachedCatalogType(entity.getType());
    this.uuid = UUID.fromString(entity.getUniqueId().toString());
    this.location = new CachedLocation(entity.getLocation());

    this.rotation = entity.getRotation().clone();
    this.velocity = entity.getVelocity().clone();
    this.scale = entity.getScale().clone();

    if (entity instanceof Carrier) {
        try {
            this.inventory = new CachedInventory(((Carrier) entity).getInventory());
        } catch (AbstractMethodError ignored) {}
    }
}
 
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: EntityEventHandler.java    From GriefDefender with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onEntityMount(RideEntityEvent.Mount event) {
    if (!GDFlags.ENTITY_RIDING) {
        return;
    }

    final Entity entity = event.getTargetEntity();
    final World world = entity.getWorld();
    if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(world.getUniqueId())) {
        return;
    }
    if (GriefDefenderPlugin.isSourceIdBlacklisted(Flags.ENTITY_RIDING.getName(), entity, world.getUniqueId())) {
        return;
    }
    if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.ENTITY_RIDING.getName(), entity, world.getUniqueId())) {
        return;
    }

    GDTimings.ENTITY_MOUNT_EVENT.startTiming();
    final Object source = event.getSource();
    Player player = source instanceof Player ? (Player) source : null;
    final Location<World> location = entity.getLocation();
    final GDClaim targetClaim = GriefDefenderPlugin.getInstance().dataStore.getClaimAt(location);

    if (GDPermissionManager.getInstance().getFinalPermission(event, location, targetClaim, Flags.ENTITY_RIDING, source, entity, player, TrustTypes.ACCESSOR, true) == Tristate.FALSE) {
        if (player != null) {
            //sendInteractEntityDenyMessage(targetClaim, player, null, entity);
        }
        event.setCancelled(true);
    }

    GDTimings.ENTITY_MOUNT_EVENT.stopTiming();
}
 
Example 4
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerDispenseItem(DropItemEvent.Dispense event, @Root Entity spawncause) {
    if (!GDFlags.ITEM_DROP || !(spawncause instanceof User)) {
        return;
    }

    final User user = (User) spawncause;
    final World world = spawncause.getWorld();
    if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(world.getUniqueId())) {
        return;
    }

    GDTimings.PLAYER_DISPENSE_ITEM_EVENT.startTimingIfSync();
    Player player = user instanceof Player ? (Player) user : null;
    GDPlayerData playerData = this.dataStore.getOrCreatePlayerData(world, user.getUniqueId());

    for (Entity entityItem : event.getEntities()) {
        if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.ITEM_DROP.toString(), entityItem, world.getProperties())) {
            continue;
        }

        Location<World> location = entityItem.getLocation();
        GDClaim claim = this.dataStore.getClaimAtPlayer(playerData, location);
        if (claim != null) {
            if (GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.ITEM_DROP, user, entityItem, user, TrustTypes.ACCESSOR, true) == Tristate.FALSE) {
                event.setCancelled(true);
                if (spawncause instanceof Player) {
                    final Component message = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.PERMISSION_ITEM_DROP,
                            ImmutableMap.of(
                            "player", claim.getOwnerName(),
                            "item", entityItem.getType().getId()));
                    GriefDefenderPlugin.sendClaimDenyMessage(claim, player, message);
                }
                GDTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            }
        }
    }
    GDTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
}
 
Example 5
Source File: PlayerInteractListener.java    From EagleFactions with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onItemUse(final InteractItemEvent event, @Root final Player player)
{
    if (event.getItemStack() == ItemStackSnapshot.NONE)
        return;

    final Vector3d interactionPoint = event.getInteractionPoint().orElse(player.getLocation().getPosition());
    Location<World> location = new Location<>(player.getWorld(), interactionPoint);

    //Handle hitting entities
    boolean hasHitEntity = event.getContext().containsKey(EventContextKeys.ENTITY_HIT);
    if(hasHitEntity)
    {
        final Entity hitEntity = event.getContext().get(EventContextKeys.ENTITY_HIT).get();
        if (hitEntity instanceof Living && !(hitEntity instanceof ArmorStand))
            return;

        location = hitEntity.getLocation();
    }

    final ProtectionResult protectionResult = super.getPlugin().getProtectionManager().canUseItem(location, player, event.getItemStack(), true);
    if (!protectionResult.hasAccess())
    {
        event.setCancelled(true);
        return;
    }
}
 
Example 6
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 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: GlobalListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void PlayerDropItem(DropItemEvent.Dispense e, @Root Player p) {
    for (Entity ent : e.getEntities()) {
        Location<World> l = ent.getLocation();
        Region r = RedProtect.get().rm.getTopRegion(l, this.getClass().getName());

        if (r == null && !RedProtect.get().config.globalFlagsRoot().worlds.get(p.getWorld().getName()).player_candrop && !p.hasPermission("redprotect.world.bypass")) {
            e.setCancelled(true);
        }
    }
}
 
Example 9
Source File: EntityEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onEntityExplosionDetonate(ExplosionEvent.Detonate event) {
    if (!GDFlags.EXPLOSION_ENTITY || !GriefDefenderPlugin.getInstance().claimsEnabledForWorld(event.getTargetWorld().getUniqueId())) {
        return;
    }
    if (GriefDefenderPlugin.isSourceIdBlacklisted(Flags.EXPLOSION_ENTITY.getName(), event.getSource(), event.getTargetWorld().getProperties())) {
        return;
    }

    GDTimings.ENTITY_EXPLOSION_DETONATE_EVENT.startTimingIfSync();
    final User user = CauseContextHelper.getEventUser(event);
    Iterator<Entity> iterator = event.getEntities().iterator();
    GDClaim targetClaim = null;
    Object source = event.getSource();
    if (source instanceof Explosion) {
        final Explosion explosion = (Explosion) source;
        if (explosion.getSourceExplosive().isPresent()) {
            source = explosion.getSourceExplosive().get();
        } else {
            Entity exploder = event.getCause().first(Entity.class).orElse(null);
            if (exploder != null) {
                source = exploder;
            }
        }
    }

    final String sourceId = GDPermissionManager.getInstance().getPermissionIdentifier(source);
    boolean denySurfaceExplosion = GriefDefenderPlugin.getActiveConfig(event.getTargetWorld().getUniqueId()).getConfig().claim.explosionEntitySurfaceBlacklist.contains(sourceId);
    if (!denySurfaceExplosion) {
        denySurfaceExplosion = GriefDefenderPlugin.getActiveConfig(event.getTargetWorld().getUniqueId()).getConfig().claim.explosionEntitySurfaceBlacklist.contains("any");
    }

    while (iterator.hasNext()) {
        Entity entity = iterator.next();
        final Location<World> location = entity.getLocation();
        targetClaim =  GriefDefenderPlugin.getInstance().dataStore.getClaimAt(entity.getLocation(), targetClaim);
        if (denySurfaceExplosion && location.getExtent().getDimension().getType() != DimensionTypes.NETHER && location.getBlockY() >= location.getExtent().getSeaLevel()) {
            iterator.remove();
            GDPermissionManager.getInstance().processEventLog(event, location, targetClaim, Flags.EXPLOSION_ENTITY.getPermission(), source, entity, user, "explosion-surface", Tristate.FALSE);
            continue;
        }
        if (GDPermissionManager.getInstance().getFinalPermission(event, entity.getLocation(), targetClaim, Flags.EXPLOSION_ENTITY, source, entity, user) == Tristate.FALSE) {
            iterator.remove();
        } else if (GDPermissionManager.getInstance().getFinalPermission(event, entity.getLocation(), targetClaim, Flags.ENTITY_DAMAGE, source, entity, user) == Tristate.FALSE) {
            iterator.remove();
        }
    }
    GDTimings.ENTITY_EXPLOSION_DETONATE_EVENT.stopTimingIfSync();
}
 
Example 10
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 11
Source File: ExplosionListener.java    From EagleFactions with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onExplosion(final ExplosionEvent.Detonate event)
{
    final List<Location<World>> locationList = new ArrayList<>(event.getAffectedLocations());
    final List<Entity> entityList = new ArrayList<>(event.getEntities());
    User user = null;
    final Cause cause = event.getCause();
    final EventContext context = event.getContext();
    if (cause.root() instanceof TileEntity) {
        user = context.get(EventContextKeys.OWNER)
                .orElse(context.get(EventContextKeys.NOTIFIER)
                        .orElse(context.get(EventContextKeys.CREATOR)
                                .orElse(null)));
    } else {
        user = context.get(EventContextKeys.NOTIFIER)
                .orElse(context.get(EventContextKeys.OWNER)
                        .orElse(context.get(EventContextKeys.CREATOR)
                                .orElse(null)));
    }

    if(event.getCause().containsType(Player.class))
    {
        user = event.getCause().first(Player.class).get();
    }
    else if(event.getCause().containsType(User.class))
    {
        user = event.getCause().first(User.class).get();
    }

    for(final Entity entity : entityList)
    {
        final Location<World> entityLocation = entity.getLocation();
        if(user != null)
        {
            if(!super.getPlugin().getProtectionManager().canExplode(entityLocation, user, false).hasAccess())
            {
                event.getEntities().remove(entity);
            }
        }
        else if(!super.getPlugin().getProtectionManager().canExplode(entityLocation).hasAccess())
        {
            event.getEntities().remove(entity);
        }
    }

    for(final Location<World> location : locationList)
    {
        if(user != null)
        {
            if(!super.getPlugin().getProtectionManager().canExplode(location, user, false).hasAccess())
            {
                event.getAffectedLocations().remove(location);
            }
        }
        else if(!super.getPlugin().getProtectionManager().canExplode(location).hasAccess())
        {
            event.getAffectedLocations().remove(location);
        }
    }
}
 
Example 12
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerDispenseItem(DropItemEvent.Dispense event, @Root Entity spawncause) {
    if (!GPFlags.ITEM_DROP || !(spawncause instanceof User)) {
        return;
    }

    final User user = (User) spawncause;
    final World world = spawncause.getWorld();
    if (!GriefPreventionPlugin.instance.claimsEnabledForWorld(world.getProperties())) {
        return;
    }

    // in creative worlds, dropping items is blocked
    if (GriefPreventionPlugin.instance.claimModeIsActive(world.getProperties(), ClaimsMode.Creative)) {
        event.setCancelled(true);
        return;
    }

    GPTimings.PLAYER_DISPENSE_ITEM_EVENT.startTimingIfSync();
    Player player = user instanceof Player ? (Player) user : null;
    GPPlayerData playerData = this.dataStore.getOrCreatePlayerData(world, user.getUniqueId());

    // FEATURE: players in PvP combat, can't throw items on the ground to hide
    // them or give them away to other players before they are defeated

    // if in combat, don't let him drop it
    if (player != null && !GriefPreventionPlugin.getActiveConfig(world.getProperties()).getConfig().pvp.allowCombatItemDrops && playerData.inPvpCombat(player.getWorld())) {
        GriefPreventionPlugin.sendMessage(player, GriefPreventionPlugin.instance.messageData.pvpNoItemDrop.toText());
        event.setCancelled(true);
        GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
        return;
    }

    for (Entity entityItem : event.getEntities()) {
        if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.ITEM_DROP.toString(), entityItem, world.getProperties())) {
            continue;
        }

        Location<World> location = entityItem.getLocation();
        GPClaim claim = this.dataStore.getClaimAtPlayer(playerData, location);
        if (claim != null) {
            final Tristate override = GPPermissionHandler.getFlagOverride(event, location, claim, GPPermissions.ITEM_DROP,  user, entityItem, user, playerData, true);
            if (override != Tristate.UNDEFINED) {
                if (override == Tristate.TRUE) {
                    GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                    return;
                }

                event.setCancelled(true);
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            }

            // allow trusted users
            if (claim.isUserTrusted(user, TrustType.ACCESSOR)) {
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            }

            Tristate perm = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.ITEM_DROP, user, entityItem, user);
            if (perm == Tristate.TRUE) {
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            } else if (perm == Tristate.FALSE) {
                event.setCancelled(true);
                if (spawncause instanceof Player) {
                    final Text message = GriefPreventionPlugin.instance.messageData.permissionItemDrop
                            .apply(ImmutableMap.of(
                            "owner", claim.getOwnerName(),
                            "item", entityItem.getType().getId())).build();
                    GriefPreventionPlugin.sendClaimDenyMessage(claim, player, message);
                }
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            }
        }
    }
    GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
}
 
Example 13
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 14
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
public InteractEvent handleItemInteract(InteractEvent event, Player player, World world, ItemStack itemInHand) {
    if (lastInteractItemSecondaryTick == Sponge.getServer().getRunningTimeTicks() || lastInteractItemPrimaryTick == Sponge.getServer().getRunningTimeTicks()) {
        // ignore
        return event;
    }

    if (event instanceof InteractItemEvent.Primary) {
        lastInteractItemPrimaryTick = Sponge.getServer().getRunningTimeTicks();
    } else {
        lastInteractItemSecondaryTick = Sponge.getServer().getRunningTimeTicks();
    }

    final ItemType itemType = itemInHand.getType();
    if (itemInHand.isEmpty() || itemType instanceof ItemFood) {
        return event;
    }

    final boolean primaryEvent = event instanceof InteractItemEvent.Primary || event instanceof InteractBlockEvent.Primary;
    if (!GPFlags.INTERACT_ITEM_PRIMARY && primaryEvent || !GPFlags.INTERACT_ITEM_SECONDARY && !primaryEvent || !GriefPreventionPlugin.instance.claimsEnabledForWorld(world.getProperties())) {
        return event;
    }

    if (primaryEvent && GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.INTERACT_ITEM_PRIMARY.toString(), itemInHand.getType(), world.getProperties())) {
        return event;
    }
    if (!primaryEvent && GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.INTERACT_ITEM_SECONDARY.toString(), itemInHand.getType(), world.getProperties())) {
        return event;
    }

    final Cause cause = event.getCause();
    final EventContext context = cause.getContext();
    final BlockSnapshot blockSnapshot = context.get(EventContextKeys.BLOCK_HIT).orElse(BlockSnapshot.NONE);
    final Vector3d interactPoint = event.getInteractionPoint().orElse(null);
    final Entity entity = context.get(EventContextKeys.ENTITY_HIT).orElse(null);
    final Location<World> location = entity != null ? entity.getLocation() 
            : blockSnapshot != BlockSnapshot.NONE ? blockSnapshot.getLocation().get() 
                    : interactPoint != null ? new Location<World>(world, interactPoint) 
                            : player.getLocation();

    final String ITEM_PERMISSION = primaryEvent ? GPPermissions.INTERACT_ITEM_PRIMARY : GPPermissions.INTERACT_ITEM_SECONDARY;
    final GPPlayerData playerData = this.dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());
    if (!itemInHand.isEmpty() && (itemInHand.getType().equals(GriefPreventionPlugin.instance.modificationTool) ||
            itemInHand.getType().equals(GriefPreventionPlugin.instance.investigationTool))) {
        GPPermissionHandler.addEventLogEntry(event, location, itemInHand, blockSnapshot == null ? entity : blockSnapshot, player, ITEM_PERMISSION, null, Tristate.TRUE);
        if (investigateClaim(event, player, blockSnapshot, itemInHand)) {
            return event;
        }

        onPlayerHandleShovelAction(event, blockSnapshot, player,  ((HandInteractEvent) event).getHandType(), playerData);
        return event;
    }

    final GPClaim claim = this.dataStore.getClaimAtPlayer(location, playerData, true);
    if (GPPermissionHandler.getClaimPermission(event, location, claim, ITEM_PERMISSION, player, itemType, player, TrustType.ACCESSOR, true) == Tristate.FALSE) {
        Text message = GriefPreventionPlugin.instance.messageData.permissionInteractItem
                .apply(ImmutableMap.of(
                "owner", claim.getOwnerName(),
                "item", itemInHand.getType().getId())).build();
        GriefPreventionPlugin.sendClaimDenyMessage(claim, player, message);
        if (event instanceof InteractItemEvent) {
            if (!SpongeImplHooks.isFakePlayer(((EntityPlayerMP) player)) && itemType == ItemTypes.WRITABLE_BOOK) {
                ((EntityPlayerMP) player).closeScreen();
            }
        }
        event.setCancelled(true);
    }
    return event;
}