Jump to content

[1.10.2] CreativeTab Sort using Class reference instead of Item Instance


Recommended Posts

Posted

So I have my code set up such that (following diesieben07's tutorial here) I have the ordering list setup, and it works just fine, but I have to use individual Item references in order to get all of my blocks in the ordering list.  My question is, since a good number of them are instances of just a few classes, is it possible to send a reference to the class into the sorter instead of having to list every one by itself?

For example:

Spoiler

List<Item> order = Arrays.asList(Item.getItemFromBlock(PCBlocks.glowClothAcid), Item.getItemFromBlock(PCBlocks.glowClothCryonite),
	Item.getItemFromBlock(PCBlocks.glowClothNeptunium), Item.getItemFromBlock(PCBlocks.glowClothNetherflow),
	Item.getItemFromBlock(PCBlocks.glowClothObsidium), Item.getItemFromBlock(PCBlocks.glowClothPlutonium),
	Item.getItemFromBlock(PCBlocks.glowClothRadionite), Item.getItemFromBlock(PCBlocks.glowClothUranium),
	Item.getItemFromBlock(PCBlocks.oreLead), Item.getItemFromBlock(PCBlocks.oreNeptunium), Item.getItemFromBlock(PCBlocks.oreObsidium),
	Item.getItemFromBlock(PCBlocks.orePlutonium), Item.getItemFromBlock(PCBlocks.oreRadionite), Item.getItemFromBlock(PCBlocks.oreUranium),
	Item.getItemFromBlock(PCBlocks.frozenCryonite), Item.getItemFromBlock(PCBlocks.reinforcedGlass),
	/*Item.getItemFromBlock(PCBlocks.acidTnt),*/ Item.getItemFromBlock(PCBlocks.acidBarrier),
	PCItems.GOOP, PCItems.INGOTS, PCItems.VIALS /*PCItems.causticBoat,
	PCItems.battery, PCItems.beamSplitter, PCItems.energyCell*/, PCItems.THERMOPELLET,
	PCItems.ACID_GUN/*, PCItems.cryoblaster, PCItems.lasershotgun, PCItems.lasergun, PCItems.lasergunsplit, PCItems.plasmagun, PCItems.plasmagunsplit, PCItems.railgun,
	PCItems.acidGrenade, PCItems.hazmatBoots, PCItems.hazmatHood, PCItems.hazmatJacket, PCItems.hazmatPants, PCItems.plasmaLeather*/);

tabSorter = Ordering.explicit(order).onResultOf(new Function<ItemStack, Item>() {
	@Override
	public Item apply(ItemStack input)
	{
		return input.getItem();
	}
});

 

All of those glowcloths listed first.  It would cut down my code by 7 references if I could just use the BlockGlowCloth class as the sorting element instead of all these instances.  I initially moved away from the metadata block process because it seemed unnecessary anymore.  Is having them be subitems on an ItemBlock the only option I have?

Posted (edited)

No, because those references are to instantiated objects. You have no way of passing a class to anything and get back that instance. You simply can't do it.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (edited)

whipped this up on the fly, have no idea if this will work, and I don't like the way it works (the getting class part), but try it out anyways:

//put this into your creativetab instance
@Override
public void displayAllRelevantItem(NonNullList<ItemStack> itemsToDisplay){
	List<Item> l = StreamSupport.stream(Item.REGISTRY.spliterator(), false).filter(yourPredicate())
      .collect(Collectors.<Item>toList());
  	
  	l.sort( (Item i, Item j) ->
           i.getClass().getSuperclass().getName().compareTo(j.getClass().getSuperclass().getName() ));
  		
  	l.forEach( i -> i.getSubItems(i, this, itemsToDisplay));
}

private Predicate<Item> yourPredicate(){
	return i -> i != null && i.getCreativeTab() == this;
}

Even if this does work, I don't think I will cause of the .getClass.getSuperclass.getName() part, you still should try to avoid using it; the getting class name parts is usually a very bad idea. You could try making your own comparitor, and try to work something out from there. Not sure it this helps, but better than nothing, and hopefully it sparks your imagination towards a very creative means to an end. Good luck.

Edited by draganz
Posted (edited)

Here's another idea that you could use, if this is that important to you: (1) you get a list of all of your items. This could be done via the Item.REGESTRY method I provided in my above example, or you just making your own static list, then having your items register themselves to said list upon initialization. (2) have an enum, with specific sorting types (see provided example). (3) Have your item classes return a specific (or a default) enum type. (4) upon the creative gui sorting through all of your stuff to display via the desplayAllRelevantItem method, you have your items to display sort based on the enum ordinal. Example below (my not be functional, but should give the general idea):

//////////in your creative tab instance///////////
@Override
public void displayAllRelevantItem(NonNullList<ItemStack> itemsToDisplay){
	ModItemes.YOUR_ITEMS.stream().filter(yourPredicate()).forEach(i -> i.getSubItems(i, this, itemsToDisplay)); 
}

private Predicate<Item> yourPredicate(){
	return i -> i != null && i.getCreativeTab() == this;
}
/////////////Your Item Registry Location///////////
public class ModItems{
  //NOTE:For this class, in your  mods preinitialization stage, you should call the init() method
  
  
  public static final List<ItemBase> YOUR_ITEMS = new ArrayList();
  
  /*Register your static final Items here. Since they are static, they to will have the ability to say use 
  ModItems.YOUR_ITEMS.add(this) in their constructers in order to self initializes themselves to the list, order 
  you state them desiding in what order*/
  
  public static void init(){
    YOUR_ITEMS.sort( (ItemBase i, ItemBase j) -> i.getSortOrder() - j.getSortOrder() );
  }
}
/////////ItemBase class, is used as a starting point for all your items////////////
public abstract class ItemBase extends Item{
  
  private final SortEnum SORT_ORDER;//have this initialized in a constructer
  
  ///....various code you would otherwise put here
  public int getSortOrder(){
    return SORT_ORDER.ordinal;
  }
}
/////the enum
public enum SortEnum{
  APPLE,ORANGES,PLUMS;/*it doesn't matter what values these are, just the integer (ordinal) value they provide. 
  Depending of if you have (itemOne - itemTwo) or (itemTwo - itemOne) will alter the order that these are sorted by. 
  Beyond that, it will be the order of initialization, then should meta types come into play, they the getSubItems 
  method will determine.*/
}

 

Edited by draganz

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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