Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.14.4] Create a crafting tile entity + gui
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
BlockyPenguin

[1.14.4] Create a crafting tile entity + gui

By BlockyPenguin, August 12, 2020 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

BlockyPenguin    4

BlockyPenguin

BlockyPenguin    4

  • Creeper Killer
  • BlockyPenguin
  • Members
  • 4
  • 113 posts
Posted August 12, 2020 (edited)

So I'm trying to create a new crafting block. I've already got the recipe, the serialiser, etc. I've taken a look different crafting blocks in vanilla's code (mostly AbstractFunaceTileEntity, as the other blocks are done without a tile), and I keep writing code, getting stuck, then rewriting it, over and over. How would I go about making this work? I have a custom cauldron block, and I want a player to right-click it to add one of the items in the stack they're holding to the tile's inventory, or if they're holding nothing then drop all the items in the inventory. Here's my code so far:

 

Block:

public class LabKauldron extends Block {
	
	public LabKauldron() {
		super(Block.Properties.from(Blocks.CAULDRON)
			.lootFrom(Blocks.CAULDRON)
		);
	}
	
	@Override
	public BlockRenderLayer getRenderLayer() {
		return BlockRenderLayer.CUTOUT;
	}
	
	@Override
	public boolean hasTileEntity(BlockState state) {
		return true;
	}
	
	@Override
	public TileEntity createTileEntity(BlockState state, IBlockReader world) {
		return new LabKauldronTileEntity();
	}
	
	@Override
	public boolean onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
		TileEntity tile = world.getTileEntity(pos);
		
		//Do something with the tile
		
	}
}

 

Tile:

public class LabKauldronTileEntity extends TileEntity {
	
	private LazyOptional<IItemHandler> itemHandler = LazyOptional.of(this::createItemHandler);
	
	private ArrayList<CauldronRecipe> recipes = CauldronRecipe.getAllCauldronRecipes();
	private CauldronRecipe currentRecipe;
	
	public LabKauldronTileEntity() {
		super(ModRegistry.TileTypes.LABKAULDRON.get());
	}
	
	@Override
	@SuppressWarnings("unchecked")
    public void read(CompoundNBT tag) {
        CompoundNBT invTag = tag.getCompound("inv");
        itemHandler.ifPresent(h -> ((INBTSerializable<CompoundNBT>) h).deserializeNBT(invTag));
        
        super.read(tag);
    }

	@Override
	@SuppressWarnings("unchecked")
    public CompoundNBT write(CompoundNBT tag) {
        itemHandler.ifPresent(h -> {
			CompoundNBT compound = ((INBTSerializable<CompoundNBT>) h).serializeNBT();
            tag.put("inv", compound);
        });
        
        return super.write(tag);
    }

    private IItemHandler createItemHandler() {
        return new ItemStackHandler(50) {

            @Override
            protected void onContentsChanged(int slot) {
                markDirty();
            }

            @Override
            public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
                return true;
            }
            
        };
    }
}

 

CauldronRecipe:

public class CauldronRecipe implements ICauldronRecipe {
	
	private ResourceLocation id;
	private ItemStack output;
	private int cookingDelay;
	private Ingredient[] ingredients;
	
	public CauldronRecipe(ResourceLocation id, ItemStack output, int cookingDelay, Ingredient... ingredients) {
		this.id = id;
		this.output = output;
		this.cookingDelay = cookingDelay;
		this.ingredients = ingredients;
	}
	
	public ItemStack getIcon() {
		return new ItemStack(Blocks.CAULDRON);
	}
	
	public IRecipeSerializer<?> getSerializer() {
		return ModRegistry.RecipeSerializers.CAULDRON_RECIPE.get();
	}

	@Override
	public boolean matches(RecipeWrapper inv, World world) {
		for(Ingredient ingredient : ingredients) {
			if(ingredient.test(inv.getStackInSlot(0)) == false) {
				return false;
			}
		}
		return true;
	}

	@Override
	public ItemStack getCraftingResult(RecipeWrapper inv) {
		return output;
	}

	@Override
	public ItemStack getRecipeOutput() {
		return output;
	}

