Java Code Examples for net.minecraft.item.ItemStack#setTag()

The following examples show how to use net.minecraft.item.ItemStack#setTag() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CmdSkull.java    From bleachhack-1.14 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCommand(String command, String[] args) throws Exception {
	ItemStack item = new ItemStack(Items.PLAYER_HEAD, 64);
	
	if (args.length < 2) {
		item.setTag(StringNbtReader.parse("{SkullOwner:{Name:\"" + args[0] + "\"}}"));
	} else if (args[0].equalsIgnoreCase("img")) {
		CompoundTag tag = StringNbtReader.parse("{SkullOwner:{Id:\"" + UUID.randomUUID() + "\",Properties:{textures:[{Value:\""
				+ Base64.getEncoder().encodeToString(("{\"textures\":{\"SKIN\":{\"url\":\"" + args[1] + "\"}}}").getBytes())
				+ "\"}]}}}");
		item.setTag(tag);
		System.out.println(tag);
	}
	
	mc.player.inventory.addPickBlock(item);
}
 
Example 2
Source File: ServerCrasherHack.java    From Wurst7 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onEnable()
{
	if(!MC.player.abilities.creativeMode)
	{
		ChatUtils.error("Creative mode only.");
		setEnabled(false);
		return;
	}
	
	Item item = Registry.ITEM.get(new Identifier("creeper_spawn_egg"));
	ItemStack stack = new ItemStack(item, 1);
	stack.setTag(createNBT());
	
	placeStackInHotbar(stack);
	setEnabled(false);
}
 
Example 3
Source File: ModifyCmd.java    From Wurst7 with GNU General Public License v3.0 6 votes vote down vote up
private void set(ItemStack stack, String[] args) throws CmdError
{
	String nbt = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
	nbt = nbt.replace("$", "\u00a7").replace("\u00a7\u00a7", "$");
	
	try
	{
		CompoundTag tag = StringNbtReader.parse(nbt);
		stack.setTag(tag);
		
	}catch(CommandSyntaxException e)
	{
		ChatUtils.message(e.getMessage());
		throw new CmdError("NBT data is invalid.");
	}
}
 
Example 4
Source File: CmdSkull.java    From bleachhack-1.14 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCommand(String command, String[] args) throws Exception {
	ItemStack item = new ItemStack(Items.PLAYER_HEAD, 64);
	
	if (args.length < 2) {
		item.setTag(StringNbtReader.parse("{SkullOwner:{Name:\"" + args[0] + "\"}}"));
	} else if (args[0].equalsIgnoreCase("img")) {
		CompoundTag tag = StringNbtReader.parse("{SkullOwner:{Id:\"" + UUID.randomUUID() + "\",Properties:{textures:[{Value:\""
				+ Base64.getEncoder().encodeToString(("{\"textures\":{\"SKIN\":{\"url\":\"" + args[1] + "\"}}}").getBytes())
				+ "\"}]}}}");
		item.setTag(tag);
		System.out.println(tag);
	}
	
	mc.player.inventory.addPickBlock(item);
}
 
Example 5
Source File: PotionCmd.java    From Wurst7 with GNU General Public License v3.0 5 votes vote down vote up
private void remove(ItemStack stack, String[] args) throws CmdSyntaxError
{
	if(args.length != 2)
		throw new CmdSyntaxError();
	
	int id = parseEffectId(args[1]);
	
	List<StatusEffectInstance> oldEffects =
		PotionUtil.getCustomPotionEffects(stack);
	
	ListTag newEffects = new ListTag();
	for(StatusEffectInstance oldEffect : oldEffects)
	{
		int oldId = StatusEffect.getRawId(oldEffect.getEffectType());
		
		if(oldId == id)
			continue;
		
		CompoundTag effect = new CompoundTag();
		effect.putInt("Id", oldId);
		effect.putInt("Amplifier", oldEffect.getAmplifier());
		effect.putInt("Duration", oldEffect.getDuration());
		newEffects.add(effect);
	}
	
	CompoundTag nbt = new CompoundTag();
	nbt.put("CustomPotionEffects", newEffects);
	stack.setTag(nbt);
	ChatUtils.message("Effect removed.");
}
 
