Search in sources :

Example 86 with ItemStack

use of net.minecraft.world.item.ItemStack 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 87 with ItemStack

use of net.minecraft.world.item.ItemStack in project Denizen-For-Bukkit by DenizenScript.

the class DenizenNetworkManagerImpl method processEquipmentForPacket.

public boolean processEquipmentForPacket(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
    if (FakeEquipCommand.overrides.isEmpty()) {
        return false;
    }
    try {
        if (packet instanceof ClientboundSetEquipmentPacket) {
            int eid = ((ClientboundSetEquipmentPacket) packet).getEntity();
            Entity ent = player.level.getEntity(eid);
            if (ent == null) {
                return false;
            }
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(ent.getUUID(), player.getBukkitEntity());
            if (override == null) {
                return false;
            }
            List<Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack>> equipment = new ArrayList<>(((ClientboundSetEquipmentPacket) packet).getSlots());
            ClientboundSetEquipmentPacket newPacket = new ClientboundSetEquipmentPacket(eid, equipment);
            for (int i = 0; i < equipment.size(); i++) {
                Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack> pair = equipment.get(i);
                ItemStack use = pair.getSecond();
                switch(pair.getFirst()) {
                    case MAINHAND:
                        use = override.hand == null ? use : CraftItemStack.asNMSCopy(override.hand.getItemStack());
                        break;
                    case OFFHAND:
                        use = override.offhand == null ? use : CraftItemStack.asNMSCopy(override.offhand.getItemStack());
                        break;
                    case CHEST:
                        use = override.chest == null ? use : CraftItemStack.asNMSCopy(override.chest.getItemStack());
                        break;
                    case HEAD:
                        use = override.head == null ? use : CraftItemStack.asNMSCopy(override.head.getItemStack());
                        break;
                    case LEGS:
                        use = override.legs == null ? use : CraftItemStack.asNMSCopy(override.legs.getItemStack());
                        break;
                    case FEET:
                        use = override.boots == null ? use : CraftItemStack.asNMSCopy(override.boots.getItemStack());
                        break;
                }
                equipment.set(i, new Pair<>(pair.getFirst(), use));
            }
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundEntityEventPacket) {
            Entity ent = ((ClientboundEntityEventPacket) packet).getEntity(player.level);
            if (!(ent instanceof net.minecraft.world.entity.LivingEntity)) {
                return false;
            }
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(ent.getUUID(), player.getBukkitEntity());
            if (override == null || (override.hand == null && override.offhand == null)) {
                return false;
            }
            if (((ClientboundEntityEventPacket) packet).getEventId() != (byte) 55) {
                return false;
            }
            List<Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack>> equipment = new ArrayList<>();
            ItemStack hand = override.hand != null ? CraftItemStack.asNMSCopy(override.hand.getItemStack()) : ((net.minecraft.world.entity.LivingEntity) ent).getMainHandItem();
            ItemStack offhand = override.offhand != null ? CraftItemStack.asNMSCopy(override.offhand.getItemStack()) : ((net.minecraft.world.entity.LivingEntity) ent).getOffhandItem();
            equipment.add(new Pair<>(net.minecraft.world.entity.EquipmentSlot.MAINHAND, hand));
            equipment.add(new Pair<>(net.minecraft.world.entity.EquipmentSlot.OFFHAND, offhand));
            ClientboundSetEquipmentPacket newPacket = new ClientboundSetEquipmentPacket(ent.getId(), equipment);
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundContainerSetContentPacket) {
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(player.getUUID(), player.getBukkitEntity());
            if (override == null) {
                return false;
            }
            int window = ((ClientboundContainerSetContentPacket) packet).getContainerId();
            if (window != 0) {
                return false;
            }
            NonNullList<ItemStack> items = (NonNullList<ItemStack>) ((ClientboundContainerSetContentPacket) packet).getItems();
            if (override.head != null) {
                items.set(5, CraftItemStack.asNMSCopy(override.head.getItemStack()));
            }
            if (override.chest != null) {
                items.set(6, CraftItemStack.asNMSCopy(override.chest.getItemStack()));
            }
            if (override.legs != null) {
                items.set(7, CraftItemStack.asNMSCopy(override.legs.getItemStack()));
            }
            if (override.boots != null) {
                items.set(8, CraftItemStack.asNMSCopy(override.boots.getItemStack()));
            }
            if (override.offhand != null) {
                items.set(45, CraftItemStack.asNMSCopy(override.offhand.getItemStack()));
            }
            if (override.hand != null) {
                items.set(player.getInventory().selected + 36, CraftItemStack.asNMSCopy(override.hand.getItemStack()));
            }
            ClientboundContainerSetContentPacket newPacket = new ClientboundContainerSetContentPacket(window, ((ClientboundContainerSetContentPacket) packet).getStateId(), items, ((ClientboundContainerSetContentPacket) packet).getCarriedItem());
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundContainerSetSlotPacket) {
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(player.getUUID(), player.getBukkitEntity());
            if (override == null) {
                return false;
            }
            int window = ((ClientboundContainerSetSlotPacket) packet).getContainerId();
            if (window != 0) {
                return false;
            }
            int slot = ((ClientboundContainerSetSlotPacket) packet).getSlot();
            org.bukkit.inventory.ItemStack item = null;
            if (slot == 5 && override.head != null) {
                item = override.head.getItemStack();
            } else if (slot == 6 && override.chest != null) {
                item = override.chest.getItemStack();
            } else if (slot == 7 && override.legs != null) {
                item = override.legs.getItemStack();
            } else if (slot == 8 && override.boots != null) {
                item = override.boots.getItemStack();
            } else if (slot == 45 && override.offhand != null) {
                item = override.offhand.getItemStack();
            } else if (slot == player.getInventory().selected + 36 && override.hand != null) {
                item = override.hand.getItemStack();
            }
            if (item == null) {
                return false;
            }
            ClientboundContainerSetSlotPacket newPacket = new ClientboundContainerSetSlotPacket(window, ((ClientboundContainerSetSlotPacket) packet).getStateId(), slot, CraftItemStack.asNMSCopy(item));
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        }
    } catch (Throwable ex) {
        Debug.echoError(ex);
    }
    return false;
}
Also used : CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) Entity(net.minecraft.world.entity.Entity) FakeEquipCommand(com.denizenscript.denizen.scripts.commands.entity.FakeEquipCommand) NonNullList(net.minecraft.core.NonNullList) Pair(com.mojang.datafixers.util.Pair) NonNullList(net.minecraft.core.NonNullList) ItemStack(net.minecraft.world.item.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)