	@Override
	public ResourceLocation getId() {
		return id;
	}
	
	@Override
	public Ingredient[] getInputs() {
		return ingredients;
	}
	
	@Override
	public int getCookingDelay() {
		return cookingDelay;
	}
	
	@Override
	public NonNullList<Ingredient> getIngredients() {
		return NonNullList.from(null, getInputs());
	}
	
	public static ArrayList<CauldronRecipe> getAllCauldronRecipes() {
    	ArrayList<CauldronRecipe> recipeList = new ArrayList<CauldronRecipe>();
    	
    	RecipeManager rm = new RecipeManager();
    	for (IRecipe<?> r : rm.getRecipes()) {
			if(r.getType() == ModRecipeTypes.CAULDRON) {
				recipeList.add((CauldronRecipe) r);
			}
		}
    	return recipeList;
    }
}

 

Also, here's the json file for an example recipe:

{
	"type": "labkit:cauldron",

	"inputs": [
		{
			"item": "minecraft:dirt",
			"count": 1
		},
		{
			"tag": "forge:seeds",
			"count": 3
		}
	],

	"cooking_delay": 50,

	"output": {
		"item": "minecraft:grass_block",
		"count": 1
	}
}

This all gets parsed correctly, it's just for reference :)

The order of the inputs doesn't matter to me, I'd just like to know how to check if they equal a recipe's ingredients.

Thanks!

Edited August 18, 2020 by BlockyPenguin
Clarified the title
  • Quote

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Share this post


Link to post
Share on other sites

Draco18s    2402

Draco18s

Draco18s    2402

  • Reality Controller
  • Draco18s
  • Members
  • 2402
  • 15924 posts
Posted August 12, 2020
15 minutes ago, BlockyPenguin said:

private ArrayList<CauldronRecipe> recipes = CauldronRecipe.getAllCauldronRecipes();

There's no ready to do this, you can just call CauldronRecipe.getAllCauldronRecipes() any time you need them.

If you just want a shorthand, make recipes static.

16 minutes ago, BlockyPenguin said:

private CauldronRecipe currentRecipe;

This is unused.

 

