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));
}
}
}
}
Aggregations