Search in sources :

Example 6 with Capability

use of net.minecraftforge.common.capabilities.Capability in project MorePlanets by SteveKunG.

the class CapabilityHandlerMP method register.

public static void register() {
    CapabilityManager.INSTANCE.register(AbstractCapabilityDataMP.class, new Capability.IStorage<AbstractCapabilityDataMP>() {

        @Override
        public NBTBase writeNBT(Capability<AbstractCapabilityDataMP> capability, AbstractCapabilityDataMP instance, EnumFacing side) {
            NBTTagCompound nbt = new NBTTagCompound();
            instance.writeNBT(nbt);
            return nbt;
        }

        @Override
        public void readNBT(Capability<AbstractCapabilityDataMP> capability, AbstractCapabilityDataMP instance, EnumFacing side, NBTBase nbt) {
            instance.readNBT((NBTTagCompound) nbt);
        }
    }, CapabilityDataMP::new);
    CommonUtils.registerEventHandler(new CapabilityHandlerMP());
}
Also used : Capability(net.minecraftforge.common.capabilities.Capability) NBTBase(net.minecraft.nbt.NBTBase) EnumFacing(net.minecraft.util.EnumFacing) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Example 7 with Capability

use of net.minecraftforge.common.capabilities.Capability in project Tropicraft by Tropicraft.

the class ScubaData method onPlayerTick.

@SubscribeEvent
public static void onPlayerTick(PlayerTickEvent event) {
    Level world = event.player.level;
    if (event.phase == Phase.END) {
        // TODO support more than chest slot?
        ItemStack chestStack = event.player.getItemBySlot(EquipmentSlot.CHEST);
        Item chestItem = chestStack.getItem();
        if (chestItem instanceof ScubaArmorItem) {
            LazyOptional<ScubaData> data = event.player.getCapability(CAPABILITY);
            if (!world.isClientSide) {
                underwaterPlayers.add((ServerPlayer) event.player);
            }
            if (isUnderWater(event.player)) {
                data.ifPresent(d -> {
                    d.tick(event.player);
                    if (!world.isClientSide) {
                        d.updateClient((ServerPlayer) event.player, false);
                    }
                });
                ((ScubaArmorItem) chestItem).tickAir(event.player, EquipmentSlot.CHEST, chestStack);
                if (!world.isClientSide && world.getGameTime() % 60 == 0) {
                    // TODO this effect could be better, custom packet?
                    Vec3 eyePos = event.player.getEyePosition(0);
                    Vec3 motion = event.player.getDeltaMovement();
                    Vec3 particlePos = eyePos.add(motion.reverse());
                    ((ServerLevel) world).sendParticles(ParticleTypes.BUBBLE, particlePos.x(), particlePos.y(), particlePos.z(), 4 + world.random.nextInt(3), 0.25, 0.25, 0.25, motion.length());
                }
            } else if (!world.isClientSide && underwaterPlayers.remove(event.player)) {
                // Update client state as they leave the water
                data.ifPresent(d -> d.updateClient((ServerPlayer) event.player, false));
            }
        }
    }
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Direction(net.minecraft.core.Direction) Item(net.minecraft.world.item.Item) CapabilityManager(net.minecraftforge.common.capabilities.CapabilityManager) EventBusSubscriber(net.minecraftforge.fml.common.Mod.EventBusSubscriber) PacketDistributor(net.minecraftforge.fmllegacy.network.PacketDistributor) TropicraftDimension(net.tropicraft.core.common.dimension.TropicraftDimension) FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) ServerLevel(net.minecraft.server.level.ServerLevel) LazyOptional(net.minecraftforge.common.util.LazyOptional) ServerPlayer(net.minecraft.server.level.ServerPlayer) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) Nonnull(javax.annotation.Nonnull) Phase(net.minecraftforge.event.TickEvent.Phase) MessageUpdateScubaData(net.tropicraft.core.common.network.message.MessageUpdateScubaData) Nullable(javax.annotation.Nullable) WeakHashMap(java.util.WeakHashMap) PlayerEvent(net.minecraftforge.event.entity.player.PlayerEvent) PlayerTickEvent(net.minecraftforge.event.TickEvent.PlayerTickEvent) PlayerChangedDimensionEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerChangedDimensionEvent) FluidTags(net.minecraft.tags.FluidTags) Set(java.util.Set) AttachCapabilitiesEvent(net.minecraftforge.event.AttachCapabilitiesEvent) Capability(net.minecraftforge.common.capabilities.Capability) Player(net.minecraft.world.entity.player.Player) PlayerRespawnEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerRespawnEvent) TropicraftPackets(net.tropicraft.core.common.network.TropicraftPackets) ICapabilitySerializable(net.minecraftforge.common.capabilities.ICapabilitySerializable) CompoundTag(net.minecraft.nbt.CompoundTag) Entity(net.minecraft.world.entity.Entity) BlockPos(net.minecraft.core.BlockPos) Vec3(net.minecraft.world.phys.Vec3) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Constants(net.tropicraft.Constants) PlayerLoggedInEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerLoggedInEvent) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) CapabilityToken(net.minecraftforge.common.capabilities.CapabilityToken) ParticleTypes(net.minecraft.core.particles.ParticleTypes) Bus(net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus) Collections(java.util.Collections) INBTSerializable(net.minecraftforge.common.util.INBTSerializable) Item(net.minecraft.world.item.Item) MessageUpdateScubaData(net.tropicraft.core.common.network.message.MessageUpdateScubaData) ServerLevel(net.minecraft.server.level.ServerLevel) Vec3(net.minecraft.world.phys.Vec3) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 8 with Capability

