Search in sources :

Example 1 with PlayerTickEvent

use of net.minecraftforge.event.TickEvent.PlayerTickEvent 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)

Aggregations

Collections (java.util.Collections)1 Set (java.util.Set)1 WeakHashMap (java.util.WeakHashMap)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 BlockPos (net.minecraft.core.BlockPos)1 Direction (net.minecraft.core.Direction)1 ParticleTypes (net.minecraft.core.particles.ParticleTypes)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 ServerPlayer (net.minecraft.server.level.ServerPlayer)1 FluidTags (net.minecraft.tags.FluidTags)1 Entity (net.minecraft.world.entity.Entity)1 EquipmentSlot (net.minecraft.world.entity.EquipmentSlot)1 Player (net.minecraft.world.entity.player.Player)1 Item (net.minecraft.world.item.Item)1 ItemStack (net.minecraft.world.item.ItemStack)1 Level (net.minecraft.world.level.Level)1