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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Add crash-reports with sites like https://paste.ee/ Maybe an issue with blur, essentials or cumulus_menus
    • Add the crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here  
    • I have a problem, I am trying to put two different effects to two different armors but when I run it only the emerald armor effect works. This is the code public class ModArmorItem extends ArmorItem{ private static final Map<ArmorMaterial, MobEffectInstance> MATERIAL_TO_EFFECT_MAP = (new ImmutableMap.Builder<ArmorMaterial, MobEffectInstance>()) .put(ModArmorMaterials.EMERALD, new MobEffectInstance(MobEffects.HERO_OF_THE_VILLAGE,200, 1,false,false, true)) .put(ModArmorMaterials.OBSIDIAN, new MobEffectInstance(MobEffects.FIRE_RESISTANCE,200, 1,false,false, true)).build(); public ModArmorItem(ArmorMaterial pMaterial, Type pType, Properties pProperties) { super(pMaterial, pType, pProperties); } @Override public void onArmorTick(ItemStack stack, Level world, Player player){ if (!world.isClientSide()) { if (hasFullSuitOfArmorOn(player)) { evaluateArmorEffects(player); } } } private void evaluateArmorEffects(Player player) { for (Map.Entry<ArmorMaterial,MobEffectInstance> entry : MATERIAL_TO_EFFECT_MAP.entrySet()){ ArmorMaterial mapArmorMaterial = entry.getKey(); MobEffectInstance mapStatusEffect = entry.getValue(); if (hasCorrectArmorOn(mapArmorMaterial, player)) { addStatusEffectForMaterial(player, mapArmorMaterial, mapStatusEffect); } } } private void addStatusEffectForMaterial(Player player, ArmorMaterial mapArmorMaterial, MobEffectInstance mapStatusEffect) { boolean hasPlayerEffect = player.hasEffect(mapStatusEffect.getEffect()); if (hasCorrectArmorOn(mapArmorMaterial, player) && !hasPlayerEffect) { player.addEffect(new MobEffectInstance(mapStatusEffect)); } } private boolean hasCorrectArmorOn(ArmorMaterial material, Player player) { for (ItemStack armorStack : player.getInventory().armor){ if (!(armorStack.getItem() instanceof ArmorItem)) { return false; } } ArmorItem helmet = ((ArmorItem)player.getInventory().getArmor(3).getItem()); ArmorItem breastplace = ((ArmorItem)player.getInventory().getArmor(2).getItem()); ArmorItem leggins = ((ArmorItem)player.getInventory().getArmor(1).getItem()); ArmorItem boots = ((ArmorItem)player.getInventory().getArmor(0).getItem()); return helmet.getMaterial() == material && breastplace.getMaterial() == material && leggins.getMaterial() == material && boots.getMaterial() == material; } private boolean hasFullSuitOfArmorOn(Player player){ ItemStack helmet = player.getInventory().getArmor(3); ItemStack breastplace = player.getInventory().getArmor(2); ItemStack leggins = player.getInventory().getArmor(1); ItemStack boots = player.getInventory().getArmor(0); return !helmet.isEmpty() && !breastplace.isEmpty() && !leggins.isEmpty() && !boots.isEmpty(); } } Also when I place two effects on the same armor, the game crashes. Here is the crash file. The code is the same, only this part is different   private static final Map<ArmorMaterial, MobEffectInstance> MATERIAL_TO_EFFECT_MAP = (new ImmutableMap.Builder<ArmorMaterial, MobEffectInstance>()) .put(ModArmorMaterials.EMERALD, new MobEffectInstance(MobEffects.HERO_OF_THE_VILLAGE,200, 1,false,false, true)) .put(ModArmorMaterials.EMERALD, new MobEffectInstance(MobEffects.FIRE_RESISTANCE,200, 1,false,false, true)).build(); I hope you guys can help me. Thanks.
  • Topics

×
×
  • Create New...

Important Information

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