Files
ForgeMultipart/codechicken/multipart/minecraft/EventHandler.java
T

123 lines
4.8 KiB
Java
Raw Normal View History

2013-05-20 17:09:57 +10:00
package codechicken.multipart.minecraft;
import codechicken.lib.packet.PacketCustom;
import codechicken.lib.raytracer.RayTracer;
import codechicken.lib.vec.BlockCoord;
import codechicken.lib.vec.Vector3;
2013-05-20 17:09:57 +10:00
import codechicken.multipart.TileMultipart;
import net.minecraft.block.Block;
2013-08-01 23:30:26 +10:00
import net.minecraft.block.BlockFence;
2013-05-20 17:09:57 +10:00
import net.minecraft.entity.player.EntityPlayer;
2014-04-07 23:14:31 +10:00
import net.minecraft.init.Blocks;
2013-05-20 17:09:57 +10:00
import net.minecraft.item.ItemStack;
2014-04-07 23:14:31 +10:00
import net.minecraft.network.play.client.C08PacketPlayerBlockPlacement;
2013-05-20 17:09:57 +10:00
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
2014-04-07 23:14:31 +10:00
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
2013-05-20 17:09:57 +10:00
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.Action;
public class EventHandler
{
private ThreadLocal<Object> placing = new ThreadLocal<Object>();
2014-04-07 23:14:31 +10:00
@SubscribeEvent
2013-05-20 17:09:57 +10:00
public void playerInteract(PlayerInteractEvent event)
{
if(event.action == Action.RIGHT_CLICK_BLOCK && event.entityPlayer.worldObj.isRemote)
{
if(placing.get() != null)
return;//for mods that do dumb stuff and call this event like MFR
placing.set(event);
if(place(event.entityPlayer, event.entityPlayer.worldObj))
event.setCanceled(true);
placing.set(null);
}
2013-05-20 17:09:57 +10:00
}
public static boolean place(EntityPlayer player, World world)
2013-05-20 17:09:57 +10:00
{
MovingObjectPosition hit = RayTracer.reTrace(world, player);
2013-08-19 08:04:10 +10:00
if(hit == null)
return false;
2013-05-20 17:09:57 +10:00
BlockCoord pos = new BlockCoord(hit.blockX, hit.blockY, hit.blockZ).offset(hit.sideHit);
ItemStack held = player.getHeldItem();
McBlockPart part = null;
2013-05-29 18:28:08 +10:00
if(held == null)
return false;
2014-04-07 23:14:31 +10:00
Block heldBlock = Block.getBlockFromItem(held.getItem());
if(heldBlock == Blocks.air)
return false;
if(heldBlock == Blocks.torch)
2013-05-20 17:09:57 +10:00
part = TorchPart.placement(world, pos, hit.sideHit);
2014-04-07 23:14:31 +10:00
else if(heldBlock == Blocks.lever)
2013-06-02 13:38:59 +10:00
part = LeverPart.placement(world, pos, player, hit.sideHit);
2014-04-07 23:14:31 +10:00
else if(heldBlock == Blocks.stone_button)
2013-06-04 01:47:51 +10:00
part = ButtonPart.placement(world, pos, hit.sideHit, 0);
2014-04-07 23:14:31 +10:00
else if(heldBlock == Blocks.wooden_button)
2013-06-04 01:47:51 +10:00
part = ButtonPart.placement(world, pos, hit.sideHit, 1);
2014-04-07 23:14:31 +10:00
else if(heldBlock == Blocks.redstone_torch)
2013-06-04 01:47:51 +10:00
part = RedstoneTorchPart.placement(world, pos, hit.sideHit);
2013-05-20 17:09:57 +10:00
if(part == null)
return false;
if(world.isRemote && !player.isSneaking())//attempt to use block activated like normal and tell the server the right stuff
2013-05-20 17:09:57 +10:00
{
Vector3 f = new Vector3(hit.hitVec).add(-hit.blockX, -hit.blockY, -hit.blockZ);
2014-04-07 23:14:31 +10:00
Block block = world.getBlock(hit.blockX, hit.blockY, hit.blockZ);
if(!ignoreActivate(block) && block.onBlockActivated(world, hit.blockX, hit.blockY, hit.blockZ, player, hit.sideHit, (float)f.x, (float)f.y, (float)f.z))
2013-05-20 17:09:57 +10:00
{
player.swingItem();
2014-04-07 23:14:31 +10:00
PacketCustom.sendToServer(new C08PacketPlayerBlockPlacement(
2013-05-20 17:09:57 +10:00
hit.blockX, hit.blockY, hit.blockZ, hit.sideHit,
player.inventory.getCurrentItem(),
(float)f.x, (float)f.y, (float)f.z));
return false;
}
}
TileMultipart tile = TileMultipart.getOrConvertTile(world, pos);
if(tile == null || !tile.canAddPart(part))
return false;
if(!world.isRemote)
{
TileMultipart.addPart(world, pos, part);
world.playSoundEffect(pos.x + 0.5, pos.y + 0.5, pos.z + 0.5,
2014-04-07 23:14:31 +10:00
part.getBlock().stepSound.func_150496_b(),
(part.getBlock().stepSound.getVolume() + 1.0F) / 2.0F,
part.getBlock().stepSound.getPitch() * 0.8F);
if(!player.capabilities.isCreativeMode)
{
held.stackSize--;
if (held.stackSize == 0)
{
player.inventory.mainInventory[player.inventory.currentItem] = null;
MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(player, held));
}
2013-05-20 17:09:57 +10:00
}
}
else
{
player.swingItem();
new PacketCustom(McMultipartSPH.channel, 1).sendToServer();
}
return true;
2013-05-20 17:09:57 +10:00
}
2013-08-01 23:30:26 +10:00
/**
* Because vanilla is weird.
*/
private static boolean ignoreActivate(Block block)
{
if(block instanceof BlockFence)
return true;
return false;
}
2013-05-20 17:09:57 +10:00
}