Jump to content

[1.15.2] Flag problem with cauldron-like block


SW Raki

Recommended Posts

Hello my dear friends.

I created a custom cauldron-like block. everything works fine, it renders like i want it to. The block cant accept duplicate items to generate tier-based dung item.

Example: to generate tier 1 crafting dung, you need 1 dirt, then 1 bone meal, and then 3 different tier 0 crop products (vanilla items like wheat, carrot, potato etc.).
The Problem: if i use, for example, wheat on one DungInfuser Block, i can't use wheat anymore on any DungInfuser Block. It doesn't matter if i destroy the block or place multiple blocks, destroy and replace them.
I started modding this week and i havent used java for quite a while, so i dont really get what i did wrong.

I apologise in advance for my terrible english and hope you get my issue right.

PS: Currently, it doesnt drop the resulting crafting dung, it is not implemented yet. I want to fix this issue first.

package com.kahmi.elementcrops.objects.blocks;

import java.util.Arrays;

import com.kahmi.elementcrops.objects.items.FruitModItem;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.pathfinding.PathType;
import net.minecraft.state.IntegerProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.stats.Stats;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.shapes.IBooleanFunction;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;

public class DungInfuserBlock extends Block {
	public static final IntegerProperty LEVEL = IntegerProperty.create("level", 0, 5);
	private static final VoxelShape INSIDE = makeCuboidShape(2.0D, 4.0D, 2.0D, 14.0D, 16.0D, 14.0D);
	private static final VoxelShape SHAPE = VoxelShapes.combineAndSimplify(VoxelShapes.fullCube(), VoxelShapes.or(makeCuboidShape(0.0D, 0.0D, 4.0D, 16.0D, 3.0D, 12.0D), makeCuboidShape(4.0D, 0.0D, 0.0D, 12.0D, 3.0D, 16.0D), makeCuboidShape(2.0D, 0.0D, 2.0D, 14.0D, 3.0D, 14.0D), INSIDE), IBooleanFunction.ONLY_FIRST);
	private int mContentTier = 0;
	private Item[] mUsedContent = new Item[3];
	public DungInfuserBlock(Properties pProperties) {
	   super(pProperties);
	   this.setDefaultState(this.stateContainer.getBaseState().with(LEVEL, Integer.valueOf(0)));
	}

	public VoxelShape getShape(BlockState pState, IBlockReader pWorldIn, BlockPos pPos, ISelectionContext pContext) {
		return SHAPE;
	}

	public VoxelShape getRaytraceShape(BlockState pState, IBlockReader pWworldIn, BlockPos pPos) {
		return INSIDE;
	}

