Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Is player in creative mode? Testing on dedicated server


solitone
 Share

Recommended Posts

Hi,

as I wrote in a previous postI'm trying to fix the PythonTool-Mod so that it can run correctly also in a multiplayer game (currently it works well only in singleplayer). At the moment it is for 1.10.2, although perhaps  one day I'll try to update it to a more recent version. Basically this mod allows the player to run a python programs, saved on the client machine. It depends on raspberryjammod to launch the .py program, via a '/python script.py' command.

 

I have the following code in a ComputerBlock class. The idea is that when a player breaks a computer block, the block should drop, but only when playing in survival mode. When playing in creative mode, the computer block should not drop:

 

public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
		// If not in creative mode: survival, etc. break and drop the block itself
		if (!Minecraft.getMinecraft().thePlayer.capabilities.isCreativeMode) {
			// Make the block drop itself 
			// [...]
			// Give it a random drop speed
			// [...]
			// Spawn the item in the world
			// [...]
			// Super MUST be called last because it removes the tile entity
			super.breakBlock(worldIn, pos, state);
		} else {
			// If in creative mode, don't drop the block
			super.breakBlock(worldIn, pos, state);
		}
}

 

It is clear though that this method works only in singleplayer mode, but not in multiplayer, since there is no Minecraft class on the dedicated server.

 

I've moved that code in the client proxy, and at least I don't get any server side exception any longer.  However I miss the server side code that I should put in the server proxy. How can I test server side that the player that breaks the block is not in creative mode?

Link to comment
Share on other sites

Use PlayerCapabilities::isCreativeMode to check if the player is in creative. The PlayerCapabilities of a player is stored in EntityPlayer::capabilities. You can use this on any instances of EntityPlayer (i.e. passed in to the method you are overriding) on the server.

 

In addition, in your case, I would recommend overriding Block#onBlockHarvested instead of Block#breakBlock, as the former passes in the player that broke the block in the method's parameter.

Edited by DavidM
  • Like 1

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

12 hours ago, DavidM said:

I would recommend overriding Block#onBlockHarvested instead of Block#breakBlock, as the former passes in the player that broke the block in the method's parameter.

 

I've tried and moved the block of code for dropping the computer block into method harvestBlock:

 

@Override
	public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, @Nullable ItemStack stack) {
		// Make the block drop itself
		ItemStack itemTemp = new ItemStack(pythontool.computerblock.StartupCommon.computerBlock, 1);
		EntityItem item = new EntityItem(worldIn, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
				itemTemp);
		// Give it a random drop speed
		float multiplier = 0.1f;
		float motionX = worldIn.rand.nextFloat() - 0.5f;
		float motionY = worldIn.rand.nextFloat() - 0.5f;
		float motionZ = worldIn.rand.nextFloat() - 0.5f;
		item.motionX = motionX * multiplier;
		item.motionY = motionY * multiplier;
		item.motionZ = motionZ * multiplier;
		// Spawn the item in the world
		worldIn.spawnEntityInWorld(item);

		super.harvestBlock(worldIn, player, pos, state, te, stack);
	}

 

It doesn't work though, the computer block is not dropped. I tried breaking the block using a pickaxe as suggested by diesieben07 but it's not dropped anyway.

 

What am I getting wrong? Perhaps I misunderstood what you meant.

Edited by solitone
Link to comment
Share on other sites

 

I found a possible solution in this old thread. Here's what I've done:

 

	@Override
	public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
		if (willHarvest) {
			// Delay deletion of the block until after getDrops
			return true;
		}
		if (!player.capabilities.isCreativeMode) {
			harvestBlock(world, player, pos, state, null, null);
		}
		return super.removedByPlayer(state, world, pos, player, willHarvest);
	}

 

The computer block is dropped when I break it, unless I'm in creative mode. So it works as expected. Yet I'm not sure that it results in the exact same effect as the original code. And I don't fully understand what's the meaning of delay deletion of the block until after getDrops.

 

Anyhow, I no longer get errors or exceptions when in multiplayer. Plus now, when I re-enter the game, I have the same player inventory that I had when I exited. So it looks alright.

 

Any comment, suggestion, or correction would be highly appreciated. Thanks!

Edited by solitone
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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.