Jump to content

Problem with use() function in block class


Eveningofstorms

Recommended Posts

I'm trying to make a block that you can take items out of and put them in, but if you have an axe it will craft an item. But it's running multiple times, I believe because clientside and serverside are both using the same code. Can someone help me make this code work?

 

public class WorkStation extends Block implements EntityBlock  {
	
	public WorkStation(Properties props) {
        super(props);
    }
	
	
	@Override
	public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand handIn,
		BlockHitResult hit) { //When the block is clicked
		if (!level.isClientSide){ //If server side
			player.sendMessage(new TextComponent("Not client side"), player.getUUID());
			ItemStack item = player.getItemInHand(handIn); //Get Item in hand
			BlockEntity currentBlockEntityInstance = level.getBlockEntity(pos);
			if (item.isEmpty()) { //If there is no item
				player.sendMessage(new TextComponent("hand empty"), player.getUUID());
				if (currentBlockEntityInstance instanceof WorkStationBlockEntity workstation) {
					Container currentWorkStationInventory = workstation.getItemHandler();
					for (int i = 0; i < workstation.inventorySize(); i++) {  //Loop through the workstation inventory
						if (workstation.getItemHandler().getItem(i).isEmpty()) { //If an Item is empty
							if(i > 0) { //If the index is not 0 (so that we don't index a negative number)
								player.setItemInHand(InteractionHand.MAIN_HAND, workstation.getItemHandler().getItem(i-1)); //Put the last item (the one before we hit the empty spot) in the players hand
								workstation.getItemHandler().setItem(i-1, ItemStack.EMPTY); //Remove this item from the workstation
								level.playSound(null, pos, SoundEvents.WOOD_BREAK, SoundSource.BLOCKS, 0.1F, 10F);
								return InteractionResult.SUCCESS; //End the sequence
							} else { //If the item will be negative then end
								return InteractionResult.FAIL;
							}
						}
					}
					return InteractionResult.FAIL;
				} else {
					return InteractionResult.FAIL;
				}
			
			} else { //If there is an item in the players hand
				player.sendMessage(new TextComponent("hand full"), player.getUUID());
				if(item.getItem() instanceof AxeItem) { //If the player has a n axe in their hand (if the item is an axe)
					player.sendMessage(new TextComponent("hand has axe"), player.getUUID());
					if (currentBlockEntityInstance instanceof WorkStationBlockEntity workstation) {
						Container inventory = workstation.getItemHandler();
						Optional<WorkStationAxeRecipe> match = level.getRecipeManager().getRecipeFor(WorkStationAxeRecipe.Type.INSTANCE, inventory, level);
						
						if(match.isPresent()) { //If there is a recipe
							player.sendMessage(new TextComponent("match present"), player.getUUID());
							Item outputItem = match.get().getResultItem().getItem();
							for (int i = 0; i < workstation.inventorySize(); i++) { //Go through workstation inventory
								workstation.getItemHandler().setItem(i, ItemStack.EMPTY); //make every item gone
							}
							workstation.getItemHandler().setItem(0, new ItemStack(outputItem)); //Put the output block in the workstation
							level.playSound(null, pos, SoundEvents.WOOD_BREAK, SoundSource.BLOCKS, 0.1F, 10F);
							return InteractionResult.SUCCESS; //End
						}else { //If there is no recipe
							return InteractionResult.FAIL; //end
						}
					} else {
						return InteractionResult.FAIL;
					}
				} else { //If the hand does not have an axe
					player.sendMessage(new TextComponent("hand has no axe"), player.getUUID());
					if (currentBlockEntityInstance instanceof WorkStationBlockEntity workstation) {
						Container currentWorkStationInventory = workstation.getItemHandler();
						for (int i = 0; i < workstation.inventorySize(); i++) { //go through the workstation inventory
							if (workstation.getItemHandler().getItem(i).isEmpty()) { //until there is an empty spot
								if(i < 11) { //as long if it's at eleven or under spots
									workstation.getItemHandler().setItem(i, item.split(1)); //set the spots item as the one in the players hand
									if (item.getCount() > 0) { //If the player has more than one item
										player.setItemInHand(handIn, item.split(item.getCount())); //Take away one from the stack
										level.playSound(null, pos, SoundEvents.WOOD_BREAK, SoundSource.BLOCKS, 0.5F, 50F);
										return InteractionResult.SUCCESS; //end
									} else { // If the player has just one item
										player.setItemInHand(handIn, ItemStack.EMPTY); //Empty the hand slot
										level.playSound(null, pos, SoundEvents.WOOD_BREAK, SoundSource.BLOCKS, 0.5F, 50F);
										return InteractionResult.SUCCESS; //end
									}
								} else {
									return InteractionResult.FAIL;
								}
							}
						}
						return InteractionResult.FAIL;
					} else {
						return InteractionResult.FAIL;
					}
				}
			}
		} else {
			player.sendMessage(new TextComponent("client side"), player.getUUID());
			return InteractionResult.FAIL;
		}
	}
	
	@Nullable
    @Override
    public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
        return new WorkStationBlockEntity(blockPos, blockState);
    }
	
	@Override
	public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext ctx) {
		VoxelShape SLABSHAPE = Block.box(0, 0, 0, 16, 9, 16);
		return SLABSHAPE;
	}
	
	
	
}

 

Edited by Eveningofstorms
Link to comment
Share on other sites

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

  • 5 weeks later...

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.



×
×
  • Create New...

Important Information

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