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));
}
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);
}
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()));
}
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;
})));
}
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;
}
}
Aggregations