Example 88 with ItemStack

use of net.minecraft.world.item.ItemStack in project Denizen-For-Bukkit by DenizenScript.

the class DenizenNetworkManagerImpl method processEquipmentForPacket.

public boolean processEquipmentForPacket(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
    if (FakeEquipCommand.overrides.isEmpty()) {
        return false;
    }
    try {
        if (packet instanceof ClientboundSetEquipmentPacket) {
            int eid = ((ClientboundSetEquipmentPacket) packet).getEntity();
            Entity ent = player.level.getEntity(eid);
            if (ent == null) {
                return false;
            }
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(ent.getUUID(), player.getBukkitEntity());
            if (override == null) {
                return false;
            }
            List<Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack>> equipment = new ArrayList<>(((ClientboundSetEquipmentPacket) packet).getSlots());
            ClientboundSetEquipmentPacket newPacket = new ClientboundSetEquipmentPacket(eid, equipment);
            for (int i = 0; i < equipment.size(); i++) {
                Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack> pair = equipment.get(i);
                ItemStack use = pair.getSecond();
                switch(pair.getFirst()) {
                    case MAINHAND:
                        use = override.hand == null ? use : CraftItemStack.asNMSCopy(override.hand.getItemStack());
                        break;
                    case OFFHAND:
                        use = override.offhand == null ? use : CraftItemStack.asNMSCopy(override.offhand.getItemStack());
                        break;
                    case CHEST:
                        use = override.chest == null ? use : CraftItemStack.asNMSCopy(override.chest.getItemStack());
                        break;
                    case HEAD:
                        use = override.head == null ? use : CraftItemStack.asNMSCopy(override.head.getItemStack());
                        break;
                    case LEGS:
                        use = override.legs == null ? use : CraftItemStack.asNMSCopy(override.legs.getItemStack());
                        break;
                    case FEET:
                        use = override.boots == null ? use : CraftItemStack.asNMSCopy(override.boots.getItemStack());
                        break;
                }
                equipment.set(i, new Pair<>(pair.getFirst(), use));
            }
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundEntityEventPacket) {
            Entity ent = ((ClientboundEntityEventPacket) packet).getEntity(player.level);
            if (!(ent instanceof net.minecraft.world.entity.LivingEntity)) {
                return false;
            }
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(ent.getUUID(), player.getBukkitEntity());
            if (override == null || (override.hand == null && override.offhand == null)) {
                return false;
            }
            if (((ClientboundEntityEventPacket) packet).getEventId() != (byte) 55) {
                return false;
            }
            List<Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack>> equipment = new ArrayList<>();
            ItemStack hand = override.hand != null ? CraftItemStack.asNMSCopy(override.hand.getItemStack()) : ((net.minecraft.world.entity.LivingEntity) ent).getMainHandItem();
            ItemStack offhand = override.offhand != null ? CraftItemStack.asNMSCopy(override.offhand.getItemStack()) : ((net.minecraft.world.entity.LivingEntity) ent).getOffhandItem();
            equipment.add(new Pair<>(net.minecraft.world.entity.EquipmentSlot.MAINHAND, hand));
            equipment.add(new Pair<>(net.minecraft.world.entity.EquipmentSlot.OFFHAND, offhand));
            ClientboundSetEquipmentPacket newPacket = new ClientboundSetEquipmentPacket(ent.getId(), equipment);
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundContainerSetContentPacket) {
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(player.getUUID(), player.getBukkitEntity());
            if (override == null) {
                return false;
            }
            int window = ((ClientboundContainerSetContentPacket) packet).getContainerId();
            if (window != 0) {
                return false;
            }
            NonNullList<ItemStack> items = (NonNullList<ItemStack>) ((ClientboundContainerSetContentPacket) packet).getItems();
            if (override.head != null) {
                items.set(5, CraftItemStack.asNMSCopy(override.head.getItemStack()));
            }
            if (override.chest != null) {
                items.set(6, CraftItemStack.asNMSCopy(override.chest.getItemStack()));
            }
            if (override.legs != null) {
                items.set(7, CraftItemStack.asNMSCopy(override.legs.getItemStack()));
            }
            if (override.boots != null) {
                items.set(8, CraftItemStack.asNMSCopy(override.boots.getItemStack()));
            }
            if (override.offhand != null) {
                items.set(45, CraftItemStack.asNMSCopy(override.offhand.getItemStack()));
            }
            if (override.hand != null) {
                items.set(player.getInventory().selected + 36, CraftItemStack.asNMSCopy(override.hand.getItemStack()));
            }
            ClientboundContainerSetContentPacket newPacket = new ClientboundContainerSetContentPacket(window, ((ClientboundContainerSetContentPacket) packet).getStateId(), items, ((ClientboundContainerSetContentPacket) packet).getCarriedItem());
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundContainerSetSlotPacket) {
            FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(player.getUUID(), player.getBukkitEntity());
            if (override == null) {
                return false;
            }
            int window = ((ClientboundContainerSetSlotPacket) packet).getContainerId();
            if (window != 0) {
                return false;
            }
            int slot = ((ClientboundContainerSetSlotPacket) packet).getSlot();
            org.bukkit.inventory.ItemStack item = null;
            if (slot == 5 && override.head != null) {
                item = override.head.getItemStack();
            } else if (slot == 6 && override.chest != null) {
                item = override.chest.getItemStack();
            } else if (slot == 7 && override.legs != null) {
                item = override.legs.getItemStack();
            } else if (slot == 8 && override.boots != null) {
                item = override.boots.getItemStack();
            } else if (slot == 45 && override.offhand != null) {
                item = override.offhand.getItemStack();
            } else if (slot == player.getInventory().selected + 36 && override.hand != null) {
                item = override.hand.getItemStack();
            }
            if (item == null) {
                return false;
            }
            ClientboundContainerSetSlotPacket newPacket = new ClientboundContainerSetSlotPacket(window, ((ClientboundContainerSetSlotPacket) packet).getStateId(), slot, CraftItemStack.asNMSCopy(item));
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        }
    } catch (Throwable ex) {
        Debug.echoError(ex);
    }
    return false;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) Entity(net.minecraft.world.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftEntity) LivingEntity(org.bukkit.entity.LivingEntity) FakeEquipCommand(com.denizenscript.denizen.scripts.commands.entity.FakeEquipCommand) NonNullList(net.minecraft.core.NonNullList) Pair(com.mojang.datafixers.util.Pair) NonNullList(net.minecraft.core.NonNullList) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) ItemStack(net.minecraft.world.item.ItemStack)

