Java Code Examples for net.minecraft.item.Item#getByNameOrId()

The following examples show how to use net.minecraft.item.Item#getByNameOrId() . 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: CustomItemInfoJson.java    From ExNihiloAdscensio with MIT License 6 votes vote down vote up
@Override
public ItemInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
    JsonHelper helper = new JsonHelper(json);
    
    String name = helper.getString("name");
    int meta = helper.getNullableInteger("meta", 0);

    Item item = Item.getByNameOrId(name);
    
    if(item == null)
    {
        LogUtil.error("Error parsing JSON: Invalid Item: " + json.toString());
        LogUtil.error("This may result in crashing or other undefined behavior");
    }
    
    return new ItemInfo(item, meta);
}
 
Example 2
Source File: CustomItemStackJson.java    From ExNihiloAdscensio with MIT License 6 votes vote down vote up
@Override
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
    JsonHelper helper = new JsonHelper(json);
    
    String name = helper.getString("name");
    int amount = helper.getNullableInteger("amount", 1);
    int meta = helper.getNullableInteger("meta", 0);
    
    Item item = Item.getByNameOrId(name);
    
    if(item == null)
    {
        LogUtil.error("Error parsing JSON: Invalid Item: " + json.toString());
        LogUtil.error("This may result in crashing or other undefined behavior");
    }
    
    return new ItemStack(item, amount, meta);
}
 
Example 3
Source File: ScreenRenderOptions.java    From WearableBackpacks with MIT License 6 votes vote down vote up
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
	long currentTime = Minecraft.getSystemTime();
	if (currentTime > _lastUpdateTime + ScreenEntityEntry.UPDATE_TIMESPAN) {
		_lastUpdateTime = currentTime;
		_index = (_index + 1) % _backpacks.size();
		BackpackEntry entry = _backpacks.get(_index);
		Item item = Item.getByNameOrId(entry.backpack);
		_stack = (item != null) ? new ItemStack(item) : ItemStack.EMPTY;
		if (entry.colorRange != null)
			NbtUtils.set(_stack, entry.colorRange.getRandom(), "display", "color");
	}
	
	buttonDone.setEnabled(listEntries.getEntries().allMatch(IConfigEntry::isValid));
	buttonUndo.setEnabled(listEntries.getEntries().anyMatch(IConfigEntry::isChanged));
	
	_backpack.setStack(_stack);
	RendererBackpack.Layer.setOverride(_backpack, getValue());
	super.drawScreen(mouseX, mouseY, partialTicks);
	RendererBackpack.Layer.resetOverride();
}
 
Example 4
Source File: ShredderRecipes.java    From EmergingTechnology with MIT License 5 votes vote down vote up
private static List<ItemStack> getShredderPlasticItems() {
    List<ItemStack> itemInputs = new ArrayList<ItemStack>();

    itemInputs.add(new ItemStack(ModItems.frame));
    itemInputs.add(new ItemStack(ModItems.plasticsheet));
    itemInputs.add(new ItemStack(ModItems.plasticblock));
    itemInputs.add(new ItemStack(ModItems.clearplasticblock));
    itemInputs.add(new ItemStack(ModItems.plasticrod));
    itemInputs.add(new ItemStack(ModItems.light));
    itemInputs.add(new ItemStack(ModItems.hydroponic));
    itemInputs.add(new ItemStack(ModItems.machinecase));
    itemInputs.add(new ItemStack(ModItems.shredder));
    itemInputs.add(new ItemStack(ModItems.processor));
    itemInputs.add(new ItemStack(ModItems.redbulb));
    itemInputs.add(new ItemStack(ModItems.greenbulb));
    itemInputs.add(new ItemStack(ModItems.bluebulb));
    itemInputs.add(new ItemStack(ModItems.purplebulb));
    itemInputs.add(new ItemStack(ModItems.plasticwaste));

    Item p = Item.getByNameOrId("rats:plastic_waste");

    if (p != null) {
        itemInputs.add(new ItemStack(p));
    }

    List<String> oreInputs = new ArrayList<String>();

    oreInputs.add("sheetPlastic");
    oreInputs.add("rodPlastic");
    oreInputs.add("stickPlastic");
    oreInputs.add("platePlastic");
    oreInputs.add("blockPlastic");
    oreInputs.add("itemPlastic");
    oreInputs.add("bioplastic");

    List<ItemStack> inputs = RecipeBuilder.buildRecipeList(itemInputs, oreInputs);

    return inputs;
}
 
