Jump to content

[1.8] [90% SOLVED] Cannot Interact With CustomFire E.G. Hit to Extinguish


saxon564

Recommended Posts

KeyBinding#unpressKey() is private, so you should use reflection to get reach to it.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

  • Replies 70
  • Created
  • Last Reply

Top Posters In This Topic

KeyBinding#unpressKey() is private, so you should use reflection to get reach to it.

I will admit, I have never looked into reflection before and have no idea how to do it. So far what I have found about it, shows, but doesnt really explain really well.

Link to comment
Share on other sites

You can also check KeyBinding#isPressed() to see if the key was pressed - this will decrement the pressed value (which should only have been at 1 since it was just pressed), then unset it with setKeyBindState as you were doing.

 

Another thing to keep in mind is that the KeyInputEvent fires TWICE for each key press: once when the key is pressed (that's the one you want to intercept), and once again when it is released. Use Keyboard#getEventKeyState to check which state (press or release) the key is in.

 

The trouble, I think, may be that KeyInputEvent, since it is not designed to be cancelable, fires too late to stop the client's action from sending a packet to the server, and when that packet is received, it no longer cares about the state of any client-side input. You may have to take some more drastic measures and attempt to intercept this packet, or just call it 'good enough' to expect most users to be using the LMB (mouse) to attack, and cancel the MouseEvent when appropriate.

 

At least that way, you can move on to other things, then come back to this issue if users of your mod complain about it (very few people don't use the mouse, but they do exist...).

Link to comment
Share on other sites

I do have it setup to run when the key is pressed and not released.

 

EDIT: I set it up so that if someone is using a keyboard press to try to get rid of the block it will say "Please contact saxon564 if you are trying to put out a fire using a key on your keyboard as your attack key." This will help show me how many people actually use their keyboard.

 

Now I just need to get the packet sending all figured out and get the mouse input to run on the server.

Link to comment
Share on other sites

I meant to send the packet, which with the wording I used would still technically be correct, but its all on interpretation there.

 

EDIT:

I have gotten everything working right with the mouse. You can see the final code here for the event handler and here for the packet management

 

EDIT2: I have determined, the only way this would work all around would be using the PlayerInteractEvent, but I would have to translate it to a client event and send it as a packet to the client to cancel it. Just some final thoughts about it. Im probably just going to stick with what I already have for now.

Link to comment
Share on other sites

Here's my code:

 

package common.zeroquest.events;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.client.event.MouseEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent;

import common.zeroquest.ModBlocks;

public class InteractiveEvents {

@SubscribeEvent
public void onMouseEvent(MouseEvent event) {
	int button = event.button;
	EntityPlayer player = Minecraft.getMinecraft().thePlayer;
	World world = Minecraft.getMinecraft().theWorld;

	BlockPos pos = Minecraft.getMinecraft().objectMouseOver.getBlockPos();
	EnumFacing face = Minecraft.getMinecraft().objectMouseOver.sideHit;

	if (button == 0) {
		if (world.getBlockState(pos).getBlock() != null) {
			this.extinguishFire(player, pos, face, world, event);
		}
	}
}

@SubscribeEvent
public void onPlayerMovement(PlayerTickEvent event) {
	EntityPlayer player = event.player;
	BlockPos pos = player.getPosition();
	World world = player.worldObj;
	Block block = world.getBlockState(pos).getBlock();
	if (((block == ModBlocks.nileFire) || (world.getBlockState(pos.up()).getBlock() == ModBlocks.nileFire)) && (!player.capabilities.isCreativeMode)) {
		player.setFire(;
	}

	else if (((block == ModBlocks.darkFire) || (world.getBlockState(pos.up()).getBlock() == ModBlocks.darkFire)) && (!player.capabilities.isCreativeMode)) {
		player.setFire(;
	}
}

private void extinguishFire(EntityPlayer player, BlockPos pos, EnumFacing face, World world, MouseEvent event) {
	pos = pos.offset(face);

        int i = pos.getX();
        int j = pos.getY();
        int k = pos.getZ();
	if (world.getBlockState(pos).getBlock() == ModBlocks.nileFire) {
            world.playSoundEffect((double)((float)i + 0.5F), (double)((float)j + 0.5F), (double)((float)k + 0.5F), "random.fizz", 0.5F, 2.6F + (world.rand.nextFloat() - world.rand.nextFloat()) * 0.8F);
		world.setBlockToAir(pos);
		event.setCanceled(true);
	}
	else if (world.getBlockState(pos).getBlock() == ModBlocks.darkFire) {
            world.playSoundEffect((double)((float)i + 0.5F), (double)((float)j + 0.5F), (double)((float)k + 0.5F), "random.fizz", 0.5F, 2.6F + (world.rand.nextFloat() - world.rand.nextFloat()) * 0.8F);
		world.setBlockToAir(pos);
		event.setCanceled(true);
	}

}
}

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Link to comment
Share on other sites

Everything you have there is only happening on the client that did the action. the server does not update the block.

 

EDIT: You also need to check for pos to be null, because if you hit an entity, pos is null and it will crash.

Link to comment
Share on other sites

My code now

 

package common.zeroquest.events;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.client.event.MouseEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent;

import common.zeroquest.ModBlocks;

public class InteractiveEvents {

@SubscribeEvent
public void onMouseEvent(MouseEvent event) {
	int button = event.button;
	EntityPlayer player = Minecraft.getMinecraft().thePlayer;
	World world = Minecraft.getMinecraft().theWorld;

	BlockPos pos = Minecraft.getMinecraft().objectMouseOver.getBlockPos();
	EnumFacing face = Minecraft.getMinecraft().objectMouseOver.sideHit;

	if (button == 0) {
		if (world.getBlockState(pos).getBlock() != null) {
			this.extinguishFire(player, pos, face, world, event);
		}
	}
}

@SubscribeEvent
public void onPlayerMovement(PlayerTickEvent event) {
	EntityPlayer player = event.player;
	BlockPos pos = player.getPosition();
	World world = player.worldObj;
	Block block = world.getBlockState(pos).getBlock();
	if (pos != null) {
		if (((block == ModBlocks.nileFire) || (world.getBlockState(pos.up()).getBlock() == ModBlocks.nileFire)) && (!player.capabilities.isCreativeMode)) {
			player.setFire(;
		}

		else if (((block == ModBlocks.darkFire) || (world.getBlockState(pos.up()).getBlock() == ModBlocks.darkFire)) && (!player.capabilities.isCreativeMode)) {
			player.setFire(;
		}
	}
}

private void extinguishFire(EntityPlayer player, BlockPos pos, EnumFacing face, World world, MouseEvent event) {
	pos = pos.offset(face);

	int i = pos.getX();
	int j = pos.getY();
	int k = pos.getZ();
	if (pos != null) {
		if (world.getBlockState(pos).getBlock() == ModBlocks.nileFire) {
			world.playSoundEffect(i + 0.5F, j + 0.5F, k + 0.5F, "random.fizz", 0.5F, 2.6F + (world.rand.nextFloat() - world.rand.nextFloat()) * 0.8F);
			world.setBlockToAir(pos);
			event.setCanceled(true);
		}
		else if (world.getBlockState(pos).getBlock() == ModBlocks.darkFire) {
			world.playSoundEffect(i + 0.5F, j + 0.5F, k + 0.5F, "random.fizz", 0.5F, 2.6F + (world.rand.nextFloat() - world.rand.nextFloat()) * 0.8F);
			world.setBlockToAir(pos);
			event.setCanceled(true);
		}
	}

}
}

 

I'm still not hearing the fizz sfx when I extinguish the fire

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Link to comment
Share on other sites

For pos check it in the MouseEvent, thats where it has a possibility to be null, as for the fizz, you have to use packets to send the information to the server, on the server is when you play the fizz sound. You can look at my github for how I did it.

Link to comment
Share on other sites

package common.zeroquest.events;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.client.event.MouseEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent;
import common.zeroquest.ModBlocks;
import common.zeroquest.network.PacketHandler;
import common.zeroquest.network.imessage.FireSound;

public class InteractiveEvents {

@SubscribeEvent
public void onMouseEvent(MouseEvent event) {
	int button = event.button;
	EntityPlayer player = Minecraft.getMinecraft().thePlayer;
	World world = Minecraft.getMinecraft().theWorld;

	BlockPos pos = Minecraft.getMinecraft().objectMouseOver.getBlockPos();
	EnumFacing face = Minecraft.getMinecraft().objectMouseOver.sideHit;

	if (button == 0) {
		if (pos != null) {
			if (world.getBlockState(pos).getBlock() != null) {
				this.extinguishFire(player, pos, face, world, event);
			}
		}
	}
}

@SubscribeEvent
public void onPlayerMovement(PlayerTickEvent event) {
	EntityPlayer player = event.player;
	BlockPos pos = player.getPosition();
	World world = player.worldObj;
	Block block = world.getBlockState(pos).getBlock();
	if (((block == ModBlocks.nileFire) || (world.getBlockState(pos.up()).getBlock() == ModBlocks.nileFire)) && (!player.capabilities.isCreativeMode)) {
		player.setFire(;
	}

	else if (((block == ModBlocks.darkFire) || (world.getBlockState(pos.up()).getBlock() == ModBlocks.darkFire)) && (!player.capabilities.isCreativeMode)) {
		player.setFire(;
	}
}

private void extinguishFire(EntityPlayer player, BlockPos pos, EnumFacing face, World world, MouseEvent event) {
	pos = pos.offset(face);

	int i = pos.getX();
	int j = pos.getY();
	int k = pos.getZ();
	if (world.getBlockState(pos).getBlock() == ModBlocks.nileFire) {
		world.setBlockToAir(pos);
		event.setCanceled(true);
		PacketHandler.sendToServer(new FireSound());
	}
	else if (world.getBlockState(pos).getBlock() == ModBlocks.darkFire) {
		world.setBlockToAir(pos);
		event.setCanceled(true);
		PacketHandler.sendToServer(new FireSound());
	}
}
}

 

package common.zeroquest.network.imessage;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class FireSound implements IMessage {

private String text;
private String player;
private int face;

public FireSound() {}

public FireSound(EntityPlayer playerIn, EnumFacing faceIn) {
	player = playerIn.toString();
	face = faceIn.getIndex();
}

@Override
public void fromBytes(ByteBuf buf) {
	player = ByteBufUtils.readUTF8String(buf);
	face = ByteBufUtils.readVarInt(buf, 5);
}

@Override
public void toBytes(ByteBuf buf) {
	ByteBufUtils.writeUTF8String(buf, player);
	ByteBufUtils.writeVarInt(buf, face, 5);
}

public static class Handler implements IMessageHandler<FireSound, IMessage> {

	@Override
	public IMessage onMessage(FireSound message, MessageContext ctx) {
		EntityPlayer player = ctx.getServerHandler().playerEntity;
		World world = player.worldObj;
		System.out.println(String.format("Received %s from %s", message.text, ctx.getServerHandler().playerEntity.getDisplayName()));
		world.playSoundAtEntity(player, "random.fizz", player.getPosition().getX(), player.getPosition().getY());
		return null;
	}
}
}

 

Still not working

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Link to comment
Share on other sites

If you're setting the block to air on the server and playing the sound in your Packet class, as well as canceling the event in your event handler, you're done. That's all you need to do, it will work on both server and client.

Link to comment
Share on other sites

Hi. I saw this thread. In World.java on line 2430

 

Changing if (this.getBlockState(pos).getBlock() == Blocks.fire) >> if (this.getBlockState(pos).getBlock() instanceof net.minecraft.block.BlockFire)

 

I think should be fix with another mod. And no conflict (Maybe. -.-)

Link to comment
Share on other sites

Hi. I saw this thread. In World.java on line 2430

 

Changing if (this.getBlockState(pos).getBlock() == Blocks.fire) >> if (this.getBlockState(pos).getBlock() instanceof net.minecraft.block.BlockFire)

 

I think should be fix with another mod. And no conflict (Maybe. -.-)

That would be a simple solution, yes. But there is no reason you should ever edit a base class. If you find yourself trying to edit a base class to do something, then you need a core mod. If you dont know how to make a core mod, then you should just stop trying what you are doing and go learn how to, or ask if there is another way to accomplish it.

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.