Example 6
Source File: CmdNBT.java    From bleachhack-1.14 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCommand(String command, String[] args) throws Exception {
    if (args[0].isEmpty()) {
        BleachLogger.errorMessage("Invalid Syntax!");
        BleachLogger.infoMessage(CommandManager.prefix + getSyntax());
        return;
    }
    ItemStack item = mc.player.inventory.getMainHandStack();

    if (args[0].equalsIgnoreCase("get")) {
    	BleachLogger.infoMessage("\u00a76\u00a7lNBT:\n" + item.getTag() + "");
    } else if (args[0].equalsIgnoreCase("copy")) {
        mc.keyboard.setClipboard(item.getTag() + "");
        BleachLogger.infoMessage("\u00a76Copied\n\u00a7f" + (item.getTag() + "\n") + "\u00a76to clipboard.");
    } else if (args[0].equalsIgnoreCase("set")) {
        try {
            if (args[1].isEmpty()) {
                BleachLogger.errorMessage("Invalid Syntax!");
                BleachLogger.infoMessage(CommandManager.prefix + getSyntax());
                return;
            }
            item.setTag(StringNbtReader.parse(args[1]));
            BleachLogger.infoMessage("\u00a76Set NBT of " + item.getItem().getName() + "to\n\u00a7f" + (item.getTag()));
        } catch (Exception e) {
            BleachLogger.errorMessage("Invalid Syntax!");
            BleachLogger.infoMessage(getSyntax());
        }
    } else if (args[0].equalsIgnoreCase("wipe")) {
        item.setTag(new CompoundTag());
    }

}
 
Example 7
Source File: CmdEnchant.java    From bleachhack-1.14 with GNU General Public License v3.0 5 votes vote down vote up
public void enchant(ItemStack item, Enchantment e, int level) {
	if (item.getTag() == null) item.setTag(new CompoundTag());
	if (!item.getTag().contains("Enchantments", 9)) {
		item.getTag().put("Enchantments", new ListTag());
    }

    ListTag listnbt = item.getTag().getList("Enchantments", 10);
    CompoundTag compoundnbt = new CompoundTag();
    compoundnbt.putString("id", String.valueOf(Registry.ENCHANTMENT.getId(e)));
    compoundnbt.putInt("lvl", level);
    listnbt.add(compoundnbt);
}
 
Example 8
Source File: CmdEnchant.java    From bleachhack-1.14 with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public void enchant(ItemStack item, Enchantment e, int level) {
	if (item.getTag() == null) item.setTag(new CompoundNBT());
	if (!item.getTag().contains("Enchantments", 9)) {
		item.getTag().put("Enchantments", new ListNBT());
    }

    ListNBT listnbt = item.getTag().getList("Enchantments", 10);
    CompoundNBT compoundnbt = new CompoundNBT();
    compoundnbt.putString("id", String.valueOf(Registry.ENCHANTMENT.getKey(e)));
    compoundnbt.putInt("lvl", level);
    listnbt.add(compoundnbt);
}
 
Example 9
Source File: CmdNBT.java    From bleachhack-1.14 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCommand(String command, String[] args) throws Exception {
    if (args[0].isEmpty()) {
        BleachLogger.errorMessage("Invalid Syntax!");
        BleachLogger.infoMessage(CommandManager.prefix + getSyntax());
        return;
    }
    ItemStack item = mc.player.inventory.getMainHandStack();

    if (args[0].equalsIgnoreCase("get")) {
    	BleachLogger.infoMessage("§6§lNBT:\n" + item.getTag() + "");
    } else if (args[0].equalsIgnoreCase("copy")) {
        mc.keyboard.setClipboard(item.getTag() + "");
        BleachLogger.infoMessage("§6Copied\n§f" + (item.getTag() + "\n") + "§6to clipboard.");
    } else if (args[0].equalsIgnoreCase("set")) {
        try {
            if (args[1].isEmpty()) {
                BleachLogger.errorMessage("Invalid Syntax!");
                BleachLogger.infoMessage(CommandManager.prefix + getSyntax());
                return;
            }
            item.setTag(StringNbtReader.parse(args[1]));
            BleachLogger.infoMessage("§6Set NBT of " + item.getItem().getName() + "to\n§f" + (item.getTag()));
        } catch (Exception e) {
            BleachLogger.errorMessage("Invalid Syntax!");
            BleachLogger.infoMessage(getSyntax());
        }
    } else if (args[0].equalsIgnoreCase("wipe")) {
        item.setTag(new CompoundTag());
    }

}
 
Example 10
Source File: CmdEnchant.java    From bleachhack-1.14 with GNU General Public License v3.0 5 votes vote down vote up
public void enchant(ItemStack item, Enchantment e, int level) {
	if (item.getTag() == null) item.setTag(new CompoundTag());
	if (!item.getTag().containsKey("Enchantments", 9)) {
		item.getTag().put("Enchantments", new ListTag());
    }

    ListTag listnbt = item.getTag().getList("Enchantments", 10);
    CompoundTag compoundnbt = new CompoundTag();
    compoundnbt.putString("id", String.valueOf(Registry.ENCHANTMENT.getId(e)));
    compoundnbt.putInt("lvl", level);
    listnbt.add(compoundnbt);
}
 
