Search in sources :

Example 11 with ItemStack

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

the class CraftingHelper method getItemStack.

public static ItemStack getItemStack(JsonObject json, boolean readNBT, boolean disallowsAirInRecipe) {
    String itemName = GsonHelper.getAsString(json, "item");
    ResourceLocation itemKey = new ResourceLocation(itemName);
    if (!ForgeRegistries.ITEMS.containsKey(itemKey))
        throw new JsonSyntaxException("Unknown item '" + itemName + "'");
    Item item = ForgeRegistries.ITEMS.getValue(itemKey);
    if (disallowsAirInRecipe && item == Items.AIR)
        throw new JsonSyntaxException("Invalid item: " + itemName);
    if (readNBT && json.has("nbt")) {
        // Lets hope this works? Needs test
        try {
            JsonElement element = json.get("nbt");
            CompoundTag nbt;
            if (element.isJsonObject())
                nbt = TagParser.parseTag(GSON.toJson(element));
            else
                nbt = TagParser.parseTag(GsonHelper.convertToString(element, "nbt"));
            CompoundTag tmp = new CompoundTag();
            if (nbt.contains("ForgeCaps")) {
                tmp.put("ForgeCaps", nbt.get("ForgeCaps"));
                nbt.remove("ForgeCaps");
            }
            tmp.put("tag", nbt);
            tmp.putString("id", itemName);
            tmp.putInt("Count", GsonHelper.getAsInt(json, "count", 1));
            return ItemStack.of(tmp);
        } catch (CommandSyntaxException e) {
            throw new JsonSyntaxException("Invalid NBT Entry: " + e.toString());
        }
    }
    return new ItemStack(item, GsonHelper.getAsInt(json, "count", 1));
}
Also used : Item(net.minecraft.world.item.Item) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.resources.ResourceLocation) ItemStack(net.minecraft.world.item.ItemStack) CompoundTag(net.minecraft.nbt.CompoundTag) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Example 12 with ItemStack

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

the class VanillaIngredientSerializer method write.

public void write(FriendlyByteBuf buffer, Ingredient ingredient) {
    ItemStack[] items = ingredient.getItems();
    buffer.writeVarInt(items.length);
    for (ItemStack stack : items) buffer.writeItem(stack);
}
Also used : ItemStack(net.minecraft.world.item.ItemStack)

Example 13 with ItemStack

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

the class NewFluidTest method loadComplete.

public void loadComplete(FMLLoadCompleteEvent event) {
    // some sanity checks
    BlockState state = Fluids.WATER.defaultFluidState().createLegacyBlock();
    BlockState state2 = Fluids.WATER.getAttributes().getBlock(null, null, Fluids.WATER.defaultFluidState());
    Validate.isTrue(state.getBlock() == Blocks.WATER && state2 == state);
    ItemStack stack = Fluids.WATER.getAttributes().getBucket(new FluidStack(Fluids.WATER, 1));
    Validate.isTrue(stack.getItem() == Fluids.WATER.getBucket());
    event.enqueueWork(() -> DispenserBlock.registerBehavior(test_fluid_bucket.get(), DispenseFluidContainer.getInstance()));
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) FluidStack(net.minecraftforge.fluids.FluidStack) ItemStack(net.minecraft.world.item.ItemStack)

Example 14 with ItemStack

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

the class FakePlayerTest method registerCommands.

@SubscribeEvent
public void registerCommands(RegisterCommandsEvent event) {
    event.getDispatcher().register(Commands.literal("fakeplayer").then(Commands.literal("attack").then(Commands.argument("target", EntityArgument.entity()).executes(context -> {
        FakePlayerFactory.getMinecraft(context.getSource().getLevel()).attack(EntityArgument.getEntity(context, "target"));
        return 1;
    }))).then(Commands.literal("opencontainer").executes(context -> {
        ServerPlayer fakePlayer = FakePlayerFactory.getMinecraft(context.getSource().getLevel());
        InteractionHand hand = InteractionHand.MAIN_HAND;
        ItemStack stack = Items.WRITABLE_BOOK.getDefaultInstance();
        fakePlayer.setItemInHand(hand, stack);
        fakePlayer.openItemGui(stack, hand);
        return 1;
    })));
}
Also used : ServerPlayer(net.minecraft.server.level.ServerPlayer) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 15 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_17_R1.entity.CraftFishHook) ItemStack(net.minecraft.world.item.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) FishingHook(net.minecraft.world.entity.projectile.FishingHook) CraftWorld(org.bukkit.craftbukkit.v1_17_R1.CraftWorld)

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