2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
package appeng.fmp;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.BlockFence;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.network.play.client.C08PacketPlayerBlockPlacement;
|
|
|
|
import net.minecraft.util.MovingObjectPosition;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent.Action;
|
2014-12-29 21:59:05 +01:00
|
|
|
|
|
|
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import codechicken.lib.packet.PacketCustom;
|
|
|
|
import codechicken.lib.raytracer.RayTracer;
|
|
|
|
import codechicken.lib.vec.BlockCoord;
|
|
|
|
import codechicken.lib.vec.Vector3;
|
|
|
|
import codechicken.multipart.TMultiPart;
|
|
|
|
import codechicken.multipart.TileMultipart;
|
2014-12-29 21:59:05 +01:00
|
|
|
|
|
|
|
import appeng.block.AEBaseItemBlock;
|
|
|
|
import appeng.core.sync.network.NetworkHandler;
|
|
|
|
import appeng.core.sync.packets.PacketMultiPart;
|
|
|
|
import appeng.integration.modules.helpers.FMPPacketEvent;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
/**
|
|
|
|
* Basically a total rip of of the FMP version for vanilla, seemed to work well enough...
|
|
|
|
*/
|
|
|
|
public class FMPEvent
|
|
|
|
{
|
|
|
|
|
2014-09-29 09:54:34 +02:00
|
|
|
private final ThreadLocal<Object> placing = new ThreadLocal<Object>();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
@SubscribeEvent
|
2015-09-25 23:10:56 +02:00
|
|
|
public void ServerFMPEvent( final FMPPacketEvent event )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
FMPEvent.place( event.sender, event.sender.worldObj );
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public static boolean place( final EntityPlayer player, final World world )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final MovingObjectPosition hit = RayTracer.reTrace( world, player );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( hit == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final BlockCoord pos = new BlockCoord( hit.blockX, hit.blockY, hit.blockZ ).offset( hit.sideHit );
|
|
|
|
final ItemStack held = player.getHeldItem();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( held == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 22:52:41 +02:00
|
|
|
Block blk = null;
|
|
|
|
TMultiPart part = null;
|
2015-04-03 08:54:31 +02:00
|
|
|
if( held.getItem() instanceof AEBaseItemBlock )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final AEBaseItemBlock ib = (AEBaseItemBlock) held.getItem();
|
2014-09-24 02:26:27 +02:00
|
|
|
blk = Block.getBlockFromItem( ib );
|
|
|
|
part = PartRegistry.getPartByBlock( blk, hit.sideHit );
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( part == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( world.isRemote && !player.isSneaking() )// attempt to use block activated like normal and tell the server
|
|
|
|
// the right stuff
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final Vector3 f = new Vector3( hit.hitVec ).add( -hit.blockX, -hit.blockY, -hit.blockZ );
|
|
|
|
final Block block = world.getBlock( hit.blockX, hit.blockY, hit.blockZ );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( block != null && !ignoreActivate( block ) && block.onBlockActivated( world, hit.blockX, hit.blockY, hit.blockZ, player, hit.sideHit, (float) f.x, (float) f.y, (float) f.z ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
player.swingItem();
|
2015-04-03 08:54:31 +02:00
|
|
|
PacketCustom.sendToServer( new C08PacketPlayerBlockPlacement( hit.blockX, hit.blockY, hit.blockZ, hit.sideHit, player.inventory.getCurrentItem(), (float) f.x, (float) f.y, (float) f.z ) );
|
2014-09-24 02:26:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final TileMultipart tile = TileMultipart.getOrConvertTile( world, pos );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( tile == null || !tile.canAddPart( part ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !world.isRemote )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
TileMultipart.addPart( world, pos, part );
|
2015-04-03 08:54:31 +02:00
|
|
|
world.playSoundEffect( pos.x + 0.5, pos.y + 0.5, pos.z + 0.5, blk.stepSound.func_150496_b(), ( blk.stepSound.getVolume() + 1.0F ) / 2.0F, blk.stepSound.getPitch() * 0.8F );
|
|
|
|
if( !player.capabilities.isCreativeMode )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
held.stackSize--;
|
2015-04-03 08:54:31 +02:00
|
|
|
if( held.stackSize == 0 )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
player.inventory.mainInventory[player.inventory.currentItem] = null;
|
|
|
|
MinecraftForge.EVENT_BUS.post( new PlayerDestroyItemEvent( player, held ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
player.swingItem();
|
2014-10-04 08:08:28 +02:00
|
|
|
NetworkHandler.instance.sendToServer( new PacketMultiPart() );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Because vanilla is weird.
|
|
|
|
*/
|
2015-09-25 23:10:56 +02:00
|
|
|
private static boolean ignoreActivate( final Block block )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( block instanceof BlockFence )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return true;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
@SubscribeEvent
|
2015-09-25 23:10:56 +02:00
|
|
|
public void playerInteract( final PlayerInteractEvent event )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
if( event.action == Action.RIGHT_CLICK_BLOCK && event.entityPlayer.worldObj.isRemote )
|
|
|
|
{
|
|
|
|
if( this.placing.get() != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
this.placing.set( event );
|
|
|
|
if( place( event.entityPlayer, event.entityPlayer.worldObj ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
event.setCanceled( true );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
this.placing.set( null );
|
|
|
|
}
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|