Example 11
Source File: CmdSkull.java    From bleachhack-1.14 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCommand(String command, String[] args) throws Exception {
	ItemStack item = new ItemStack(Items.PLAYER_HEAD, 64);
	
	if (args.length < 2) {
		item.setTag(StringNbtReader.parse("{SkullOwner:{Name:\"" + args[0] + "\"}}"));
	} else if (args[0].equalsIgnoreCase("img")) {
		item.setTag(StringNbtReader.parse("{SkullOwner:{Id:\"" + UUID.randomUUID() + "\",Properties:{textures:[{Value:\""
				+ Base64.getEncoder().encodeToString(("{\"textures\":{\"SKIN\":{\"url\":\"" + args[1] + "\"}}}").getBytes())
				+ "\"}]}}}"));
	}
	
	mc.player.inventory.addPickBlock(item);
}
 
Example 12
Source File: CmdEnchant.java    From bleachhack-1.14 with GNU General Public License v3.0 5 votes vote down vote up
public void enchant(ItemStack item, Enchantment e, int level) {
	if (item.getTag() == null) item.setTag(new CompoundTag());
	if (!item.getTag().contains("Enchantments", 9)) {
		item.getTag().put("Enchantments", new ListTag());
    }

    ListTag listnbt = item.getTag().getList("Enchantments", 10);
    CompoundTag compoundnbt = new CompoundTag();
    compoundnbt.putString("id", String.valueOf(Registry.ENCHANTMENT.getId(e)));
    compoundnbt.putInt("lvl", level);
    listnbt.add(compoundnbt);
}
 
Example 13
Source File: CmdNBT.java    From bleachhack-1.14 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCommand(String command, String[] args) throws Exception {
    if (args[0].isEmpty()) {
        BleachLogger.errorMessage("Invalid Syntax!");
        BleachLogger.infoMessage(CommandManager.prefix + getSyntax());
        return;
    }
    ItemStack item = mc.player.inventory.getMainHandStack();

    if (args[0].equalsIgnoreCase("get")) {
    	BleachLogger.infoMessage("§6§lNBT:\n" + item.getTag() + "");
    } else if (args[0].equalsIgnoreCase("copy")) {
        mc.keyboard.setClipboard(item.getTag() + "");
        BleachLogger.infoMessage("§6Copied\n§f" + (item.getTag() + "\n") + "§6to clipboard.");
    } else if (args[0].equalsIgnoreCase("set")) {
        try {
            if (args[1].isEmpty()) {
                BleachLogger.errorMessage("Invalid Syntax!");
                BleachLogger.infoMessage(CommandManager.prefix + getSyntax());
                return;
            }
            item.setTag(StringNbtReader.parse(args[1]));
            BleachLogger.infoMessage("§6Set NBT of " + item.getItem().getName() + "to\n§f" + (item.getTag()));
        } catch (Exception e) {
            BleachLogger.errorMessage("Invalid Syntax!");
            BleachLogger.infoMessage(getSyntax());
        }
    } else if (args[0].equalsIgnoreCase("wipe")) {
        item.setTag(new CompoundTag());
    }

}
 
Example 14
Source File: CrashChestHack.java    From Wurst7 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onEnable()
{
	if(!MC.player.abilities.creativeMode)
	{
		ChatUtils.error("Creative mode only.");
		setEnabled(false);
		return;
	}
	
	if(!MC.player.inventory.getArmorStack(0).isEmpty())
	{
		ChatUtils.error("Please clear your shoes slot.");
		setEnabled(false);
		return;
	}
	
	// generate item
	ItemStack stack = new ItemStack(Blocks.CHEST);
	CompoundTag nbtCompound = new CompoundTag();
	ListTag nbtList = new ListTag();
	for(int i = 0; i < 40000; i++)
		nbtList.add(new ListTag());
	nbtCompound.put("www.wurstclient.net", nbtList);
	stack.setTag(nbtCompound);
	stack.setCustomName(new LiteralText("Copy Me"));
	
	// give item
	MC.player.inventory.armor.set(0, stack);
	ChatUtils.message("Item has been placed in your shoes slot.");
	setEnabled(false);
}
 
Example 15
Source File: KillPotionHack.java    From Wurst7 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onEnable()
{
	// check gamemode
	if(!MC.player.abilities.creativeMode)
	{
		ChatUtils.error("Creative mode only.");
		setEnabled(false);
		return;
	}
	
	// generate potion
	ItemStack stack = new ItemStack(Items.SPLASH_POTION);
	CompoundTag effect = new CompoundTag();
	effect.putInt("Amplifier", 125);
	effect.putInt("Duration", 2000);
	effect.putInt("Id", 6);
	ListTag effects = new ListTag();
	effects.add(effect);
	CompoundTag nbt = new CompoundTag();
	nbt.put("CustomPotionEffects", effects);
	stack.setTag(nbt);
	String name = "\u00a7rSplash Potion of \u00a74\u00a7lINSTANT DEATH";
	stack.setCustomName(new LiteralText(name));
	
	// give potion
	if(placeStackInHotbar(stack))
		ChatUtils.message("Potion created.");
	else
		ChatUtils.error("Please clear a slot in your hotbar.");
	
	setEnabled(false);
}
 