Example 5
Source File: ModTissueProvider.java    From EmergingTechnology with MIT License 5 votes vote down vote up
public static ItemStack getResultItemStackByEntityId(String entityId) {
    for(ModTissue modTissue: allTissues) {
        if (entityId.equalsIgnoreCase(modTissue.entityId) && modTissue.result != null) {
            Item item = Item.getByNameOrId(modTissue.result);

            if (item == null) {
                return ItemStack.EMPTY;
            }

            return new ItemStack(item);
        }
    }
    return ItemStack.EMPTY;
}
 
Example 6
Source File: BaseMetals.java    From BaseMetals with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Parses a String in the format (stack-size)*(modid):(item/block name)#(metadata value). The 
 * stacksize and metadata value parameters are optional.
 * @param str A String describing an itemstack (e.g. "4*minecraft:dye#15" or "minecraft:bow")
 * @param allowWildcard If true, then item strings that do not specify a metadata value will use 
 * the OreDictionary wildcard value. If false, then the default meta value is 0 instead.
 * @return An ItemStack representing the item, or null if the item is not found
 */
public static ItemStack parseStringAsItemStack(String str, boolean allowWildcard){
	str = str.trim();
	int count = 1;
	int meta;
	if(allowWildcard){
		meta = OreDictionary.WILDCARD_VALUE;
	} else {
		meta = 0;
	}
	int nameStart = 0;
	int nameEnd = str.length();
	if(str.contains("*")){
		count = Integer.parseInt(str.substring(0,str.indexOf("*")).trim());
		nameStart = str.indexOf("*")+1;
	}
	if(str.contains("#")){
		meta = Integer.parseInt(str.substring(str.indexOf("#")+1,str.length()).trim());
		nameEnd = str.indexOf("#");
	}
	String id = str.substring(nameStart,nameEnd).trim();
	if(Block.getBlockFromName(id) != null){
		// is a block
		return new ItemStack(Block.getBlockFromName(id),count,meta);
	} else if(Item.getByNameOrId(id) != null){
		// is an item
		return new ItemStack(Item.getByNameOrId(id),count,meta);
	} else {
		// item not found
		FMLLog.severe("Failed to find item or block for ID '"+id+"'");
		return null;
	}
}
 
Example 7
Source File: ModValidator.java    From AgriCraft with MIT License 5 votes vote down vote up
@Override
public boolean isValidItem(String item) {
    String[] parts = item.split(":");
    if (parts.length < 2) {
        return false;
    } else if (OreDictUtil.isValidOre(item)) {
        return true;
    } else {
        Item i = Item.getByNameOrId(parts[0] + ":" + parts[1]);
        //AgriCore.getLogger("agricraft").debug(i);
        return i != null;
    }
}
 
Example 8
Source File: ScreenEntityEntry.java    From WearableBackpacks with MIT License 5 votes vote down vote up
private void updateBackpackItem() {
	Item item = Item.getByNameOrId(fieldBackpack.getText());
	_backpack = (item != null) ? new ItemStack(item) : ItemStack.EMPTY;
	if (switchColorOn.isSwitchOn()) {
		boolean isValid = (fieldColorMin.getText().length() == 6) &&
		                  (fieldColorMax.getText().length() == 6);
		int minColor = isValid ? Integer.parseInt(fieldColorMin.getText(), 16) : 0xFF0000;
		int maxColor = isValid ? Integer.parseInt(fieldColorMax.getText(), 16) : 0xFF0000;
		ColorRange range = new ColorRange(minColor, maxColor);
		int color        = range.isValid() ? range.getRandom() : 0xFF0000;
		NbtUtils.set(_backpack, color, "display", "color");
	}
	itemBackpack.setStack(_backpack);
}
 
