Jump to content

[1.6.4] Attaching NBT Data to Crafting Result Based on Recipe Items


Recommended Posts

Posted

Hi!

 

I guess the title is fairly self-explanatory, but I feel the need to elaborate so as to avoid any confusion. My question pretty much boils down to: "Is there a way to attach NBT data to the result of a vanilla crafting recipe based on the items used therein?" I've been trying to find something along these lines for the past two hours or so, but to no avail. (granted, I may have been trying out the wrong keywords...)

 

More elaboration: I have an item I call a Boon, and based on the items used in its crafting recipe, it can take on different 'jobs' so to speak. For example, using four Diamond Swords makes it a Boon with job: "Knight", or four bows for a Boon with job: "Archer".

 

Is there a way to do this using vanilla crafting or do I have to come up with my own method of crafting the item?

Posted

Hey there!

 

I had a similar problem in my mod, where I had multiple outcomes based on item combinations.

You can add NBT to an item stack and then pass it to the GameRegistry, and that NBT will be present when you craft it!

 

Here I used two metadata items, similar to how the color dyes work.

 

// iterate through all damage values for meat item

for(int i = 0; i < meatItem.getItemData().size(); i++) {

        // create item stack for crafting component

        ItemStack meatStack = new ItemStack(meatItem, 1, i);

        // iterate through all damage values for cheese item

        for(int j = 0; j < cheeseItem.getItemData().size(); j++) {

                // create item stack for crafting component

                ItemStack cheeseStack = new ItemStack(cheeseItem, 1, j);

 

                // this item stack will be the result with the NBT

                ItemStack sandwich = new ItemStack(sandwichItem, 1);

                // assign a new tag compound to the item stack

                sandwich.setTagCompound(new NBTTagCompound());

                // retrieve said compound for modifying

                NBTTagCompound tags = sandwich.stackTagCompound;

                // here you can set your NBT data on the item

                tags.setString("meat", meatStack.getDisplayName());

                tags.setByte("meatTier", (byte) ItemMeat.getTier(meatStack));

                tags.setString("cheese", cheeseStack.getDisplayName());

 

                // add the recipe to the GameRegistry

              GameRegistry.addShapelessRecipe(sandwich, new Object[] { breadSliceItem, breadSliceItem, meatStack, cheeseStack });

      }

}

 

Hope this helps! :) If I didn't explain anything well enough, please don't hesitate to ask!

Posted

Oooh! Thank you so much!

 

I was a bit confused at first but I think I understand what you're doing. *nods* Correct me if I'm wrong, but am I right in assuming that this code goes where I register my items/recipes?

 

Also, what you are doing is basically attaching the specific ingredients used to the resulting item as NBT data, right? I'm trying to reason out how to do this only AND ONLY for specific item combinations, while the recipe won't exist for others. Say, Three diamond swords plus one bow wouldn't work, but four diamond swords or two diamond swords and two bows would. *ponders*

Posted

So I was looking around, and I have no idea if the way I'm planning to do this will work. I'm not even sure exactly how to do it just yet, being a newbie to Java and all...

 

However, I have something in mind, and I may need some help implementing it. The fact that I want this thing to be configurable is beating me up. xD.

 

So, first, and obviously not done in the same class would be retrieving the number of player jobs that are configured. So that should be an integer. Say it's something like below. For now I'll set an arbitrary integer as its value.

 

public static final int NUMBER_OF_JOBS = 4;

 

Then, this is what I currently have written down on Eclipse after the above.:

 

public static void Init(){

     String[] jobNames = {
         //This is just arbitrary. In the future I'd like to pull these values from the config.
         "Knight", "Thief", "Mage", "Archer"
     }

     HashMap<String, Object> jobmap = new HashMap<String, Object>();
     for (int i = 0; i < NUMBER_OF_CLASSES; ++i){
  jobmap.put(jobKeys[i], *jobRecipes*);
     }

}

 

Where *jobRecipes* are to be the arrays of the "recipes" so to speak, and it's at this point that I am stuck because what I want to do next is a for loop with an if statement that checks the "recipes" against the hashmap key, and if the recipes match, sets the job NBT data to the same as the key...

 

I'm beginning to think it's worth the effort to create a custom block instead...

 

Posted

It seems you are trying to over complicate things just a tad. You know, just a teensy weensy bit.

 

After reading what you have said, it seems you are wanting to make it so certain jobs have certain recipes right? Well, if that's the case, time to harness the power of object orientated programming 8)

 

