Jump to content

Recommended Posts

Posted (edited)

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
Posted

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.

  • 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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Sorry for the late response, but the game opened and I made a world but it's stuck at 0% Here's the latest.log https://mclo.gs/peEb1R8 I disabled The Factory Must Grow and soulsweapons
    • Hey everyone! Two of my friends downloaded this modpack and are having an issue with the blocks loading in correctly. Grass looks like bamboo, waystones look like two barrels stacked on eachother, and wheat looks like water and stairs. What is this problem? How can we fix it? Neither of my other friends or myself had this issue. 
    • I removed Yung's cave biome mod and It wasnt in one of those biomes however the log file said the same line (([25Apr2025 21:20:15.500] [Flywheel Task Executor #5/WARN] [Embeddium-MixinTaintDetector/]: Mod(s) [oculus] are modifying Embeddium class me.jellysquid.mods.sodium.client.render.vertex.serializers.VertexSerializerRegistryImpl, which may cause instability.))
    • Note: i had a couple of ideas, but i just don't know how to execute them. The main idea was to make the new block (let's exemplify with a mixer) be essentially a clone of the mechanical mixer (different texture tho) that would have a different recipe type (instead of mixing, advanced_mixing) and i would copy all the create mod recipes and edit the processing time while also adding the tier dependent ones.
    • Hi! Before everything, thank you for even reading. I'm coming here in need of help making a few aspects for a create mod addon. It's an addon that aims to add almost the full periodic table with realistic ways of obtaining all the elements and capability of almost full automation. For what purpose? A techy armor and to go to the moon and rocky planets of the solar system. It'll have 3 different tiers of machines (mixer, millstone, crushing wheels): basic (just the normal create mod machines but renamed), advanced (25% faster and has recipes only possible for this tier and above) end elite (75% faster than basic and recipes only available for this tier). The problem is, I'm not a coder. I know less than the basics. I know how to do everything else but these machine tiers and i need some help if you can.
  • Topics

×
×
  • Create New...

Important Information

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