17 minutes ago, BlockyPenguin said:

		for(Ingredient ingredient : ingredients) {
			if(ingredient.test(inv.getStackInSlot(0)) == false) {

"For all required ingredients:
If the stack in slot 0 is not the ingredient:

Recipe is not matched."

 

Uh...

  • Quote

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.

Share this post


Link to post
Share on other sites

BlockyPenguin    4

BlockyPenguin

BlockyPenguin    4

  • Creeper Killer
  • BlockyPenguin
  • Members
  • 4
  • 113 posts
Posted August 13, 2020
Quote

private ArrayList<CauldronRecipe> recipes = CauldronRecipe.getAllCauldronRecipes();

The reason I did this is because I heard that RecipeManager#getRecipes() is system intensive, and shouldn't be called too frequently.

currentRecipe will be used to keep track of the recipe that's currently being brewed.

Quote

@Override
public boolean matches(RecipeWrapper inv, World world) {
	for(Ingredient ingredient : ingredients) {
		if(ingredient.test(inv.getStackInSlot(0)) == false) {
			return false;
		}
	}
	return true;
}

Looking at that, I can tell I was tired! 😆
Would this be better?

@Override
public boolean matches(RecipeWrapper inv, World world) {
	for(int i = 0; i <= ingredients.length; i++) {
		Ingredient ingredient = ingredients[i];
		
		if(ingredient.matches(inv.getStackInSlot(i))) {
			return true;
		}
	}
	return false;
}

 

  • Quote

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Share this post


Link to post
Share on other sites

Draco18s    2402

Draco18s

Draco18s    2402

  • Reality Controller
  • Draco18s
  • Members
  • 2402
  • 15924 posts
Posted August 13, 2020
6 hours ago, BlockyPenguin said:

is system intensive, and shouldn't be called too frequently.

Then...

15 hours ago, Draco18s said:

...make recipes static.

 

6 hours ago, BlockyPenguin said:

Would this be better?

Dunno. Does it work?

  • Quote

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.

Share this post


Link to post
Share on other sites

BlockyPenguin    4

BlockyPenguin

BlockyPenguin    4

  • Creeper Killer
  • BlockyPenguin
  • Members
  • 4
  • 113 posts
Posted August 14, 2020 (edited)
18 hours ago, Draco18s said:

Dunno. Does it work?

Well, by looking at it again, I've changed it to this:

@Override
public boolean matches(RecipeWrapper inv, World world) {
	ArrayList<Boolean> list = new ArrayList<Boolean>();
	for(int i = 0; i <= ingredients.length; i++) {
		Ingredient ingredient = ingredients[i];
		
		if(ingredient.test(inv.getStackInSlot(i))) {
			list.add(true);
		}
	}
	list.add(false);
	
	if(list.contains(false)) {
		return false;
	}
	
	return true;
}

Which will hopefully work better, but I have no way of checking at the moment, I need some way of finding the recipe the player is currently making. Any ideas?

EDIT: Also, how would making recipes static help?

Edited August 14, 2020 by BlockyPenguin
  • Quote

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Share this post


Link to post
Share on other sites

Draco18s    2402

Draco18s

Draco18s    2402

  • Reality Controller
  • Draco18s
  • Members
  • 2402
  • 15924 posts
Posted August 14, 2020
6 hours ago, BlockyPenguin said:

@Override
public boolean matches(RecipeWrapper inv, World world) {
	ArrayList<Boolean> list = new ArrayList<Boolean>();
	for(int i = 0; i <= ingredients.length; i++) {
		Ingredient ingredient = ingredients[i];
		
		if(ingredient.test(inv.getStackInSlot(i))) {
			list.add(true);
		}
	}
	list.add(false);
	
	if(list.contains(false)) {
		return false;
	}
	
	return true;
}

Lets run through this real fast.

You create a list and loop over your ingredients, adding some number of "true"s to your list.

 

Then, regardless of everything, you add a false.

 

Then you query "is false in this list?"

 

What I mean is, lets remove the loop:

@Override
public boolean matches(RecipeWrapper inv, World world) {
	ArrayList<Boolean> list = new ArrayList<Boolean>();
	
	list.add(false);
	
	if(list.contains(false)) {
		return false;
	}
	
	return true;
}

This will never return true.

  • Quote

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.

Share this post


Link to post
Share on other sites

BlockyPenguin    4

BlockyPenguin

BlockyPenguin    4

  • Creeper Killer
  • BlockyPenguin
  • Members
  • 4
  • 113 posts
Posted August 14, 2020
3 hours ago, Draco18s said:

Lets run through this real fast.

You create a list and loop over your ingredients, adding some number of "true"s to your list.

 

Then, regardless of everything, you add a false.

 

Then you query "is false in this list?"

 

What I mean is, lets remove the loop:


@Override
public boolean matches(RecipeWrapper inv, World world) {
	ArrayList<Boolean> list = new ArrayList<Boolean>();
	
	list.add(false);
	
	if(list.contains(false)) {
		return false;
	}
	
	return true;
}

This will never return true.

Oh oops! Thanks for pointing that out. I've changed it to this now:

@Override
public boolean matches(RecipeWrapper inv, World world) {
	ArrayList<Boolean> list = new ArrayList<Boolean>();
	for(int i = 0; i <= ingredients.length; i++) {
		Ingredient ingredient = ingredients[i];
		
		if(ingredient.test(inv.getStackInSlot(i))) {
			list.add(true);
		}else {
			list.add(false);
		}
		
	}
	
	if(list.contains(false)) {
		return false;
	}
	
	return true;
}

 

  • Quote

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Share this post


Link to post
Share on other sites

Draco18s    2402

Draco18s

Draco18s    2402

  • Reality Controller
  • Draco18s
  • Members
  • 2402
  • 15924 posts
Posted August 14, 2020
	for(int i = 0; i <= ingredients.length; i++) {
		Ingredient ingredient = ingredients[i];
		
		if(!ingredient.test(inv.getStackInSlot(i))) {
			return false;
		}
	}

Only one false matters. The problem with your original code was that you weren't using i as your index, but 0. When you changed it to i you also screwed with the boolean check and return values.

  • Quote

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.

Share this post


Link to post
Share on other sites

BlockyPenguin    4

BlockyPenguin

BlockyPenguin    4

  • Creeper Killer
  • BlockyPenguin
  • Members
  • 4
  • 113 posts
Posted August 14, 2020
3 hours ago, Draco18s said:

	for(int i = 0; i <= ingredients.length; i++) {
		Ingredient ingredient = ingredients[i];
		
		if(!ingredient.test(inv.getStackInSlot(i))) {
			return false;
		}
	}

Only one false matters. The problem with your original code was that you weren't using i as your index, but 0. When you changed it to i you also screwed with the boolean check and return values.

Ah, okay, thank you so much! I feel like ashamed for not thinking of this earlier! I'm a noob lol.

  • Quote

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Share this post


Link to post
Share on other sites

BlockyPenguin    4

BlockyPenguin

BlockyPenguin    4

  • Creeper Killer
  • BlockyPenguin
  • Members
  • 4
  • 113 posts
Posted August 18, 2020 (edited)

Actually, I've changed my mind on `how I want the crafting system to work. I'd like a GUI instead, so I can show more information. I have a basic container and screen class, but I can't seem to get the screen to register (The container I had no issues with). I have this code in my client setup method:

ScreenManager.registerFactory(ModRegistry.ContainerTypes.LABKAULDRON.get(), LabKauldronScreen::new);

But I'm getting this warning from my IDE:

The type LabKauldronScreen does not define LabKauldronScreen(M, PlayerInventory, ITextComponent) that is applicable here.

 

Here's my screen's code:

public class LabKauldronScreen extends ContainerScreen<LabKauldronContainer> {
	public LabKauldronScreen(LabKauldronContainer container, PlayerInventory inv, ITextComponent name) {
		super(container, inv, name);
	}

	public void render(int mouseX, int mouseY, float partialTicks) {
		this.renderBackground();
		super.render(mouseX, mouseY, partialTicks);
		this.renderHoveredToolTip(mouseX, mouseY);
	}
	
	//Draw the foreground layer for the GuiContainer (everything in front of the items)
	protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
		this.font.drawString(this.title.getFormattedText(), 8.0F, 6.0F, 4210752);
		this.font.drawString(this.playerInventory.getDisplayName().getFormattedText(), 8.0F, (float)(this.ySize - 96 + 2), 4210752);
	}
	   
	//Draws the background layer of this container (behind the items).
	protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
		GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
		this.minecraft.getTextureManager().bindTexture(ModGUITextures.LABKAULDRON_GUI_TEXTURE);
		int relX = (this.width - this.xSize) / 2;
		int relY = (this.height - this.ySize) / 2;
		this.blit(relX, relY, 0, 0, this.xSize, this.ySize);
	}
}