Example 9
Source File: XrayModChooserGui.java    From MinecraftX-RAY with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void drawScreen(int par1, int par2, float par3) {
	drawDefaultBackground();
	int widthPlus100 = this.width / 2 + 100;
	int widthMinus100 = this.width / 2 - 100;
	int heightCalc = this.height / 3;
	int renderPosition = this.listPos - 4;
	int currentPos1Calc = heightCalc + 12 + (24 * 2);
	int currentPos2Calc = currentPos1Calc - 24;
	drawRect(widthPlus100 + 2, currentPos1Calc, widthMinus100 - 2, currentPos2Calc, -0x3F3F40);
	for (int i = 0; i < 40; ++i) {
		if (renderPosition >= 0 && renderPosition < this.idList.size()) {
			ItemStack currentIcon = new ItemStack((Item.getByNameOrId(this.idList.get(renderPosition))));
			if (currentIcon.getItem() != null) {
				this.itemRender.renderItemAndEffectIntoGUI(currentIcon, this.width / 2 - 97, heightCalc - 60 + 4 + i * 24);
			}
			if (this.invisibleIdList.indexOf((this.idList.get(renderPosition))) >= 0) {
				drawRect(widthPlus100, heightCalc + 10 - 48 + i * 24, widthMinus100, heightCalc - 10 - 48 + i * 24, -0x010000);
			}
			String currentBlockName = Block.getBlockFromName(this.idList.get(renderPosition)).getLocalizedName();
			if (currentBlockName == null) {
				currentBlockName = "No Name";
			}
			drawCenteredString(this.fontRenderer, currentBlockName, this.width / 2, heightCalc - 60 + 7 + i * 24, 0xFFFFFF);
		}
		++renderPosition;
	}
	super.drawScreen(par1, par2, par3);
}
 
Example 10
Source File: WrapperItem.java    From ClientBase with MIT License 4 votes vote down vote up
public static WrapperItem getByNameOrId(String var0) {
    return new WrapperItem(Item.getByNameOrId(var0));
}
 
Example 11
Source File: PlaceCommandsImplementation.java    From malmo with MIT License 4 votes vote down vote up
@Override
protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
    EntityPlayerSP player = Minecraft.getMinecraft().player;
    if (player == null)
        return false;

    if (!verb.equalsIgnoreCase("place"))
        return false;

    Item item = Item.getByNameOrId(parameter);
    Block block = Block.getBlockFromItem(item);
    if (item == null || item.getRegistryName() == null || block.getRegistryName() == null)
        return false;

    InventoryPlayer inv = player.inventory;
    boolean blockInInventory = false;
    ItemStack stackInInventory = null;
    int stackIndex = -1;
    for (int i = 0; !blockInInventory && i < inv.getSizeInventory(); i++) {
        Item stack = inv.getStackInSlot(i).getItem();
        if (stack.getRegistryName() != null && stack.getRegistryName().equals(item.getRegistryName())) {
            stackInInventory = inv.getStackInSlot(i);
            stackIndex = i;
            blockInInventory = true;
        }
    }

    // We don't have that item in our inventories
    if (!blockInInventory)
        return false;

    RayTraceResult mop = Minecraft.getMinecraft().objectMouseOver;
    if (mop.typeOfHit == RayTraceResult.Type.BLOCK) {
        BlockPos pos = mop.getBlockPos().add(mop.sideHit.getDirectionVec());
        // Can we place this block here?
        AxisAlignedBB axisalignedbb = block.getDefaultState().getCollisionBoundingBox(player.world, pos);
        if (axisalignedbb == null || player.world.checkNoEntityCollision(axisalignedbb.offset(pos), null)) {
            MalmoMod.network.sendToServer(new DiscreteMovementCommandsImplementation.UseActionMessage(mop.getBlockPos(), new ItemStack(block), mop.sideHit, false, mop.hitVec));
            if (stackInInventory.getCount() == 1)
                inv.setInventorySlotContents(stackIndex, new ItemStack(Block.getBlockById(0)));
            else
                stackInInventory.setCount(stackInInventory.getCount() - 1);
        }
    }

    return true;
}
 
Example 12
Source File: BackpackRegistry.java    From WearableBackpacks with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends Item & IBackpackType> T getBackpackItem() {
	Item item = Item.getByNameOrId(backpack);
	return (item instanceof IBackpackType) ? (T)item : null;
}
 
Example 13
Source File: GTMaterialGen.java    From GT-Classic with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * A getter for retrieving modded items.
 * 
 * @param modname - String, the mod name
 * @param itemid  - String, the item by name
 * @return ItemStack - the ItemStack requested
 */
public static ItemStack getModItem(String modname, String itemid) {
	String pair = modname + ":" + itemid;
	return new ItemStack(Item.getByNameOrId(pair));
}
 
Example 14
Source File: GTMaterialGen.java    From GT-Classic with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * A getter for retrieving modded items.
 * 
 * @param modname - String, the mod name
 * @param itemid  - String, the item by name
 * @param amount  - int, the count
 * @return ItemStack - the ItemStack requested
 */
public static ItemStack getModItem(String modname, String itemid, int amount) {
	String pair = modname + ":" + itemid;
	return new ItemStack(Item.getByNameOrId(pair), amount);
}