Search in sources :

Example 1 with LootContext

use of net.minecraft.world.level.storage.loot.LootContext in project Denizen-For-Bukkit by DenizenScript.

the class FishingHelperImpl method getRandomReward.

public ItemStack getRandomReward(FishingHook hook, ResourceLocation key) {
    ServerLevel worldServer = (ServerLevel) hook.level;
    LootContext.Builder playerFishEvent2 = new LootContext.Builder(worldServer);
    LootTables registry = ((ServerLevel) hook.level).getServer().getLootTables();
    // registry.getLootTable(key).getLootContextParameterSet()
    LootContext info = playerFishEvent2.withOptionalParameter(LootContextParams.ORIGIN, new Vec3(hook.getX(), hook.getY(), hook.getZ())).withOptionalParameter(LootContextParams.TOOL, new ItemStack(Items.FISHING_ROD)).create(LootContextParamSets.FISHING);
    List<ItemStack> itemStacks = registry.get(key).getRandomItems(info);
    return itemStacks.get(worldServer.random.nextInt(itemStacks.size()));
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) LootContext(net.minecraft.world.level.storage.loot.LootContext) BuiltInLootTables(net.minecraft.world.level.storage.loot.BuiltInLootTables) LootTables(net.minecraft.world.level.storage.loot.LootTables) Vec3(net.minecraft.world.phys.Vec3) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) ItemStack(net.minecraft.world.item.ItemStack)

Example 2 with LootContext

use of net.minecraft.world.level.storage.loot.LootContext in project Denizen-For-Bukkit by DenizenScript.

the class FishingHelperImpl method getRandomReward.

public ItemStack getRandomReward(FishingHook hook, ResourceLocation key) {
    ServerLevel worldServer = (ServerLevel) hook.level;
    LootContext.Builder playerFishEvent2 = new LootContext.Builder(worldServer);
    LootTables registry = ((ServerLevel) hook.level).getServer().getLootTables();
    // registry.getLootTable(key).getLootContextParameterSet()
    LootContext info = playerFishEvent2.withOptionalParameter(LootContextParams.ORIGIN, new Vec3(hook.getX(), hook.getY(), hook.getZ())).withOptionalParameter(LootContextParams.TOOL, new ItemStack(Items.FISHING_ROD)).create(LootContextParamSets.FISHING);
    List<ItemStack> itemStacks = registry.get(key).getRandomItems(info);
    return itemStacks.get(worldServer.random.nextInt(itemStacks.size()));
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) LootContext(net.minecraft.world.level.storage.loot.LootContext) BuiltInLootTables(net.minecraft.world.level.storage.loot.BuiltInLootTables) LootTables(net.minecraft.world.level.storage.loot.LootTables) Vec3(net.minecraft.world.phys.Vec3) ItemStack(net.minecraft.world.item.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)

Example 3 with LootContext

use of net.minecraft.world.level.storage.loot.LootContext 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)

Aggregations

ServerLevel (net.minecraft.server.level.ServerLevel)3 ItemStack (net.minecraft.world.item.ItemStack)3 LootContext (net.minecraft.world.level.storage.loot.LootContext)3 BuiltInLootTables (net.minecraft.world.level.storage.loot.BuiltInLootTables)2 LootTables (net.minecraft.world.level.storage.loot.LootTables)2 Vec3 (net.minecraft.world.phys.Vec3)2 ServerPlayer (net.minecraft.server.level.ServerPlayer)1 ExperienceOrb (net.minecraft.world.entity.ExperienceOrb)1 ItemEntity (net.minecraft.world.entity.item.ItemEntity)1 Player (net.minecraft.world.entity.player.Player)1 Item (net.minecraft.world.item.Item)1 LootTable (net.minecraft.world.level.storage.loot.LootTable)1 CraftItemStack (org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)1 CraftItemStack (org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1 Transaction (org.spongepowered.api.data.Transaction)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 Overwrite (org.spongepowered.asm.mixin.Overwrite)1