Jump to content

[1.8] Player destroy block with pickaxe


oznecniV97

Recommended Posts

Hi guys,

I want to create a method to press a key and break a block (of stone or other) in front of me with my pickaxe.

I write this method but it do nothing and minecraft crash.

 

public static void rompi(){
	int max = Minecraft.getMinecraft().thePlayer.inventory.getCurrentItem().getMaxDamage();
	int curDam = Minecraft.getMinecraft().thePlayer.inventory.getCurrentItem().getItemDamage();
	int nextDam=Minecraft.getMinecraft().thePlayer.inventory.getCurrentItem().getItemDamage();
	while(curDam==nextDam){
		Minecraft.getMinecraft().thePlayer.swingItem();
		nextDam=Minecraft.getMinecraft().thePlayer.inventory.getCurrentItem().getItemDamage();
	}
}

 

swingItem() make a swing of my item, but in this while isn't true...

help  :o

Link to comment
Share on other sites

firrst of all you need to destroy the block on server side.

second you cant use the Miencraft class on Server side, its client only

Pressing a key is client side (use a KeyHandler for it this thread should help http://www.minecraftforge.net/forum/index.php?topic=15960.0)

After that you need to send a packet to the server which infor,ms about the pressed button (this should helphttp://www.minecraftforge.net/forum/index.php/topic,20135.0.html)

Link to comment
Share on other sites

I want to create a method to press a key and break a block (of stone or other) in front of me with my pickaxe.

 

I'm having a little bit of a hard time understanding this sentence. Which of these scenarios are you trying to achieve?

 

a) You press the button. The block instantly breaks, and the pickaxe takes damage.

 

b) You press the button. This causes the player to dig the block until it breaks, like as you would with left clicking.

 

c) Other. Please clarify?

 

EDIT: What Failender mentioned is also very good. You should always be aware of which side you are working on; client or server. Any Minecraft functions are exclusively client side, so calling them on the server will result in a crash. You should avoid using those functions if possible.

Did I help you? Don't be afraid to give that 'thank you' button a good smack for me!

It's been looking at me weirdly for a while now.

Link to comment
Share on other sites

Option B is relatively simple to execute.

 

What I would do is find the KeyBinding of the left click button, and press it.

I'm assuming this is the left click KeyBinding:

KeyBinding bind = FMLClientHandler.instance().getClient().gameSettings.keyBindAttack;

I'm assuming you want to stop once the block is broken. You also have to consider the player looking away from it. I would do a raytrace to see which block the player is looking at, store it's BlockPos, and compare it to the BlockPos the player is currently looking at. I'll let you figure out that part on your own. After, do something like this:

KeyBinding bind = FMLClientHandler.instance().getClient().gameSettings.keyBindForward;
if (!bind.isKeyDown() ) { //Player is not digging, make them start
    KeyBinding.setKeyBindState(bind.getKeyCode(), true);
else if (oldBlockPos != currentBlockPos) {//Player is no longer looking at the block. Either it has been broken, or they lost interest.
    KeyBinding.setKeyBindState(bind.getKeyCode(), false);
}

Keep in mind I haven't tested this, but it should work.

Did I help you? Don't be afraid to give that 'thank you' button a good smack for me!

It's been looking at me weirdly for a while now.

Link to comment
Share on other sites

Ufff well that could be tricky

You will need a TickHandler (google event handling)

On keypress you will need to safe the block the player is looking at and in the Tickhandler simulate the left mouse button until the block is broken.

If you have no idea what I am talking about you probably shouldnt write this

Link to comment
Share on other sites

Yes, getting the "only break that block" done might take a bit of work.

 

If you don't mind me asking @oznecniV97, what do you need this for? If you're only breaking a single block, it might be easier just to manually hold down left click. If you're trying to make a mod to make mining easier, it would be far easier to just toggle left mouse button on and off yourself and not worry about only breaking a single block.

Did I help you? Don't be afraid to give that 'thank you' button a good smack for me!

It's been looking at me weirdly for a while now.

Link to comment
Share on other sites

KeyBinding.setKeyBindState(bind.getKeyCode(), false);

is used to "depress" the button.

 

Essentially, when you press the button by setting it's state to true, it will remain pressed until something depresses it. For example, if you set the state to true, the act of physically using the button will depress it. Otherwise, you have to set the state to false or the button will remain pressed and will continue to break things.

 

This is why you need the TickHandler. You need it to check if the player is still looking at the block to determine if the block has been broken. When the player is no longer looking at the block, you need to set the state to false or the player will keep digging.

 

This thread may be of interest to you.

Did I help you? Don't be afraid to give that 'thank you' button a good smack for me!

It's been looking at me weirdly for a while now.

Link to comment
Share on other sites

Ufff well that could be tricky

You will need a TickHandler (google event handling)

On keypress you will need to safe the block the player is looking at and in the Tickhandler simulate the left mouse button until the block is broken.

If you have no idea what I am talking about you probably shouldnt write this

Link to comment
Share on other sites

A Tick is 1/20 of a second. It is the base operation time in Minecraft. Everything the game is updated once a tick.

A Handler is something that handles things, usually events. (EventHandler, KeyHandler, etc).

Therefore, a TickHandler is a Handler that handles Ticks.

Did I help you? Don't be afraid to give that 'thank you' button a good smack for me!

It's been looking at me weirdly for a while now.

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.