Search in sources :

Example 1 with ILinkableCart

use of mods.railcraft.api.carts.ILinkableCart in project Railcraft by Railcraft.

the class CrowbarHandler method onEntityInteract.

@SubscribeEvent
public void onEntityInteract(MinecartInteractEvent event) {
    EntityPlayer thePlayer = event.getPlayer();
    Entity entity = event.getEntity();
    EnumHand hand = event.getHand();
    if (event.getItem() != null && event.getItem().getItem() instanceof IToolCrowbar)
        event.setCanceled(true);
    ItemStack stack = event.getItem();
    if (!InvTools.isEmpty(stack) && stack.getItem() instanceof IToolCrowbar) {
        thePlayer.swingArm(event.getHand());
        event.setCanceled(true);
    } else
        return;
    if (Game.isClient(thePlayer.worldObj))
        return;
    boolean used = false;
    IToolCrowbar crowbar = (IToolCrowbar) stack.getItem();
    if (entity instanceof EntityMinecart) {
        EntityMinecart cart = (EntityMinecart) entity;
        if (RailcraftModuleManager.isModuleEnabled(ModuleTrain.class) && crowbar.canLink(thePlayer, hand, stack, cart)) {
            boolean linkable = cart instanceof ILinkableCart;
            if (!linkable || ((ILinkableCart) cart).isLinkable()) {
                EntityMinecart last = linkMap.remove(thePlayer);
                if (last != null && last.isEntityAlive()) {
                    LinkageManager lm = LinkageManager.instance();
                    if (lm.areLinked(cart, last, false)) {
                        lm.breakLink(cart, last);
                        used = true;
                        ChatPlugin.sendLocalizedChatFromServer(thePlayer, "gui.railcraft.link.broken");
                        LinkageManager.printDebug("Reason For Broken Link: User removed link.");
                    } else {
                        used = lm.createLink(last, (EntityMinecart) entity);
                        if (used)
                            ChatPlugin.sendLocalizedChatFromServer(thePlayer, "gui.railcraft.link.created");
                    }
                    if (!used)
                        ChatPlugin.sendLocalizedChatFromServer(thePlayer, "gui.railcraft.link.failed");
                } else {
                    linkMap.put(thePlayer, (EntityMinecart) entity);
                    ChatPlugin.sendLocalizedChatFromServer(thePlayer, "gui.railcraft.link.started");
                }
            }
            if (used)
                crowbar.onLink(thePlayer, hand, stack, cart);
        } else if (crowbar.canBoost(thePlayer, hand, stack, cart)) {
            thePlayer.addExhaustion(1F);
            //noinspection StatementWithEmptyBody
            if (thePlayer.getRidingEntity() != null) {
            // NOOP
            } else //noinspection StatementWithEmptyBody
            if (cart instanceof EntityTunnelBore) {
            // NOOP
            } else if (cart instanceof IDirectionalCart)
                ((IDirectionalCart) cart).reverse();
            else {
                int lvl = RailcraftEnchantments.SMACK.getLevel(stack);
                if (lvl == 0) {
                    CartTools.smackCart(cart, thePlayer, SMACK_VELOCITY);
                }
                float smackVelocity = SMACK_VELOCITY * (float) Math.pow(1.7, lvl);
                Train train = Train.getTrain(cart);
                smackVelocity /= (float) Math.pow(train.size(), 1D / (1 + lvl));
                for (EntityMinecart each : train) {
                    CartTools.smackCart(cart, each, thePlayer, smackVelocity);
                }
            }
            crowbar.onBoost(thePlayer, hand, stack, cart);
        }
    }
}
Also used : Entity(net.minecraft.entity.Entity) ILinkableCart(mods.railcraft.api.carts.ILinkableCart) IToolCrowbar(mods.railcraft.api.core.items.IToolCrowbar) ModuleTrain(mods.railcraft.common.modules.ModuleTrain) EntityMinecart(net.minecraft.entity.item.EntityMinecart) EnumHand(net.minecraft.util.EnumHand) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ItemStack(net.minecraft.item.ItemStack) ModuleTrain(mods.railcraft.common.modules.ModuleTrain) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with ILinkableCart

use of mods.railcraft.api.carts.ILinkableCart in project Railcraft by Railcraft.

the class LinkageManager method breakLink.

@Nullable
private EntityMinecart breakLink(EntityMinecart cart, LinkType linkType) {
    Train.deleteTrain(cart);
    UUID link = getLink(cart, linkType);
    removeLinkTags(cart, linkType);
    EntityMinecart other = CartTools.getCartFromUUID(cart.worldObj, link);
    if (other != null) {
        breakLink(other, cart);
    }
    if (cart instanceof ILinkableCart)
        ((ILinkableCart) cart).onLinkBroken(other);
    printDebug("Carts {0}({1}) and {2}({3}) unlinked ({4}).", getLinkageId(cart), cart, link, other != null ? other : "null", linkType.name());
    return other;
}
Also used : ILinkableCart(mods.railcraft.api.carts.ILinkableCart) UUID(java.util.UUID) EntityMinecart(net.minecraft.entity.item.EntityMinecart) Nullable(javax.annotation.Nullable)

Aggregations

ILinkableCart (mods.railcraft.api.carts.ILinkableCart)2 EntityMinecart (net.minecraft.entity.item.EntityMinecart)2 UUID (java.util.UUID)1 Nullable (javax.annotation.Nullable)1 IToolCrowbar (mods.railcraft.api.core.items.IToolCrowbar)1 ModuleTrain (mods.railcraft.common.modules.ModuleTrain)1 Entity (net.minecraft.entity.Entity)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 ItemStack (net.minecraft.item.ItemStack)1 EnumHand (net.minecraft.util.EnumHand)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1