use of net.minecraftforge.common.capabilities.Capability in project Charset by CharsetMC.

the class Capabilities method init.

public static void init() {
    CapabilityManager.INSTANCE.register(IBundledEmitter.class, new DefaultBundledEmitterStorage(), DefaultBundledEmitter.class);
    CapabilityManager.INSTANCE.register(IRedstoneEmitter.class, new DefaultRedstoneEmitterStorage(), DefaultRedstoneEmitter.class);
    CapabilityManager.INSTANCE.register(IBundledReceiver.class, new Capability.IStorage<IBundledReceiver>() {

        @Override
        public NBTBase writeNBT(Capability<IBundledReceiver> capability, IBundledReceiver instance, EnumFacing side) {
            return null;
        }

        @Override
        public void readNBT(Capability<IBundledReceiver> capability, IBundledReceiver instance, EnumFacing side, NBTBase nbt) {
        }
    }, DummyRedstoneReceiver.class);
    CapabilityManager.INSTANCE.register(IRedstoneReceiver.class, new Capability.IStorage<IRedstoneReceiver>() {

        @Override
        public NBTBase writeNBT(Capability<IRedstoneReceiver> capability, IRedstoneReceiver instance, EnumFacing side) {
            return null;
        }

        @Override
        public void readNBT(Capability<IRedstoneReceiver> capability, IRedstoneReceiver instance, EnumFacing side, NBTBase nbt) {
        }
    }, DummyRedstoneReceiver.class);
    if (Loader.isModLoaded("mcmultipart")) {
        initMultiplePants();
    }
}
Also used : DefaultBundledEmitterStorage(pl.asie.charset.lib.capability.DefaultBundledEmitterStorage) Capability(net.minecraftforge.common.capabilities.Capability) NBTBase(net.minecraft.nbt.NBTBase) IBundledReceiver(pl.asie.charset.api.wires.IBundledReceiver) EnumFacing(net.minecraft.util.EnumFacing) IRedstoneReceiver(pl.asie.charset.api.wires.IRedstoneReceiver) DefaultRedstoneEmitterStorage(pl.asie.charset.lib.capability.DefaultRedstoneEmitterStorage)

Example 9 with Capability

use of net.minecraftforge.common.capabilities.Capability in project MC-Prefab by Brian-Wuest.

the class TileEntityBase method transferCapabilities.

/**
 * Transfers capabilities available for transferring to the supplied itemstack.
 * @param stack The item stack to copy capabilities for.
 * @return The updated item stack with the original ItemStack's capabilities.
 */
public ItemStack transferCapabilities(ItemStack stack) {
    // Transfer each transferable capability to the itemstack.
    for (Capability allowedCapability : this.getAllowedCapabilities()) {
        // Get the interfaces for this capability.
        Object stackCapability = stack.getCapability(allowedCapability, EnumFacing.NORTH);
        Object tileEntityCapability = this.getCapability(allowedCapability, EnumFacing.NORTH);
        if (stackCapability != null && tileEntityCapability != null && stackCapability instanceof ITransferable && tileEntityCapability instanceof ITransferable) {
            // transfer the capability data, it's up to the capability to transfer the data.
            ((ITransferable) stackCapability).Transfer((ITransferable) tileEntityCapability);
        }
    }
    return stack;
}
Also used : Capability(net.minecraftforge.common.capabilities.Capability) ITransferable(com.wuest.prefab.Capabilities.ITransferable)

Example 10 with Capability

use of net.minecraftforge.common.capabilities.Capability in project BuildCraft by BuildCraft.

the class CapabilityHelper method getCapability.

@SuppressWarnings("unchecked")
@Override
public <T> T getCapability(@Nonnull Capability<T> capability, EnumFacing facing) {
    Map<Capability<?>, Supplier<?>> capMap = getCapMap(facing);
    Supplier<?> supplier = capMap.get(capability);
    if (supplier != null) {
        return (T) supplier.get();
    }
    for (ICapabilityProvider provider : additional) {
        if (provider.hasCapability(capability, facing)) {
            return provider.getCapability(capability, facing);
        }
    }
    return null;
}
Also used : Capability(net.minecraftforge.common.capabilities.Capability) ICapabilityProvider(net.minecraftforge.common.capabilities.ICapabilityProvider) Supplier(java.util.function.Supplier)

Aggregations

Capability (net.minecraftforge.common.capabilities.Capability)16 EnumFacing (net.minecraft.util.EnumFacing)9 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)7 NBTBase (net.minecraft.nbt.NBTBase)6 Nonnull (javax.annotation.Nonnull)5 Nullable (javax.annotation.Nullable)5 ItemStack (net.minecraft.item.ItemStack)4 TileEntity (net.minecraft.tileentity.TileEntity)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Block (net.minecraft.block.Block)3 BlockPos (net.minecraft.util.math.BlockPos)3 World (net.minecraft.world.World)3 ICapabilityProvider (net.minecraftforge.common.capabilities.ICapabilityProvider)3 Collectors (java.util.stream.Collectors)2 IBlockState (net.minecraft.block.state.IBlockState)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 Direction (net.minecraft.util.Direction)2 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)2 IBPPartBlock (com.bluepowermod.api.multipart.IBPPartBlock)1