Search in sources :

Example 1 with EnumActionResult

use of net.minecraft.util.EnumActionResult in project Railcraft by Railcraft.

the class ItemChargeMeter method onItemUseFirst.

@Override
public EnumActionResult onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
    if (Game.isClient(world))
        return EnumActionResult.PASS;
    EnumActionResult returnValue = EnumActionResult.PASS;
    ChargeNetwork.ChargeNode node = ChargeManager.getNetwork(world).getNode(pos);
    if (!node.isNull() && !node.isGraphNull()) {
        sendChat(player, "gui.railcraft.charge.meter.start", SECONDS_TO_RECORD);
        node.startRecordingUsage(SECONDS_TO_RECORD * 20, (n, avg) -> {
            ChargeNetwork.ChargeGraph graph = n.getChargeGraph();
            sendChat(player, "gui.railcraft.charge.meter.network", graph.size(), graph.isInfinite() ? "INF" : graph.getCharge(), graph.getAverageUsagePerTick(), graph.getMaintenanceCost());
            if (n.getBattery() == null)
                sendChat(player, "gui.railcraft.charge.meter.node", avg, n.getChargeDef().getMaintenanceCost());
            else {
                boolean infiniteBat = n.getBattery().isInfinite();
                sendChat(player, "gui.railcraft.charge.meter.producer", infiniteBat ? "INF" : n.getBattery().getCharge(), infiniteBat ? "INF" : 0.0);
            }
        });
        returnValue = EnumActionResult.SUCCESS;
    }
    //        }
    return returnValue;
}
Also used : ChargeNetwork(mods.railcraft.common.blocks.charge.ChargeNetwork) EnumActionResult(net.minecraft.util.EnumActionResult)

Example 2 with EnumActionResult

use of net.minecraft.util.EnumActionResult in project MinecraftForge by MinecraftForge.

the class ForgeHooks method onPlaceItemIntoWorld.

public static EnumActionResult onPlaceItemIntoWorld(@Nonnull ItemStack itemstack, @Nonnull EntityPlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing side, float hitX, float hitY, float hitZ, @Nonnull EnumHand hand) {
    // handle all placement events here
    int meta = itemstack.getItemDamage();
    int size = itemstack.getCount();
    NBTTagCompound nbt = null;
    if (itemstack.getTagCompound() != null) {
        nbt = (NBTTagCompound) itemstack.getTagCompound().copy();
    }
    if (// if not bucket
    !(itemstack.getItem() instanceof ItemBucket)) {
        world.captureBlockSnapshots = true;
    }
    EnumActionResult ret = itemstack.getItem().onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ);
    world.captureBlockSnapshots = false;
    if (ret == EnumActionResult.SUCCESS) {
        // save new item data
        int newMeta = itemstack.getItemDamage();
        int newSize = itemstack.getCount();
        NBTTagCompound newNBT = null;
        if (itemstack.getTagCompound() != null) {
            newNBT = (NBTTagCompound) itemstack.getTagCompound().copy();
        }
        net.minecraftforge.event.world.BlockEvent.PlaceEvent placeEvent = null;
        @SuppressWarnings("unchecked") List<net.minecraftforge.common.util.BlockSnapshot> blockSnapshots = (List<BlockSnapshot>) world.capturedBlockSnapshots.clone();
        world.capturedBlockSnapshots.clear();
        // make sure to set pre-placement item data for event
        itemstack.setItemDamage(meta);
        itemstack.setCount(size);
        if (nbt != null) {
            itemstack.setTagCompound(nbt);
        }
        if (blockSnapshots.size() > 1) {
            placeEvent = ForgeEventFactory.onPlayerMultiBlockPlace(player, blockSnapshots, side, hand);
        } else if (blockSnapshots.size() == 1) {
            placeEvent = ForgeEventFactory.onPlayerBlockPlace(player, blockSnapshots.get(0), side, hand);
        }
        if (placeEvent != null && (placeEvent.isCanceled())) {
            // cancel placement
            ret = EnumActionResult.FAIL;
            // revert back all captured blocks
            for (net.minecraftforge.common.util.BlockSnapshot blocksnapshot : blockSnapshots) {
                world.restoringBlockSnapshots = true;
                blocksnapshot.restore(true, false);
                world.restoringBlockSnapshots = false;
            }
        } else {
            // Change the stack to its new content
            itemstack.setItemDamage(newMeta);
            itemstack.setCount(newSize);
            if (nbt != null) {
                itemstack.setTagCompound(newNBT);
            }
            for (BlockSnapshot snap : blockSnapshots) {
                int updateFlag = snap.getFlag();
                IBlockState oldBlock = snap.getReplacedBlock();
                IBlockState newBlock = world.getBlockState(snap.getPos());
                if (// Containers get placed automatically
                !newBlock.getBlock().hasTileEntity(newBlock)) {
                    newBlock.getBlock().onBlockAdded(world, snap.getPos(), newBlock);
                }
                world.markAndNotifyBlock(snap.getPos(), null, oldBlock, newBlock, updateFlag);
            }
            player.addStat(StatList.getObjectUseStats(itemstack.getItem()));
        }
    }
    world.capturedBlockSnapshots.clear();
    return ret;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) BlockSnapshot(net.minecraftforge.common.util.BlockSnapshot) BlockSnapshot(net.minecraftforge.common.util.BlockSnapshot) ItemBucket(net.minecraft.item.ItemBucket) EnumActionResult(net.minecraft.util.EnumActionResult) NonNullList(net.minecraft.util.NonNullList) ArrayList(java.util.ArrayList) StatList(net.minecraft.stats.StatList) List(java.util.List) NoteBlockEvent(net.minecraftforge.event.world.NoteBlockEvent) BlockEvent(net.minecraftforge.event.world.BlockEvent)

