Search in sources :

Example 1 with ExperienceOrb

use of net.minecraft.world.entity.ExperienceOrb in project SpongeCommon by SpongePowered.

the class FishingHookMixin method retrieve.

/**
 * @author Aaron1011 - February 6th, 2015
 * @author Minecrell - December 24th, 2016 (Updated to Minecraft 1.11.2)
 * @author Minecrell - June 14th, 2017 (Rewritten to handle cases where no items are dropped)
 * @reason This needs to handle for both cases where a fish and/or an entity is being caught.
 */
@Overwrite
public int retrieve(final ItemStack stack) {
    final Player playerEntity = this.shadow$getPlayerOwner();
    if (!this.level.isClientSide && playerEntity != null) {
        int i = 0;
        // Sponge start
        final List<Transaction<@NonNull ItemStackSnapshot>> transactions;
        if (this.nibble > 0) {
            // Moved from below
            final LootContext.Builder lootcontext$builder = new LootContext.Builder((ServerLevel) this.level).withParameter(LootContextParams.ORIGIN, this.shadow$position()).withParameter(LootContextParams.TOOL, stack).withParameter(LootContextParams.THIS_ENTITY, (FishingHook) (Object) this).withRandom(this.random).withLuck((float) this.luck + playerEntity.getLuck());
            final LootTable lootTable = this.level.getServer().getLootTables().get(BuiltInLootTables.FISHING);
            final List<ItemStack> list = lootTable.getRandomItems(lootcontext$builder.create(LootContextParamSets.FISHING));
            transactions = list.stream().map(ItemStackUtil::snapshotOf).map(snapshot -> new Transaction<>(snapshot, snapshot)).collect(Collectors.toList());
            CriteriaTriggers.FISHING_ROD_HOOKED.trigger((ServerPlayer) playerEntity, stack, (FishingHook) (Object) this, list);
        } else {
            transactions = new ArrayList<>();
        }
        PhaseTracker.getCauseStackManager().pushCause(playerEntity);
        if (SpongeCommon.post(SpongeEventFactory.createFishingEventStop(PhaseTracker.getCauseStackManager().currentCause(), ((FishingBobber) this), transactions))) {
            // Event is cancelled
            return 0;
        }
        if (this.hookedIn != null) {
            this.bringInHookedEntity();
            CriteriaTriggers.FISHING_ROD_HOOKED.trigger((ServerPlayer) playerEntity, stack, (FishingHook) (Object) this, Collections.emptyList());
            this.level.broadcastEntityEvent((FishingHook) (Object) this, (byte) 31);
            i = this.hookedIn instanceof ItemEntity ? 3 : 5;
        }
        // Sponge start - Moved up to event call
        if (!transactions.isEmpty()) {
            // Use transactions
            for (final Transaction<@NonNull ItemStackSnapshot> transaction : transactions) {
                if (!transaction.isValid()) {
                    continue;
                }
                final ItemStack itemstack = (ItemStack) (Object) transaction.finalReplacement().createStack();
                // Sponge end
                final ItemEntity entityitem = new ItemEntity(this.level, this.shadow$getX(), this.shadow$getY(), this.shadow$getZ(), itemstack);
                final double d0 = playerEntity.getX() - this.shadow$getX();
                final double d1 = playerEntity.getY() - this.shadow$getY();
                final double d2 = playerEntity.getZ() - this.shadow$getZ();
                final double d3 = Mth.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
                // double d4 = 0.1D;
                entityitem.setDeltaMovement(d0 * 0.1D, d1 * 0.1D + Mth.sqrt(d3) * 0.08D, d2 * 0.1D);
                this.level.addFreshEntity(entityitem);
                playerEntity.level.addFreshEntity(new ExperienceOrb(playerEntity.level, playerEntity.getX(), playerEntity.getY() + 0.5D, playerEntity.getZ() + 0.5D, this.random.nextInt(6) + 1));
                final Item item = itemstack.getItem();
                if (item.is(ItemTags.FISHES)) {
                    playerEntity.awardStat(Stats.FISH_CAUGHT, 1);
                }
            }
            PhaseTracker.getCauseStackManager().popCause();
            // Sponge: Don't lower damage if we've also caught an entity
            i = Math.max(i, 1);
        }
        if (this.onGround) {
            i = 2;
        }
        this.shadow$remove();
        return i;
    } else {
        return 0;
    }
}
Also used : LootTable(net.minecraft.world.level.storage.loot.LootTable) ServerLevel(net.minecraft.server.level.ServerLevel) ServerPlayer(net.minecraft.server.level.ServerPlayer) Player(net.minecraft.world.entity.player.Player) ItemEntity(net.minecraft.world.entity.item.ItemEntity) LootContext(net.minecraft.world.level.storage.loot.LootContext) ExperienceOrb(net.minecraft.world.entity.ExperienceOrb) Item(net.minecraft.world.item.Item) Transaction(org.spongepowered.api.data.Transaction) NonNull(org.checkerframework.checker.nullness.qual.NonNull) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(net.minecraft.world.item.ItemStack) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with ExperienceOrb

use of net.minecraft.world.entity.ExperienceOrb in project SpongeCommon by SpongePowered.

the class AttackEntityPacketState method getSpawnTypeForTransaction.

@Override
public Supplier<SpawnType> getSpawnTypeForTransaction(final BasicPacketContext context, final net.minecraft.world.entity.Entity entityToSpawn) {
    final ServerboundInteractPacket useEntityPacket = context.getPacket();
    final ServerPlayer player = context.getPacketPlayer();
    final net.minecraft.world.entity.@Nullable Entity entity = useEntityPacket.getTarget(player.level);
    if (entity != null && (entity.removed || entity instanceof LivingEntity && ((LivingEntity) entity).isDeadOrDying())) {
        return entityToSpawn instanceof ExperienceOrb ? SpawnTypes.EXPERIENCE : SpawnTypes.DROPPED_ITEM;
    }
    if (entityToSpawn instanceof ItemEntity) {
        return SpawnTypes.DROPPED_ITEM;
    }
    return super.getSpawnTypeForTransaction(context, entityToSpawn);
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) ServerPlayer(net.minecraft.server.level.ServerPlayer) ExperienceOrb(net.minecraft.world.entity.ExperienceOrb) ServerboundInteractPacket(net.minecraft.network.protocol.game.ServerboundInteractPacket) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

ServerPlayer (net.minecraft.server.level.ServerPlayer)2 ExperienceOrb (net.minecraft.world.entity.ExperienceOrb)2 ItemEntity (net.minecraft.world.entity.item.ItemEntity)2 ServerboundInteractPacket (net.minecraft.network.protocol.game.ServerboundInteractPacket)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1 Player (net.minecraft.world.entity.player.Player)1 Item (net.minecraft.world.item.Item)1 ItemStack (net.minecraft.world.item.ItemStack)1 LootContext (net.minecraft.world.level.storage.loot.LootContext)1 LootTable (net.minecraft.world.level.storage.loot.LootTable)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 Transaction (org.spongepowered.api.data.Transaction)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 Overwrite (org.spongepowered.asm.mixin.Overwrite)1