	public ActionResultType onBlockActivated(BlockState pState, World pWorldIn, BlockPos pPos, PlayerEntity pPlayer, Hand pHandIn, BlockRayTraceResult p_225533_6_) {
		ItemStack heldItemStack = pPlayer.getHeldItem(pHandIn);
		int level = pState.get(LEVEL);
		if (heldItemStack.isEmpty() || level == 5) {
			return ActionResultType.PASS;
		} else {
			Item heldItem = heldItemStack.getItem();
			if ((heldItem == Items.DIRT || heldItem == Items.COARSE_DIRT) && level == 0 && !pWorldIn.isRemote) {
				if (!pPlayer.abilities.isCreativeMode) {
					pPlayer.setHeldItem(pHandIn, ItemStack.EMPTY);
				}

				pPlayer.addStat(Stats.FILL_CAULDRON);
				this.setContentLevel(pWorldIn, pPos, pState, 1);
				return ActionResultType.SUCCESS;
			} else if (heldItem == Items.BONE_MEAL && level == 1 && !pWorldIn.isRemote) {
				if (!pPlayer.abilities.isCreativeMode) {
					pPlayer.setHeldItem(pHandIn, ItemStack.EMPTY);
				}

				pPlayer.addStat(Stats.FILL_CAULDRON);
				this.setContentLevel(pWorldIn, pPos, pState, 2);
				return ActionResultType.SUCCESS;
			} else if ((heldItem == Items.POTATO || heldItem == Items.CARROT || heldItem == Items.WHEAT || heldItem == Items.BEETROOT || heldItem == Items.MELON_SLICE || heldItem == Items.PUMPKIN) && level >= 2 && !Arrays.asList(mUsedContent).contains(heldItem) && !pWorldIn.isRemote) {
				if (!pPlayer.abilities.isCreativeMode) {
					pPlayer.setHeldItem(pHandIn, ItemStack.EMPTY);
				}
				
				pPlayer.addStat(Stats.FILL_CAULDRON);
				this.setContentLevel(pWorldIn, pPos, pState, level + 1);
				this.mUsedContent[level - 2] = heldItem;
				return ActionResultType.SUCCESS;
			} else if (heldItem instanceof FruitModItem && level >= 2 && !pWorldIn.isRemote) {
				FruitModItem iHeldItem = (FruitModItem)heldItem;
				if (level == 2) {
					this.mContentTier = iHeldItem.TIER;
					if (!pPlayer.abilities.isCreativeMode) {
						pPlayer.setHeldItem(pHandIn, ItemStack.EMPTY);
					}
					
					pPlayer.addStat(Stats.FILL_CAULDRON);
					this.setContentLevel(pWorldIn, pPos, pState, level + 1);
					this.mUsedContent[level - 2] = heldItem;
					return ActionResultType.SUCCESS;
				} else if (this.mContentTier == iHeldItem.TIER) {
					if (!pPlayer.abilities.isCreativeMode) {
						pPlayer.setHeldItem(pHandIn, ItemStack.EMPTY);
					}
					
					pPlayer.addStat(Stats.FILL_CAULDRON);
					this.setContentLevel(pWorldIn, pPos, pState, level + 1);
					this.mUsedContent[level - 2] = heldItem;
					return ActionResultType.SUCCESS;
				}
			}
		}
		return ActionResultType.PASS;
	}

	   public void setContentLevel(World pWorldIn, BlockPos pPos, BlockState pState, int pLevel) {
	      pWorldIn.setBlockState(pPos, pState.with(LEVEL, Integer.valueOf(MathHelper.clamp(pLevel, 0, 5))), 2);
	      pWorldIn.updateComparatorOutputLevel(pPos, this);
	   }

	   /**
	    * @deprecated call via {@link IBlockState#hasComparatorInputOverride()} whenever possible. Implementing/overriding
	    * is fine.
	    */
	   public boolean hasComparatorInputOverride(BlockState pState) {
	      return true;
	   }

	   /**
	    * @deprecated call via {@link IBlockState#getComparatorInputOverride(World,BlockPos)} whenever possible.
	    * Implementing/overriding is fine.
	    */
	   public int getComparatorInputOverride(BlockState pBlockState, World pWorldIn, BlockPos pPos) {
	      return pBlockState.get(LEVEL);
	   }

	   protected void fillStateContainer(StateContainer.Builder<Block, BlockState> pBuilder) {
	      pBuilder.add(LEVEL);
	   }

	   public boolean allowsMovement(BlockState pState, IBlockReader pWorldIn, BlockPos pPos, PathType pType) {
	      return false;
	   }
}

 

Link to comment
Share on other sites

Howdy

 

The problem is that there is only one instance of your DungInfuserBlock block, not one for each placed block.  If you try and store information about each world block in your DungInfuserBlock, it can only store one at a time.  (The DungInfuserBlock is a "flyweight" object - google Design Pattern Flyweight for more info).

 

You should use a TileEntity instead, to store information about each placed block.  The vanilla CampfireBlock (with CampfireTileEntity) does this, for example.

There is also an example of this in the following tutorial project:

https://github.com/TheGreyGhost/MinecraftByExample/tree/master

see mbe20.

 

-TGG

 

  • Thanks 1
Link to comment
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.
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.