I have no idea what's wrong, I've even copy-pasted from several open-source mods and modified it to my classes. The thing is, the code is exactly the same, and it still produces the same errors...

 

All help appreciated, thanks!

EDIT: Also, I forgot to mention, how would making recipes static help?

Edited August 18, 2020 by BlockyPenguin
  • Quote

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Share this post


Link to post
Share on other sites

Draco18s    2402

Draco18s

Draco18s    2402

  • Reality Controller
  • Draco18s
  • Members
  • 2402
  • 15924 posts
Posted August 18, 2020
5 hours ago, BlockyPenguin said:

how would making recipes static help?

If your goal is to reduce the fetching time of CauldronRecipe.getAllCauldronRecipes();

Would you rather:

(a) do it every time a new TileEntity is created

(b) once, ever

 

Its not like the recipes change or are otherwise unique per cauldron...

  • Like 1
  • Quote

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.

Share this post


Link to post
Share on other sites

BlockyPenguin    4

BlockyPenguin

BlockyPenguin    4

  • Creeper Killer
  • BlockyPenguin
  • Members
  • 4
  • 113 posts
Posted August 18, 2020
5 minutes ago, Draco18s said:

If your goal is to reduce the fetching time of CauldronRecipe.getAllCauldronRecipes();

Would you rather:

(a) do it every time a new TileEntity is created