Example 16
Source File: TrollPotionHack.java    From Wurst7 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onEnable()
{
	// check gamemode
	if(!MC.player.abilities.creativeMode)
	{
		ChatUtils.error("Creative mode only.");
		setEnabled(false);
		return;
	}
	
	// generate potion
	ItemStack stack = new ItemStack(Items.SPLASH_POTION);
	ListTag effects = new ListTag();
	for(int i = 1; i <= 23; i++)
	{
		CompoundTag effect = new CompoundTag();
		effect.putInt("Amplifier", Integer.MAX_VALUE);
		effect.putInt("Duration", Integer.MAX_VALUE);
		effect.putInt("Id", i);
		effects.add(effect);
	}
	CompoundTag nbt = new CompoundTag();
	nbt.put("CustomPotionEffects", effects);
	stack.setTag(nbt);
	String name = "\u00a7rSplash Potion of Trolling";
	stack.setCustomName(new LiteralText(name));
	
	// give potion
	if(placeStackInHotbar(stack))
		ChatUtils.message("Potion created.");
	else
		ChatUtils.error("Please clear a slot in your hotbar.");
	
	setEnabled(false);
}
 
Example 17
Source File: InventoryHelper.java    From fabric-carpet with MIT License 5 votes vote down vote up
public static boolean cleanUpShulkerBoxTag(ItemStack stack)
{
    boolean changed = false;
    CompoundTag tag = stack.getTag();

    if (tag == null || !tag.contains("BlockEntityTag", TAG_COMPOUND))
        return false;

    CompoundTag bet = tag.getCompound("BlockEntityTag");
    if (bet.contains("Items", TAG_LIST) && bet.getList("Items", TAG_COMPOUND).isEmpty())
    {
        bet.remove("Items");
        changed = true;
    }

    if (bet.isEmpty())
    {
        tag.remove("BlockEntityTag");
        changed = true;
    }
    if (tag.isEmpty())
    {
        stack.setTag(null);
        changed = true;
    }
    return changed;
}
 
Example 18
Source File: BatteryItem.java    From Galacticraft-Rewoven with MIT License 4 votes vote down vote up
@Override
public void onCraft(ItemStack battery, World world_1, PlayerEntity playerEntity_1) {
    CompoundTag batteryTag = battery.getOrCreateTag();
    battery.setDamage(BatteryItem.MAX_ENERGY);
    battery.setTag(batteryTag);
}
 
Example 19
Source File: GiveCmd.java    From Wurst7 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void call(String[] args) throws CmdException
{
	// validate input
	if(args.length < 1)
		throw new CmdSyntaxError();
	
	if(!MC.player.abilities.creativeMode)
		throw new CmdError("Creative mode only.");
	
	// id/name
	Item item = getItem(args[0]);
	
	if(item == Items.AIR && MathUtils.isInteger(args[0]))
		item = Item.byRawId(Integer.parseInt(args[0]));
	
	if(item == Items.AIR)
		throw new CmdError("Item \"" + args[0] + "\" could not be found.");
	
	// amount
	int amount = 1;
	if(args.length >= 2)
	{
		if(!MathUtils.isInteger(args[1]))
			throw new CmdSyntaxError("Not a number: " + args[1]);
		
		amount = Integer.valueOf(args[1]);
		
		if(amount < 1)
			throw new CmdError("Amount cannot be less than 1.");
		
		if(amount > 64)
			throw new CmdError("Amount cannot be more than 64.");
	}
	
	// nbt data
	String nbt = null;
	if(args.length >= 3)
		nbt = String.join(" ", Arrays.copyOfRange(args, 2, args.length));
	
	// generate item
	ItemStack stack = new ItemStack(item, amount);
	if(nbt != null)
		try
		{
			CompoundTag tag = StringNbtReader.parse(nbt);
			stack.setTag(tag);
			
		}catch(CommandSyntaxException e)
		{
			ChatUtils.message(e.getMessage());
			throw new CmdSyntaxError("NBT data is invalid.");
		}
	
	// give item
	if(placeStackInHotbar(stack))
		ChatUtils.message("Item" + (amount > 1 ? "s" : "") + " created.");
	else
		throw new CmdError("Please clear a slot in your hotbar.");
}
 
Example 20
Source File: IForgeItem.java    From patchwork-api with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Override this method to decide what to do with the NBT data received from
 * getShareTag().
 *
 * @param stack The stack that received NBT
 * @param nbt   Received NBT, can be null
 */
default void readShareTag(ItemStack stack, @Nullable CompoundTag nbt) {
	stack.setTag(nbt);
}