I would make a class called Job, and that class has all the basic stuff in it that every job will have. So an array of recipes etc.. Then, for each job, subclass Job (e.g. JobKnight, JobMage) and implement the job only stuff you need. You may need to make your own custom 'recipe' class as well that creates the special recipes you want.

 

Just my thoughts :)

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

Posted

*nods*

 

The problem is, that's perfectly fine for hard-coded jobs, which, in hindsight, are the better and far nicer way to go at this point in time. However, one of the features on the feature-list my friend -- who'll be working on the project with me eventually -- and I compiled is the ability to create jobs through a config file.

 

Unless there's a way to automatically create a subclass from the input of a config file...

 

*shakes his feature-list for being ambitious*

Posted

Hmm... Well, you probably could do that xD

 

You could modify what I said to suit that. Just store all the information you would need in the config file (I would suggest creating your own way of handling this config file though) and then make the Job class more generic. You would make the Job class so that it has multiple parameters passed to it that are taken from the config - same for the custom recipe class.

 

A lot of helper methods would need writing, but its doable.

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

Posted

Yeaah. I don't think the config that comes with forge has the functionality we need. That other config is still useful for the basic stuff though.

 

*nodnods* I'll get back to everyone if I manage to pull it off with the configurability, but for now, I'll do the easy way of defining jobs and then, when I have that done, I'll start working on making it all configurable.

 

Sidenote: I call the jobs classes in the mod, but because talking java, well, it would get really confusing really fast. xD.

Posted

*flails*

 

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister register){

	Icon[] iconArray = new Icon[2];

	//registering the two icons
	iconArray[0] = register.registerIcon(BCMInfo.ID + ":" + getUnlocalizedName());
	iconArray[1] = register.registerIcon(BCMInfo.ID + ":" + getUnlocalizedName() + "Active");

}

@Override
@SideOnly(Side.CLIENT)
public Icon getIcon(ItemStack itemstack, int renderPass, EntityPlayer player, ItemStack usingItem, int useRemaining){
	NBTTagCompound properties = itemstack.stackTagCompound;
	if (!properties.getString("Owner").isEmpty()){
		return iconArray[1];
	} else {
		return iconArray[0];
	}
}

 