(b) once, ever

 

Its not like the recipes change or are otherwise unique per cauldron...

Oh, ok, thanks, I understand now! :)

  • Quote

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Share this post


Link to post
Share on other sites

BlockyPenguin    4

BlockyPenguin

BlockyPenguin    4

  • Creeper Killer
  • BlockyPenguin
  • Members
  • 4
  • 113 posts
Posted August 20, 2020 (edited)

bump? Just to reclarify, I'm currently asking: why is this not working, and how do I make my block a crafting block?

Edit: I figured out what was wrong with the screen not registering

Edited August 20, 2020 by BlockyPenguin
  • Quote

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Share this post


Link to post
Share on other sites

BlockyPenguin    4

BlockyPenguin

BlockyPenguin    4

  • Creeper Killer
  • BlockyPenguin
  • Members
  • 4
  • 113 posts
Posted August 21, 2020

bump

Please, I really need to know!

  • Quote

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Share this post


Link to post
Share on other sites

BlockyPenguin    4

BlockyPenguin

BlockyPenguin    4

  • Creeper Killer
  • BlockyPenguin
  • Members
  • 4
  • 113 posts
Posted August 24, 2020 (edited)

bump? How do i give my block crafting abilities?

Edited August 24, 2020 by BlockyPenguin
  • Quote

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Share this post


Link to post
Share on other sites

BlockyPenguin    4

BlockyPenguin

BlockyPenguin    4

  • Creeper Killer
  • BlockyPenguin
  • Members
  • 4
  • 113 posts
Posted August 25, 2020

Hello? Anyone know?

  • Quote

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Lpoob
      Lobby

      By Lpoob · Posted 11 minutes ago

      dfdd
    • Montyfat
      Told me I have to choose which app to open it with?

      By Montyfat · Posted 33 minutes ago

      When I tried downloading forge I waited for 6 seconds for the skip button to pop up and when it did I pressed it and then it said "open with what app?" or something along the lines.I chose internet explorer since it was the only reasonable one. It gave me adobe, internet explorer, notepad, media player, and microsoft paint. I really am confused but at the same time also really want to download mods for minecraft. Please help!  P.S I did choose the correct version of MC (1.16.5) so don't ask about that 
    • SirStavros
      1.16.5 Forge Crashing On Start

      By SirStavros · Posted 47 minutes ago

      I have tried opening the latest version of forge but I can't seem to figure out why it keeps crashing. This has happened with previous versions as well. When I open it, after the crash it says this: An unexpected issue occured and the game has crashed. We're sorry for the inconvenience. Exit Code: 0 What do I need to do so that I can run it?
    • DaemonUmbra
      Why does Forge installer 1.8.9 have Vape as an execution parent?

      By DaemonUmbra · Posted 1 hour ago

      1.8 is no longer supported on this forum. Please update to a modern version of Minecraft to receive support.
    • DaemonUmbra
      Why don't you support 1.12.2?

      By DaemonUmbra · Posted 1 hour ago

      You can't expect us to support every version ever released ad infinitum. That doesn't sound like a valid reason to not upgrade, and if that is indeed your reasoning you would probably look at getting a better PC if you plan on playing games.
  • Topics

    • Lpoob
      0
      Lobby

      By Lpoob
      Started 11 minutes ago

    • Montyfat
      0
      Told me I have to choose which app to open it with?

      By Montyfat
      Started 33 minutes ago

    • SirStavros
      0
      1.16.5 Forge Crashing On Start

      By SirStavros
      Started 47 minutes ago

    • EthanDev
      1
      Why does Forge installer 1.8.9 have Vape as an execution parent?

      By EthanDev
      Started 1 hour ago

    • Amperka_pro
      1
      Why don't you support 1.12.2?

      By Amperka_pro
      Started 2 hours ago

  • Who's Online (See full list)

    • SSWarrior
    • Aecht_Rob
    • Cheezy Amuzus
    • Lpoob
    • TheUltimateGuide
    • Veo
    • HourShower
    • thorstenschwager
    • Luf1o
    • Chumbanotz
    • Montyfat
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.14.4] Create a crafting tile entity + gui
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community