Example 89 with ItemStack

use of net.minecraft.world.item.ItemStack in project Denizen-For-Bukkit by DenizenScript.

the class FishingHelperImpl method getResult.

@Override
public org.bukkit.inventory.ItemStack getResult(FishHook fishHook, CatchType catchType) {
    ItemStack result = null;
    FishingHook nmsHook = ((CraftFishHook) fishHook).getHandle();
    if (catchType == CatchType.DEFAULT) {
        float f = ((CraftWorld) fishHook.getWorld()).getHandle().random.nextFloat();
        int i = EnchantmentHelper.getMobLooting(nmsHook.getPlayerOwner());
        int j = EnchantmentHelper.getEnchantmentLevel(Enchantments.FISHING_LUCK, nmsHook.getPlayerOwner());
        float f1 = 0.1F - (float) i * 0.025F - (float) j * 0.01F;
        float f2 = 0.05F + (float) i * 0.01F - (float) j * 0.01F;
        f1 = Mth.clamp(f1, 0.0F, 1.0F);
        f2 = Mth.clamp(f2, 0.0F, 1.0F);
        if (f < f1) {
            result = catchRandomJunk(nmsHook);
        } else {
            f -= f1;
            if (f < f2) {
                result = catchRandomTreasure(nmsHook);
            } else {
                result = catchRandomFish(nmsHook);
            }
        }
    } else if (catchType == CatchType.JUNK) {
        result = catchRandomJunk(nmsHook);
    } else if (catchType == CatchType.TREASURE) {
        result = catchRandomTreasure(nmsHook);
    } else if (catchType == CatchType.FISH) {
        result = catchRandomFish(nmsHook);
    }
    if (result != null) {
        return CraftItemStack.asBukkitCopy(result);
    } else {
        return null;
    }
}
Also used : CraftFishHook(org.bukkit.craftbukkit.v1_18_R1.entity.CraftFishHook) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) ItemStack(net.minecraft.world.item.ItemStack) FishingHook(net.minecraft.world.entity.projectile.FishingHook) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld)