I have a new problem. >:( I have no idea what I'm doing wrong, but Eclipse is telling me that where I return the iconArray[] that it cannot be resolved to a variable. *is confused*

Posted

That's because you are trying to access it from somewhere it isn't.

Try this:

        @SideOnly(Side.CLIENT(
        private Icon[] iconArray;

        @Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister register) {
                iconArray = new Icon[2];

	//registering the two icons
	iconArray[0] = register.registerIcon(BCMInfo.ID + ":" + getUnlocalizedName());
	iconArray[1] = register.registerIcon(BCMInfo.ID + ":" + getUnlocalizedName() + "Active");
}

@Override
@SideOnly(Side.CLIENT)
public Icon getIcon(ItemStack itemstack, int renderPass, EntityPlayer player, ItemStack usingItem, int useRemaining){
	NBTTagCompound properties = itemstack.stackTagCompound;
	if (!properties.getString("Owner").isEmpty()){
		return iconArray[1];
	} else {
		return iconArray[0];
	}
}

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

Posted

I seem to be running headlong into problems all day today. I got the thing working... sort of.

 

http://pastebin.com/WCqYX6yC

 

That's the crash report.

 

The full item class is over here: https://github.com/Malkuthe/BattleClasses/blob/master/battleclassmod/items/BoonItem.java#L68

 

It may have to do with rendering the object in the player's hand. The reason I say that is because the item is perfectly fine in my inventory. However, another issue is that instead of displaying a sprite, minecraft displays the no-icon texture. Anyway, the one with the iconIndex[1] works just fine rendering in the hand, but as soon as the one with iconIndex[0] is selected, minecraft crashes. I'm not sure what's going on.

 

 

Posted

No, your crash has to do with your NBT tag/and or the return from getString("Owner") being null. How about you change your check to:

        @Override
@SideOnly(Side.CLIENT)
public Icon getIcon(ItemStack itemstack, int renderPass, EntityPlayer player, ItemStack usingItem, int useRemaining){
	NBTTagCompound properties = itemstack.stackTagCompound;
                if (properties != null) {
	        if (!properties.getString("Owner").equals("none")) {
		        return iconIndex[1];
	        } 
		return iconIndex[0];
                }
                return this.itemIcon;
}

 

And if that still crashes with a NullPointerException again, change:

if (!properties.getString("Owner").equals("none")) {

to:

if (properties.getString("Owner") != null && !properties.getString("Owner").equals("none")) {

 

Hope that helps :D

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

Posted

Yaay! It works now! I had to do a little fiddling around with the code too. Turns out part of the reason for the crash was that I was just taking the item out of the creative inventory and therefore it had no NBT Tag Compound associated with it. I fixed that part with a quick modification to the onUpdate method and made it so that if the item tag compound of the item is null, to set "Owner" to "none" by default.

 

Thanks so much for the help! Sorry for the relentless questions. I'm trying to learn as much as I can as fast as I can. :D

Posted

Good job on that fix :) It probably isn't the most efficient, but it works right? :P

 

No worries mate, questions are the only way you can learn really.

 

Keep asking away!

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

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hey all, I've been working a lot with datapacks lately, and I'm wondering what the most efficient way to get said data from server to client is.  I'm currently using packets, but given that a lot of the data I'm storing involves maps along the lines of Map<ResourceLocation, CustomDataType>, it can easily start to get messy if I need to transmit a lot of that data all at once. Recently I started looking into the ReloadableServerResources class, which is where Minecraft stores its built-ins.  I see you can access it via the server from the server's resources.managers, and it seems like this can be done even from the client to appropriately retrieve data from the server, unless I'm misunderstanding.  However, from what I can tell, this only works via built-in methods such as getRecipeManager() or getLootTables(), etc.  These are all SimpleJsonResourceReloadListeners, just like my datapack entries are, so it seems like it could be possible for me to access my datapack entries similarly?  But I don't see anywhere in ReloadableServerResources that stores loaded modded entries, so either I'm looking in the wrong place or it doesn't seem to be a thing. Are packets really the best way of doing this, or am I missing a method that would let me use ReloadableServerResources or something similar?
    • Im trying add to block a hole in center, just a usual block and in center of it on up side, there is should be a hole. I tried to add it via BlockModelBuilder, but its not working. Problem is that it only can change block size outside. I tried it to do with VoxelShape and its working, but its has been on server side and looks like its just changed collision shape, but for client, there is a texture covering this hole. I tried to use: base.renderType("cutout"); and removed some pixels from texture. I thought its should work, but game optimization makes block inside looks transparent. So, only custom model?
    • I Made this modpack myself and When i Open a world with embeddium enabeld no chuncks are loading in and when i have the mod brute force rendering culling eanbled the game will not start     This is the Modlist:   Chef's Delight [Forge] (by redstone3game) Yung Structures Addon for Loot Integrations (by someaddon) Macaw's Paths and Pavings (by sketch_macaw) My Nether's Delight (by soytutta) Client Crafting (by someaddon) Integrated Stronghold (by CraisinLord) Delightful (by brnbrd) Immersive Aircraft [Fabric/Forge] (by Conczin) Mysterious Mountain Lib (by sz0999312) Infinity Buttons (by LarsMans) SuperMartijn642's Core Lib (by SuperMartijn642) Clumps (by Jaredlll08) Embeddium (by FiniteReality) Cosmetic Armor Reworked (by LainMI) Creeper Overhaul (by joosh_7889) Balm (by BlayTheNinth) HT's TreeChop (by hammertater) Fastload (by StockiesLad) ImmediatelyFast (by RaphiMC) Integrated Dungeons and Structures (by CraisinLord) Mobtimizations - Entity Performance Fixes (by Corosus) Handcrafted (by terrariumearth) SuperMartijn642's Config Lib (by SuperMartijn642) Dramatic Doors (by FizzWare) Allurement (by TeamAbnormals) Xaero's World Map (by xaero96) Panda's Extra Details (by ThePandaOliver) Geophilic – Vanilla Biome Overhauls (by bebebea_loste) Auroras (by Verph) Naturalist (by Starfish_Studios) Iron Chests (by ProgWML6) Corpse (by henkelmax) FastSuite (by Shadows_of_Fire) Controlling (by Jaredlll08) Ash API (by Trikzon) Eating Animation [Neo/Forge] (by matyrobbrt) Macaw's Doors (by sketch_macaw) ForgeEndertech (by EnderLanky) Philip's Ruins (by philipmoddev) Resourceful Config (by ThatGravyBoat) ModernFix (by embeddedt) Quark (by Vazkii) Illager Invasion [Forge & Fabric] (by Fuzs) Integrated API (by CraisinLord) Redirector [Modern] (by pOtAto__bOy) Macaw's Fences and Walls (by sketch_macaw) Diagonal Windows [Forge & Fabric] (by Fuzs) Waystones (by BlayTheNinth) [EMF] Entity Model Features [Fabric & Forge] (by Traben) FerriteCore ((Neo)Forge) (by malte0811) CodeChicken Lib 1.8.+ (by covers1624) Macaw's Roofs (by sketch_macaw) Server Country Flags (by khajiitos) Bookshelf (by DarkhaxDev) Comforts (Fabric/Forge/Quilt) (by TheIllusiveC4) Enchantment Descriptions (by DarkhaxDev) Entity Culling Fabric/Forge (by tr7zw) Embeddium++ (by SrRapero720) Embeddium (Rubidium) Extra (by dimadencep) Smooth Boot (Reloaded) (by AbdElAziz333) Dungeons and Taverns (by Nova_Wostra) Placeables (by ItzSkay) CoroUtil (by Corosus) Small Ships [Fabric & Forge] (by talhanation) Trade Cycling (by henkelmax) Better Fps - Render Distance[Forge] (by someaddon) Better Villages - Forge (by jtl_elisa) PandaLib (by ThePandaOliver) Curios API (Forge/NeoForge) (by TheIllusiveC4) Stoneworks [Forge & Fabric] (by Fuzs) Pastel Shaders (by ElocinDev) Integrated Villages (by CraisinLord) More Axolotl Variants API (by AkashiiKun69) Potato Shader (by RRe36) Better Archeology (by Pandarix) Block Runner [Forge & Fabric] (by Fuzs) Cloth Config API (Fabric/Forge/NeoForge) (by shedaniel) Kiwi 🥝 (Neo/Forge) (by Snownee) Macaw's Lights and Lamps (by sketch_macaw) Loot Integrations (by someaddon) Farmers Structures (by BlackAuresArt) Wilderness (by ketok) Corpse x Curios API Compat (by Project8gbDeRam) Friends&Foes (Forge/NeoForge) (Copper Golem,Glare,Crab,Moobloom,Iceologer,Rascal,Tuff Golem,Wildfire,Illusioner) (by Faboslav) More Mob Variants (by nyuppo) Better Than Mending (by legobmw99) Remove Reloading Screen (by dimadencep) Simply Swords [Fabric & Forge] (by sweenuss) Better Brightness Slider (by LaidBackSloth) Krypton Reforged (by Txni) AppleSkin (by squeek502) Supplementaries Squared (by plantspookable) Horse Expert (by Fuzs) YUNG's Menu Tweaks (Forge) (by YUNGNICKYOUNG) Pet Cemetery (by TeamAbnormals) Leaky - Item Lag Fix[Forge/Fabric] (by someaddon) Tectonic (by Apollo) Saturn (by AbdElAziz333) Searchables (by Jaredlll08) Inventory HUD+ (by dmitrylovin) Moonlight Lib (by MehVahdJukaar) Explorify – Dungeons & Structures (by bebebea_loste) Placebo (by Shadows_of_Fire) Overflowing Bars [Forge & Fabric] (by Fuzs) Citadel (by sbom_xela) Alex's Mobs - Naturalist Compat (by Kanadeyoru) Forge CIT (by tfarecnim) Create (by simibubi) Starlight (Forge) (by Spottedleaf) [ETF] Entity Texture Features - [Fabric & Forge] (by Traben) Advanced Chimneys (by EnderLanky) Corn Delight[Forge] (by sz0999312) Trials Chambers [1.20.1 Backport] (by Jusey1z) MVS - Moog's Voyager Structures (by finndog_123) Macaw's Paintings (by sketch_macaw) Alternate Current (by SpaceWalkerRS) Hopo Better Ruined Portals (Fabric/Forge/NeoForge) (by hoponopono) Alex's Delight (by Baisylia) Macaw's Biomes O' Plenty (by Samlegamer_) Resourceful Lib (by ThatGravyBoat) FindMe (by Buuz135) Macaw's Bridges (by sketch_macaw) YUNG's Better Witch Huts (Forge) (by YUNGNICKYOUNG) MNS - Moog's Nether Structures (by finndog_123) Mowzie's Mobs (by bobmowzie) FastWorkbench (by Shadows_of_Fire) Transparent (by Trikzon) Artifacts (by ochotonida) Passable Foliage 🌳 (Neo/Forge) (by Snownee) Supplementaries (by MehVahdJukaar) Atmospheric (by TeamAbnormals) Alex's Caves (by sbom_xela) Alex's Caves Delight (by FixerLink1) Ocean's Delight (by scouter567) Brute force Rendering Culling (by RogoShum) fix GPU memory leak[Forge/Fabric] (by someaddon) Burnt: Better Vanilla Fire (by pxlbnk) YDM's Weapon Master (by YourDailyModderx) Curios Compat Layer for Accessories (by Blodhgarm) MES - Moog's End Structures (by finndog_123) Pale Garden and Creaking (by MinoBanana) YUNG's Better Jungle Temples (Forge) (by YUNGNICKYOUNG) AddonsLib (by Samlegamer_) Athena (by CodexAdrian) MakeUp - Ultra Fast | Shaders (by XavierFST) Xaero's Minimap (by xaero96) Macaw's Trapdoors (by sketch_macaw) Translocators 1.8.+ (by covers1624) Additional Structures (by XxRexRaptorxX) AI Improvements (by QueenOfMissiles) Autochef's Delight (by Snownee) Iron Furnaces (by XenoMustache) Iris/Oculus & GeckoLib Compat (by ElocinDev) Connectivity (by someaddon) Easy Magic [Forge & Fabric] (by Fuzs) Blueprint (by TeamAbnormals) VillagersPlus (FORGE) (by finallion_13) Panda's Falling Tree's (by ThePandaOliver) Xaero's Minimap & World Map - Waystones Compatibility [Forge] (by ArcaneAlloy) ServerCore (by Wesley8081) MrCrayfish's Furniture Mod: Refurbished (by MrCrayfish) Just Enough Items (JEI) (by mezz) Farmer's Delight (by vectorwing) Old fisherman swamp house (by SuperWarioModTeam) YUNG's Better Nether Fortresses (Forge) (by YUNGNICKYOUNG) Lithostitched (by Apollo) FastFurnace (by Shadows_of_Fire) DrDestens MCShaders (by DrDesten) Server Performance - Smooth Chunk Save[Forge/Fabric] (by someaddon) Noisium (by Steveplays28) Puzzles Lib [Forge & Fabric] (by Fuzs) YUNG's Bridges (Forge) (by YUNGNICKYOUNG) Alex's Mobs (by sbom_xela) Regions Unexplored (forge/fabric) (by UHQ_GAMES) Architectury API (by shedaniel) Radium Reforged (by Asek3) Separated Leaves (by LarsMans) CullLessLeaves Reforged (Unofficial) (by ccr4ft3r) Just Enough Effect Descriptions (JEED) (by MehVahdJukaar) FastBoot (by dnlayu) Framework (by MrCrayfish) Chipped (by terrariumearth) MSS - Moog's Soaring Structures (by finndog_123) GeckoLib (by Gecko) CB Multipart (by covers1624) GlitchCore (by TheAdubbz) Realm RPG: Fallen Adventurers (by nocubeyt) Zume (by Nolij) Library Ferret - Forge (by jtl_elisa) Recipe Essentials[Forge/Fabric] (by someaddon) Macaw's Stairs (by sketch_macaw) Accessories (by Blodhgarm) What Are They Up To (Watut) (by Corosus) GroovyModLoader (GML) (by matyrobbrt) Fruits Delight (by lcy0x1) Just Enough Breeding (JEBr) (by Christofmeg) Farmer's Respite (by lumpazl) YUNG's API (Forge) (by YUNGNICKYOUNG) Underground Villages (by Mrbysco) Easy Anvils [Forge & Fabric] (by Fuzs) Cupboard (by someaddon) Diagonal Walls (by Fuzs) Large Ore Deposits (by EnderLanky) More Axolotl Variants Mod (by AkashiiKun69) Zeta (by Vazkii) TerraBlender (Forge) (by TheAdubbz) Biomes O' Plenty (by Forstride) Bocchium (by pOtAto__bOy) FramedBlocks (by XFactHD) Patchouli (by Vazkii) Quark Delight (by nocubeyt) Just Enough Resources (JER) (by way2muchnoise) Amendments (by plantspookable) Simple Hats (by fonnymunkey) Oculus (by Asek3) OpenBlocks Elevator (by vsngarcia) YUNG's Better Mineshafts (Forge) (by YUNGNICKYOUNG) Skin Layers 3D (Fabric/Forge) (by tr7zw) Mouse Tweaks (by YaLTeR) YUNG's Extras (Forge) (by YUNGNICKYOUNG)
    • After removing shieldexpansion it wont let me join the server because it's included in the server's modpack. is there a way to bypass that?  
  • Topics

×
×
  • Create New...

Important Information

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