TechReborn/ToAddBack/partSystem/fmp/CableConverter.java

135 lines
5.7 KiB
Java
Raw Normal View History

package techreborn.partSystem.fmp;
2015-10-23 17:04:56 +02:00
import codechicken.lib.packet.PacketCustom;
import codechicken.lib.raytracer.RayTracer;
import codechicken.lib.vec.BlockCoord;
2015-10-23 17:04:56 +02:00
import codechicken.lib.vec.Vector3;
import codechicken.multipart.MultiPartRegistry;
import codechicken.multipart.TMultiPart;
2015-10-23 17:04:56 +02:00
import codechicken.multipart.TileMultipart;
2015-11-23 15:40:30 +01:00
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import ic2.api.item.IC2Items;
import ic2.core.block.wiring.BlockCable;
import ic2.core.block.wiring.TileEntityCable;
import net.minecraft.block.Block;
2015-10-23 17:04:56 +02:00
import net.minecraft.block.BlockFence;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.play.client.C08PacketPlayerBlockPlacement;
import net.minecraft.tileentity.TileEntity;
2015-10-23 17:04:56 +02:00
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
2015-10-23 17:04:56 +02:00
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import reborncore.common.packets.AddDiscriminatorEvent;
import reborncore.common.packets.PacketHandler;
import techreborn.partSystem.parts.CablePart;
import java.util.Arrays;
public class CableConverter implements MultiPartRegistry.IPartConverter {
@Override
public Iterable<Block> blockTypes() {
return Arrays.asList(Block.getBlockFromItem(IC2Items.getItem("copperCableBlock").getItem()));
}
@Override
public TMultiPart convert(World world, BlockCoord blockCoord) {
Block block = world.getBlock(blockCoord.x, blockCoord.y, blockCoord.z);
if (block instanceof BlockCable) {
TileEntity tileEntity = world.getTileEntity(blockCoord.x, blockCoord.y, blockCoord.z);
if (tileEntity instanceof TileEntityCable) {
TileEntityCable cable = (TileEntityCable) tileEntity;
int type = cable.cableType;
CablePart newPart = new CablePart();
newPart.setType(type);
return new FMPModPart(newPart);
}
}
return null;
}
2015-10-23 17:04:56 +02:00
private final ThreadLocal<Object> placing = new ThreadLocal<Object>();
2015-10-23 17:04:56 +02:00
@SubscribeEvent(priority = EventPriority.LOW)
2015-11-08 13:15:45 +01:00
public void playerInteract(PlayerInteractEvent event) {
if (event.action == PlayerInteractEvent.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
2015-10-23 17:04:56 +02:00
placing.set(event);
if (place(event.entityPlayer, event.entityPlayer.worldObj)) event.setCanceled(true);
2015-10-23 17:04:56 +02:00
placing.set(null);
}
}
2015-11-08 13:15:45 +01:00
public static boolean place(EntityPlayer player, World world) {
2015-10-23 17:04:56 +02:00
MovingObjectPosition hit = RayTracer.reTrace(world, player);
if (hit == null) return false;
2015-10-23 17:04:56 +02:00
BlockCoord pos = new BlockCoord(hit.blockX, hit.blockY, hit.blockZ);
2015-10-23 17:04:56 +02:00
ItemStack held = player.getHeldItem();
2015-10-23 17:04:56 +02:00
FMPModPart part = null;
if (held == null) return false;
2015-10-23 17:04:56 +02:00
Item heldItem = held.getItem();
if (heldItem == IC2Items.getItem("copperCableItem").getItem()) {
CablePart cablePart = new CablePart();
cablePart.setType(held.getItemDamage());
part = new FMPModPart(cablePart);
}
2015-10-23 17:04:56 +02:00
if (part == null) return false;
2015-10-23 17:04:56 +02:00
2015-11-08 13:15:45 +01:00
if (world.isRemote && !player.isSneaking())//attempt to use block activated like normal and tell the server the right stuff
2015-10-23 17:04:56 +02:00
{
Vector3 f = new Vector3(hit.hitVec).add(-hit.blockX, -hit.blockY, -hit.blockZ);
Block block = world.getBlock(hit.blockX, hit.blockY, hit.blockZ);
2015-11-08 13:15:45 +01:00
if (!ignoreActivate(block) && block.onBlockActivated(world, hit.blockX, hit.blockY, hit.blockZ, player, hit.sideHit, (float) f.x, (float) f.y, (float) f.z)) {
2015-10-23 17:04:56 +02:00
player.swingItem();
PacketCustom.sendToServer(new C08PacketPlayerBlockPlacement(hit.blockX, hit.blockY, hit.blockZ, hit.sideHit, player.inventory.getCurrentItem(), (float) f.x, (float) f.y, (float) f.z));
2015-10-23 17:04:56 +02:00
return true;
}
}
TileMultipart tile = TileMultipart.getOrConvertTile(world, pos);
if (tile == null || !tile.canAddPart(part)) {
pos = pos.offset(hit.sideHit);
tile = TileMultipart.getOrConvertTile(world, pos);
if (tile == null || !tile.canAddPart(part)) return false;
}
2015-10-23 17:04:56 +02:00
2015-11-08 13:15:45 +01:00
if (!world.isRemote) {
2015-10-23 17:04:56 +02:00
TileMultipart.addPart(world, pos, part);
world.playSoundEffect(pos.x + 0.5, pos.y + 0.5, pos.z + 0.5, Blocks.wool.stepSound.func_150496_b(), (Blocks.wool.stepSound.getVolume() + 1.0F) / 2.0F, Blocks.wool.stepSound.getPitch() * 0.8F);
2015-11-08 13:15:45 +01:00
if (!player.capabilities.isCreativeMode) {
2015-10-23 17:04:56 +02:00
held.stackSize--;
2015-11-08 13:15:45 +01:00
if (held.stackSize == 0) {
2015-10-23 17:04:56 +02:00
player.inventory.mainInventory[player.inventory.currentItem] = null;
MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(player, held));
}
}
2015-11-08 13:15:45 +01:00
} else {
2015-10-23 17:04:56 +02:00
player.swingItem();
PacketHandler.sendPacketToServer(new PacketFMPPlacePart());
2015-10-23 17:04:56 +02:00
}
return true;
}
/**
* Because vanilla is weird.
*/
2015-11-08 13:15:45 +01:00
private static boolean ignoreActivate(Block block) {
if (block instanceof BlockFence) return true;
2015-10-23 17:04:56 +02:00
return false;
}
@SubscribeEvent
public void addDiscriminator(AddDiscriminatorEvent event) {
event.getPacketHandler().addDiscriminator(event.getPacketHandler().nextDiscriminator, PacketFMPPlacePart.class);
2015-10-23 17:04:56 +02:00
}
}