Example 3 with EnumActionResult

use of net.minecraft.util.EnumActionResult in project Overloaded by CJ-MC-Mods.

the class TileItemManipulator method update.

@Override
public void update() {
    ItemStack currentItem = itemStack.getStackInSlot(0);
    if (currentItem.isEmpty())
        return;
    FakePlayer player = getPlayer();
    BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos(this.getPos());
    for (int i = 0; i < player.interactionManager.getBlockReachDistance(); i++) {
        if (!this.getWorld().isAirBlock(blockPos.move(this.facing))) {
            EnumActionResult result = currentItem.getItem().onItemUse(player, getWorld(), blockPos, EnumHand.MAIN_HAND, facing.getOpposite(), 0.5f, 0.5f, 0.5f);
            //                        blockPos,this.facing.getOpposite(),0.5f,0.5f,0.5f);
            break;
        }
    }
}
Also used : EnumActionResult(net.minecraft.util.EnumActionResult) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 4 with EnumActionResult

use of net.minecraft.util.EnumActionResult in project Railcraft by Railcraft.

the class ItemMagnifyingGlass method onItemUseFirst.

@Override
public EnumActionResult onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
    if (Game.isClient(world))
        return EnumActionResult.PASS;
    TileEntity t = world.getTileEntity(pos);
    EnumActionResult returnValue = EnumActionResult.PASS;
    if (t instanceof IOwnable) {
        IOwnable ownable = (IOwnable) t;
        ChatPlugin.sendLocalizedChatFromServer(player, "gui.railcraft.mag.glass.placedby", ownable.getDisplayName(), ownable.getOwner());
        returnValue = EnumActionResult.SUCCESS;
    }
    if (t instanceof TileMultiBlock) {
        TileMultiBlock tile = (TileMultiBlock) t;
        if (tile.isStructureValid())
            ChatPlugin.sendLocalizedChatFromServer(player, "railcraft.multiblock.state.valid");
        else
            for (MultiBlockStateReturn returnState : EnumSet.complementOf(EnumSet.of(MultiBlockStateReturn.VALID))) {
                List<Integer> pats = tile.patternStates.get(returnState);
                if (!pats.isEmpty())
                    ChatPlugin.sendLocalizedChatFromServer(player, returnState.message, pats.toString());
            }
        returnValue = EnumActionResult.SUCCESS;
    }
    if (t instanceof IDualHeadSignal) {
        IDualHeadSignal signal = (IDualHeadSignal) t;
        SignalAspect top = signal.getSignalAspect(DualLamp.TOP);
        SignalAspect bottom = signal.getSignalAspect(DualLamp.BOTTOM);
        ChatPlugin.sendLocalizedChatFromServer(player, "gui.railcraft.mag.glass.aspect.dual", top.getLocalizationTag(), bottom.getLocalizationTag());
        returnValue = EnumActionResult.SUCCESS;
    } else if (t instanceof TileSignalBase) {
        ChatPlugin.sendLocalizedChatFromServer(player, "gui.railcraft.mag.glass.aspect", ((TileSignalBase) t).getSignalAspect().getLocalizationTag());
        returnValue = EnumActionResult.SUCCESS;
    }
    return returnValue;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) SignalAspect(mods.railcraft.api.signals.SignalAspect) EnumActionResult(net.minecraft.util.EnumActionResult) TileMultiBlock(mods.railcraft.common.blocks.machine.TileMultiBlock) MultiBlockStateReturn(mods.railcraft.common.blocks.machine.TileMultiBlock.MultiBlockStateReturn) IDualHeadSignal(mods.railcraft.common.blocks.wayobjects.IDualHeadSignal) TileSignalBase(mods.railcraft.common.blocks.wayobjects.TileSignalBase) IOwnable(mods.railcraft.api.core.IOwnable) List(java.util.List)

Aggregations

EnumActionResult (net.minecraft.util.EnumActionResult)4 List (java.util.List)2 ArrayList (java.util.ArrayList)1 IOwnable (mods.railcraft.api.core.IOwnable)1 SignalAspect (mods.railcraft.api.signals.SignalAspect)1 ChargeNetwork (mods.railcraft.common.blocks.charge.ChargeNetwork)1 TileMultiBlock (mods.railcraft.common.blocks.machine.TileMultiBlock)1 MultiBlockStateReturn (mods.railcraft.common.blocks.machine.TileMultiBlock.MultiBlockStateReturn)1 IDualHeadSignal (mods.railcraft.common.blocks.wayobjects.IDualHeadSignal)1 TileSignalBase (mods.railcraft.common.blocks.wayobjects.TileSignalBase)1 IBlockState (net.minecraft.block.state.IBlockState)1 ItemBucket (net.minecraft.item.ItemBucket)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 StatList (net.minecraft.stats.StatList)1 TileEntity (net.minecraft.tileentity.TileEntity)1 NonNullList (net.minecraft.util.NonNullList)1 BlockPos (net.minecraft.util.math.BlockPos)1 BlockSnapshot (net.minecraftforge.common.util.BlockSnapshot)1 FakePlayer (net.minecraftforge.common.util.FakePlayer)1