Example 90 with ItemStack

use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.

the class ForgeInternalHandler method onEntityJoinWorld.

@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onEntityJoinWorld(EntityJoinWorldEvent event) {
    Entity entity = event.getEntity();
    if (entity.getClass().equals(ItemEntity.class)) {
        ItemStack stack = ((ItemEntity) entity).getItem();
        Item item = stack.getItem();
        if (item.hasCustomEntity(stack)) {
            Entity newEntity = item.createEntity(event.getWorld(), entity, stack);
            if (newEntity != null) {
                entity.discard();
                event.setCanceled(true);
                var executor = LogicalSidedProvider.WORKQUEUE.get(event.getWorld().isClientSide ? LogicalSide.CLIENT : LogicalSide.SERVER);
                executor.tell(new TickTask(0, () -> event.getWorld().addFreshEntity(newEntity)));
            }
        }
    }
}
Also used : Entity(net.minecraft.world.entity.Entity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) Item(net.minecraft.world.item.Item) ItemEntity(net.minecraft.world.entity.item.ItemEntity) ItemStack(net.minecraft.world.item.ItemStack) TickTask(net.minecraft.server.TickTask) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

ItemStack (net.minecraft.world.item.ItemStack)178 Level (net.minecraft.world.level.Level)62 ItemEntity (net.minecraft.world.entity.item.ItemEntity)61 InteractionHand (net.minecraft.world.InteractionHand)58 Player (net.minecraft.world.entity.player.Player)57 Items (net.minecraft.world.item.Items)55 InvocationTargetException (java.lang.reflect.InvocationTargetException)54 InteractionResult (net.minecraft.world.InteractionResult)54 EntitySize (de.Keyle.MyPet.api.entity.EntitySize)52 MyPet (de.Keyle.MyPet.api.entity.MyPet)52 EntityDataAccessor (net.minecraft.network.syncher.EntityDataAccessor)44 EntityDataSerializers (net.minecraft.network.syncher.EntityDataSerializers)44 SynchedEntityData (net.minecraft.network.syncher.SynchedEntityData)44 ServerLevel (net.minecraft.server.level.ServerLevel)44 MyPetApi (de.Keyle.MyPet.MyPetApi)40 Pair (com.mojang.datafixers.util.Pair)39 Bukkit (org.bukkit.Bukkit)39 CraftItemStack (org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)39 CraftItemStack (org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack)39 Util (de.Keyle.MyPet.api.Util)38