Jump to content

[1.7.10] Blacklisting Items From Appearing


SilentThief

Recommended Posts

Hello MCF community! I have a little issue with 'black-listing items', from appearing when I right click this item. This item gives the player a random item, block, etc, out of the pool of items that minecraft provides. I have tried to make it so certain items don't appear in the pool. I have tried creating array lists of all of the items in Minecraft, also tried simply creating itemstacks and then doing a switch statement saying if they appear, the action will loop, no luck there. So, I'd like a little bit of help if possible.

 

package com.rien.nn.items;

import java.util.List;
import java.util.Random;

import com.rien.nn.Main;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class ItemLootBag extends Item 
{	
public ItemLootBag() 
{
	this.setUnlocalizedName("itemLootBag");
	this.setTextureName("niknaks:itemLootBag");
	this.setCreativeTab(Main.NNTab);
	this.setMaxStackSize(16);
}

public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) 
{	
	//Banned Items
	ItemStack netherPortalBlock = new ItemStack(Blocks.portal);
	ItemStack endPortalBlock = new ItemStack(Blocks.end_portal);
	ItemStack endPortalFrame = new ItemStack(Blocks.end_portal_frame);
	ItemStack fireBlock = new ItemStack(Blocks.fire);
	ItemStack waterBlock = new ItemStack(Blocks.water);

	int min = 1;
	int max = 3;
	Random r = new Random();
	int randNum = r.nextInt(max-min) + min;

	if (player.capabilities.isCreativeMode) 
	{
		player.dropPlayerItemWithRandomChoice(new ItemStack(getRandomItem(), randNum), false);
		return item;
	} 
	else 
	{
		--item.stackSize;
		player.dropPlayerItemWithRandomChoice(new ItemStack(getRandomItem(), randNum), false);
		return item;
	}
}

static Random rand = new Random();

public static Item getRandomItem() 
{
	Item i = null;

	int length = Item.itemRegistry.getKeys().toArray().length;
	Object select = Item.itemRegistry.getObjectById(ItemLootBag.rand.nextInt(length));

	if(select != null && select instanceof Item) 
	{
		i = (Item) select;
	}
	else 
	{
		return getRandomItem();
	}
	return i;
}
}

Link to comment
Share on other sites

No offense but I am not sure how the code you posted would possibly work to prevent items.

 

Where do you check if the item is a banned item?  You would at least need to create a list or array of the banned items and test the random item against it.  Your code just gets a random item and never checks if it is banned.

 

I don't think you really need to create instances of each banned item either, that is what the instanceof operator is for (comparing to see if something is an instance).

 

Also, when you check if select instanceof Item you don't also check if it is null because instanceof won't match if it is null already. 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

I have two ideas on how to do this. My interests would lead me to use recursion. The other option is to use a do-

 

... Wait. You will need two for loops and recursion.

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

No offense but I am not sure how the code you posted would possibly work to prevent items.

 

Where do you check if the item is a banned item?  You would at least need to create a list or array of the banned items and test the random item against it.  Your code just gets a random item and never checks if it is banned.

 

I don't think you really need to create instances of each banned item either, that is what the instanceof operator is for (comparing to see if something is an instance).

 

Also, when you check if select instanceof Item you don't also check if it is null because instanceof won't match if it is null already.

No no no, that isn't the code. Those item stacks listed are just the items that are going to be banned. :P

There is no code for the blacklist yet.

Link to comment
Share on other sites

I have two ideas on how to do this. My interests would lead me to use recursion. The other option is to use a do-

 

... Wait. You will need two for loops and recursion.

Recursion is a good idea, I will try and figure out a way to incorporate that without creating a loop that will throw stack traces. xD

 

But, that's for making the method run once an argument is true or false, or equal to something, I don't know what to do to check if the items I want banned, will be not drawn from the pool of items. Because I can't use -

if (getRandomItem == (new ItemStack(Blocks.portal)
{
getRandomItem();
}

 

or

 

if (ItemStack == (new ItemStack(Blocks.portal)
{
getRandomItem();
}

Link to comment
Share on other sites

Okay, if the list of banned items is short, like it seems to be, I don't think you need an array or list of banned items but can just check them directly.

 

The point of the "recursion" is simply the idea that if you get a random item that is banned you'll want to choose again (and it may again find a banned item).

 

The Java while statement is meant for these sort of situations -- it keeps looping until it finds a condition that is true (in this case until you get a non-banned item.

 

So I suggest something like this (probably needs some debug as I didn't run this, but it should be right idea):

1. create a local boolean variable called something like foundItem and initialize to false.

2. create a local ItemStack variable called something like itemToDrop and initialize to null.

3. have a while loop that continues as long as !foundItem.

4. inside the loop, set itemToDrop to a generated random item and check if it is any of the banned items.  If they are not any banned item (probably should also check for null), then set founditem to true.

5. loop should exit with a valid itemToDrop so then you can just drop it.

 

Anyway, hope you get the idea.  Main point is that the while loop is intended for such repeated checks.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Okay, if the list of banned items is short, like it seems to be, I don't think you need an array or list of banned items but can just check them directly.

 

The point of the "recursion" is simply the idea that if you get a random item that is banned you'll want to choose again (and it may again find a banned item).

 

The Java while statement is meant for these sort of situations -- it keeps looping until it finds a condition that is true (in this case until you get a non-banned item.

 

So I suggest something like this (probably needs some debug as I didn't run this, but it should be right idea):

1. create a local boolean variable called something like foundItem and initialize to false.

2. create a local ItemStack variable called something like itemToDrop and initialize to null.

3. have a while loop that continues as long as !foundItem.

4. inside the loop, set itemToDrop to a generated random item and check if it is any of the banned items.  If they are not any banned item (probably should also check for null), then set founditem to true.

5. loop should exit with a valid itemToDrop so then you can just drop it.

 

Anyway, hope you get the idea.  Main point is that the while loop is intended for such repeated checks.

Didn't think to do it that way...guess I wasn't thinking you could create an ItemStack variable. It did indeed work, thank you very much. :)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.