Jump to content

Recommended Posts

Posted

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;
}
}

Posted

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/

Posted

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.

Posted

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.

Posted

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

Posted

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/

Posted

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. :)

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I've been using this modpack for a few days now with no issues, it was working properly just an hour or two ago and now all of a sudden when I try to load it I get this error: 'The game crashed: exception in server tick loop Error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $' debug log: https://pastebin.com/1htBuhsP crash report: https://pastebin.com/QggHKYDg
    • Everything worked fine until I decided to install supplementaries squared, amendments and a couple other mods like artifacts, and compats for it. I'm having a hard time fixing this, and I get this crash whenever I try to enter a world. Not even a new world is feasible. Any help would be highly appreciated!! https://pastebin.com/7KXYZ6fM
    • Internal Exception: com.google.gson.JsonParseException: Error loading registry data: Unknown element: ResourceKey[minecraft:worldgen/configured_feature / minecraft:crimson_forest_vegetation]   This error is there when i try to join my friends server. We had some mods and it was okay but after 3 days i cant join.
    • It just says the mod i had but i dont have now.. and i didnt even use these mods and it still was crashing with the mods  
    • Hello,   I've been back and forth in emails with some CurseForge support staff on the issue but there's been no progress. In the CurseForge app, I have custom profiles and some public modpacks that will not load no matter what. I've goen through every link sent to me, I've reinstalled Minecraft, reinstalled the launcher, repaired the installations individually, uninstalled and reinstalled the public modpacks, downloaded the latest Java--all of it. But each time, I'm always met with either "java runtime exception", "exit code 1" on the rare occasions Minecraft does launch, or "java is out of date" (which literally cannot be possible since I reinstalled the latest version a few different times.) Making a new profile works, but I want to be able to play the ones I had before.  I had gotten one of the old public Minecraft modpacks to work, the prehistoric one about dinosaurs being featured right now, but then it only worked once. I've tried to retrace my steps and it just would not work. I have genuinely no clue what's going on.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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